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.
Files changed (84) hide show
  1. package/README.md +3 -0
  2. package/bin/index.js +312 -313
  3. package/cjs/Api.d.ts +6 -6
  4. package/cjs/Api.js +99 -143
  5. package/cjs/ApiTypes.d.ts +1 -1
  6. package/cjs/ApiUtils.d.ts +2 -1
  7. package/cjs/ApiUtils.js +41 -33
  8. package/cjs/Endpoint.d.ts +1 -1
  9. package/cjs/Endpoint.js +112 -175
  10. package/cjs/EndpointBuilder.js +23 -32
  11. package/cjs/QueryHandling.js +12 -13
  12. package/cjs/RequestConfig.js +54 -36
  13. package/cjs/RequestContext.d.ts +1 -1
  14. package/cjs/RequestContext.js +125 -180
  15. package/cjs/RequestError.js +16 -19
  16. package/cjs/Requester.js +230 -312
  17. package/cjs/TextDecoding.js +31 -31
  18. package/cjs/Utils.js +20 -23
  19. package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
  20. package/cjs/backend/AxiosRequestBackend.js +65 -172
  21. package/cjs/backend/FetchRequestBackend.d.ts +1 -1
  22. package/cjs/backend/FetchRequestBackend.js +108 -154
  23. package/cjs/backend/MockRequestBackend.d.ts +1 -1
  24. package/cjs/backend/MockRequestBackend.js +146 -208
  25. package/cjs/cache/ClientCaching.js +26 -74
  26. package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
  27. package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
  28. package/cjs/index.d.ts +11 -11
  29. package/cjs/index.js +24 -21
  30. package/cjs/middleware/ClientCacheMiddleware.js +80 -103
  31. package/cjs/middleware/LoggingMiddleware.js +71 -42
  32. package/cjs/util/retry/index.js +42 -9
  33. package/cjs/util/retry/lib/retry.js +25 -27
  34. package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
  35. package/cjs/util/retry/lib/retryOperation.js +21 -23
  36. package/esm/Api.d.ts +11 -11
  37. package/esm/Api.js +12 -23
  38. package/esm/ApiConstants.d.ts +1 -1
  39. package/esm/ApiTypes.d.ts +5 -5
  40. package/esm/ApiUtils.d.ts +4 -3
  41. package/esm/ApiUtils.js +15 -7
  42. package/esm/Endpoint.d.ts +6 -6
  43. package/esm/Endpoint.js +22 -28
  44. package/esm/EndpointBuilder.d.ts +4 -4
  45. package/esm/EndpointBuilder.js +2 -2
  46. package/esm/MockingTypes.d.ts +1 -1
  47. package/esm/QueryHandling.d.ts +1 -1
  48. package/esm/QueryHandling.js +2 -3
  49. package/esm/RequestConfig.d.ts +1 -1
  50. package/esm/RequestConfig.js +6 -7
  51. package/esm/RequestContext.d.ts +7 -7
  52. package/esm/RequestContext.js +15 -27
  53. package/esm/RequestError.d.ts +3 -3
  54. package/esm/RequestError.js +2 -3
  55. package/esm/Requester.d.ts +2 -2
  56. package/esm/Requester.js +44 -50
  57. package/esm/UtilTypes.d.ts +1 -1
  58. package/esm/Utils.js +2 -1
  59. package/esm/Validation.d.ts +1 -1
  60. package/esm/backend/AxiosRequestBackend.d.ts +4 -4
  61. package/esm/backend/AxiosRequestBackend.js +47 -84
  62. package/esm/backend/FetchRequestBackend.d.ts +5 -5
  63. package/esm/backend/FetchRequestBackend.js +49 -63
  64. package/esm/backend/MockRequestBackend.d.ts +4 -4
  65. package/esm/backend/MockRequestBackend.js +105 -136
  66. package/esm/backend/RequestBackend.d.ts +2 -2
  67. package/esm/cache/ClientCaching.d.ts +1 -1
  68. package/esm/cache/ClientCaching.js +8 -17
  69. package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
  70. package/esm/cache/LocalForageClientCacheBackend.js +8 -25
  71. package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
  72. package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
  73. package/esm/index.d.ts +16 -16
  74. package/esm/index.js +15 -15
  75. package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
  76. package/esm/middleware/ClientCacheMiddleware.js +8 -17
  77. package/esm/middleware/LoggingMiddleware.d.ts +1 -1
  78. package/esm/middleware/LoggingMiddleware.js +5 -6
  79. package/esm/util/retry/index.js +1 -1
  80. package/esm/util/retry/lib/retry.d.ts +1 -1
  81. package/esm/util/retry/lib/retry.js +13 -7
  82. package/esm/util/retry/lib/retryOperation.d.ts +1 -26
  83. package/esm/util/retry/lib/retryOperation.js +1 -1
  84. package/package.json +66 -28
