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';
6
6
export type Document = any ;
7
7
8
8
export default interface FacadeConfig < E extends Entity > {
9
- readonly axios : AxiosInstance ;
9
+ readonly axios : ( ) => Promise < AxiosInstance > ;
10
10
readonly constructDocument : ( patch : Partial < E > ) => Document ;
11
11
readonly constructEntity : ( document : Document ) => E ;
12
12
readonly constructFilter : ( filter : Filter < E > ) => any ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { AxiosInstance } from 'axios';
6
6
export type Document = any ;
7
7
8
8
export default interface FactoryConfig < E extends Entity > {
9
- readonly axios : AxiosInstance ;
9
+ readonly axios : ( ) => Promise < AxiosInstance > ;
10
10
readonly constructDocument ?: ( patch : Partial < E > ) => Document ;
11
11
readonly constructEntity ?: ( document : Document ) => E ;
12
12
readonly constructFilter ?: ( filter : Filter < E > ) => any ;
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ after(async () => {
32
32
} ) ;
33
33
34
34
facadeTest ( factory < TestEntity > ( {
35
- axios : axios . create ( {
35
+ axios : async ( ) => axios . create ( {
36
36
baseURL : `http://localhost:${ testServerPort } ${ testServerRoute } ` ,
37
37
} ) ,
38
38
entityName : 'Test Entity' ,
Original file line number Diff line number Diff line change @@ -4,9 +4,10 @@ import FacadeConfig from '../FacadeConfig';
4
4
5
5
export default < E extends Entity > ( config : FacadeConfig < E > ) : CountEntities < E > => {
6
6
return async ( { filter = { } } ) => {
7
+ const connection = await config . axios ( ) ;
7
8
const constructedFilter = config . constructFilter ( filter ) ;
8
9
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 } ) ) ;
10
11
return { count : response . data } ;
11
12
} ;
12
13
} ;
Original file line number Diff line number Diff line change @@ -6,8 +6,9 @@ import FacadeConfig from '../FacadeConfig';
6
6
7
7
export default < E extends Entity > ( config : FacadeConfig < E > ) : CreateEntity < E > => {
8
8
return async ( { id, entity } ) => {
9
+ const connection = await config . axios ( ) ;
9
10
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 ) => {
11
12
if ( err . response . status === CONFLICT ) {
12
13
throw new ConflictingEntityError ( config . entityName , id ) ;
13
14
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
14
14
} ;
15
15
const defaultSort = { id : asc } as Sort < E > ;
16
16
return async ( { filter = { } , sort = defaultSort , pagination = defaultPagination } ) => {
17
+ const connection = await config . axios ( ) ;
17
18
const constructedFilter = config . constructFilter ( filter ) ;
18
19
const constructedSort = config . constructSort ( sort ) ;
19
20
const params = {
@@ -23,7 +24,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
23
24
limit : pagination . limit ,
24
25
sort : JSON . stringify ( constructedSort ) ,
25
26
} ;
26
- const response = await Promise . resolve ( config . axios . get ( '' , { params } ) ) ;
27
+ const response = await Promise . resolve ( connection . get ( '' , { params } ) ) ;
27
28
28
29
const entities = response . data . map ( config . constructEntity ) ;
29
30
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';
6
6
7
7
export default < E extends Entity > ( config : FacadeConfig < E > ) : GetEntity < E > => {
8
8
return async ( { id, filter = { } } ) => {
9
+ const connection = await config . axios ( ) ;
9
10
const constructedFilter = config . constructFilter ( filter ) ;
10
11
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 ) => {
12
13
if ( err . response . status === NOT_FOUND ) {
13
14
throw new MissingEntityError ( config . entityName , id ) ;
14
15
}
Original file line number Diff line number Diff line change @@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';
6
6
7
7
export default < E extends Entity > ( config : FacadeConfig < E > ) : PatchEntity < E > => {
8
8
return async ( { id, patch, filter = { } } ) => {
9
+ const connection = await config . axios ( ) ;
9
10
const data = config . constructDocument ( { ...patch as any , id } ) ;
10
11
const constructedFilter = config . constructFilter ( filter ) ;
11
12
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 ) => {
13
14
if ( err . response . status === NOT_FOUND ) {
14
15
throw new MissingEntityError ( config . entityName , id ) ;
15
16
}
Original file line number Diff line number Diff line change @@ -4,8 +4,9 @@ import FacadeConfig from '../FacadeConfig';
4
4
5
5
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntities < E > => {
6
6
return async ( { filter = { } } ) => {
7
+ const connection = await config . axios ( ) ;
7
8
const constructedFilter = config . constructFilter ( filter ) ;
8
9
const params = { filter : JSON . stringify ( constructedFilter ) } ;
9
- await Promise . resolve ( config . axios . delete ( '' , { params } ) ) ;
10
+ await Promise . resolve ( connection . delete ( '' , { params } ) ) ;
10
11
} ;
11
12
} ;
Original file line number Diff line number Diff line change @@ -6,9 +6,10 @@ import FacadeConfig from '../FacadeConfig';
6
6
7
7
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntity < E > => {
8
8
return async ( { id, filter = { } } ) => {
9
+ const connection = await config . axios ( ) ;
9
10
const constructedFilter = config . constructFilter ( filter ) ;
10
11
const params = { filter : JSON . stringify ( constructedFilter ) } ;
11
- await config . axios . delete ( `/${ id } ` , { params } ) . catch ( ( err ) => {
12
+ await connection . delete ( `/${ id } ` , { params } ) . catch ( ( err ) => {
12
13
if ( err . response . status === NOT_FOUND ) {
13
14
throw new MissingEntityError ( config . entityName , id ) ;
14
15
}
Original file line number Diff line number Diff line change @@ -6,10 +6,11 @@ import FacadeConfig from '../FacadeConfig';
6
6
7
7
export default < E extends Entity > ( config : FacadeConfig < E > ) : ReplaceEntity < E > => {
8
8
return async ( { id, entity, filter = { } } ) => {
9
+ const connection = await config . axios ( ) ;
9
10
const data = config . constructDocument ( { ...entity as any , id } ) ;
10
11
const constructedFilter = config . constructFilter ( filter ) ;
11
12
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 ) => {
13
14
if ( err . response . status === NOT_FOUND ) {
14
15
throw new MissingEntityError ( config . entityName , id ) ;
15
16
}
You can’t perform that action at this time.
0 commit comments