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

Commit 208a5a2

Browse files
authored
feat: Allows async connections. (#29)
BREAKING CHANGE: `axios` property in `FactoryConfig` must be an async function returning an `AxiosInstance` instead of an `AxiosInstance` itself.
1 parent bcd7b20 commit 208a5a2

11 files changed

+19
-11
lines changed

src/FacadeConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
66
export type Document = any;
77

88
export default interface FacadeConfig<E extends Entity> {
9-
readonly axios: AxiosInstance;
9+
readonly axios: () => Promise<AxiosInstance>;
1010
readonly constructDocument: (patch: Partial<E>) => Document;
1111
readonly constructEntity: (document: Document) => E;
1212
readonly constructFilter: (filter: Filter<E>) => any;

src/FactoryConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
66
export type Document = any;
77

88
export default interface FactoryConfig<E extends Entity> {
9-
readonly axios: AxiosInstance;
9+
readonly axios: () => Promise<AxiosInstance>;
1010
readonly constructDocument?: (patch: Partial<E>) => Document;
1111
readonly constructEntity?: (document: Document) => E;
1212
readonly constructFilter?: (filter: Filter<E>) => any;

src/factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ after(async () => {
3232
});
3333

3434
facadeTest(factory<TestEntity>({
35-
axios: axios.create({
35+
axios: async () => axios.create({
3636
baseURL: `http://localhost:${testServerPort}${testServerRoute}`,
3737
}),
3838
entityName: 'Test Entity',

src/functions/countEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import FacadeConfig from '../FacadeConfig';
44

55
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
66
return async ({ filter = {} }) => {
7+
const connection = await config.axios();
78
const constructedFilter = config.constructFilter(filter);
89
const params = { filter: JSON.stringify(constructedFilter) };
9-
const response = await Promise.resolve(config.axios.get('/count', { params }));
10+
const response = await Promise.resolve(connection.get('/count', { params }));
1011
return { count: response.data };
1112
};
1213
};

src/functions/createEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
88
return async ({ id, entity }) => {
9+
const connection = await config.axios();
910
const data = config.constructDocument({ ...entity as any, id });
10-
const response = await config.axios.post('', data).catch((err) => {
11+
const response = await connection.post('', data).catch((err) => {
1112
if (err.response.status === CONFLICT) {
1213
throw new ConflictingEntityError(config.entityName, id);
1314
}

src/functions/getEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
1414
};
1515
const defaultSort = { id: asc } as Sort<E>;
1616
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
17+
const connection = await config.axios();
1718
const constructedFilter = config.constructFilter(filter);
1819
const constructedSort = config.constructSort(sort);
1920
const params = {
@@ -23,7 +24,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
2324
limit: pagination.limit,
2425
sort: JSON.stringify(constructedSort),
2526
};
26-
const response = await Promise.resolve(config.axios.get('', { params }));
27+
const response = await Promise.resolve(connection.get('', { params }));
2728

2829
const entities = response.data.map(config.constructEntity);
2930
const backwardCursor = response.headers['x-entities-backward-cursor'];

src/functions/getEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
88
return async ({ id, filter = {} }) => {
9+
const connection = await config.axios();
910
const constructedFilter = config.constructFilter(filter);
1011
const params = { filter: JSON.stringify(constructedFilter) };
11-
const response = await config.axios.get(`/${id}`, { params }).catch((err) => {
12+
const response = await connection.get(`/${id}`, { params }).catch((err) => {
1213
if (err.response.status === NOT_FOUND) {
1314
throw new MissingEntityError(config.entityName, id);
1415
}

src/functions/patchEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
88
return async ({ id, patch, filter = {} }) => {
9+
const connection = await config.axios();
910
const data = config.constructDocument({ ...patch as any, id });
1011
const constructedFilter = config.constructFilter(filter);
1112
const params = { filter: JSON.stringify(constructedFilter) };
12-
const response = await config.axios.patch(`/${id}`, data, { params }).catch((err) => {
13+
const response = await connection.patch(`/${id}`, data, { params }).catch((err) => {
1314
if (err.response.status === NOT_FOUND) {
1415
throw new MissingEntityError(config.entityName, id);
1516
}

src/functions/removeEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import FacadeConfig from '../FacadeConfig';
44

55
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
66
return async ({ filter = {} }) => {
7+
const connection = await config.axios();
78
const constructedFilter = config.constructFilter(filter);
89
const params = { filter: JSON.stringify(constructedFilter) };
9-
await Promise.resolve(config.axios.delete('', { params }));
10+
await Promise.resolve(connection.delete('', { params }));
1011
};
1112
};

src/functions/removeEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
88
return async ({ id, filter = {} }) => {
9+
const connection = await config.axios();
910
const constructedFilter = config.constructFilter(filter);
1011
const params = { filter: JSON.stringify(constructedFilter) };
11-
await config.axios.delete(`/${id}`, { params }).catch((err) => {
12+
await connection.delete(`/${id}`, { params }).catch((err) => {
1213
if (err.response.status === NOT_FOUND) {
1314
throw new MissingEntityError(config.entityName, id);
1415
}

src/functions/replaceEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';
66

77
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
88
return async ({ id, entity, filter = {} }) => {
9+
const connection = await config.axios();
910
const data = config.constructDocument({ ...entity as any, id });
1011
const constructedFilter = config.constructFilter(filter);
1112
const params = { filter: JSON.stringify(constructedFilter) };
12-
const response = await config.axios.put(`/${id}`, data, { params }).catch((err) => {
13+
const response = await connection.put(`/${id}`, data, { params }).catch((err) => {
1314
if (err.response.status === NOT_FOUND) {
1415
throw new MissingEntityError(config.entityName, id);
1516
}

0 commit comments

Comments
 (0)