@@ -1,4 +1,4 @@
1
- import type { RequestMiddleware } from "../ApiTypes";
1
+ import type { RequestMiddleware } from "../ApiTypes.js";
2
2
  export interface ClientCacheMiddlewareOptions {
3
3
  defaultExpiry?: number;
4
4
  predicate?: () => boolean;
@@ -1,17 +1,8 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) => __awaiter(void 0, void 0, void 0, function* () {
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
- yield ClientCaching.setClientCachedItem(context.key, context.response, expiry);
14
+ await ClientCaching.setClientCachedItem(context.key, context.response, expiry);
24
15
  }
25
- }),
26
- [RequestEvent.BEFORE_SEND]: (context) => __awaiter(void 0, void 0, void 0, function* () {
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 = yield ClientCaching.getClientCachedItem(context.key);
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,4 +1,4 @@
1
- import type { RequestMiddleware } from "../ApiTypes";
1
+ import type { RequestMiddleware } from "../ApiTypes.js";
2
2
  export interface LoggingMiddlewareOptions {
3
3
  predicate?: () => boolean;
4
4
  }
@@ -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 === null || data === void 0 ? void 0 : data.code;
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 ${(_a = context.response) === null || _a === void 0 ? void 0 : _a.status}${cacheSource ? ` (cached by ${cacheSource})` : ""}`, config);
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) {
@@ -1,4 +1,4 @@
1
- import * as retrier from "./lib/retry";
1
+ import * as retrier from "./lib/retry.js";
2
2
  // Retry is just transplanted from here:
3
3
  // https://www.npmjs.com/package/retry (lib), and
4
4
  // https://www.npmjs.com/package/async-retry (RetryFunction)
@@ -1,3 +1,3 @@
1
1
  import type { RetryOptions } from "../interfaces";
2
- import RetryOperation from "./retryOperation";
2
+ import RetryOperation from "./retryOperation.js";
3
3
  export declare const operation: (options: RetryOptions) => RetryOperation;
@@ -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 === null || options === void 0 ? void 0 : options.unref,
7
- maxRetryTime: options === null || options === void 0 ? void 0 : options.maxRetryTime,
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 = Object.assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout: Number.POSITIVE_INFINITY, randomize: false }, options);
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 = (_a = opts.retries) !== null && _a !== void 0 ? _a : defaultRetries;
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 ((options === null || options === void 0 ? void 0 : options.forever) && !timeouts.length) {
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
- declare class RetryOperation {
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 = (options === null || options === void 0 ? void 0 : options.maxRetryTime) || Number.POSITIVE_INFINITY;
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.12.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": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check --write",
18
- "check": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome 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": "tsc --module es2015 --target es2016 --outDir esm --preserveWatchOutput && rm -rf esm/tests",
22
- "build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput && rm -rf cjs/tests",
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
- "keywords": ["typescript", "javascript", "node", "web", "api", "typed", "cache", "fetch", "retry", "middleware"],
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": ["LICENSE", "README.md", "esm/", "cjs/", "bin/"],
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
- "env": {}
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": "1.9.4",
79
+ "@biomejs/biome": "2.4.16",
44
80
  "@redocly/openapi-core": "^1.34.3",
45
- "@swc/core": "^1.12.11",
81
+ "@swc/core": "^1.15.40",
46
82
  "@swc/jest": "^0.2.39",
47
83
  "@types/axios": "0.14.4",
48
- "@types/bun": "^1.2.18",
84
+ "@types/bun": "^1.3.14",
49
85
  "@types/jest": "^30.0.0",
50
- "@types/lodash": "^4.17.20",
51
- "@types/node": "24.0.12",
52
- "@types/qs": "6.14.0",
53
- "axios": "1.10.0",
54
- "commander": "^14.0.0",
55
- "cross-env": "7.0.3",
56
- "esbuild": "^0.25.6",
57
- "jest": "latest",
58
- "lodash": "^4.17.21",
59
- "chalk": "4.1.2",
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.6.1",
62
- "qs": "6.14.0",
63
- "rimraf": "6.0.1",
64
- "typescript": "^5.8.3",
65
- "zod": "3.25.76"
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
  }