api-def 0.12.0 → 0.13.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.
- package/README.md +3 -0
- package/bin/index.js +312 -313
- package/cjs/Api.d.ts +6 -6
- package/cjs/Api.js +99 -143
- package/cjs/ApiTypes.d.ts +1 -1
- package/cjs/ApiUtils.d.ts +2 -1
- package/cjs/ApiUtils.js +41 -33
- package/cjs/Endpoint.d.ts +1 -1
- package/cjs/Endpoint.js +112 -175
- package/cjs/EndpointBuilder.js +23 -32
- package/cjs/QueryHandling.js +12 -13
- package/cjs/RequestConfig.js +54 -36
- package/cjs/RequestContext.d.ts +1 -1
- package/cjs/RequestContext.js +125 -180
- package/cjs/RequestError.js +16 -19
- package/cjs/Requester.js +230 -312
- package/cjs/TextDecoding.js +31 -31
- package/cjs/Utils.js +20 -23
- package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
- package/cjs/backend/AxiosRequestBackend.js +65 -172
- package/cjs/backend/FetchRequestBackend.d.ts +1 -1
- package/cjs/backend/FetchRequestBackend.js +108 -154
- package/cjs/backend/MockRequestBackend.d.ts +1 -1
- package/cjs/backend/MockRequestBackend.js +146 -208
- package/cjs/cache/ClientCaching.js +26 -74
- package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
- package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
- package/cjs/index.d.ts +11 -11
- package/cjs/index.js +24 -21
- package/cjs/middleware/ClientCacheMiddleware.js +80 -103
- package/cjs/middleware/LoggingMiddleware.js +71 -42
- package/cjs/util/retry/index.js +42 -9
- package/cjs/util/retry/lib/retry.js +25 -27
- package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
- package/cjs/util/retry/lib/retryOperation.js +21 -23
- package/esm/Api.d.ts +11 -11
- package/esm/Api.js +12 -23
- package/esm/ApiConstants.d.ts +1 -1
- package/esm/ApiTypes.d.ts +5 -5
- package/esm/ApiUtils.d.ts +4 -3
- package/esm/ApiUtils.js +15 -7
- package/esm/Endpoint.d.ts +6 -6
- package/esm/Endpoint.js +22 -28
- package/esm/EndpointBuilder.d.ts +4 -4
- package/esm/EndpointBuilder.js +2 -2
- package/esm/MockingTypes.d.ts +1 -1
- package/esm/QueryHandling.d.ts +1 -1
- package/esm/QueryHandling.js +2 -3
- package/esm/RequestConfig.d.ts +1 -1
- package/esm/RequestConfig.js +6 -7
- package/esm/RequestContext.d.ts +7 -7
- package/esm/RequestContext.js +15 -27
- package/esm/RequestError.d.ts +3 -3
- package/esm/RequestError.js +2 -3
- package/esm/Requester.d.ts +2 -2
- package/esm/Requester.js +44 -50
- package/esm/UtilTypes.d.ts +1 -1
- package/esm/Utils.js +2 -1
- package/esm/Validation.d.ts +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +4 -4
- package/esm/backend/AxiosRequestBackend.js +47 -84
- package/esm/backend/FetchRequestBackend.d.ts +5 -5
- package/esm/backend/FetchRequestBackend.js +49 -63
- package/esm/backend/MockRequestBackend.d.ts +4 -4
- package/esm/backend/MockRequestBackend.js +105 -136
- package/esm/backend/RequestBackend.d.ts +2 -2
- package/esm/cache/ClientCaching.d.ts +1 -1
- package/esm/cache/ClientCaching.js +8 -17
- package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalForageClientCacheBackend.js +8 -25
- package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
- package/esm/index.d.ts +16 -16
- package/esm/index.js +15 -15
- package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
- package/esm/middleware/ClientCacheMiddleware.js +8 -17
- package/esm/middleware/LoggingMiddleware.d.ts +1 -1
- package/esm/middleware/LoggingMiddleware.js +5 -6
- package/esm/util/retry/index.js +1 -1
- package/esm/util/retry/lib/retry.d.ts +1 -1
- package/esm/util/retry/lib/retry.js +13 -7
- package/esm/util/retry/lib/retryOperation.d.ts +1 -26
- package/esm/util/retry/lib/retryOperation.js +1 -1
- package/package.json +66 -28
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { CacheSource, EventResultType, RequestEvent, RequestMethod } from "../ApiConstants";
|
|
11
|
-
import * as ClientCaching from "../cache/ClientCaching";
|
|
1
|
+
import { CacheSource, EventResultType, RequestEvent, RequestMethod } from "../ApiConstants.js";
|
|
2
|
+
import * as ClientCaching from "../cache/ClientCaching.js";
|
|
12
3
|
const ClientCacheMiddleware = (options = {}) => {
|
|
13
4
|
return {
|
|
14
|
-
[RequestEvent.SUCCESS]: (context) =>
|
|
5
|
+
[RequestEvent.SUCCESS]: async (context) => {
|
|
15
6
|
if (context.method !== RequestMethod.GET)
|
|
16
7
|
return;
|
|
17
8
|
const { clientCache } = context.requestConfig || {};
|
|
@@ -20,17 +11,17 @@ const ClientCacheMiddleware = (options = {}) => {
|
|
|
20
11
|
const expiry = typeof clientCache === "number"
|
|
21
12
|
? clientCache
|
|
22
13
|
: options.defaultExpiry || ClientCaching.DEFAULT_CLIENT_CACHE_EXPIRY;
|
|
23
|
-
|
|
14
|
+
await ClientCaching.setClientCachedItem(context.key, context.response, expiry);
|
|
24
15
|
}
|
|
25
|
-
}
|
|
26
|
-
[RequestEvent.BEFORE_SEND]: (context) =>
|
|
16
|
+
},
|
|
17
|
+
[RequestEvent.BEFORE_SEND]: async (context) => {
|
|
27
18
|
if (context.method !== RequestMethod.GET)
|
|
28
19
|
return;
|
|
29
20
|
const { clientCache } = context.requestConfig || {};
|
|
30
21
|
const shouldCache = !options.predicate || options.predicate();
|
|
31
22
|
if (clientCache && shouldCache) {
|
|
32
23
|
if (clientCache) {
|
|
33
|
-
const cachedValue =
|
|
24
|
+
const cachedValue = await ClientCaching.getClientCachedItem(context.key);
|
|
34
25
|
if (cachedValue) {
|
|
35
26
|
context.stats.cached = {
|
|
36
27
|
is: true,
|
|
@@ -54,7 +45,7 @@ const ClientCacheMiddleware = (options = {}) => {
|
|
|
54
45
|
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
55
46
|
});
|
|
56
47
|
}
|
|
57
|
-
}
|
|
48
|
+
},
|
|
58
49
|
};
|
|
59
50
|
};
|
|
60
51
|
export default ClientCacheMiddleware;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { RequestEvent } from "../ApiConstants";
|
|
2
|
-
import * as ApiUtils from "../ApiUtils";
|
|
3
|
-
import * as Utils from "../Utils";
|
|
1
|
+
import { RequestEvent } from "../ApiConstants.js";
|
|
2
|
+
import * as ApiUtils from "../ApiUtils.js";
|
|
3
|
+
import * as Utils from "../Utils.js";
|
|
4
4
|
var LogType;
|
|
5
5
|
(function (LogType) {
|
|
6
6
|
LogType[LogType["INFO"] = 0] = "INFO";
|
|
@@ -25,7 +25,7 @@ const diagnoseError = (error) => {
|
|
|
25
25
|
return { message: "client-side error", error };
|
|
26
26
|
}
|
|
27
27
|
const { status, data } = error.response;
|
|
28
|
-
const code = data
|
|
28
|
+
const code = data?.code;
|
|
29
29
|
return {
|
|
30
30
|
message: `responded with ${status}${code ? ` (${code})` : ""}`,
|
|
31
31
|
response: error.response,
|
|
@@ -57,9 +57,8 @@ const LoggingMiddleware = (config = {}) => {
|
|
|
57
57
|
log(context, LogType.INFO, context.stats.attempt > 1 ? "retrying" : "sending", config);
|
|
58
58
|
},
|
|
59
59
|
[RequestEvent.SUCCESS]: (context) => {
|
|
60
|
-
var _a;
|
|
61
60
|
const cacheSource = context.cacheInfo.source;
|
|
62
|
-
log(context, LogType.SUCCESS, `responded with ${
|
|
61
|
+
log(context, LogType.SUCCESS, `responded with ${context.response?.status}${cacheSource ? ` (cached by ${cacheSource})` : ""}`, config);
|
|
63
62
|
},
|
|
64
63
|
[RequestEvent.ERROR]: (context) => {
|
|
65
64
|
if (context.error) {
|
package/esm/util/retry/index.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import RetryOperation from "./retryOperation";
|
|
1
|
+
import RetryOperation from "./retryOperation.js";
|
|
2
2
|
export const operation = (options) => {
|
|
3
3
|
const timeouts = _timeouts(options);
|
|
4
4
|
return new RetryOperation(timeouts, {
|
|
5
5
|
forever: options && (options.forever || options.retries === Number.POSITIVE_INFINITY),
|
|
6
|
-
unref: options
|
|
7
|
-
maxRetryTime: options
|
|
6
|
+
unref: options?.unref,
|
|
7
|
+
maxRetryTime: options?.maxRetryTime,
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
const _timeouts = (options) => {
|
|
11
|
-
var _a;
|
|
12
11
|
const createTimeout = (attempt, opts) => {
|
|
13
12
|
const random = opts.randomize ? Math.random() + 1 : 1;
|
|
14
13
|
let timeout = Math.round(random * Math.max(opts.minTimeout, 1) * opts.factor ** attempt);
|
|
@@ -16,16 +15,23 @@ const _timeouts = (options) => {
|
|
|
16
15
|
return timeout;
|
|
17
16
|
};
|
|
18
17
|
const defaultRetries = 10;
|
|
19
|
-
const opts =
|
|
18
|
+
const opts = {
|
|
19
|
+
retries: defaultRetries,
|
|
20
|
+
factor: 2,
|
|
21
|
+
minTimeout: 1 * 1000,
|
|
22
|
+
maxTimeout: Number.POSITIVE_INFINITY,
|
|
23
|
+
randomize: false,
|
|
24
|
+
...options,
|
|
25
|
+
};
|
|
20
26
|
if (opts.minTimeout > opts.maxTimeout) {
|
|
21
27
|
throw new Error("minTimeout is greater than maxTimeout");
|
|
22
28
|
}
|
|
23
29
|
const timeouts = [];
|
|
24
|
-
const numRetries =
|
|
30
|
+
const numRetries = opts.retries ?? defaultRetries;
|
|
25
31
|
for (let i = 0; i < numRetries; i++) {
|
|
26
32
|
timeouts.push(createTimeout(i, opts));
|
|
27
33
|
}
|
|
28
|
-
if (
|
|
34
|
+
if (options?.forever && !timeouts.length) {
|
|
29
35
|
timeouts.push(createTimeout(numRetries, opts));
|
|
30
36
|
}
|
|
31
37
|
// sort the array numerically ascending
|
|
@@ -1,27 +1,2 @@
|
|
|
1
|
-
export default RetryOperation;
|
|
2
1
|
declare function RetryOperation(timeouts: any, options: any): void;
|
|
3
|
-
|
|
4
|
-
constructor(timeouts: any, options: any);
|
|
5
|
-
_originalTimeouts: any;
|
|
6
|
-
_timeouts: any;
|
|
7
|
-
_options: any;
|
|
8
|
-
_maxRetryTime: any;
|
|
9
|
-
_fn: any;
|
|
10
|
-
_errors: any[];
|
|
11
|
-
_attempts: number;
|
|
12
|
-
_operationTimeout: any;
|
|
13
|
-
_operationTimeoutCb: any;
|
|
14
|
-
_timeout: NodeJS.Timeout | null;
|
|
15
|
-
_operationStart: number | null;
|
|
16
|
-
_timer: NodeJS.Timeout | null;
|
|
17
|
-
_cachedTimeouts: any;
|
|
18
|
-
reset(): void;
|
|
19
|
-
stop(): void;
|
|
20
|
-
retry(err: any): boolean;
|
|
21
|
-
attempt(fn: any, timeoutOps: any): void;
|
|
22
|
-
try(fn: any): void;
|
|
23
|
-
start: (fn: any) => void;
|
|
24
|
-
errors(): any[];
|
|
25
|
-
attempts(): number;
|
|
26
|
-
mainError(): any;
|
|
27
|
-
}
|
|
2
|
+
export default RetryOperation;
|
|
@@ -2,7 +2,7 @@ function RetryOperation(timeouts, options) {
|
|
|
2
2
|
this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
|
|
3
3
|
this._timeouts = timeouts;
|
|
4
4
|
this._options = options || {};
|
|
5
|
-
this._maxRetryTime =
|
|
5
|
+
this._maxRetryTime = options?.maxRetryTime || Number.POSITIVE_INFINITY;
|
|
6
6
|
this._fn = null;
|
|
7
7
|
this._errors = [];
|
|
8
8
|
this._attempts = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-def",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Typed API definitions with middleware support",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"types": "esm/index.d.ts",
|
|
@@ -8,60 +8,98 @@
|
|
|
8
8
|
"bin": {
|
|
9
9
|
"api-def": "bin/index.js"
|
|
10
10
|
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=22"
|
|
13
|
+
},
|
|
11
14
|
"sideEffects": false,
|
|
12
15
|
"scripts": {
|
|
13
16
|
"generate:fixtures": "bun scripts/generateFixtures.ts",
|
|
14
17
|
"prepublishOnly": "npm run check:fix && npm run test && npm run build",
|
|
15
|
-
"test": "jest",
|
|
18
|
+
"test": "jest --watchman=false",
|
|
16
19
|
"example:start": "cd example && npm run start",
|
|
17
|
-
"check:fix": "
|
|
18
|
-
"check": "
|
|
20
|
+
"check:fix": "tsgo --noEmit --project tsconfig.json && npx @biomejs/biome check --write",
|
|
21
|
+
"check": "tsgo --noEmit --project tsconfig.json && npx @biomejs/biome check",
|
|
19
22
|
"cleanup": "rimraf esm && rimraf cjs",
|
|
20
23
|
"build": "npm run cleanup && npm run build:esm && npm run build:cjs && npm run build:cli",
|
|
21
|
-
"build:esm": "
|
|
22
|
-
"build:cjs": "
|
|
24
|
+
"build:esm": "tsgo --project tsconfig.esm.json --outDir esm --preserveWatchOutput && tsc-alias --project tsconfig.esm.json --outDir esm --resolve-full-paths --resolve-full-extension .js && rm -rf esm/tests",
|
|
25
|
+
"build:cjs": "tsgo --project tsconfig.cjs.json --outDir cjs --preserveWatchOutput && rm -rf cjs/tests",
|
|
23
26
|
"build:cli": "esbuild cli/index.ts --bundle --platform=node --outfile=bin/index.js --minify --legal-comments=none",
|
|
24
27
|
"website:dev": "cd website && npm run start",
|
|
25
28
|
"website:deploy": "cd website && npm run deploy"
|
|
26
29
|
},
|
|
27
|
-
"
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./esm/index.d.ts",
|
|
33
|
+
"import": "./esm/index.js",
|
|
34
|
+
"require": "./cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"typescript",
|
|
40
|
+
"javascript",
|
|
41
|
+
"node",
|
|
42
|
+
"web",
|
|
43
|
+
"api",
|
|
44
|
+
"typed",
|
|
45
|
+
"cache",
|
|
46
|
+
"fetch",
|
|
47
|
+
"retry",
|
|
48
|
+
"middleware"
|
|
49
|
+
],
|
|
28
50
|
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
29
51
|
"license": "MIT",
|
|
30
|
-
"files": [
|
|
52
|
+
"files": [
|
|
53
|
+
"LICENSE",
|
|
54
|
+
"README.md",
|
|
55
|
+
"esm/",
|
|
56
|
+
"cjs/",
|
|
57
|
+
"bin/"
|
|
58
|
+
],
|
|
31
59
|
"jest": {
|
|
32
60
|
"transform": {
|
|
33
61
|
"^.+\\.(t|j)sx?$": [
|
|
34
62
|
"@swc/jest",
|
|
35
63
|
{
|
|
36
|
-
"
|
|
64
|
+
"jsc": {
|
|
65
|
+
"target": "es2020"
|
|
66
|
+
},
|
|
67
|
+
"module": {
|
|
68
|
+
"type": "commonjs"
|
|
69
|
+
}
|
|
37
70
|
}
|
|
38
71
|
]
|
|
39
|
-
}
|
|
72
|
+
},
|
|
73
|
+
"transformIgnorePatterns": [
|
|
74
|
+
"/node_modules/(?!parse-json|index-to-position)/"
|
|
75
|
+
]
|
|
40
76
|
},
|
|
41
77
|
"repository": "https://github.com/Censkh/api-def",
|
|
42
78
|
"devDependencies": {
|
|
43
|
-
"@biomejs/biome": "
|
|
79
|
+
"@biomejs/biome": "2.4.16",
|
|
44
80
|
"@redocly/openapi-core": "^1.34.3",
|
|
45
|
-
"@swc/core": "^1.
|
|
81
|
+
"@swc/core": "^1.15.40",
|
|
46
82
|
"@swc/jest": "^0.2.39",
|
|
47
83
|
"@types/axios": "0.14.4",
|
|
48
|
-
"@types/bun": "^1.
|
|
84
|
+
"@types/bun": "^1.3.14",
|
|
49
85
|
"@types/jest": "^30.0.0",
|
|
50
|
-
"@types/lodash": "^4.17.
|
|
51
|
-
"@types/node": "
|
|
52
|
-
"@types/qs": "6.
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
86
|
+
"@types/lodash": "^4.17.24",
|
|
87
|
+
"@types/node": "25.9.1",
|
|
88
|
+
"@types/qs": "6.15.1",
|
|
89
|
+
"@typescript/native-preview": "7.0.0-dev.20260603.1",
|
|
90
|
+
"axios": "1.17.0",
|
|
91
|
+
"commander": "^15.0.0",
|
|
92
|
+
"cross-env": "10.1.0",
|
|
93
|
+
"esbuild": "^0.28.0",
|
|
94
|
+
"jest": "30.4.2",
|
|
95
|
+
"kleur": "^4.1.5",
|
|
96
|
+
"lodash": "^4.18.1",
|
|
60
97
|
"npm-run-all": "4.1.5",
|
|
61
|
-
"openapi-typescript": "7.
|
|
62
|
-
"qs": "6.
|
|
63
|
-
"rimraf": "6.
|
|
64
|
-
"
|
|
65
|
-
"
|
|
98
|
+
"openapi-typescript": "7.13.0",
|
|
99
|
+
"qs": "6.15.2",
|
|
100
|
+
"rimraf": "6.1.3",
|
|
101
|
+
"tsc-alias": "^1.8.16",
|
|
102
|
+
"typescript": "^6.0.3",
|
|
103
|
+
"zod": "4.4.3"
|
|
66
104
|
}
|
|
67
105
|
}
|