supaapps-api-kit-client 0.4.0 → 0.5.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.
@@ -24,6 +24,9 @@ jobs:
24
24
  - name: Install dependencies
25
25
  run: npm install
26
26
 
27
+ - name: Test
28
+ run: npm test
29
+
27
30
  - name: Build
28
31
  run: npm run build
29
32
  env:
@@ -21,6 +21,9 @@ jobs:
21
21
  - name: Install dependencies
22
22
  run: npm install
23
23
 
24
+ - name: Test
25
+ run: npm test
26
+
24
27
  - name: Build
25
28
  run: npm run build
26
29
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # supaapps-api-kit-client
2
2
 
3
- A versatile, type-safe API client designed for TypeScript applications. It simplifies making HTTP requests and handling responses with built-in support for authorization and automatic handling of unauthorized access scenarios.
3
+ A versatile, type-safe API client designed for TypeScript applications. It simplifies making HTTP requests and handling responses with built-in support for authorization and automatic handling of unauthorized access scenarios, integrates with `supaapps-auth` to handle <b>Automatic Token Refresh</b> and <b>Callbacks</b> when auth fails.
4
4
 
5
5
  ## Features
6
6
 
@@ -42,10 +42,31 @@ const useAuth: boolean = // Optional: default is false, use true if you want to
42
42
  // in case of using auth
43
43
  ApiKitClient.initialize(BASE_URL, authTokenCallback, unauthorizationCallback, true);
44
44
 
45
+
45
46
  // in case of not using auth
46
47
  ApiKitClient.initialize(BASE_URL);
