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

Commit dc47fe5

Browse files
authored
fix(deps): Updates core to 6.0.2. (#11)
BREAKING CHANGE: The `filter`, `sort`, and `paginate` options are now optional. BREAKING CHANGE: The `upsertEntity` function has been removed due to filter issues. BREAKING CHANGE: The `overwriteEntity` function was renamed to `replaceEntity`. BREAKING CHANGE: Removes Id interface from all functions.
1 parent 7833e7f commit dc47fe5

40 files changed

+319
-264
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
command: npm run lint
3939
- run:
4040
name: Checking Code Duplication
41-
command: npm run duplication -- --limit 10
41+
command: npm run duplication -- --limit 15
4242
- deploy:
4343
name: Semantic Release
4444
command: npm run semantic-release

package-lock.json

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

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"check-coverage": true
2626
},
2727
"dependencies": {
28-
"@js-entity-repos/core": "^4.1.2",
28+
"@js-entity-repos/core": "^6.0.2",
2929
"axios": "^0.17.1",
3030
"http-status-codes": "^1.3.0",
3131
"lodash": "^4.17.4"
3232
},
3333
"devDependencies": {
3434
"@ht2-labs/semantic-release": "1.0.27",
3535
"@ht2-labs/typescript-project": "1.0.9",
36-
"@js-entity-repos/memory": "1.0.3",
36+
"@js-entity-repos/memory": "^3.0.0",
3737
"@types/body-parser": "1.16.8",
3838
"@types/dotenv": "4.0.2",
3939
"@types/express": "4.11.1",

readme.md

+25-19
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,59 @@
44
### Usage
55
1. Install it with `npm i @js-entity-repos/axios`.
66
1. For each entity you will need to do the following.
7-
1. [Create Id and Entity interfaces](#id-and-entity-interface).
8-
1. [Create a facade config](#facade-config).
9-
1. [Construct the facade with the config and interfaces](#calling-the-facade).
7+
1. [Create an Entity interface](#entity-interface).
8+
1. [Create a factory config](#factory-config).
9+
1. [Construct the facade](#construct-the-facade).
1010
1. [Use the facade](https://github.com/js-entity-repos/core/blob/master/docs/facade.md).
1111

12-
### Id and Entity Interface
12+
### Entity Interface
1313

1414
```ts
15-
export interface TodoId {
16-
readonly id: string;
17-
}
15+
import Entity from '@js-entity-repos/core/dist/types/Entity';
1816

19-
export interface TodoEntity extends TodoId {
17+
export interface TodoEntity extends Entity {
2018
readonly description: string;
2119
readonly completed: boolean;
2220
}
2321
```
2422

25-
### Facade Config
23+
### Factory Config
2624

2725
```ts
28-
import FacadeConfig from '@js-entity-repos/axios/dist/Config';
29-
import connectToDb from '@js-entity-repos/axios/dist/utils/connectToDb';
26+
import FactoryConfig from '@js-entity-repos/axios/dist/FactoryConfig';
3027
import axios from 'axios';
3128

32-
const todoFacadeConfig: FacadeConfig = {
29+
const todoFactoryConfig: FactoryConfig = {
3330
axios: axios.create({
3431
baseURL: `http://localhost:80/api/todos`,
3532
}),
36-
constructDocument: (id, patch) => {
37-
// Converts an entity to a document for the database.
38-
return { ...patch, ...id }
33+
constructDocument: (patch) => {
34+
// Optional property that converts an entity to a document for the database.
35+
return patch;
3936
},
4037
constructEntity: (document) => {
41-
// Converts a document from the database to an entity.
38+
// Optional property that converts a document from the database to an entity.
4239
return document;
4340
},
41+
constructFilter: (filter) => {
42+
// Optional property that converts an entity filter to a filter for the DB.
43+
return filter;
44+
},
45+
constructSort: (sort) => {
46+
// Optional property that converts an entity sort to a sort for the DB.
47+
return sort;
48+
},
49+
defaultPaginationLimit: 100, // Optional property.
4450
entityName: 'todo',
4551
};
4652
```
4753

48-
The tests in this package contain a [demonstration of how to implement an Express router for the functions in this facade](./src/utils/express).
54+
The tests in this package contain a [demonstration of how to implement an Express router for the functions in this facade](./src/utils/expressPresenter).
4955

5056
### Construct the Facade
5157

5258
```ts
53-
import facade from '@js-entity-repos/axios/dist/facade';
59+
import factory from '@js-entity-repos/axios/dist/factory';
5460

55-
const todosFacade = facade<TodoId, TodoEntity>(todoFacadeConfig);
61+
const todosFacade = factory(todoFactoryConfig);
5662
```

src/Config.ts

-10
This file was deleted.

src/FacadeConfig.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Filter } from '@js-entity-repos/core/dist/types/Filter';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
4+
import { AxiosInstance } from 'axios';
5+
6+
export type Document = any;
7+
8+
export default interface FacadeConfig<E extends Entity> {
9+
readonly axios: AxiosInstance;
10+
readonly constructDocument: (patch: Partial<E>) => Document;
11+
readonly constructEntity: (document: Document) => E;
12+
readonly constructFilter: (filter: Filter<E>) => any;
13+
readonly constructSort: (sort: Sort<E>) => any;
14+
readonly defaultPaginationLimit: number;
15+
readonly entityName: string;
16+
}

src/FactoryConfig.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Filter } from '@js-entity-repos/core/dist/types/Filter';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
4+
import { AxiosInstance } from 'axios';
5+
6+
export type Document = any;
7+
8+
export default interface FactoryConfig<E extends Entity> {
9+
readonly axios: AxiosInstance;
10+
readonly constructDocument?: (patch: Partial<E>) => Document;
11+
readonly constructEntity?: (document: Document) => E;
12+
readonly constructFilter?: (filter: Filter<E>) => any;
13+
readonly constructSort?: (sort: Sort<E>) => any;
14+
readonly defaultPaginationLimit?: number;
15+
readonly entityName: string;
16+
}

src/facade.ts

-25
This file was deleted.

src/facade.test.ts renamed to src/factory.test.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as sourceMapSupport from 'source-map-support';
22
sourceMapSupport.install();
33

44
import facadeTest from '@js-entity-repos/core/dist/tests';
5-
import { TestEntity, TestId } from '@js-entity-repos/core/dist/tests/utils/testEntity';
5+
import { TestEntity } from '@js-entity-repos/core/dist/tests/utils/testEntity';
66
import axios from 'axios';
77
import { config } from 'dotenv';
88
import 'mocha'; // tslint:disable-line:no-import-side-effect
9-
import facade from './facade';
9+
import factory from './factory';
1010
import createTestServer from './utils/createTestServer';
1111
config();
1212

@@ -27,15 +27,13 @@ before(async () => {
2727
});
2828

2929
after(async () => {
30-
const app = await testServer;
31-
app.close();
30+
const server = await testServer;
31+
server.close();
3232
});
3333

34-
facadeTest(facade<TestId, TestEntity>({
34+
facadeTest(factory<TestEntity>({
3535
axios: axios.create({
3636
baseURL: `http://localhost:${testServerPort}${testServerRoute}`,
3737
}),
38-
constructDocument: (id, patch) => ({ ...patch, ...id }),
39-
constructEntity: (document) => document,
4038
entityName: 'Test Entity',
4139
}));

src/factory.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Facade from '@js-entity-repos/core/dist/Facade';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from './FacadeConfig';
4+
import FactoryConfig from './FactoryConfig';
5+
import countEntities from './functions/countEntities';
6+
import createEntity from './functions/createEntity';
7+
import getEntities from './functions/getEntities';
8+
import getEntity from './functions/getEntity';
9+
import patchEntity from './functions/patchEntity';
10+
import removeEntities from './functions/removeEntities';
11+
import removeEntity from './functions/removeEntity';
12+
import replaceEntity from './functions/replaceEntity';
13+
14+
export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> => {
15+
const facadeConfig: FacadeConfig<E> = {
16+
constructDocument: (patch) => patch,
17+
constructEntity: (document) => document,
18+
constructFilter: (filter) => filter,
19+
constructSort: (sort) => sort,
20+
defaultPaginationLimit: 100,
21+
...factoryConfig,
22+
};
23+
return {
24+
countEntities: countEntities<E>(facadeConfig),
25+
createEntity: createEntity<E>(facadeConfig),
26+
getEntities: getEntities<E>(facadeConfig),
27+
getEntity: getEntity<E>(facadeConfig),
28+
patchEntity: patchEntity<E>(facadeConfig),
29+
removeEntities: removeEntities<E>(facadeConfig),
30+
removeEntity: removeEntity<E>(facadeConfig),
31+
replaceEntity: replaceEntity<E>(facadeConfig),
32+
};
33+
};

src/functions/countEntities.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import CountEntities from '@js-entity-repos/core/dist/signatures/CountEntities';
2-
import Config from '../Config';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import FacadeConfig from '../FacadeConfig';
34

4-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): CountEntities<Entity> => {
5-
return async ({ filter }) => {
6-
const params = { filter: JSON.stringify(filter) };
5+
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
6+
return async ({ filter = {} }) => {
7+
const constructedFilter = config.constructFilter(filter);
8+
const params = { filter: JSON.stringify(constructedFilter) };
79
const response = await Promise.resolve(config.axios.get('/count', { params }));
810
return { count: response.data };
911
};

src/functions/createEntity.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import ConflictingEntityError from '@js-entity-repos/core/dist/errors/ConflictingEntityError';
22
import CreateEntity from '@js-entity-repos/core/dist/signatures/CreateEntity';
3+
import Entity from '@js-entity-repos/core/dist/types/Entity';
34
import { CONFLICT } from 'http-status-codes';
4-
import Config from '../Config';
5+
import FacadeConfig from '../FacadeConfig';
56

6-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): CreateEntity<Id, Entity> => {
7+
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
78
return async ({ id, entity }) => {
8-
const data = config.constructDocument(id, entity);
9+
const data = config.constructDocument({ ...entity as any, id });
910
const response = await config.axios.post('', data).catch((err) => {
1011
if (err.response.status === CONFLICT) {
1112
throw new ConflictingEntityError(config.entityName, id);

src/functions/getEntities.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import GetEntities from '@js-entity-repos/core/dist/signatures/GetEntities';
2-
import Config from '../Config';
2+
import Entity from '@js-entity-repos/core/dist/types/Entity';
3+
import Sort from '@js-entity-repos/core/dist/types/Sort';
4+
import FacadeConfig from '../FacadeConfig';
35

4-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): GetEntities<Entity> => {
5-
return async ({ filter, sort, pagination }) => {
6+
export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
7+
const defaultPagination = {
8+
cursor: undefined,
9+
forward: true,
10+
limit: config.defaultPaginationLimit,
11+
};
12+
const defaultSort = { id: true } as Sort<E>;
13+
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
14+
const constructedFilter = config.constructFilter(filter);
15+
const constructedSort = config.constructSort(sort);
616
const params = {
717
cursor: pagination.cursor,
8-
filter: JSON.stringify(filter),
18+
filter: JSON.stringify(constructedFilter),
919
forward: pagination.forward,
1020
limit: pagination.limit,
11-
sort: JSON.stringify(sort),
21+
sort: JSON.stringify(constructedSort),
1222
};
1323
const response = await Promise.resolve(config.axios.get('', { params }));
1424

src/functions/getEntity.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';
22
import GetEntity from '@js-entity-repos/core/dist/signatures/GetEntity';
3+
import Entity from '@js-entity-repos/core/dist/types/Entity';
34
import { NOT_FOUND } from 'http-status-codes';
4-
import Config from '../Config';
5+
import FacadeConfig from '../FacadeConfig';
56

6-
export default <Id, Entity extends Id>(config: Config<Id, Entity>): GetEntity<Id, Entity> => {
7-
return async ({ id }) => {
8-
const params = { id: JSON.stringify(id) };
9-
const response = await config.axios.get('', { params }).catch((err) => {
7+
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
8+
return async ({ id, filter = {} }) => {
9+
const constructedFilter = config.constructFilter(filter);
10+
const params = { filter: JSON.stringify(constructedFilter) };
11+
const response = await config.axios.get(`/${id}`, { params }).catch((err) => {
1012
if (err.response.status === NOT_FOUND) {
1113
throw new MissingEntityError(config.entityName, id);
1214
}

0 commit comments

Comments
 (0)