api-core-lib 1.1.0 → 2.1.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 +20 -2
- package/dist/index.d.ts +20 -2
- 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
|
/**
|
@@ -150,6 +150,24 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
150
150
|
|
151
151
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
152
152
|
|
153
|
+
declare class CacheManager {
|
154
|
+
private cache;
|
155
|
+
private defaultDuration;
|
156
|
+
set<T>(key: string, data: T, duration?: number): void;
|
157
|
+
get<T>(key: string): T | null;
|
158
|
+
delete(key: string): void;
|
159
|
+
clear(): void;
|
160
|
+
}
|
161
|
+
declare const cacheManager: CacheManager;
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @file src/core/processor.ts
|
165
|
+
* @description
|
166
|
+
* ...
|
167
|
+
*/
|
168
|
+
|
169
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
170
|
+
|
153
171
|
declare function useApi<T extends {
|
154
172
|
id?: string | number;
|
155
173
|
}>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): {
|
@@ -176,4 +194,4 @@ declare function useApi<T extends {
|
|
176
194
|
};
|
177
195
|
};
|
178
196
|
|
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 };
|
197
|
+
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
|
/**
|
@@ -150,6 +150,24 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
150
150
|
|
151
151
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
152
152
|
|
153
|
+
declare class CacheManager {
|
154
|
+
private cache;
|
155
|
+
private defaultDuration;
|
156
|
+
set<T>(key: string, data: T, duration?: number): void;
|
157
|
+
get<T>(key: string): T | null;
|
158
|
+
delete(key: string): void;
|
159
|
+
clear(): void;
|
160
|
+
}
|
161
|
+
declare const cacheManager: CacheManager;
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @file src/core/processor.ts
|
165
|
+
* @description
|
166
|
+
* ...
|
167
|
+
*/
|
168
|
+
|
169
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
170
|
+
|
153
171
|
declare function useApi<T extends {
|
154
172
|
id?: string | number;
|
155
173
|
}>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): {
|
@@ -176,4 +194,4 @@ declare function useApi<T extends {
|
|
176
194
|
};
|
177
195
|
};
|
178
196
|
|
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 };
|
197
|
+
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
|
};
|