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

feat: Allows async connections. #29

Merged
merged 1 commit into from
Apr 30, 2018
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
2 changes: 1 addition & 1 deletion src/FacadeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
export type Document = any;

export default interface FacadeConfig<E extends Entity> {
readonly axios: AxiosInstance;
readonly axios: () => Promise<AxiosInstance>;
readonly constructDocument: (patch: Partial<E>) => Document;
readonly constructEntity: (document: Document) => E;
readonly constructFilter: (filter: Filter<E>) => any;
Expand Down
2 changes: 1 addition & 1 deletion src/FactoryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
export type Document = any;

export default interface FactoryConfig<E extends Entity> {
readonly axios: AxiosInstance;
readonly axios: () => Promise<AxiosInstance>;
readonly constructDocument?: (patch: Partial<E>) => Document;
readonly constructEntity?: (document: Document) => E;
readonly constructFilter?: (filter: Filter<E>) => any;
Expand Down
2 changes: 1 addition & 1 deletion src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ after(async () => {
});

facadeTest(factory<TestEntity>({
axios: axios.create({
axios: async () => axios.create({
baseURL: `http://localhost:${testServerPort}${testServerRoute}`,
}),
entityName: 'Test Entity',
Expand Down
3 changes: 2 additions & 1 deletion src/functions/countEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
return async ({ filter = {} }) => {
const connection = await config.axios();
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
const response = await Promise.resolve(config.axios.get('/count', { params }));
const response = await Promise.resolve(connection.get('/count', { params }));
return { count: response.data };
};
};
3 changes: 2 additions & 1 deletion src/functions/createEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
return async ({ id, entity }) => {
const connection = await config.axios();
const data = config.constructDocument({ ...entity as any, id });
const response = await config.axios.post('', data).catch((err) => {
const response = await connection.post('', data).catch((err) => {
if (err.response.status === CONFLICT) {
throw new ConflictingEntityError(config.entityName, id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/functions/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
};
const defaultSort = { id: asc } as Sort<E>;
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
const connection = await config.axios();
const constructedFilter = config.constructFilter(filter);
const constructedSort = config.constructSort(sort);
const params = {
Expand All @@ -23,7 +24,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
limit: pagination.limit,
sort: JSON.stringify(constructedSort),
};
const response = await Promise.resolve(config.axios.get('', { params }));
const response = await Promise.resolve(connection.get('', { params }));

const entities = response.data.map(config.constructEntity);
const backwardCursor = response.headers['x-entities-backward-cursor'];
Expand Down
3 changes: 2 additions & 1 deletion src/functions/getEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
return async ({ id, filter = {} }) => {
const connection = await config.axios();
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
const response = await config.axios.get(`/${id}`, { params }).catch((err) => {
const response = await connection.get(`/${id}`, { params }).catch((err) => {
if (err.response.status === NOT_FOUND) {
throw new MissingEntityError(config.entityName, id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/functions/patchEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
return async ({ id, patch, filter = {} }) => {
const connection = await config.axios();
const data = config.constructDocument({ ...patch as any, id });
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
const response = await config.axios.patch(`/${id}`, data, { params }).catch((err) => {
const response = await connection.patch(`/${id}`, data, { params }).catch((err) => {
if (err.response.status === NOT_FOUND) {
throw new MissingEntityError(config.entityName, id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/functions/removeEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
return async ({ filter = {} }) => {
const connection = await config.axios();
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
await Promise.resolve(config.axios.delete('', { params }));
await Promise.resolve(connection.delete('', { params }));
};
};
3 changes: 2 additions & 1 deletion src/functions/removeEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
return async ({ id, filter = {} }) => {
const connection = await config.axios();
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
await config.axios.delete(`/${id}`, { params }).catch((err) => {
await connection.delete(`/${id}`, { params }).catch((err) => {
if (err.response.status === NOT_FOUND) {
throw new MissingEntityError(config.entityName, id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/functions/replaceEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';

export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
return async ({ id, entity, filter = {} }) => {
const connection = await config.axios();
const data = config.constructDocument({ ...entity as any, id });
const constructedFilter = config.constructFilter(filter);
const params = { filter: JSON.stringify(constructedFilter) };
const response = await config.axios.put(`/${id}`, data, { params }).catch((err) => {
const response = await connection.put(`/${id}`, data, { params }).catch((err) => {
if (err.response.status === NOT_FOUND) {
throw new MissingEntityError(config.entityName, id);
}
Expand Down