@growi/sdk-typescript 1.7.0 → 1.8.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/apiv1/index.d.ts +19 -19
  3. package/dist/apiv3/index.d.ts +235 -235
  4. package/dist/generated/v1/index.d.ts +1 -1
  5. package/dist/generated/v1/index.schemas.d.ts +1 -1
  6. package/dist/generated/v3/index.d.ts +1 -1
  7. package/dist/generated/v3/index.schemas.d.ts +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/utils/axios-instance-manager.d.ts +27 -0
  13. package/dist/utils/axios-instance-manager.d.ts.map +1 -0
  14. package/dist/utils/axios-instance-manager.js +36 -0
  15. package/dist/utils/axios-instance-manager.js.map +1 -0
  16. package/dist/utils/types/custom-instance.d.ts +6 -0
  17. package/dist/utils/types/custom-instance.d.ts.map +1 -0
  18. package/dist/utils/types/custom-instance.js +2 -0
  19. package/dist/utils/types/custom-instance.js.map +1 -0
  20. package/dist/utils/v1/axios-instance.d.ts +2 -1
  21. package/dist/utils/v1/axios-instance.d.ts.map +1 -1
  22. package/dist/utils/v1/axios-instance.js +9 -5
  23. package/dist/utils/v1/axios-instance.js.map +1 -1
  24. package/dist/utils/v3/axios-instance.d.ts +2 -1
  25. package/dist/utils/v3/axios-instance.d.ts.map +1 -1
  26. package/dist/utils/v3/axios-instance.js +9 -5
  27. package/dist/utils/v3/axios-instance.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/generated/v1/index.schemas.ts +1 -1
  30. package/src/generated/v1/index.ts +1 -1
  31. package/src/generated/v3/index.schemas.ts +1 -1
  32. package/src/generated/v3/index.ts +1 -1
  33. package/src/index.ts +1 -1
  34. package/src/utils/axios-instance-manager.ts +41 -0
  35. package/src/utils/types/custom-instance.ts +6 -0
  36. package/src/utils/v1/axios-instance.test.ts +26 -15
  37. package/src/utils/v1/axios-instance.ts +13 -6
  38. package/src/utils/v3/axios-instance.test.ts +43 -25
  39. package/src/utils/v3/axios-instance.ts +13 -6
  40. package/dist/utils/axios-default-instance.d.ts +0 -14
  41. package/dist/utils/axios-default-instance.d.ts.map +0 -1
  42. package/dist/utils/axios-default-instance.js +0 -22
  43. package/dist/utils/axios-default-instance.js.map +0 -1
  44. package/src/utils/axios-default-instance.test.ts +0 -108
  45. package/src/utils/axios-default-instance.ts +0 -23
@@ -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 { AXIOS_DEFAULT } from '../axios-default-instance.js';
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-default-instance.js');
15
+ vi.mock('../axios-instance-manager.js');
16
16
 
17
17
  const mockedAxios = vi.mocked(Axios);
18
- const mockedAXIOS_DEFAULT = vi.mocked(AXIOS_DEFAULT);
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
- // Mock for AXIOS_DEFAULT
55
- mockedAXIOS_DEFAULT.instance = mockAxiosInstance as unknown as typeof AXIOS_DEFAULT.instance;
56
- Object.defineProperty(mockedAXIOS_DEFAULT.instance, 'defaults', {
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
- mockedAXIOS_DEFAULT.instance.defaults.baseURL = '';
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 { AXIOS_DEFAULT } from '../axios-default-instance.js';
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 defaultInstance = AXIOS_DEFAULT.instance;
8
- const baseURL = `${options?.baseURL ?? config?.baseURL ?? defaultInstance.defaults.baseURL ?? ''}/_api/v3`;
14
+ const instance = axiosInstanceManager.getAxiosInstance(appName);
15
+ const baseURL = `${axiosOptions?.baseURL ?? config?.baseURL ?? instance.defaults.baseURL ?? ''}/_api/v3`;
9
16
 
10
- const promise = AXIOS_DEFAULT.instance({
17
+ const promise = instance({
11
18
  ...config,
12
- ...options,
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
- };