48
+
49
+ ```
50
+
51
+ You can initialize multiple ApiClient instances with different configurations if needed.
52
+ here is ways to initialize multiple instances
53
+
54
+ ```ts
55
+
56
+ import {ApiKitClient} from "./ApiKitClient";
57
+
58
+ const apiClient1 = (new ApiKitClient()).initialize(BASE_URL); // this initialize apiClient with key 'default'
59
+ const apiClient2 = (new ApiKitClient('default')).initialize(BASE_URL); // this initialize apiClient with 'default' as well
60
+ const apiClient3 = (new ApiKitClient('public')).initialize(BASE_URL); // this initialize apiClient with 'public'
61
+
62
+ // set key with method
63
+ const apiClient4 = ApiKitClient.i('public').initialize(BASE_URL); // this initialize apiClient with 'public' key
64
+
65
+ const apiClient5 = ApiKitClient.i('billing').initialize(BASE_URL, authTokenCallback, unauthorizationCallback, true); // this initialize apiClient with 'billing' key
66
+
47
67
  ```
48
68
 
69
+
49
70
  ## Making Requests
50
71
 
51
72
  Use the `ApiKitClient` instance to make API requests. Here are some examples:
@@ -1,19 +1,35 @@
1
- import { AxiosResponse } from 'axios';
1
+ import { AxiosInstance, AxiosResponse } from 'axios';
2
2
  import { PaginatedResponse } from './types';
3
- type UnauthorizationCallback = () => void;
3
+ type UnAuthorizedCallback = () => void;
4
4
  type AuthTokenCallback = () => Promise<string>;
5
+ type ApiClient = {
6
+ baseURL: string;
7
+ authTokenCallback?: AuthTokenCallback;
8
+ unauthorizedCallback?: UnAuthorizedCallback;
9
+ useAuth: boolean;
10
+ axiosInstance: AxiosInstance;
11
+ };
5
12
  export declare class ApiKitClient {
6
- private static instance;
7
- private static authTokenCallback?;
8
- private static unauthorizationCallback?;
9
- private static useAuth;
10
- static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback, useAuth?: boolean): void;
11
- private static setupInterceptors;
13
+ private apiClientKey;
14
+ static apiClients: Record<string, ApiClient>;
15
+ static i(configInstance: string): ApiKitClient;
16
+ private constructor();
17
+ initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizedCallback?: UnAuthorizedCallback, useAuth?: boolean): void;
18
+ private setupInterceptors;
19
+ private checkInitialization;
12
20
  private static checkInitialization;
21
+ get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>>;
22
+ getOne<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>>;
23
+ getPaginated<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<PaginatedResponse<T>>>;
24
+ post<T>(endpoint: string, data?: T): Promise<AxiosResponse<T>>;
25
+ put<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>>;
26
+ patch<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>>;
27
+ delete<T>(endpoint: string): Promise<AxiosResponse<T>>;
28
+ static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizedCallback?: UnAuthorizedCallback, useAuth?: boolean): void;
13
29
  static get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>>;
14
30
  static getOne<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>>;
15
31
  static getPaginated<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<PaginatedResponse<T>>>;
16
- static post<T>(endpoint: string, data: T): Promise<AxiosResponse<T>>;
32
+ static post<T>(endpoint: string, data?: T): Promise<AxiosResponse<T>>;
17
33
  static put<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>>;
18
34
  static patch<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>>;
19
35
  static delete<T>(endpoint: string): Promise<AxiosResponse<T>>;
@@ -12,79 +12,140 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ApiKitClient = void 0;
13
13
  const axios_1 = require("axios");
14
14
  class ApiKitClient {
15
- static initialize(baseURL, authTokenCallback, unauthorizationCallback, useAuth = false) {
15
+ static i(configInstance) {
16
+ return new ApiKitClient(configInstance);
17
+ }
18
+ constructor(apiClientKey = 'default') {
19
+ this.apiClientKey = apiClientKey;
20
+ }
21
+ initialize(baseURL, authTokenCallback, unauthorizedCallback, useAuth = false) {
16
22
  if (useAuth && !authTokenCallback) {
17
23
  throw new Error("authTokenCallback must be provided if useAuth is true.");
18
24
  }
19
- this.authTokenCallback = authTokenCallback;
20
- this.unauthorizationCallback = unauthorizationCallback;
21
- this.useAuth = useAuth;
22
- this.instance = axios_1.default.create({ baseURL });
23
- this.setupInterceptors();
24
- }
25
- static setupInterceptors() {
26
- this.instance.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
27
- if (this.useAuth && this.authTokenCallback) {
28
- const authToken = yield this.authTokenCallback();
25
+ ApiKitClient.apiClients[this.apiClientKey] = {
26
+ baseURL,
27
+ authTokenCallback,
28
+ unauthorizedCallback: unauthorizedCallback,
29
+ useAuth,
30
+ axiosInstance: axios_1.default.create({ baseURL }),
31
+ };
32
+ (new ApiKitClient(this.apiClientKey)).setupInterceptors();
33
+ }
34
+ setupInterceptors() {
35
+ const apiClient = ApiKitClient.apiClients[this.apiClientKey];
36
+ apiClient.axiosInstance.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
37
+ if (apiClient.useAuth && apiClient.authTokenCallback) {
38
+ const authToken = yield apiClient.authTokenCallback();
29
39
  config.headers.Authorization = `Bearer ${authToken}`;
30
40
  }
31
41
  return config;
32
42
  }));
33
- this.instance.interceptors.response.use((response) => response, (error) => {
43
+ apiClient.axiosInstance.interceptors.response.use((response) => response, (error) => {
34
44
  var _a;
35
- if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 && this.unauthorizationCallback) {
36
- this.unauthorizationCallback();
45
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 && apiClient.unauthorizedCallback) {
46
+ apiClient.unauthorizedCallback();
37
47
  }
38
48
  return Promise.reject(error);
39
49
  });
40
50
  }
51
+ checkInitialization() {
52
+ if (!ApiKitClient.apiClients[this.apiClientKey]) {
53
+ throw new Error(`ApiKitClient has not been initialized for '${this.apiClientKey}' apiClientKey . Please call ApiKitClient.i('${this.apiClientKey}').initialize() before making requests.`);
54
+ }
55
+ }
41
56
  static checkInitialization() {
42
- if (!this.instance) {
57
+ if (!this.apiClients.default) {
43
58
  throw new Error("ApiKitClient has not been initialized. Please call ApiKitClient.initialize() before making requests.");
44
59
  }
45
60
  }
46
- static get(endpoint, params) {
61
+ get(endpoint, params) {
47
62
  return __awaiter(this, void 0, void 0, function* () {
48
63
  this.checkInitialization();
49
- return this.instance.get(endpoint, { params });
64
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
65
+ return axiosInstance.get(endpoint, { params });
50
66
  });
51
67
  }
52
- static getOne(endpoint, params) {
68
+ getOne(endpoint, params) {
53
69
  return __awaiter(this, void 0, void 0, function* () {
54
70
  this.checkInitialization();
55
- return this.instance.get(endpoint, { params });
71
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
72
+ return axiosInstance.get(endpoint, { params });
56
73
  });
57
74
  }
58
- static getPaginated(endpoint, params) {
75
+ getPaginated(endpoint, params) {
59
76
  return __awaiter(this, void 0, void 0, function* () {
60
77
  this.checkInitialization();
61
- return this.instance.get(endpoint, { params });
78
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
79
+ return axiosInstance.get(endpoint, { params });
62
80
  });
63
81
  }
64
- static post(endpoint, data) {
82
+ post(endpoint, data) {
65
83
  return __awaiter(this, void 0, void 0, function* () {
66
84
  this.checkInitialization();
67
- return this.instance.post(endpoint, data);
85
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
86
+ return axiosInstance.post(endpoint, data);
68
87
  });
69
88
  }
70
- static put(endpoint, data) {
89
+ put(endpoint, data) {
71
90
  return __awaiter(this, void 0, void 0, function* () {
72
91
  this.checkInitialization();
73
- return this.instance.put(endpoint, data);
92
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
93
+ return axiosInstance.put(endpoint, data);
74
94
  });
75
95
  }
76
- static patch(endpoint, data) {
96
+ patch(endpoint, data) {
77
97
  return __awaiter(this, void 0, void 0, function* () {
78
98
  this.checkInitialization();
79
- return this.instance.patch(endpoint, data);
99
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
100
+ return axiosInstance.patch(endpoint, data);
80
101
  });
81
102
  }
82
- static delete(endpoint) {
103
+ delete(endpoint) {
83
104
  return __awaiter(this, void 0, void 0, function* () {
84
105
  this.checkInitialization();
85
- return this.instance.delete(endpoint);
106
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
107
+ return axiosInstance.delete(endpoint);
108
+ });
109
+ }
110
+ // backward compatible methods
111
+ static initialize(baseURL, authTokenCallback, unauthorizedCallback, useAuth = false) {
112
+ new ApiKitClient('default').initialize(baseURL, authTokenCallback, unauthorizedCallback, useAuth);
113
+ }
114
+ static get(endpoint, params) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ return (new ApiKitClient('default')).get(endpoint, params);
117
+ });
118
+ }
119
+ static getOne(endpoint, params) {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ return (new ApiKitClient('default')).getOne(endpoint, params);
122
+ });
123
+ }
124
+ static getPaginated(endpoint, params) {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ return (new ApiKitClient('default')).getPaginated(endpoint, params);
127
+ });
128
+ }
129
+ static post(endpoint, data) {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ return (new ApiKitClient('default')).post(endpoint, data);
132
+ });
133
+ }
134
+ static put(endpoint, data) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ return (new ApiKitClient('default')).put(endpoint, data);
137
+ });
138
+ }
139
+ static patch(endpoint, data) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ return (new ApiKitClient('default')).patch(endpoint, data);
142
+ });
143
+ }
144
+ static delete(endpoint) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ return (new ApiKitClient('default')).delete(endpoint);
86
147
  });
87
148
  }
88
149
  }
89
150
  exports.ApiKitClient = ApiKitClient;
90
- ApiKitClient.useAuth = false;
151
+ ApiKitClient.apiClients = {};
package/jest.config.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * For a detailed explanation regarding each configuration property, visit:
3
+ * https://jestjs.io/docs/configuration
4
+ */
5
+
6
+ import type {Config} from 'jest';
7
+
8
+ const config: Config = {
9
+ // All imported modules in your tests should be mocked automatically
10
+ // automock: false,
11
+
12
+ // Stop running tests after `n` failures
13
+ // bail: 0,
14
+
15
+ // The directory where Jest should store its cached dependency information
16
+ // cacheDirectory: "/private/var/folders/_m/mvw8154d7jl5gjvxw7j66wn00000gn/T/jest_dx",
17
+
18
+ // Automatically clear mock calls, instances, contexts and results before every test
19
+ clearMocks: true,
20
+
21
+ // Indicates whether the coverage information should be collected while executing the test
22
+ collectCoverage: true,
23
+
24
+ // An array of glob patterns indicating a set of files for which coverage information should be collected
25
+ // collectCoverageFrom: undefined,
26
+
27
+ // The directory where Jest should output its coverage files
28
+ coverageDirectory: "coverage",
29
+
30
+ // An array of regexp pattern strings used to skip coverage collection
31
+ // coveragePathIgnorePatterns: [
32
+ // "/node_modules/"
33
+ // ],
34
+
35
+ // Indicates which provider should be used to instrument code for coverage
36
+ coverageProvider: "v8",
37
+
38
+ // A list of reporter names that Jest uses when writing coverage reports
39
+ // coverageReporters: [
40
+ // "json",
41
+ // "text",
42
+ // "lcov",
43
+ // "clover"
44
+ // ],
45
+
46
+ // An object that configures minimum threshold enforcement for coverage results
47
+ // coverageThreshold: undefined,
48
+
49
+ // A path to a custom dependency extractor
50
+ // dependencyExtractor: undefined,
51
+
52
+ // Make calling deprecated APIs throw helpful error messages
53
+ // errorOnDeprecated: false,
54
+
55
+ // The default configuration for fake timers
56
+ // fakeTimers: {
57
+ // "enableGlobally": false
58
+ // },
59
+
60
+ // Force coverage collection from ignored files using an array of glob patterns
61
+ // forceCoverageMatch: [],
62
+
63
+ // A path to a module which exports an async function that is triggered once before all test suites
64
+ // globalSetup: undefined,
65
+
66
+ // A path to a module which exports an async function that is triggered once after all test suites
67
+ // globalTeardown: undefined,
68
+
69
+ // A set of global variables that need to be available in all test environments
70
+ // globals: {},
71
+
72
+ // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
73
+ // maxWorkers: "50%",
74
+
75
+ // An array of directory names to be searched recursively up from the requiring module's location
76
+ // moduleDirectories: [
77
+ // "node_modules"
78
+ // ],
79
+
80
+ // An array of file extensions your modules use
81
+ // moduleFileExtensions: [
82
+ // "js",
83
+ // "mjs",
84
+ // "cjs",
85
+ // "jsx",
86
+ // "ts",
87
+ // "tsx",
88
+ // "json",
89
+ // "node"
90
+ // ],
91
+
92
+ // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
93
+ // moduleNameMapper: {},
94
+
95
+ // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
96
+ // modulePathIgnorePatterns: [],
97
+
98
+ // Activates notifications for test results
99
+ // notify: false,
100
+
101
+ // An enum that specifies notification mode. Requires { notify: true }
102
+ // notifyMode: "failure-change",
103
+
104
+ // A preset that is used as a base for Jest's configuration
105
+ preset: 'ts-jest',
106
+
107
+ // Run tests from one or more projects
108
+ // projects: undefined,
109
+
110
+ // Use this configuration option to add custom reporters to Jest
111
+ // reporters: undefined,
112
+
113
+ // Automatically reset mock state before every test
114
+ // resetMocks: false,
115
+
116
+ // Reset the module registry before running each individual test
117
+ // resetModules: false,
118
+
119
+ // A path to a custom resolver
120
+ // resolver: undefined,
121
+
122
+ // Automatically restore mock state and implementation before every test
123
+ // restoreMocks: false,
124
+
125
+ // The root directory that Jest should scan for tests and modules within
126
+ // rootDir: undefined,
127
+
128
+ // A list of paths to directories that Jest should use to search for files in
129
+ // roots: [
130
+ // "<rootDir>"
131
+ // ],
132
+
133
+ // Allows you to use a custom runner instead of Jest's default test runner
134
+ // runner: "jest-runner",
135
+
136
+ // The paths to modules that run some code to configure or set up the testing environment before each test
137
+ // setupFiles: [],
138
+
139
+ // A list of paths to modules that run some code to configure or set up the testing framework before each test
140
+ // setupFilesAfterEnv: [],
141
+
142
+ // The number of seconds after which a test is considered as slow and reported as such in the results.
143
+ // slowTestThreshold: 5,
144
+
145
+ // A list of paths to snapshot serializer modules Jest should use for snapshot testing
146
+ // snapshotSerializers: [],
147
+
148
+ // The test environment that will be used for testing
149
+ testEnvironment: "jsdom",
150
+
151
+ // Options that will be passed to the testEnvironment
152
+ // testEnvironmentOptions: {},
153
+
154
+ // Adds a location field to test results
155
+ // testLocationInResults: false,
156
+
157
+ // The glob patterns Jest uses to detect test files
158
+ // testMatch: [
159
+ // "**/__tests__/**/*.[jt]s?(x)",
160
+ // "**/?(*.)+(spec|test).[tj]s?(x)"
161
+ // ],
162
+
163
+ // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
164
+ // testPathIgnorePatterns: [
165
+ // "/node_modules/"
166
+ // ],
167
+
168
+ // The regexp pattern or array of patterns that Jest uses to detect test files
169
+ // testRegex: [],
170
+
171
+ // This option allows the use of a custom results processor
172
+ // testResultsProcessor: undefined,
173
+
174
+ // This option allows use of a custom test runner
175
+ // testRunner: "jest-circus/runner",
176
+
177
+ // A map from regular expressions to paths to transformers
178
+ // transform: undefined,
179
+ moduleFileExtensions: ['ts', 'js'], // Recognize .ts and .js files
180
+ transform: {
181
+ '^.+\\.ts$': 'ts-jest', // Transform TypeScript files using ts-jest
182
+ },
183
+ transformIgnorePatterns: ['/node_modules/'], // Ignore node_modules by default
184
+
185
+ // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
186
+ // transformIgnorePatterns: [
187
+ // "/node_modules/",
188
+ // "\\.pnp\\.[^\\/]+$"
189
+ // ],
190
+
191
+
192
+ // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
193
+ // unmockedModulePathPatterns: undefined,
194
+
195
+ // Indicates whether each individual test should be reported during the run
196
+ // verbose: undefined,
197
+
198
+ // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
199
+ // watchPathIgnorePatterns: [],
200
+
201
+ // Whether to use watchman for file crawling
202
+ // watchman: true,
203
+ };
204
+
205
+ export default config;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "supaapps-api-kit-client",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "A versatile, type-safe API kit client designed for TypeScript applications.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1",
8
+ "test": "jest",
9
9
  "build": "tsc"
10
10
  },
11
11
  "author": "Supaapps GmbH",
@@ -15,7 +15,13 @@
15
15
  },
16
16
  "devDependencies": {
17
17
  "@types/axios": "^0.14.0",
18
+ "@types/jest": "^29.5.14",
18
19
  "@types/node": "^20.11.17",
19
- "typescript": "^5.3.3"
20
+ "axios-mock-adapter": "^2.1.0",
21
+ "jest": "^29.7.0",
22
+ "jest-environment-jsdom": "^29.7.0",
23
+ "ts-jest": "^29.2.5",
24
+ "ts-node": "^10.9.2",
25
+ "typescript": "^5.6.3"
20
26
  }
21
27
  }
@@ -1,84 +1,153 @@
1
1
  import axios, { AxiosInstance, AxiosResponse, AxiosError } from 'axios';
2
2
  import { PaginatedResponse } from './types';
3
3
 
4
- type UnauthorizationCallback = () => void;
4
+ type UnAuthorizedCallback = () => void;
5
5
  type AuthTokenCallback = () => Promise<string>;
6
+ type ApiClient = {
7
+ baseURL: string;
8
+ authTokenCallback?: AuthTokenCallback;
9
+ unauthorizedCallback?: UnAuthorizedCallback;
10
+ useAuth: boolean;
11
+ axiosInstance: AxiosInstance;
12
+ };
13
+
6
14
 
7
15
  export class ApiKitClient {
8
- private static instance: AxiosInstance;
9
- private static authTokenCallback?: AuthTokenCallback;
10
- private static unauthorizationCallback?: UnauthorizationCallback;
11
- private static useAuth: boolean = false;
12
-
13
- public static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback, useAuth: boolean = false): void {
14
- if (useAuth && !authTokenCallback) {
15
- throw new Error("authTokenCallback must be provided if useAuth is true.");
16
+ private apiClientKey: string;
17
+ public static apiClients: Record<string, ApiClient> = {};
18
+
19
+ public static i(configInstance: string): ApiKitClient {
20
+ return new ApiKitClient(configInstance);
21
+ }
22
+
23
+ private constructor(apiClientKey: string = 'default') {
24
+ this.apiClientKey = apiClientKey;
16
25
  }
17
26
 
18
- this.authTokenCallback = authTokenCallback;
19
- this.unauthorizationCallback = unauthorizationCallback;
20
- this.useAuth = useAuth;
21
27
 
22
- this.instance = axios.create({ baseURL });
28
+ public initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizedCallback?: UnAuthorizedCallback, useAuth: boolean = false): void {
29
+ if (useAuth && !authTokenCallback) {
30
+ throw new Error("authTokenCallback must be provided if useAuth is true.");
31
+ }
23
32
 
24
- this.setupInterceptors();
25
- }
33
+ ApiKitClient.apiClients[this.apiClientKey] = {
34
+ baseURL,
35
+ authTokenCallback,
36
+ unauthorizedCallback: unauthorizedCallback,
37
+ useAuth,
38
+ axiosInstance: axios.create({ baseURL }),
39
+ };
40
+ (new ApiKitClient(this.apiClientKey)).setupInterceptors();
26
41
 
27
- private static setupInterceptors(): void {
28
- this.instance.interceptors.request.use(async (config) => {
29
- if (this.useAuth && this.authTokenCallback) {
30
- const authToken = await this.authTokenCallback();
42
+ }
43
+
44
+
45
+
46
+ private setupInterceptors(): void {
47
+ const apiClient = ApiKitClient.apiClients[this.apiClientKey];
48
+ apiClient.axiosInstance.interceptors.request.use(async (config) => {
49
+ if (apiClient.useAuth && apiClient.authTokenCallback) {
50
+ const authToken = await apiClient.authTokenCallback();
31
51
  config.headers.Authorization = `Bearer ${authToken}`;
32
52
  }
33
53
  return config;
34
54
  });
35
55
 
36
- this.instance.interceptors.response.use((response: AxiosResponse) => response, (error: AxiosError) => {
37
- if (error.response?.status === 401 && this.unauthorizationCallback) {
38
- this.unauthorizationCallback();
56
+ apiClient.axiosInstance.interceptors.response.use((response: AxiosResponse) => response, (error: AxiosError) => {
57
+ if (error.response?.status === 401 && apiClient.unauthorizedCallback) {
58
+ apiClient.unauthorizedCallback();
39
59
  }
40
60
  return Promise.reject(error);
41
61
  });
42
62
  }
43
63
 
64
+ private checkInitialization(): void {
65
+ if (!ApiKitClient.apiClients[this.apiClientKey]) {
66
+ throw new Error(`ApiKitClient has not been initialized for '${this.apiClientKey}' apiClientKey . Please call ApiKitClient.i('${this.apiClientKey}').initialize() before making requests.`);
67
+ }
68
+ }
69
+
44
70
  private static checkInitialization(): void {
45
- if (!this.instance) {
71
+ if (!this.apiClients.default) {
46
72
  throw new Error("ApiKitClient has not been initialized. Please call ApiKitClient.initialize() before making requests.");
47
73
  }
48
74
  }
49
75
 
50
- public static async get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
76
+
77
+ public async get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
51
78
  this.checkInitialization();
52
- return this.instance!.get<T>(endpoint, { params });
79
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
80
+ return axiosInstance!.get<T>(endpoint, { params });
53
81
  }
54
82
 
55
- public static async getOne<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
83
+ public async getOne<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
56
84
  this.checkInitialization();
57
- return this.instance!.get<T>(endpoint, { params });
85
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
86
+ return axiosInstance!.get<T>(endpoint, { params });
58
87
  }
59
88
 
60
- public static async getPaginated<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<PaginatedResponse<T>>> {
89
+ public async getPaginated<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<PaginatedResponse<T>>> {
61
90
  this.checkInitialization();
62
- return this.instance!.get<PaginatedResponse<T>>(endpoint, { params });
91
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
92
+ return axiosInstance!.get<PaginatedResponse<T>>(endpoint, { params });
63
93
  }
64
94
 
65
- public static async post<T>(endpoint: string, data: T): Promise<AxiosResponse<T>> {
95
+ public async post<T>(endpoint: string, data?: T): Promise<AxiosResponse<T>> {
66
96
  this.checkInitialization();
67
- return this.instance!.post<T>(endpoint, data);
97
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
98
+ return axiosInstance!.post<T>(endpoint, data);
68
99
  }
69
100
 
70
- public static async put<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
101
+ public async put<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
71
102
  this.checkInitialization();
72
- return this.instance!.put<T>(endpoint, data);
103
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
104
+ return axiosInstance!.put<T>(endpoint, data);
73
105
  }
74
106
 
75
- public static async patch<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
107
+ public async patch<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
76
108
  this.checkInitialization();
77
- return this.instance!.patch<T>(endpoint, data);
109
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
110
+ return axiosInstance!.patch<T>(endpoint, data);
78
111
  }
79
112
 
80
- public static async delete<T>(endpoint: string): Promise<AxiosResponse<T>> {
113
+ public async delete<T>(endpoint: string): Promise<AxiosResponse<T>> {
81
114
  this.checkInitialization();
82
- return this.instance!.delete<T>(endpoint);
115
+ const axiosInstance = ApiKitClient.apiClients[this.apiClientKey].axiosInstance;
116
+ return axiosInstance!.delete<T>(endpoint);
117
+ }
118
+
119
+
120
+ // backward compatible methods
121
+
122
+ public static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizedCallback?: UnAuthorizedCallback, useAuth: boolean = false): void {
123
+ new ApiKitClient('default').initialize(baseURL, authTokenCallback, unauthorizedCallback, useAuth);
124
+ }
125
+
126
+ public static async get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
127
+ return (new ApiKitClient('default')).get<T>(endpoint, params);
128
+ }
129
+
130
+ public static async getOne<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>> {
131
+ return (new ApiKitClient('default')).getOne<T>(endpoint, params);
132
+ }
133
+
134
+ public static async getPaginated<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<PaginatedResponse<T>>> {
135
+ return (new ApiKitClient('default')).getPaginated<T>(endpoint, params);
136
+ }
137
+
138
+ public static async post<T>(endpoint: string, data?: T): Promise<AxiosResponse<T>> {
139
+ return (new ApiKitClient('default')).post<T>(endpoint, data);
140
+ }
141
+
142
+ public static async put<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
143
+ return (new ApiKitClient('default')).put<T>(endpoint, data);
144
+ }
145
+
146
+ public static async patch<T>(endpoint: string, data: Partial<T>): Promise<AxiosResponse<T>> {
147
+ return (new ApiKitClient('default')).patch<T>(endpoint, data);
148
+ }
149
+
150
+ public static async delete<T>(endpoint: string): Promise<AxiosResponse<T>> {
151
+ return (new ApiKitClient('default')).delete<T>(endpoint);
83
152
  }
84
153
  }
@@ -0,0 +1,91 @@
1
+ import axios from 'axios';
2
+ import MockAdapter from 'axios-mock-adapter';
3
+ import { ApiKitClient } from '../src/ApiKitClient';
4
+
5
+ describe('ApiKitClient', () => {
6
+ // @ts-ignore
7
+ let mockAxios;
8
+
9
+ beforeEach(() => {
10
+ // Initialize mock for Axios
11
+ mockAxios = new MockAdapter(axios);
12
+ });
13
+
14
+ afterEach(() => {
15
+ mockAxios.reset();
16
+ });
17
+
18
+ it('should initialize the client with correct configuration', () => {
19
+ const baseURL = 'https://api.example.com';
20
+ const authTokenCallback = jest.fn(() => Promise.resolve('fake-token'));
21
+ const unauthorizedCallback = jest.fn();
22
+
23
+ const client = ApiKitClient.i('testClient');
24
+ client.initialize(baseURL, authTokenCallback, unauthorizedCallback, true);
25
+
26
+ const apiClient = ApiKitClient.apiClients['testClient'];
27
+ expect(apiClient.baseURL).toBe(baseURL);
28
+ expect(apiClient.authTokenCallback).toBe(authTokenCallback);
29
+ expect(apiClient.unauthorizedCallback).toBe(unauthorizedCallback);
30
+ expect(apiClient.useAuth).toBe(true);
31
+ });
32
+
33
+ it('should throw an error if authTokenCallback is not provided when useAuth is true', () => {
34
+ const baseURL = 'https://api.example.com';
35
+ const client = ApiKitClient.i('testClient');
36
+ expect(() => client.initialize(baseURL, undefined, undefined, true)).toThrowError(
37
+ 'authTokenCallback must be provided if useAuth is true.'
38
+ );
39
+ });
40
+
41
+ it('should make a GET request successfully', async () => {
42
+ const baseURL = 'https://api.example.com';
43
+ const endpoint = '/test-endpoint';
44
+ const responseData = { success: true };
45
+
46
+ mockAxios.onGet(`${baseURL}${endpoint}`).reply(200, responseData);
47
+
48
+ const client = ApiKitClient.i('testClient');
49
+ client.initialize(baseURL);
50
+
51
+ const response = await client.get(endpoint);
52
+ expect(response.data).toEqual(responseData);
53
+ });
54
+
55
+ it('should add Authorization header when useAuth is true', async () => {
56
+ const baseURL = 'https://api.example.com';
57
+ const endpoint = '/auth-endpoint';
58
+ const authToken = 'test-token';
59
+ const responseData = { authorized: true };
60
+
61
+ mockAxios.onGet(`${baseURL}${endpoint}`).reply(200, responseData);
62
+
63
+ const authTokenCallback = jest.fn(() => Promise.resolve(authToken));
64
+ const client = ApiKitClient.i('authClient');
65
+ client.initialize(baseURL, authTokenCallback, undefined, true);
66
+
67
+ const response = await client.get(endpoint);
68
+
69
+ expect(authTokenCallback).toHaveBeenCalled();
70
+ expect(mockAxios.history.get[0].headers.Authorization).toBe(`Bearer ${authToken}`);
71
+ expect(response.data).toEqual(responseData);
72
+ });
73
+
74
+ it('should call unauthorizedCallback on 401 response', async () => {
75
+ const baseURL = 'https://api.example.com';
76
+ const endpoint = '/unauthorized-endpoint';
77
+
78
+ mockAxios.onGet(`${baseURL}${endpoint}`).reply(401);
79
+
80
+ const unauthorizedCallback = jest.fn();
81
+ const client = ApiKitClient.i('unauthorizedClient');
82
+ client.initialize(baseURL, undefined, unauthorizedCallback);
83
+
84
+ try {
85
+ await client.get(endpoint);
86
+ } catch (error) {
87
+ // Ensure unauthorizedCallback is called
88
+ expect(unauthorizedCallback).toHaveBeenCalled();
89
+ }
90
+ });
91
+ });