@growi/sdk-typescript 1.7.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +81 -29
- package/README_JP.md +81 -29
- package/dist/apiv1/index.d.ts +19 -19
- package/dist/apiv3/index.d.ts +239 -235
- package/dist/apiv3/index.d.ts.map +1 -1
- package/dist/generated/v1/index.d.ts +1 -1
- package/dist/generated/v1/index.schemas.d.ts +47 -2
- package/dist/generated/v1/index.schemas.d.ts.map +1 -1
- package/dist/generated/v1/index.schemas.js.map +1 -1
- package/dist/generated/v3/index.d.ts +21 -13
- package/dist/generated/v3/index.d.ts.map +1 -1
- package/dist/generated/v3/index.js +65 -35
- package/dist/generated/v3/index.js.map +1 -1
- package/dist/generated/v3/index.schemas.d.ts +217 -105
- package/dist/generated/v3/index.schemas.d.ts.map +1 -1
- package/dist/generated/v3/index.schemas.js +11 -0
- package/dist/generated/v3/index.schemas.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/axios-instance-manager.d.ts +33 -0
- package/dist/utils/axios-instance-manager.d.ts.map +1 -0
- package/dist/utils/axios-instance-manager.js +59 -0
- package/dist/utils/axios-instance-manager.js.map +1 -0
- package/dist/utils/types/custom-instance.d.ts +6 -0
- package/dist/utils/types/custom-instance.d.ts.map +1 -0
- package/dist/utils/types/custom-instance.js +2 -0
- package/dist/utils/types/custom-instance.js.map +1 -0
- package/dist/utils/v1/axios-instance.d.ts +2 -1
- package/dist/utils/v1/axios-instance.d.ts.map +1 -1
- package/dist/utils/v1/axios-instance.js +9 -5
- package/dist/utils/v1/axios-instance.js.map +1 -1
- package/dist/utils/v3/axios-instance.d.ts +2 -1
- package/dist/utils/v3/axios-instance.d.ts.map +1 -1
- package/dist/utils/v3/axios-instance.js +9 -5
- package/dist/utils/v3/axios-instance.js.map +1 -1
- package/package.json +1 -1
- package/src/generated/v1/index.schemas.ts +49 -2
- package/src/generated/v1/index.ts +1 -1
- package/src/generated/v3/index.schemas.ts +238 -112
- package/src/generated/v3/index.ts +106 -54
- package/src/index.ts +1 -1
- package/src/utils/axios-instance-manager.test.ts +185 -0
- package/src/utils/axios-instance-manager.ts +61 -0
- package/src/utils/types/custom-instance.ts +6 -0
- package/src/utils/v1/axios-instance.test.ts +26 -15
- package/src/utils/v1/axios-instance.ts +13 -6
- package/src/utils/v3/axios-instance.test.ts +43 -25
- package/src/utils/v3/axios-instance.ts +13 -6
- package/dist/utils/axios-default-instance.d.ts +0 -14
- package/dist/utils/axios-default-instance.d.ts.map +0 -1
- package/dist/utils/axios-default-instance.js +0 -22
- package/dist/utils/axios-default-instance.js.map +0 -1
- package/src/utils/axios-default-instance.test.ts +0 -108
- package/src/utils/axios-default-instance.ts +0 -23
|
@@ -7,15 +7,15 @@ import Axios from 'axios';
|
|
|
7
7
|
* including automatic base URL configuration, cancellation capability, and request configuration merging.
|
|
8
8
|
*/
|
|
9
9
|
import { type MockedFunction, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
10
|
-
import {
|
|
10
|
+
import { axiosInstanceManager } from '../axios-instance-manager.js';
|
|
11
11
|
import { customInstance } from './axios-instance.js';
|
|
12
12
|
|
|
13
13
|
// Mock Axios
|
|
14
14
|
vi.mock('axios');
|
|
15
|
-
vi.mock('../axios-
|
|
15
|
+
vi.mock('../axios-instance-manager.js');
|
|
16
16
|
|
|
17
17
|
const mockedAxios = vi.mocked(Axios);
|
|
18
|
-
const
|
|
18
|
+
const mockedAxiosInstanceManager = vi.mocked(axiosInstanceManager);
|
|
19
19
|
|
|
20
20
|
// Type for the promise returned by customInstance with cancel functionality
|
|
21
21
|
type CancellablePromise<T> = Promise<T> & {
|
|
@@ -52,9 +52,9 @@ describe('v1 customInstance', () => {
|
|
|
52
52
|
config: {},
|
|
53
53
|
} as AxiosResponse<{ result: string }>);
|
|
54
54
|
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
Object.defineProperty(
|
|
55
|
+
// Create a mock AxiosInstance with defaults
|
|
56
|
+
const mockInstance = mockAxiosInstance as unknown as AxiosInstance;
|
|
57
|
+
Object.defineProperty(mockInstance, 'defaults', {
|
|
58
58
|
value: {
|
|
59
59
|
baseURL: 'http://localhost',
|
|
60
60
|
headers: {
|
|
@@ -70,6 +70,9 @@ describe('v1 customInstance', () => {
|
|
|
70
70
|
writable: true,
|
|
71
71
|
configurable: true,
|
|
72
72
|
});
|
|
73
|
+
|
|
74
|
+
// Mock for axiosInstanceManager
|
|
75
|
+
mockedAxiosInstanceManager.getAxiosInstance.mockReturnValue(mockInstance);
|
|
73
76
|
});
|
|
74
77
|
|
|
75
78
|
afterEach(() => {
|
|
@@ -82,7 +85,7 @@ describe('v1 customInstance', () => {
|
|
|
82
85
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
83
86
|
|
|
84
87
|
// Act
|
|
85
|
-
await customInstance(config);
|
|
88
|
+
await customInstance(config, { appName: 'testApp' });
|
|
86
89
|
|
|
87
90
|
// Assert
|
|
88
91
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -99,7 +102,7 @@ describe('v1 customInstance', () => {
|
|
|
99
102
|
const options: AxiosRequestConfig = { baseURL: 'https://custom.example.com' };
|
|
100
103
|
|
|
101
104
|
// Act
|
|
102
|
-
await customInstance(config, options);
|
|
105
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
103
106
|
|
|
104
107
|
// Assert
|
|
105
108
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -118,7 +121,7 @@ describe('v1 customInstance', () => {
|
|
|
118
121
|
};
|
|
119
122
|
|
|
120
123
|
// Act
|
|
121
|
-
await customInstance(config);
|
|
124
|
+
await customInstance(config, { appName: 'testApp' });
|
|
122
125
|
|
|
123
126
|
// Assert
|
|
124
127
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -143,7 +146,7 @@ describe('v1 customInstance', () => {
|
|
|
143
146
|
};
|
|
144
147
|
|
|
145
148
|
// Act
|
|
146
|
-
await customInstance(config, options);
|
|
149
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
147
150
|
|
|
148
151
|
// Assert
|
|
149
152
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -170,7 +173,7 @@ describe('v1 customInstance', () => {
|
|
|
170
173
|
};
|
|
171
174
|
|
|
172
175
|
// Act
|
|
173
|
-
await customInstance(config, options);
|
|
176
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
174
177
|
|
|
175
178
|
// Assert
|
|
176
179
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -196,7 +199,7 @@ describe('v1 customInstance', () => {
|
|
|
196
199
|
const config: AxiosRequestConfig = { method: 'GET', url: '/users' };
|
|
197
200
|
|
|
198
201
|
// Act
|
|
199
|
-
const result = await customInstance(config);
|
|
202
|
+
const result = await customInstance(config, { appName: 'testApp' });
|
|
200
203
|
|
|
201
204
|
// Assert
|
|
202
205
|
expect(result).toEqual(expectedData);
|
|
@@ -209,7 +212,7 @@ describe('v1 customInstance', () => {
|
|
|
209
212
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
210
213
|
|
|
211
214
|
// Act
|
|
212
|
-
const promise = customInstance(config) as CancellablePromise<unknown>;
|
|
215
|
+
const promise = customInstance(config, { appName: 'testApp' }) as CancellablePromise<unknown>;
|
|
213
216
|
|
|
214
217
|
// Assert
|
|
215
218
|
expect(promise).toHaveProperty('cancel');
|
|
@@ -224,7 +227,7 @@ describe('v1 customInstance', () => {
|
|
|
224
227
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
225
228
|
|
|
226
229
|
// Act
|
|
227
|
-
const promise = customInstance(config) as CancellablePromise<unknown>;
|
|
230
|
+
const promise = customInstance(config, { appName: 'testApp' }) as CancellablePromise<unknown>;
|
|
228
231
|
promise.cancel();
|
|
229
232
|
|
|
230
233
|
// Assert
|
|
@@ -244,7 +247,15 @@ describe('v1 customInstance', () => {
|
|
|
244
247
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
245
248
|
|
|
246
249
|
// Act & Assert
|
|
247
|
-
await expect(customInstance(config)).rejects.toThrow('Network error');
|
|
250
|
+
await expect(customInstance(config, { appName: 'testApp' })).rejects.toThrow('Network error');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should throw error when appName is not provided', () => {
|
|
254
|
+
// Arrange
|
|
255
|
+
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
256
|
+
|
|
257
|
+
// Act & Assert
|
|
258
|
+
expect(() => customInstance(config)).toThrow('appName is required');
|
|
248
259
|
});
|
|
249
260
|
});
|
|
250
261
|
});
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import Axios, { type AxiosRequestConfig } from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { axiosInstanceManager } from '../axios-instance-manager.js';
|
|
3
|
+
import type { CustomInstanceOptions } from '../types/custom-instance.js';
|
|
4
|
+
|
|
5
|
+
export const customInstance = <T>(config: AxiosRequestConfig, options?: CustomInstanceOptions): Promise<T> => {
|
|
6
|
+
const { appName, axiosOptions } = options ?? {};
|
|
7
|
+
|
|
8
|
+
if (appName == null) {
|
|
9
|
+
throw new Error('appName is required');
|
|
10
|
+
}
|
|
3
11
|
|
|
4
|
-
export const customInstance = <T>(config: AxiosRequestConfig, options?: AxiosRequestConfig): Promise<T> => {
|
|
5
12
|
const source = Axios.CancelToken.source();
|
|
6
13
|
|
|
7
|
-
const
|
|
8
|
-
const baseURL = `${
|
|
14
|
+
const instance = axiosInstanceManager.getAxiosInstance(appName);
|
|
15
|
+
const baseURL = `${axiosOptions?.baseURL ?? config?.baseURL ?? instance.defaults.baseURL ?? ''}/_api`;
|
|
9
16
|
|
|
10
|
-
const promise =
|
|
17
|
+
const promise = instance({
|
|
11
18
|
...config,
|
|
12
|
-
...
|
|
19
|
+
...axiosOptions,
|
|
13
20
|
baseURL,
|
|
14
21
|
cancelToken: source.token,
|
|
15
22
|
}).then(({ data }) => data);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
1
|
+
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
import Axios from 'axios';
|
|
3
3
|
/**
|
|
4
4
|
* Tests for v3/axios-instance.ts
|
|
@@ -7,15 +7,15 @@ import Axios from 'axios';
|
|
|
7
7
|
* including automatic base URL configuration (with v3 suffix), cancellation capability, and request configuration merging.
|
|
8
8
|
*/
|
|
9
9
|
import { type MockedFunction, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
10
|
-
import {
|
|
10
|
+
import { axiosInstanceManager } from '../axios-instance-manager.js';
|
|
11
11
|
import { customInstance } from './axios-instance.js';
|
|
12
12
|
|
|
13
13
|
// Mock Axios
|
|
14
14
|
vi.mock('axios');
|
|
15
|
-
vi.mock('../axios-
|
|
15
|
+
vi.mock('../axios-instance-manager.js');
|
|
16
16
|
|
|
17
17
|
const mockedAxios = vi.mocked(Axios);
|
|
18
|
-
const
|
|
18
|
+
const mockedAxiosInstanceManager = vi.mocked(axiosInstanceManager);
|
|
19
19
|
|
|
20
20
|
// Type for the promise returned by customInstance with cancel functionality
|
|
21
21
|
type CancellablePromise<T> = Promise<T> & {
|
|
@@ -51,9 +51,9 @@ describe('v3 customInstance', () => {
|
|
|
51
51
|
config: {},
|
|
52
52
|
} as AxiosResponse<{ result: string }>);
|
|
53
53
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
Object.defineProperty(
|
|
54
|
+
// Create a mock AxiosInstance with defaults
|
|
55
|
+
const mockInstance = mockAxiosInstance as unknown as AxiosInstance;
|
|
56
|
+
Object.defineProperty(mockInstance, 'defaults', {
|
|
57
57
|
value: {
|
|
58
58
|
baseURL: 'http://localhost',
|
|
59
59
|
headers: {
|
|
@@ -69,6 +69,9 @@ describe('v3 customInstance', () => {
|
|
|
69
69
|
writable: true,
|
|
70
70
|
configurable: true,
|
|
71
71
|
});
|
|
72
|
+
|
|
73
|
+
// Mock for axiosInstanceManager
|
|
74
|
+
mockedAxiosInstanceManager.getAxiosInstance.mockReturnValue(mockInstance);
|
|
72
75
|
});
|
|
73
76
|
|
|
74
77
|
afterEach(() => {
|
|
@@ -81,7 +84,7 @@ describe('v3 customInstance', () => {
|
|
|
81
84
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
82
85
|
|
|
83
86
|
// Act
|
|
84
|
-
await customInstance(config);
|
|
87
|
+
await customInstance(config, { appName: 'testApp' });
|
|
85
88
|
|
|
86
89
|
// Assert
|
|
87
90
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -98,7 +101,7 @@ describe('v3 customInstance', () => {
|
|
|
98
101
|
const options: AxiosRequestConfig = { baseURL: 'https://custom.example.com' };
|
|
99
102
|
|
|
100
103
|
// Act
|
|
101
|
-
await customInstance(config, options);
|
|
104
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
102
105
|
|
|
103
106
|
// Assert
|
|
104
107
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -117,7 +120,7 @@ describe('v3 customInstance', () => {
|
|
|
117
120
|
};
|
|
118
121
|
|
|
119
122
|
// Act
|
|
120
|
-
await customInstance(config);
|
|
123
|
+
await customInstance(config, { appName: 'testApp' });
|
|
121
124
|
|
|
122
125
|
// Assert
|
|
123
126
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -129,11 +132,18 @@ describe('v3 customInstance', () => {
|
|
|
129
132
|
|
|
130
133
|
it('should set only _api/v3 when baseURL is empty string', async () => {
|
|
131
134
|
// Arrange
|
|
132
|
-
|
|
135
|
+
const mockInstance = mockAxiosInstance as unknown as AxiosInstance;
|
|
136
|
+
Object.defineProperty(mockInstance, 'defaults', {
|
|
137
|
+
value: { baseURL: '' },
|
|
138
|
+
writable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
});
|
|
141
|
+
mockedAxiosInstanceManager.getAxiosInstance.mockReturnValue(mockInstance);
|
|
142
|
+
|
|
133
143
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
134
144
|
|
|
135
145
|
// Act
|
|
136
|
-
await customInstance(config);
|
|
146
|
+
await customInstance(config, { appName: 'testApp' });
|
|
137
147
|
|
|
138
148
|
// Assert
|
|
139
149
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -158,7 +168,7 @@ describe('v3 customInstance', () => {
|
|
|
158
168
|
};
|
|
159
169
|
|
|
160
170
|
// Act
|
|
161
|
-
await customInstance(config, options);
|
|
171
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
162
172
|
|
|
163
173
|
// Assert
|
|
164
174
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -187,7 +197,7 @@ describe('v3 customInstance', () => {
|
|
|
187
197
|
};
|
|
188
198
|
|
|
189
199
|
// Act
|
|
190
|
-
await customInstance(config, options);
|
|
200
|
+
await customInstance(config, { appName: 'testApp', axiosOptions: options });
|
|
191
201
|
|
|
192
202
|
// Assert
|
|
193
203
|
expect(mockAxiosInstance).toHaveBeenCalledWith(
|
|
@@ -221,7 +231,7 @@ describe('v3 customInstance', () => {
|
|
|
221
231
|
const config: AxiosRequestConfig = { method: 'GET', url: '/pages' };
|
|
222
232
|
|
|
223
233
|
// Act
|
|
224
|
-
const result = await customInstance(config);
|
|
234
|
+
const result = await customInstance(config, { appName: 'testApp' });
|
|
225
235
|
|
|
226
236
|
// Assert
|
|
227
237
|
expect(result).toEqual(expectedData);
|
|
@@ -241,7 +251,7 @@ describe('v3 customInstance', () => {
|
|
|
241
251
|
const config: AxiosRequestConfig = { method: 'DELETE', url: '/pages/1' };
|
|
242
252
|
|
|
243
253
|
// Act
|
|
244
|
-
const result = await customInstance(config);
|
|
254
|
+
const result = await customInstance(config, { appName: 'testApp' });
|
|
245
255
|
|
|
246
256
|
// Assert
|
|
247
257
|
expect(result).toBeNull();
|
|
@@ -254,7 +264,7 @@ describe('v3 customInstance', () => {
|
|
|
254
264
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
255
265
|
|
|
256
266
|
// Act
|
|
257
|
-
const promise = customInstance(config) as CancellablePromise<unknown>;
|
|
267
|
+
const promise = customInstance(config, { appName: 'testApp' }) as CancellablePromise<unknown>;
|
|
258
268
|
|
|
259
269
|
// Assert
|
|
260
270
|
expect(promise).toHaveProperty('cancel');
|
|
@@ -269,7 +279,7 @@ describe('v3 customInstance', () => {
|
|
|
269
279
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
270
280
|
|
|
271
281
|
// Act
|
|
272
|
-
const promise = customInstance(config) as CancellablePromise<unknown>;
|
|
282
|
+
const promise = customInstance(config, { appName: 'testApp' }) as CancellablePromise<unknown>;
|
|
273
283
|
promise.cancel();
|
|
274
284
|
|
|
275
285
|
// Assert
|
|
@@ -281,8 +291,8 @@ describe('v3 customInstance', () => {
|
|
|
281
291
|
|
|
282
292
|
it('should allow cancellation during request execution', async () => {
|
|
283
293
|
// Arrange
|
|
284
|
-
let resolveRequest: (value: unknown) => void = () => {};
|
|
285
|
-
const requestPromise = new Promise((resolve) => {
|
|
294
|
+
let resolveRequest: (value: AxiosResponse<unknown>) => void = () => {};
|
|
295
|
+
const requestPromise = new Promise<AxiosResponse<unknown>>((resolve) => {
|
|
286
296
|
resolveRequest = resolve;
|
|
287
297
|
});
|
|
288
298
|
|
|
@@ -291,7 +301,7 @@ describe('v3 customInstance', () => {
|
|
|
291
301
|
const config: AxiosRequestConfig = { method: 'GET', url: '/long-request' };
|
|
292
302
|
|
|
293
303
|
// Act
|
|
294
|
-
const promise = customInstance(config) as CancellablePromise<unknown>;
|
|
304
|
+
const promise = customInstance(config, { appName: 'testApp' }) as CancellablePromise<unknown>;
|
|
295
305
|
promise.cancel();
|
|
296
306
|
|
|
297
307
|
// Assert
|
|
@@ -299,7 +309,7 @@ describe('v3 customInstance', () => {
|
|
|
299
309
|
|
|
300
310
|
// cleanup
|
|
301
311
|
if (resolveRequest) {
|
|
302
|
-
resolveRequest({ data: 'test' });
|
|
312
|
+
resolveRequest({ data: 'test' } as AxiosResponse<unknown>);
|
|
303
313
|
}
|
|
304
314
|
await promise;
|
|
305
315
|
});
|
|
@@ -314,7 +324,7 @@ describe('v3 customInstance', () => {
|
|
|
314
324
|
const config: AxiosRequestConfig = { method: 'GET', url: '/invalid' };
|
|
315
325
|
|
|
316
326
|
// Act & Assert
|
|
317
|
-
await expect(customInstance(config)).rejects.toThrow('API v3 endpoint not found');
|
|
327
|
+
await expect(customInstance(config, { appName: 'testApp' })).rejects.toThrow('API v3 endpoint not found');
|
|
318
328
|
});
|
|
319
329
|
|
|
320
330
|
it('should handle network errors appropriately', async () => {
|
|
@@ -326,7 +336,15 @@ describe('v3 customInstance', () => {
|
|
|
326
336
|
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
327
337
|
|
|
328
338
|
// Act & Assert
|
|
329
|
-
await expect(customInstance(config)).rejects.toThrow('Network Error');
|
|
339
|
+
await expect(customInstance(config, { appName: 'testApp' })).rejects.toThrow('Network Error');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should throw error when appName is not provided', () => {
|
|
343
|
+
// Arrange
|
|
344
|
+
const config: AxiosRequestConfig = { method: 'GET', url: '/test' };
|
|
345
|
+
|
|
346
|
+
// Act & Assert
|
|
347
|
+
expect(() => customInstance(config)).toThrow('appName is required');
|
|
330
348
|
});
|
|
331
349
|
});
|
|
332
350
|
|
|
@@ -356,7 +374,7 @@ describe('v3 customInstance', () => {
|
|
|
356
374
|
const config: AxiosRequestConfig = { method: 'GET', url: '/user/1' };
|
|
357
375
|
|
|
358
376
|
// Act
|
|
359
|
-
const result = await customInstance<User>(config);
|
|
377
|
+
const result = await customInstance<User>(config, { appName: 'testApp' });
|
|
360
378
|
|
|
361
379
|
// Assert
|
|
362
380
|
expect(result).toEqual(expectedUser);
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import Axios, { type AxiosRequestConfig } from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { axiosInstanceManager } from '../axios-instance-manager.js';
|
|
3
|
+
import type { CustomInstanceOptions } from '../types/custom-instance.js';
|
|
4
|
+
|
|
5
|
+
export const customInstance = <T>(config: AxiosRequestConfig, options?: CustomInstanceOptions): Promise<T> => {
|
|
6
|
+
const { appName, axiosOptions } = options ?? {};
|
|
7
|
+
|
|
8
|
+
if (appName == null) {
|
|
9
|
+
throw new Error('appName is required');
|
|
10
|
+
}
|
|
3
11
|
|
|
4
|
-
export const customInstance = <T>(config: AxiosRequestConfig, options?: AxiosRequestConfig): Promise<T> => {
|
|
5
12
|
const source = Axios.CancelToken.source();
|
|
6
13
|
|
|
7
|
-
const
|
|
8
|
-
const baseURL = `${
|
|
14
|
+
const instance = axiosInstanceManager.getAxiosInstance(appName);
|
|
15
|
+
const baseURL = `${axiosOptions?.baseURL ?? config?.baseURL ?? instance.defaults.baseURL ?? ''}/_api/v3`;
|
|
9
16
|
|
|
10
|
-
const promise =
|
|
17
|
+
const promise = instance({
|
|
11
18
|
...config,
|
|
12
|
-
...
|
|
19
|
+
...axiosOptions,
|
|
13
20
|
baseURL,
|
|
14
21
|
cancelToken: source.token,
|
|
15
22
|
}).then(({ data }) => data);
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare const AXIOS_DEFAULT: {
|
|
2
|
-
instance: import("axios").AxiosInstance;
|
|
3
|
-
/**
|
|
4
|
-
* Set the base URL for the default Axios instance.
|
|
5
|
-
* @param baseURL The base URL to set for the Axios instance.
|
|
6
|
-
*/
|
|
7
|
-
setBaseURL: (baseURL: string) => void;
|
|
8
|
-
/**
|
|
9
|
-
* Set the Authorization header for the default Axios instance.
|
|
10
|
-
* @param token The authentication token to set in the Authorization header.
|
|
11
|
-
*/
|
|
12
|
-
setAuthorizationHeader: (token: string) => void;
|
|
13
|
-
};
|
|
14
|
-
//# sourceMappingURL=axios-default-instance.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"axios-default-instance.d.ts","sourceRoot":"","sources":["../../src/utils/axios-default-instance.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,aAAa;;IAExB;;;OAGG;0BACmB,MAAM,KAAG,IAAI;IAGnC;;;OAGG;oCAC6B,MAAM,KAAG,IAAI;CAG9C,CAAC"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import Axios from 'axios';
|
|
2
|
-
const DEFAULT_AXIOS_INSTANCE = Axios.create({
|
|
3
|
-
baseURL: 'http://localhost', // set baseURL if you need
|
|
4
|
-
});
|
|
5
|
-
export const AXIOS_DEFAULT = {
|
|
6
|
-
instance: DEFAULT_AXIOS_INSTANCE,
|
|
7
|
-
/**
|
|
8
|
-
* Set the base URL for the default Axios instance.
|
|
9
|
-
* @param baseURL The base URL to set for the Axios instance.
|
|
10
|
-
*/
|
|
11
|
-
setBaseURL: (baseURL) => {
|
|
12
|
-
DEFAULT_AXIOS_INSTANCE.defaults.baseURL = baseURL;
|
|
13
|
-
},
|
|
14
|
-
/**
|
|
15
|
-
* Set the Authorization header for the default Axios instance.
|
|
16
|
-
* @param token The authentication token to set in the Authorization header.
|
|
17
|
-
*/
|
|
18
|
-
setAuthorizationHeader: (token) => {
|
|
19
|
-
DEFAULT_AXIOS_INSTANCE.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
//# sourceMappingURL=axios-default-instance.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"axios-default-instance.js","sourceRoot":"","sources":["../../src/utils/axios-default-instance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAkC,MAAM,OAAO,CAAC;AAEvD,MAAM,sBAAsB,GAAG,KAAK,CAAC,MAAM,CAAC;IAC1C,OAAO,EAAE,kBAAkB,EAAE,0BAA0B;CACxD,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,QAAQ,EAAE,sBAAsB;IAChC;;;OAGG;IACH,UAAU,EAAE,CAAC,OAAe,EAAQ,EAAE;QACpC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC;IACpD,CAAC;IACD;;;OAGG;IACH,sBAAsB,EAAE,CAAC,KAAa,EAAQ,EAAE;QAC9C,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,GAAG,UAAU,KAAK,EAAE,CAAC;IACnF,CAAC;CACF,CAAC"}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { AxiosInstance } from 'axios';
|
|
2
|
-
/**
|
|
3
|
-
* Tests for axios-default-instance.ts
|
|
4
|
-
*
|
|
5
|
-
* This file manages the default Axios instance for GROWI SDK,
|
|
6
|
-
* providing functionality to configure base URL and authentication headers.
|
|
7
|
-
*/
|
|
8
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
9
|
-
import { AXIOS_DEFAULT } from './axios-default-instance.js';
|
|
10
|
-
|
|
11
|
-
describe('AXIOS_DEFAULT', () => {
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
// Reset the instance before each test
|
|
14
|
-
AXIOS_DEFAULT.instance.defaults.baseURL = 'http://localhost';
|
|
15
|
-
AXIOS_DEFAULT.instance.defaults.headers.common.Authorization = undefined;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('setBaseURL', () => {
|
|
19
|
-
it('should update base URL when setting a valid URL', () => {
|
|
20
|
-
// Arrange
|
|
21
|
-
const newBaseURL = 'https://api.example.com';
|
|
22
|
-
|
|
23
|
-
// Act
|
|
24
|
-
AXIOS_DEFAULT.setBaseURL(newBaseURL);
|
|
25
|
-
|
|
26
|
-
// Assert
|
|
27
|
-
expect(AXIOS_DEFAULT.instance.defaults.baseURL).toBe(newBaseURL);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should set base URL to empty string when empty string is provided', () => {
|
|
31
|
-
// Arrange
|
|
32
|
-
const emptyURL = '';
|
|
33
|
-
|
|
34
|
-
// Act
|
|
35
|
-
AXIOS_DEFAULT.setBaseURL(emptyURL);
|
|
36
|
-
|
|
37
|
-
// Assert
|
|
38
|
-
expect(AXIOS_DEFAULT.instance.defaults.baseURL).toBe(emptyURL);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should use the last value when called multiple times', () => {
|
|
42
|
-
// Arrange
|
|
43
|
-
const firstURL = 'https://first.example.com';
|
|
44
|
-
const secondURL = 'https://second.example.com';
|
|
45
|
-
|
|
46
|
-
// Act
|
|
47
|
-
AXIOS_DEFAULT.setBaseURL(firstURL);
|
|
48
|
-
AXIOS_DEFAULT.setBaseURL(secondURL);
|
|
49
|
-
|
|
50
|
-
// Assert
|
|
51
|
-
expect(AXIOS_DEFAULT.instance.defaults.baseURL).toBe(secondURL);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('setAuthorizationHeader', () => {
|
|
56
|
-
it('should set authorization header in Bearer token format when valid token is provided', () => {
|
|
57
|
-
// Arrange
|
|
58
|
-
const token = 'test-token-123';
|
|
59
|
-
const expectedHeader = `Bearer ${token}`;
|
|
60
|
-
|
|
61
|
-
// Act
|
|
62
|
-
AXIOS_DEFAULT.setAuthorizationHeader(token);
|
|
63
|
-
|
|
64
|
-
// Assert
|
|
65
|
-
expect(AXIOS_DEFAULT.instance.defaults.headers.common.Authorization).toBe(expectedHeader);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should set Bearer header with empty string when empty token is provided', () => {
|
|
69
|
-
// Arrange
|
|
70
|
-
const emptyToken = '';
|
|
71
|
-
const expectedHeader = 'Bearer ';
|
|
72
|
-
|
|
73
|
-
// Act
|
|
74
|
-
AXIOS_DEFAULT.setAuthorizationHeader(emptyToken);
|
|
75
|
-
|
|
76
|
-
// Assert
|
|
77
|
-
expect(AXIOS_DEFAULT.instance.defaults.headers.common.Authorization).toBe(expectedHeader);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('should use the last token when called multiple times', () => {
|
|
81
|
-
// Arrange
|
|
82
|
-
const firstToken = 'first-token';
|
|
83
|
-
const secondToken = 'second-token';
|
|
84
|
-
const expectedHeader = `Bearer ${secondToken}`;
|
|
85
|
-
|
|
86
|
-
// Act
|
|
87
|
-
AXIOS_DEFAULT.setAuthorizationHeader(firstToken);
|
|
88
|
-
AXIOS_DEFAULT.setAuthorizationHeader(secondToken);
|
|
89
|
-
|
|
90
|
-
// Assert
|
|
91
|
-
expect(AXIOS_DEFAULT.instance.defaults.headers.common.Authorization).toBe(expectedHeader);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe('instance', () => {
|
|
96
|
-
it('should have default base URL set to localhost', () => {
|
|
97
|
-
// Assert
|
|
98
|
-
expect(AXIOS_DEFAULT.instance.defaults.baseURL).toBe('http://localhost');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should have Axios instance properly created', () => {
|
|
102
|
-
// Assert
|
|
103
|
-
expect(AXIOS_DEFAULT.instance).toBeDefined();
|
|
104
|
-
expect(typeof AXIOS_DEFAULT.instance).toBe('function');
|
|
105
|
-
expect(AXIOS_DEFAULT.instance.defaults).toBeDefined();
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import Axios, { type AxiosRequestConfig } from 'axios';
|
|
2
|
-
|
|
3
|
-
const DEFAULT_AXIOS_INSTANCE = Axios.create({
|
|
4
|
-
baseURL: 'http://localhost', // set baseURL if you need
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
export const AXIOS_DEFAULT = {
|
|
8
|
-
instance: DEFAULT_AXIOS_INSTANCE,
|
|
9
|
-
/**
|
|
10
|
-
* Set the base URL for the default Axios instance.
|
|
11
|
-
* @param baseURL The base URL to set for the Axios instance.
|
|
12
|
-
*/
|
|
13
|
-
setBaseURL: (baseURL: string): void => {
|
|
14
|
-
DEFAULT_AXIOS_INSTANCE.defaults.baseURL = baseURL;
|
|
15
|
-
},
|
|
16
|
-
/**
|
|
17
|
-
* Set the Authorization header for the default Axios instance.
|
|
18
|
-
* @param token The authentication token to set in the Authorization header.
|
|
19
|
-
*/
|
|
20
|
-
setAuthorizationHeader: (token: string): void => {
|
|
21
|
-
DEFAULT_AXIOS_INSTANCE.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
22
|
-
},
|
|
23
|
-
};
|