File tree 5 files changed +79
-2
lines changed
5 files changed +79
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @vue-storefront/middleware " : minor
3
+ ---
4
+
5
+ [ ADDED] an ` init ` function to the ` apiClientFactory ` parameters. It reduces boilerplate code and helps with type suggestions of the input and output of the function.
6
+
7
+ ``` diff
8
+ - const init = (config) => {
9
+ - return {
10
+ - ...config,
11
+ - client: buildClient(config),
12
+ - }
13
+ - }
14
+
15
+ - const { createApiClient } = apiClientFactory({
16
+ + const { createApiClient, init } = apiClientFactory({
17
+ onCreate: (config) => config,
18
+ api: { ... }
19
+ + init: (config) => {
20
+ + return {
21
+ + ...config,
22
+ + client: buildClient(config),
23
+ + }
24
+ + },
25
+ })
26
+
27
+ export const { createApiClient, init };
28
+ ```
Original file line number Diff line number Diff line change
1
+ import { apiClientFactory } from "../../../src/apiClientFactory" ;
2
+ import * as api from "./api" ;
3
+
4
+ const { createApiClient, init } = apiClientFactory ( {
5
+ onCreate : ( config ) => {
6
+ return {
7
+ config,
8
+ client : null ,
9
+ } ;
10
+ } ,
11
+ api,
12
+ init : ( config ) => {
13
+ return {
14
+ ...config ,
15
+ isInit : true ,
16
+ } ;
17
+ } ,
18
+ } ) ;
19
+
20
+ export { createApiClient , init } ;
Original file line number Diff line number Diff line change
1
+ import request from "supertest" ;
2
+ import { createServer } from "../../src/index" ;
3
+
4
+ describe ( "[Integration] Init" , ( ) => {
5
+ it ( "should extend the configuration" , async ( ) => {
6
+ const app = await createServer ( {
7
+ integrations : {
8
+ test_integration : {
9
+ location : "./__tests__/integration/bootstrap/serverWithInit" ,
10
+ configuration : {
11
+ foo : "bar" ,
12
+ } ,
13
+ } ,
14
+ } ,
15
+ } ) ;
16
+
17
+ const { body } = await request ( app )
18
+ . post ( "/test_integration/getConfig" )
19
+ . send ( [ ] ) ;
20
+
21
+ expect ( body ) . toEqual ( {
22
+ foo : "bar" ,
23
+ integrationName : "test_integration" ,
24
+ isInit : true ,
25
+ } ) ;
26
+ } ) ;
27
+ } ) ;
Original file line number Diff line number Diff line change @@ -178,7 +178,8 @@ const apiClientFactory = <
178
178
179
179
createApiClient . _predefinedExtensions = factoryParams . extensions || [ ] ;
180
180
181
- return { createApiClient } ;
181
+ // TODO: Init function is being added to the return type but it's not being added nowhere here.
182
+ return { createApiClient, init : factoryParams . init } ;
182
183
} ;
183
184
184
185
export { apiClientFactory } ;
Original file line number Diff line number Diff line change @@ -113,6 +113,7 @@ export interface ApiClientFactoryParams<
113
113
| Promise < { client : CLIENT ; config : ApiClientConfig } >
114
114
| { client : CLIENT ; config : ApiClientConfig } ;
115
115
extensions ?: ApiClientExtension < API > [ ] ;
116
+ init ?: ( configuration : CONFIG ) => TObject ;
116
117
}
117
118
118
119
export interface ApiClientFactory <
@@ -123,7 +124,7 @@ export interface ApiClientFactory<
123
124
/**
124
125
* Sets up integration config, runs once.
125
126
*/
126
- init ?: ( configuration : TObject ) => TObject ;
127
+ init ?: ( configuration : CONFIG ) => TObject ;
127
128
}
128
129
129
130
export type CreateApiProxyFn = < CONFIG , API , CLIENT > (
You can’t perform that action at this time.
0 commit comments