@growi/sdk-typescript 1.6.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 (50) hide show
  1. package/CHANGELOG.md +12 -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 +9 -9
  7. package/dist/generated/v3/index.d.ts.map +1 -1
  8. package/dist/generated/v3/index.js +11 -11
  9. package/dist/generated/v3/index.js.map +1 -1
  10. package/dist/generated/v3/index.schemas.d.ts +14 -17
  11. package/dist/generated/v3/index.schemas.d.ts.map +1 -1
  12. package/dist/generated/v3/index.schemas.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/utils/axios-instance-manager.d.ts +27 -0
  18. package/dist/utils/axios-instance-manager.d.ts.map +1 -0
  19. package/dist/utils/axios-instance-manager.js +36 -0
  20. package/dist/utils/axios-instance-manager.js.map +1 -0
  21. package/dist/utils/types/custom-instance.d.ts +6 -0
  22. package/dist/utils/types/custom-instance.d.ts.map +1 -0
  23. package/dist/utils/types/custom-instance.js +2 -0
  24. package/dist/utils/types/custom-instance.js.map +1 -0
  25. package/dist/utils/v1/axios-instance.d.ts +2 -1
  26. package/dist/utils/v1/axios-instance.d.ts.map +1 -1
  27. package/dist/utils/v1/axios-instance.js +9 -5
  28. package/dist/utils/v1/axios-instance.js.map +1 -1
  29. package/dist/utils/v3/axios-instance.d.ts +2 -1
  30. package/dist/utils/v3/axios-instance.d.ts.map +1 -1
  31. package/dist/utils/v3/axios-instance.js +9 -5
  32. package/dist/utils/v3/axios-instance.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/generated/v1/index.schemas.ts +1 -1
  35. package/src/generated/v1/index.ts +1 -1
  36. package/src/generated/v3/index.schemas.ts +15 -18
  37. package/src/generated/v3/index.ts +20 -17
  38. package/src/index.ts +1 -1
  39. package/src/utils/axios-instance-manager.ts +41 -0
  40. package/src/utils/types/custom-instance.ts +6 -0
  41. package/src/utils/v1/axios-instance.test.ts +26 -15
  42. package/src/utils/v1/axios-instance.ts +13 -6
  43. package/src/utils/v3/axios-instance.test.ts +43 -25
  44. package/src/utils/v3/axios-instance.ts +13 -6
  45. package/dist/utils/axios-default-instance.d.ts +0 -14
  46. package/dist/utils/axios-default-instance.d.ts.map +0 -1
  47. package/dist/utils/axios-default-instance.js +0 -22
  48. package/dist/utils/axios-default-instance.js.map +0 -1
  49. package/src/utils/axios-default-instance.test.ts +0 -108
  50. package/src/utils/axios-default-instance.ts +0 -23
@@ -2,7 +2,7 @@
2
2
  * Generated by orval v7.9.0 🍺
3
3
  * Do not edit manually.
4
4
  * GROWI REST API v3
5
- * OpenAPI spec version: 7.3.0-RC.0
5
+ * OpenAPI spec version: 7.3.3-RC.0
6
6
  */
