api-core-lib 1.1.1 → 2.2.1
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/dist/index.d.mts +22 -3
- package/dist/index.d.ts +22 -3
- package/dist/index.js +35 -0
- package/dist/index.mjs +33 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance } from 'axios';
|
1
|
+
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, AxiosResponse } from 'axios';
|
2
2
|
import * as react from 'react';
|
3
3
|
|
4
4
|
/**
|
@@ -87,12 +87,13 @@ interface RequestConfig extends AxiosRequestConfig {
|
|
87
87
|
* الواجهة الرئيسية لتهيئة عميل الـ API.
|
88
88
|
*/
|
89
89
|
interface ApiClientConfig {
|
90
|
-
baseURL
|
90
|
+
baseURL?: string;
|
91
91
|
tokenManager: TokenManager;
|
92
92
|
timeout?: number;
|
93
93
|
headers?: Record<string, string>;
|
94
94
|
withCredentials?: boolean;
|
95
95
|
responseType?: ResponseType;
|
96
|
+
pathRefreshToken?: string;
|
96
97
|
onRefreshError?: (error: any) => void;
|
97
98
|
}
|
98
99
|
/**
|
@@ -150,6 +151,24 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
150
151
|
|
151
152
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
152
153
|
|
154
|
+
declare class CacheManager {
|
155
|
+
private cache;
|
156
|
+
private defaultDuration;
|
157
|
+
set<T>(key: string, data: T, duration?: number): void;
|
158
|
+
get<T>(key: string): T | null;
|
159
|
+
delete(key: string): void;
|
160
|
+
clear(): void;
|
161
|
+
}
|
162
|
+
declare const cacheManager: CacheManager;
|
163
|
+
|
164
|
+
/**
|
165
|
+
* @file src/core/processor.ts
|
166
|
+
* @description
|
167
|
+
* ...
|
168
|
+
*/
|
169
|
+
|
170
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
171
|
+
|
153
172
|
declare function useApi<T extends {
|
154
173
|
id?: string | number;
|
155
174
|
}>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): {
|
@@ -176,4 +195,4 @@ declare function useApi<T extends {
|
|
176
195
|
};
|
177
196
|
};
|
178
197
|
|
179
|
-
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, createApiClient, createApiServices, useApi };
|
198
|
+
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiClient, createApiServices, processResponse, useApi };
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance } from 'axios';
|
1
|
+
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, AxiosResponse } from 'axios';
|
2
2
|
import * as react from 'react';
|
3
3
|
|
4
4
|
/**
|
@@ -87,12 +87,13 @@ interface RequestConfig extends AxiosRequestConfig {
|
|
87
87
|
* الواجهة الرئيسية لتهيئة عميل الـ API.
|
88
88
|
*/
|
89
89
|
interface ApiClientConfig {
|
90
|
-
baseURL
|
90
|
+
baseURL?: string;
|
91
91
|
tokenManager: TokenManager;
|
92
92
|
timeout?: number;
|
93
93
|
headers?: Record<string, string>;
|
94
94
|
withCredentials?: boolean;
|
95
95
|
responseType?: ResponseType;
|
96
|
+
pathRefreshToken?: string;
|
96
97
|
onRefreshError?: (error: any) => void;
|
97
98
|
}
|
98
99
|
/**
|
@@ -150,6 +151,24 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
150
151
|
|
151
152
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
152
153
|
|
154
|
+
declare class CacheManager {
|
155
|
+
private cache;
|
156
|
+
private defaultDuration;
|
157
|
+
set<T>(key: string, data: T, duration?: number): void;
|
158
|
+
get<T>(key: string): T | null;
|
159
|
+
delete(key: string): void;
|
160
|
+
clear(): void;
|
161
|
+
}
|
162
|
+
declare const cacheManager: CacheManager;
|
163
|
+
|
164
|
+
/**
|
165
|
+
* @file src/core/processor.ts
|
166
|
+
* @description
|
167
|
+
* ...
|
168
|
+
*/
|
169
|
+
|
170
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
171
|
+
|
153
172
|
declare function useApi<T extends {
|
154
173
|
id?: string | number;
|
155
174
|
}>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): {
|
@@ -176,4 +195,4 @@ declare function useApi<T extends {
|
|
176
195
|
};
|
177
196
|
};
|
178
197
|
|
179
|
-
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, createApiClient, createApiServices, useApi };
|
198
|
+
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiClient, createApiServices, processResponse, useApi };
|
package/dist/index.js
CHANGED
@@ -31,8 +31,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
31
31
|
var index_exports = {};
|
32
32
|
__export(index_exports, {
|
33
33
|
buildPaginateQuery: () => buildPaginateQuery,
|
34
|
+
cacheManager: () => cacheManager,
|
34
35
|
createApiClient: () => createApiClient,
|
35
36
|
createApiServices: () => createApiServices,
|
37
|
+
processResponse: () => processResponse,
|
36
38
|
useApi: () => useApi
|
37
39
|
});
|
38
40
|
module.exports = __toCommonJS(index_exports);
|
@@ -248,6 +250,37 @@ function createApiServices(axiosInstance, endpoint) {
|
|
248
250
|
return { get, getWithQuery, post, patch, remove };
|
249
251
|
}
|
250
252
|
|
253
|
+
// src/core/cache.ts
|
254
|
+
var CacheManager = class {
|
255
|
+
cache = /* @__PURE__ */ new Map();
|
256
|
+
defaultDuration = 15 * 60 * 1e3;
|
257
|
+
// 15 minutes
|
258
|
+
set(key, data, duration) {
|
259
|
+
this.cache.set(key, {
|
260
|
+
data,
|
261
|
+
timestamp: Date.now(),
|
262
|
+
duration: duration || this.defaultDuration
|
263
|
+
});
|
264
|
+
}
|
265
|
+
get(key) {
|
266
|
+
const item = this.cache.get(key);
|
267
|
+
if (!item) return null;
|
268
|
+
const isExpired = Date.now() - item.timestamp > item.duration;
|
269
|
+
if (isExpired) {
|
270
|
+
this.cache.delete(key);
|
271
|
+
return null;
|
272
|
+
}
|
273
|
+
return item.data;
|
274
|
+
}
|
275
|
+
delete(key) {
|
276
|
+
this.cache.delete(key);
|
277
|
+
}
|
278
|
+
clear() {
|
279
|
+
this.cache.clear();
|
280
|
+
}
|
281
|
+
};
|
282
|
+
var cacheManager = new CacheManager();
|
283
|
+
|
251
284
|
// src/hooks/useApi.ts
|
252
285
|
var import_react = require("react");
|
253
286
|
function useApi(axiosInstance, config) {
|
@@ -352,7 +385,9 @@ function useApi(axiosInstance, config) {
|
|
352
385
|
// Annotate the CommonJS export names for ESM import in node:
|
353
386
|
0 && (module.exports = {
|
354
387
|
buildPaginateQuery,
|
388
|
+
cacheManager,
|
355
389
|
createApiClient,
|
356
390
|
createApiServices,
|
391
|
+
processResponse,
|
357
392
|
useApi
|
358
393
|
});
|
package/dist/index.mjs
CHANGED
@@ -209,6 +209,37 @@ function createApiServices(axiosInstance, endpoint) {
|
|
209
209
|
return { get, getWithQuery, post, patch, remove };
|
210
210
|
}
|
211
211
|
|
212
|
+
// src/core/cache.ts
|
213
|
+
var CacheManager = class {
|
214
|
+
cache = /* @__PURE__ */ new Map();
|
215
|
+
defaultDuration = 15 * 60 * 1e3;
|
216
|
+
// 15 minutes
|
217
|
+
set(key, data, duration) {
|
218
|
+
this.cache.set(key, {
|
219
|
+
data,
|
220
|
+
timestamp: Date.now(),
|
221
|
+
duration: duration || this.defaultDuration
|
222
|
+
});
|
223
|
+
}
|
224
|
+
get(key) {
|
225
|
+
const item = this.cache.get(key);
|
226
|
+
if (!item) return null;
|
227
|
+
const isExpired = Date.now() - item.timestamp > item.duration;
|
228
|
+
if (isExpired) {
|
229
|
+
this.cache.delete(key);
|
230
|
+
return null;
|
231
|
+
}
|
232
|
+
return item.data;
|
233
|
+
}
|
234
|
+
delete(key) {
|
235
|
+
this.cache.delete(key);
|
236
|
+
}
|
237
|
+
clear() {
|
238
|
+
this.cache.clear();
|
239
|
+
}
|
240
|
+
};
|
241
|
+
var cacheManager = new CacheManager();
|
242
|
+
|
212
243
|
// src/hooks/useApi.ts
|
213
244
|
import { useState, useEffect, useCallback, useRef } from "react";
|
214
245
|
function useApi(axiosInstance, config) {
|
@@ -312,7 +343,9 @@ function useApi(axiosInstance, config) {
|
|
312
343
|
}
|
313
344
|
export {
|
314
345
|
buildPaginateQuery,
|
346
|
+
cacheManager,
|
315
347
|
createApiClient,
|
316
348
|
createApiServices,
|
349
|
+
processResponse,
|
317
350
|
useApi
|
318
351
|
};
|