Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit 8cf9eca

Browse files
authored
feat: Adds constructEntity, constructPatch, and constructDocument. (#19)
1 parent ebf43bf commit 8cf9eca

15 files changed

+718
-745
lines changed

package-lock.json

+640-732
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"check-coverage": true
2626
},
2727
"dependencies": {
28-
"@js-entity-repos/core": "^9.0.0",
28+
"@js-entity-repos/core": "^9.1.0",
2929
"http-status-codes": "^1.3.0",
3030
"uuid": "^3.2.1"
3131
},
3232
"devDependencies": {
3333
"@ht2-labs/semantic-release": "1.0.31",
3434
"@ht2-labs/typescript-project": "1.0.10",
35-
"@js-entity-repos/axios": "5.0.1",
36-
"@js-entity-repos/memory": "4.0.3",
35+
"@js-entity-repos/axios": "^5.0.1",
36+
"@js-entity-repos/memory": "^4.3.0",
3737
"@types/dotenv": "4.0.3",
3838
"@types/express": "4.11.1",
3939
"@types/mocha": "5.2.0",

readme.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,22 @@ export interface TodoEntity extends Entity {
2727
import factory from '@js-entity-repos/express/dist/factory';
2828

2929
const todosFacade = factory<TodoEntity>({
30+
// Optional property to convert an entity to a response body. Defaults to the function below.
31+
constructDocument: ({ entity, req, res }) => {
32+
return entity;
33+
},
34+
// Optional property to convert a request body to an entity. Defaults to the function below.
35+
constructEntity: ({ document, req, res }) => {
36+
return document;
37+
},
3038
// Optional property that modifies a filter for the service.
3139
constructFilter: ({ filter, req, res }) => {
32-
// This is a great place to put authentication and authorisation logic.
3340
return filter;
3441
},
42+
// Optional property to convert a request body to a patch. Defaults to the function below.
43+
constructPatch: ({ document, req, res }) => {
44+
return document;
45+
},
3546
// Optional property. Defaults to 10.
3647
defaultPaginationLimit: 10,
3748
// Optional property to handle transactions. Defaults to "utils/handleTransaction".

renovate.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"extends": [
3-
"@ht2-labs:base"
3+
"@ht2-labs:base",
4+
":preserveSemverRanges"
45
]
56
}

src/FacadeConfig.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import Facade from '@js-entity-repos/core/dist/Facade';
22
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import DocumentConstructor from './utils/DocumentConstructor';
4+
import EntityConstructor from './utils/EntityConstructor';
35
import FilterConstructor from './utils/FilterConstructor';
6+
import PatchConstructor from './utils/PatchConstructor';
47
import TransactionHandler from './utils/TransactionHandler';
58

69
export default interface FacadeConfig<E extends Entity> {
10+
readonly constructDocument: DocumentConstructor<E>;
11+
readonly constructEntity: EntityConstructor<E>;
712
readonly constructFilter: FilterConstructor<E>;
13+
readonly constructPatch: PatchConstructor<E>;
814
readonly service: Facade<E>;
915
readonly handleTransaction: TransactionHandler;
1016
readonly defaultPaginationLimit: number;

src/FactoryConfig.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import Facade from '@js-entity-repos/core/dist/Facade';
22
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import DocumentConstructor from './utils/DocumentConstructor';
4+
import EntityConstructor from './utils/EntityConstructor';
35
import FilterConstructor from './utils/FilterConstructor';
6+
import PatchConstructor from './utils/PatchConstructor';
47
import TransactionHandler from './utils/TransactionHandler';
58

69
export default interface FactoryConfig<E extends Entity> {
10+
readonly constructDocument?: DocumentConstructor<E>;
11+
readonly constructEntity?: EntityConstructor<E>;
712
readonly constructFilter?: FilterConstructor<E>;
13+
readonly constructPatch?: PatchConstructor<E>;
814
readonly service: Facade<E>;
915
readonly handleTransaction?: TransactionHandler;
1016
readonly defaultPaginationLimit?: number;

src/factory.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import handleTransaction from './utils/handleTransaction';
1515

1616
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Router => {
1717
const facadeConfig: FacadeConfig<E> = {
18+
constructDocument: ({ entity }) => entity,
19+
constructEntity: ({ document }) => document,
1820
constructFilter: ({ filter }) => filter,
21+
constructPatch: ({ document }) => document,
1922
defaultPaginationLimit: 10,
2023
handleTransaction,
2124
...factoryConfig,

src/functions/createEntity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
77
return async (req: Request, res: Response) => {
88
await config.handleTransaction({ req, res }, async () => {
99
const { entity } = await config.service.createEntity({
10-
entity: req.body,
10+
entity: config.constructEntity({ document: req.body, req, res }),
1111
id: req.body.id,
1212
});
13-
res.status(OK).json(entity);
13+
res.status(OK).json(config.constructDocument({ entity, req, res }));
1414
});
1515
};
1616
};

src/functions/getEntities.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
2929
}
3030
res.setHeader('x-entities-has-more-backward', result.hasMoreBackward.toString());
3131
res.setHeader('x-entities-has-more-forward', result.hasMoreForward.toString());
32-
res.json(result.entities);
32+
res.json(result.entities.map((entity) => {
33+
return config.constructDocument({ entity, req, res });
34+
}));
3335
});
3436
};
3537
};

src/functions/getEntity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
1212
filter: config.constructFilter({ filter, req, res }),
1313
id: req.params.id,
1414
});
15-
res.status(OK).json(entity);
15+
res.status(OK).json(config.constructDocument({ entity, req, res }));
1616
});
1717
};
1818
};

src/functions/patchEntities.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
1111
const { entity } = await config.service.patchEntity({
1212
filter: config.constructFilter({ filter, req, res }),
1313
id: req.params.id,
14-
patch: req.body,
14+
patch: config.constructPatch({ document: req.body, req, res }),
1515
});
16-
res.status(OK).json(entity);
16+
res.status(OK).json(config.constructDocument({ entity, req, res }));
1717
});
1818
};
1919
};

src/functions/replaceEntity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
99
await config.handleTransaction({ req, res }, async () => {
1010
const filter = getJsonQueryParam(req.query, 'filter');
1111
const { entity } = await config.service.replaceEntity({
12-
entity: req.body,
12+
entity: config.constructEntity({ document: req.body, req, res }),
1313
filter: config.constructFilter({ filter, req, res }),
1414
id: req.params.id,
1515
});
16-
res.status(OK).json(entity);
16+
res.status(OK).json(config.constructDocument({ entity, req, res }));
1717
});
1818
};
1919
};

src/utils/DocumentConstructor.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Request, Response } from 'express';
3+
4+
export interface Opts<E extends Entity> {
5+
readonly req: Request;
6+
readonly res: Response;
7+
readonly entity: E;
8+
}
9+
10+
type DocumentConstructor<E extends Entity> = (opts: Opts<E>) => any;
11+
12+
export default DocumentConstructor;

src/utils/EntityConstructor.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Request, Response } from 'express';
3+
4+
export interface Opts {
5+
readonly req: Request;
6+
readonly res: Response;
7+
readonly document: any;
8+
}
9+
10+
type EntityConstructor<E extends Entity> = (opts: Opts) => E;
11+
12+
export default EntityConstructor;

src/utils/PatchConstructor.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Request, Response } from 'express';
3+
4+
export interface Opts {
5+
readonly req: Request;
6+
readonly res: Response;
7+
readonly document: any;
8+
}
9+
10+
type PatchConstructor<E extends Entity> = (opts: Opts) => Partial<E>;
11+
12+
export default PatchConstructor;

0 commit comments

Comments
 (0)