Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ npm i @quickcase/node-toolkit
* [Case Access](#case-access)
* [Definition](#definition)
* [Document](#document)
* [Express](#express)
* [Field](#field)
* [HTTP Client](#http-client)
* [OAuth2](#oauth2)
Expand Down Expand Up @@ -442,6 +443,45 @@ docDownload:
*/
```

### Express

Utilities for [ExpressJS](https://expressjs.com/) v4.x.x.

#### middlewareFactory(dependencySuppliers)(middlewareSupplier)(req, res, next)

Decorator for an Express middleware which dynamically instantiates request-based dependencies and underlying middleware upon processing of a request.

##### Arguments

| Name | Type | Description |
|------|------|-------------|
| dependencySuppliers | object | Required. Map of named dependency supplier functions which take the request object as an input |
| middlewareSupplier | function | Function that will be passed the initialised dependencies and return an initialised Express middleware. |

##### Returns

ExpressJS middleware function.

##### Example

```js
import {middlewareFactory} from '@quickcase/node-toolkit';
import express from 'express';

const router = express.Router();

const dependencySuppliers = {
tokenProvider: (req) => () => Promise.resolve(req.accessToken),
service: (req) => {...},
};

const middlewareSupplier = ({tokenProvider, service}) => (req, res) => {
// use initialised `tokenProvider` and `service`...
};

router.get('/', middlewareFactory(dependencySuppliers)(middlewareSupplier));
```

### Field

#### isNo(value)
Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './modules/case';
export * from './modules/case-access';
export * from './modules/definition';
export * from './modules/document';
export * from './modules/express';
export * from './modules/field';
export * from './modules/http-client';
export * from './modules/oauth2';
Expand Down
19 changes: 19 additions & 0 deletions modules/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Decorator for an Express middleware which dynamically instantiates request-based
* dependencies and underlying middleware upon processing of a request.
* @param {object} dependencySuppliers Dictionary of named dependency suppliers.
* Each supplier will be invoked with the instance of the current Express request.
* Each dependency supplied will be provided to the middleware under the same name.
* @param {function} middlewareSupplier Function that will be passed
* the initialised dependencies and return an initialised Express middleware.
* @return {ExpressMiddleware} Express middleware function which will invoke the
* initialised middleware when called.
*/
export const middlewareFactory = (dependencySuppliers) => (middlewareSupplier) => (req, res, next) => {
const dependencies = Object.entries(dependencySuppliers)
.reduce((acc, [key, factory]) => {
acc[key] = factory(req);
return acc;
}, {});
middlewareSupplier(dependencies)(req, res, next);
};
23 changes: 23 additions & 0 deletions modules/express.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {middlewareFactory} from './express';

describe('middlewareFactory', () => {
const ACCESS_TOKEN = 'ey123';
const ROLES = ['role1', 'role2'];

test('should create middleware with initialised dependencies', (done) => {
const middleware = ({tokenSupplier, roles}) => (req, res, next) => {
expect(tokenSupplier()).toEqual(ACCESS_TOKEN);
expect(roles).toEqual(ROLES);
done();
};
const req = {
accessToken: ACCESS_TOKEN,
roles: ROLES,
};

middlewareFactory({
tokenSupplier: (req) => () => req.accessToken,
roles: (req) => req.roles,
})(middleware)(req);
});
});