7
7
  export interface SyncStatus {
8
8
  isExecutingSync?: boolean;
@@ -2633,12 +2633,19 @@ export type PostPage201 = {
2633
2633
  revision?: Revision;
2634
2634
  };
2635
2635
 
2636
+ export type PutPageBodyUserRelatedGrantUserGroupIdsItem = {
2637
+ /** Group type */
2638
+ type?: string;
2639
+ /** UserGroup ID */
2640
+ item?: string;
2641
+ };
2642
+
2636
2643
  export type PutPageBody = {
2637
2644
  body: RevisionBody;
2638
2645
  pageId: ObjectId;
2639
2646
  revisionId: ObjectId;
2640
2647
  grant?: PageGrant;
2641
- userRelatedGrantUserGroupIds?: string[];
2648
+ userRelatedGrantUserGroupIds?: PutPageBodyUserRelatedGrantUserGroupIdsItem[];
2642
2649
  /** Determine whether the scopes of descendants should be overwritten */
2643
2650
  overwriteScopesOfDescendants?: boolean;
2644
2651
  /** Determine whether the page is enabled to be posted to Slack */
@@ -2651,15 +2658,11 @@ export type PutPageBody = {
2651
2658
  wip?: boolean;
2652
2659
  };
2653
2660
 
2654
- export type PutPage200Data = {
2661
+ export type PutPage200 = {
2655
2662
  page?: Page;
2656
2663
  revision?: Revision;
2657
2664
  };
2658
2665
 
2659
- export type PutPage200 = {
2660
- data?: PutPage200Data;
2661
- };
2662
-
2663
2666
  export type GetExistForPageParams = {
2664
2667
  /**
2665
2668
  * The path to check for existence
@@ -3245,18 +3248,12 @@ export type GetShareLinks200 = {
3245
3248
  shareLinksResult?: ShareLink[];
3246
3249
  };
3247
3250
 
3248
- export type PostShareLinksParams = {
3249
- /**
3250
- * page id of share link
3251
- */
3251
+ export type PostShareLinksBody = {
3252
+ /** page id of share link */
3252
3253
  relatedPage: string;
3253
- /**
3254
- * expiration date of share link
3255
- */
3254
+ /** expiration date of share link */
3256
3255
  expiredAt?: string;
3257
- /**
3258
- * description of share link
3259
- */
3256
+ /** description of share link */
3260
3257
  description?: string;
3261
3258
  };
3262
3259
 
@@ -3742,7 +3739,7 @@ export type GetUsers200 = {
3742
3739
  paginateResult?: PaginateResult;
3743
3740
  };
3744
3741
 
3745
- export type GetRecentById200 = {
3742
+ export type GetRecentByIdForUsers200 = {
3746
3743
  paginateResult?: PaginateResult;
3747
3744
  };
3748
3745
 
@@ -2,7 +2,7 @@
2
2
  * Generated by orval v7.9.0 🍺
3
3
  * Do not edit manually.
4
4
  * GROWI REST API v3
5
- * OpenAPI spec version: 7.3.0-RC.0
5
+ * OpenAPI spec version: 7.3.3-RC.0
6
6
  */
7
7
  import type {
8
8
  ActivityResponse,
@@ -114,7 +114,7 @@ import type {
114
114
  GetPersonalSetting200,
115
115
  GetPluginTemplatesByLocaleByTemplateIdByReposIdByOrganizationIdForTemplates200,
116
116
  GetPresetTemplatesByLocaleByTemplateIdForTemplates200,
117
- GetRecentById200,
117
+ GetRecentByIdForUsers200,
118
118
  GetRecentForPagesParams,
119
119
  GetRevisionsById200,
120
120
  GetRevisionsByIdParams,
@@ -227,7 +227,7 @@ import type {
227
227
  PostRelationTestByIdForSlackIntegrationSettingsSlackAppIntegrationsBody,
228
228
  PostResumeRenameForPages200,
229
229
  PostResumeRenameForPagesBody,
230
- PostShareLinksParams,
230
+ PostShareLinksBody,
231
231
  PostSlackAppIntegrationsForSlackIntegrationSettings200,
232
232
  PostSmtpTestForAppSettings200,
233
233
  PostTestForSlackIntegrationSettingsWithoutProxyBody,
@@ -1529,16 +1529,16 @@ export const getGrowirestapiv3 = () => {
1529
1529
  * Publish a specific page
1530
1530
  * @summary Publish page
1531
1531
  */
1532
- const putPublishByPageId = (pageId: string, options?: SecondParameter<typeof customInstance>) => {
1533
- return customInstance<Page>({ url: `/${pageId}/publish`, method: 'PUT' }, options);
1532
+ const putPublishByPageIdForPage = (pageId: string, options?: SecondParameter<typeof customInstance>) => {
1533
+ return customInstance<Page>({ url: `/page/${pageId}/publish`, method: 'PUT' }, options);
1534
1534
  };
1535
1535
 
1536
1536
  /**
1537
1537
  * Unpublish a specific page
1538
1538
  * @summary Unpublish page
1539
1539
  */
1540
- const putUnpublishByPageId = (pageId: string, options?: SecondParameter<typeof customInstance>) => {
1541
- return customInstance<Page>({ url: `/${pageId}/unpublish`, method: 'PUT' }, options);
1540
+ const putUnpublishByPageIdForPage = (pageId: string, options?: SecondParameter<typeof customInstance>) => {
1541
+ return customInstance<Page>({ url: `/page/${pageId}/unpublish`, method: 'PUT' }, options);
1542
1542
  };
1543
1543
 
1544
1544
  /**
@@ -2040,8 +2040,11 @@ export const getGrowirestapiv3 = () => {
2040
2040
  /**
2041
2041
  * Create new share link
2042
2042
  */
2043
- const postShareLinks = (params: PostShareLinksParams, options?: SecondParameter<typeof customInstance>) => {
2044
- return customInstance<ShareLinkSimple>({ url: `/share-links/`, method: 'POST', params }, options);
2043
+ const postShareLinks = (postShareLinksBody: PostShareLinksBody, options?: SecondParameter<typeof customInstance>) => {
2044
+ return customInstance<ShareLinkSimple>(
2045
+ { url: `/share-links/`, method: 'POST', headers: { 'Content-Type': 'application/json' }, data: postShareLinksBody },
2046
+ options,
2047
+ );
2045
2048
  };
2046
2049
 
2047
2050
  /**
@@ -2583,8 +2586,8 @@ export const getGrowirestapiv3 = () => {
2583
2586
  /**
2584
2587
  * @summary /usersIdReacent
2585
2588
  */
2586
- const getRecentById = (id: string, options?: SecondParameter<typeof customInstance>) => {
2587
- return customInstance<GetRecentById200>({ url: `/${id}/recent`, method: 'GET' }, options);
2589
+ const getRecentByIdForUsers = (id: string, options?: SecondParameter<typeof customInstance>) => {
2590
+ return customInstance<GetRecentByIdForUsers200>({ url: `/users/${id}/recent`, method: 'GET' }, options);
2588
2591
  };
2589
2592
 
2590
2593
  /**
@@ -2872,8 +2875,8 @@ export const getGrowirestapiv3 = () => {
2872
2875
  getExistPathsForPage,
2873
2876
  putSubscribeForPage,
2874
2877
  putContentWidthByPageId,
2875
- putPublishByPageId,
2876
- putUnpublishByPageId,
2878
+ putPublishByPageIdForPage,
2879
+ putUnpublishByPageIdForPage,
2877
2880
  getYjsDataByPageId,
2878
2881
  putSyncLatestRevisionBodyToYjsDraftByPageId,
2879
2882
  getRecentForPages,
@@ -2970,7 +2973,7 @@ export const getGrowirestapiv3 = () => {
2970
2973
  getPagesByIdForUserGroups,
2971
2974
  putUserUiSettings,
2972
2975
  getUsers,
2973
- getRecentById,
2976
+ getRecentByIdForUsers,
2974
2977
  postInviteForUsers,
2975
2978
  putGrantAdminByIdForUsers,
2976
2979
  putRevokeAdminByIdForUsers,
@@ -3182,8 +3185,8 @@ export type GetExportByPageIdForPageResult = NonNullable<Awaited<ReturnType<Retu
3182
3185
  export type GetExistPathsForPageResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getExistPathsForPage']>>>;
3183
3186
  export type PutSubscribeForPageResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putSubscribeForPage']>>>;
3184
3187
  export type PutContentWidthByPageIdResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putContentWidthByPageId']>>>;
3185
- export type PutPublishByPageIdResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putPublishByPageId']>>>;
3186
- export type PutUnpublishByPageIdResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putUnpublishByPageId']>>>;
3188
+ export type PutPublishByPageIdForPageResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putPublishByPageIdForPage']>>>;
3189
+ export type PutUnpublishByPageIdForPageResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putUnpublishByPageIdForPage']>>>;
3187
3190
  export type GetYjsDataByPageIdResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getYjsDataByPageId']>>>;
3188
3191
  export type PutSyncLatestRevisionBodyToYjsDraftByPageIdResult = NonNullable<
3189
3192
  Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putSyncLatestRevisionBodyToYjsDraftByPageId']>>
@@ -3360,7 +3363,7 @@ export type GetUserGroupRelationsByIdForUserGroupsResult = NonNullable<
3360
3363
  export type GetPagesByIdForUserGroupsResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getPagesByIdForUserGroups']>>>;
3361
3364
  export type PutUserUiSettingsResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putUserUiSettings']>>>;
3362
3365
  export type GetUsersResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getUsers']>>>;
3363
- export type GetRecentByIdResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getRecentById']>>>;
3366
+ export type GetRecentByIdForUsersResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['getRecentByIdForUsers']>>>;
3364
3367
  export type PostInviteForUsersResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['postInviteForUsers']>>>;
3365
3368
  export type PutGrantAdminByIdForUsersResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putGrantAdminByIdForUsers']>>>;
3366
3369
  export type PutRevokeAdminByIdForUsersResult = NonNullable<Awaited<ReturnType<ReturnType<typeof getGrowirestapiv3>['putRevokeAdminByIdForUsers']>>>;
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { AXIOS_DEFAULT } from './utils/axios-default-instance.js';
1
+ export { axiosInstanceManager } from './utils/axios-instance-manager.js';
2
2
 
3
3
  export type * as Apiv1Types from './apiv1/index.js';
4
4
  export type * as Apiv3Types from './apiv3/index.js';
@@ -0,0 +1,41 @@
1
+ import Axios, { type AxiosRequestConfig, type AxiosInstance } from 'axios';
2
+
3
+ /**
4
+ * A singleton class for managing multiple named Axios instances.
5
+ */
6
+ class AxiosInstanceManager {
7
+ private instances: Map<string, AxiosInstance> = new Map();
8
+
9
+ /**
10
+ * Add or update a named Axios instance.
11
+ * @param appName The name/key for the instance.
12
+ * @param config The configuration for the Axios instance.
13
+ */
14
+ addAxiosInstance(config: { appName: string; baseURL: string; token: string }) {
15
+ const axiosInstance = Axios.create({
16
+ baseURL: config.baseURL,
17
+ });
18
+
19
+ // Set the Authorization header
20
+ axiosInstance.defaults.headers.common.Authorization = `Bearer ${config.token}`;
21
+
22
+ this.instances.set(config.appName, axiosInstance);
23
+ }
24
+
25
+ /**
26
+ * Get a named Axios instance.
27
+ * @param appName The name/key of the instance.
28
+ * @returns The AxiosInstance.
29
+ * @throws Error if no instance is found for the given appName.
30
+ */
31
+ getAxiosInstance(appName: string): AxiosInstance {
32
+ const instance = this.instances.get(appName);
33
+ if (!instance) {
34
+ throw new Error(`No Axios instance found for appName: ${appName}`);
35
+ }
36
+ return instance;
37
+ }
38
+ }
39
+
40
+ // Export singleton instance
41
+ export const axiosInstanceManager = new AxiosInstanceManager();
@@ -0,0 +1,6 @@
1
+ import type { AxiosRequestConfig } from 'axios';
2
+
3
+ export type CustomInstanceOptions = {
4
+ appName?: string;
5
+ axiosOptions?: AxiosRequestConfig;
6
+ };
@@ -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 { 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> & {
@@ -52,9 +52,9 @@ describe('v1 customInstance', () => {
52
52
  config: {},
53
53
  } as AxiosResponse<{ result: string }>);
54
54
 
55
- // Mock for AXIOS_DEFAULT
56
- mockedAXIOS_DEFAULT.instance = mockAxiosInstance as unknown as typeof AXIOS_DEFAULT.instance;
57
- Object.defineProperty(mockedAXIOS_DEFAULT.instance, 'defaults', {
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 { 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`;
14
+ const instance = axiosInstanceManager.getAxiosInstance(appName);
15
+ const baseURL = `${axiosOptions?.baseURL ?? config?.baseURL ?? instance.defaults.baseURL ?? ''}/_api`;
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,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"}