This repository was archived by the owner on Jun 22, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 11 files changed +19
-11
lines changed Expand file tree Collapse file tree 11 files changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
66export type Document = any ;
77
88export 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 ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
66export type Document = any ;
77
88export 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 ;
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ after(async () => {
3232} ) ;
3333
3434facadeTest ( factory < TestEntity > ( {
35- axios : axios . create ( {
35+ axios : async ( ) => axios . create ( {
3636 baseURL : `http://localhost:${ testServerPort } ${ testServerRoute } ` ,
3737 } ) ,
3838 entityName : 'Test Entity' ,
Original file line number Diff line number Diff line change @@ -4,9 +4,10 @@ import FacadeConfig from '../FacadeConfig';
44
55export 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} ;
Original file line number Diff line number Diff line change @@ -6,8 +6,9 @@ import FacadeConfig from '../FacadeConfig';
66
77export 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 }
Original file line number Diff line number Diff 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' ] ;
Original file line number Diff line number Diff line change @@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';
66
77export 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 }
Original file line number Diff line number Diff line change @@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';
66
77export 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 }
Original file line number Diff line number Diff line change @@ -4,8 +4,9 @@ import FacadeConfig from '../FacadeConfig';
44
55export 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} ;
Original file line number Diff line number Diff line change @@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';
66
77export 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 }
You can’t perform that action at this time.
0 commit comments