api-def 0.12.1 → 0.14.0-alpha.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.
Files changed (87) hide show
  1. package/README.md +3 -0
  2. package/bin/index.js +312 -313
  3. package/cjs/Api.d.ts +1 -1
  4. package/cjs/Api.js +99 -145
  5. package/cjs/ApiTypes.d.ts +3 -2
  6. package/cjs/ApiUtils.js +30 -32
  7. package/cjs/Endpoint.d.ts +1 -1
  8. package/cjs/Endpoint.js +112 -175
  9. package/cjs/EndpointBuilder.d.ts +17 -0
  10. package/cjs/EndpointBuilder.js +55 -36
  11. package/cjs/QueryHandling.js +12 -13
  12. package/cjs/RequestConfig.js +54 -36
  13. package/cjs/RequestContext.d.ts +2 -1
  14. package/cjs/RequestContext.js +143 -180
  15. package/cjs/RequestError.js +16 -19
  16. package/cjs/Requester.js +231 -312
  17. package/cjs/TextDecoding.js +31 -31
  18. package/cjs/Utils.d.ts +1 -0
  19. package/cjs/Utils.js +74 -25
  20. package/cjs/Validation.d.ts +8 -1
  21. package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
  22. package/cjs/backend/AxiosRequestBackend.js +65 -172
  23. package/cjs/backend/FetchRequestBackend.d.ts +1 -1
  24. package/cjs/backend/FetchRequestBackend.js +108 -154
  25. package/cjs/backend/MockRequestBackend.d.ts +1 -1
  26. package/cjs/backend/MockRequestBackend.js +146 -208
  27. package/cjs/cache/ClientCaching.js +26 -74
  28. package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
  29. package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
  30. package/cjs/index.d.ts +11 -11
  31. package/cjs/index.js +24 -21
  32. package/cjs/middleware/ClientCacheMiddleware.js +80 -103
  33. package/cjs/middleware/LoggingMiddleware.js +71 -42
  34. package/cjs/util/retry/index.js +42 -9
  35. package/cjs/util/retry/lib/retry.js +25 -27
  36. package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
  37. package/cjs/util/retry/lib/retryOperation.js +21 -23
  38. package/esm/Api.d.ts +6 -6
  39. package/esm/Api.js +12 -25
  40. package/esm/ApiConstants.d.ts +1 -1
  41. package/esm/ApiTypes.d.ts +7 -6
  42. package/esm/ApiUtils.d.ts +2 -2
  43. package/esm/ApiUtils.js +4 -5
  44. package/esm/Endpoint.d.ts +6 -6
  45. package/esm/Endpoint.js +22 -28
  46. package/esm/EndpointBuilder.d.ts +21 -4
  47. package/esm/EndpointBuilder.js +38 -10
  48. package/esm/MockingTypes.d.ts +1 -1
  49. package/esm/QueryHandling.d.ts +1 -1
  50. package/esm/QueryHandling.js +2 -3
  51. package/esm/RequestConfig.d.ts +1 -1
  52. package/esm/RequestConfig.js +6 -7
  53. package/esm/RequestContext.d.ts +8 -7
  54. package/esm/RequestContext.js +33 -27
  55. package/esm/RequestError.d.ts +3 -3
  56. package/esm/RequestError.js +2 -3
  57. package/esm/Requester.d.ts +2 -2
  58. package/esm/Requester.js +45 -50
  59. package/esm/UtilTypes.d.ts +1 -1
  60. package/esm/Utils.d.ts +1 -0
  61. package/esm/Utils.js +54 -2
  62. package/esm/Validation.d.ts +8 -1
  63. package/esm/backend/AxiosRequestBackend.d.ts +4 -4
  64. package/esm/backend/AxiosRequestBackend.js +47 -84
  65. package/esm/backend/FetchRequestBackend.d.ts +5 -5
  66. package/esm/backend/FetchRequestBackend.js +50 -64
  67. package/esm/backend/MockRequestBackend.d.ts +4 -4
  68. package/esm/backend/MockRequestBackend.js +105 -136
  69. package/esm/backend/RequestBackend.d.ts +2 -2
  70. package/esm/cache/ClientCaching.d.ts +1 -1
  71. package/esm/cache/ClientCaching.js +8 -17
  72. package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
  73. package/esm/cache/LocalForageClientCacheBackend.js +8 -25
  74. package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
  75. package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
  76. package/esm/index.d.ts +16 -16
  77. package/esm/index.js +15 -15
  78. package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
  79. package/esm/middleware/ClientCacheMiddleware.js +8 -17
  80. package/esm/middleware/LoggingMiddleware.d.ts +1 -1
  81. package/esm/middleware/LoggingMiddleware.js +5 -6
  82. package/esm/util/retry/index.js +1 -1
  83. package/esm/util/retry/lib/retry.d.ts +1 -1
  84. package/esm/util/retry/lib/retry.js +13 -7
  85. package/esm/util/retry/lib/retryOperation.d.ts +1 -26
  86. package/esm/util/retry/lib/retryOperation.js +1 -1
  87. package/package.json +67 -28
package/cjs/Endpoint.js CHANGED
@@ -1,162 +1,104 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
47
7
  }
48
- };
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
49
35
  Object.defineProperty(exports, "__esModule", { value: true });
50
- var Requester = require("./Requester");
51
- var ApiUtils_1 = require("./ApiUtils");
52
- var RequestConfig_1 = require("./RequestConfig");
53
- var Endpoint = /** @class */ (function () {
54
- function Endpoint(api, options) {
55
- var _a, _b;
36
+ const ApiUtils_1 = require("./ApiUtils");
37
+ const RequestConfig_1 = require("./RequestConfig");
38
+ const Requester = __importStar(require("./Requester"));
39
+ class Endpoint {
40
+ constructor(api, options) {
56
41
  this.api = api;
57
- this.info = __assign(__assign({}, options), { config: undefined, name: options.name || options.id, validation: options.validation || {}, middleware: options.middleware || [], defaultRequestConfig: (_b = (_a = options.defaultRequestConfig) !== null && _a !== void 0 ? _a : options.config) !== null && _b !== void 0 ? _b : {} });
58
- }
59
- Object.defineProperty(Endpoint.prototype, "id", {
60
- get: function () {
61
- return this.info.id;
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
- Object.defineProperty(Endpoint.prototype, "method", {
67
- get: function () {
68
- return this.info.method;
69
- },
70
- enumerable: false,
71
- configurable: true
72
- });
73
- Object.defineProperty(Endpoint.prototype, "path", {
74
- get: function () {
75
- return this.info.path;
76
- },
77
- enumerable: false,
78
- configurable: true
79
- });
80
- Object.defineProperty(Endpoint.prototype, "name", {
81
- get: function () {
82
- return this.info.name;
83
- },
84
- enumerable: false,
85
- configurable: true
86
- });
87
- Object.defineProperty(Endpoint.prototype, "description", {
88
- get: function () {
89
- return this.info.description;
90
- },
91
- enumerable: false,
92
- configurable: true
93
- });
94
- Object.defineProperty(Endpoint.prototype, "config", {
95
- /** @deprecated Use `defaultRequestConfig` instead */
96
- get: function () {
97
- return this.defaultRequestConfig;
98
- },
99
- enumerable: false,
100
- configurable: true
101
- });
102
- Object.defineProperty(Endpoint.prototype, "defaultRequestConfig", {
103
- get: function () {
104
- return this.info.defaultRequestConfig;
105
- },
106
- enumerable: false,
107
- configurable: true
108
- });
109
- Object.defineProperty(Endpoint.prototype, "responseType", {
110
- get: function () {
111
- return this.info.responseType;
112
- },
113
- enumerable: false,
114
- configurable: true
115
- });
116
- Object.defineProperty(Endpoint.prototype, "mocking", {
117
- get: function () {
118
- return this.info.mocking;
119
- },
120
- enumerable: false,
121
- configurable: true
122
- });
123
- Object.defineProperty(Endpoint.prototype, "validation", {
124
- get: function () {
125
- return this.info.validation;
126
- },
127
- enumerable: false,
128
- configurable: true
129
- });
130
- Object.defineProperty(Endpoint.prototype, "middleware", {
131
- get: function () {
132
- return this.info.middleware || [];
133
- },
134
- enumerable: false,
135
- configurable: true
136
- });
137
- Endpoint.prototype.submit = function (config) {
138
- return __awaiter(this, void 0, void 0, function () {
139
- var mock, apiMocking, mockingEnabled;
140
- var _a, _b;
141
- return __generator(this, function (_c) {
142
- mock = false;
143
- apiMocking = this.api.mocking;
144
- if (apiMocking) {
145
- mockingEnabled = (_a = (typeof apiMocking.enabled === "function" ? apiMocking.enabled() : apiMocking.enabled)) !== null && _a !== void 0 ? _a : false;
146
- if (mockingEnabled) {
147
- if (!((_b = this.mocking) === null || _b === void 0 ? void 0 : _b.handler)) {
148
- throw new Error("[api-def] Endpoint for '".concat(this.path, "' has no mocking"));
149
- }
150
- mock = true;
151
- }
42
+ this.info = {
43
+ ...options,
44
+ config: undefined,
45
+ name: options.name || options.id,
46
+ validation: options.validation || {},
47
+ middleware: options.middleware || [],
48
+ defaultRequestConfig: options.defaultRequestConfig ?? options.config ?? {},
49
+ };
50
+ }
51
+ get id() {
52
+ return this.info.id;
53
+ }
54
+ get method() {
55
+ return this.info.method;
56
+ }
57
+ get path() {
58
+ return this.info.path;
59
+ }
60
+ get name() {
61
+ return this.info.name;
62
+ }
63
+ get description() {
64
+ return this.info.description;
65
+ }
66
+ /** @deprecated Use `defaultRequestConfig` instead */
67
+ get config() {
68
+ return this.defaultRequestConfig;
69
+ }
70
+ get defaultRequestConfig() {
71
+ return this.info.defaultRequestConfig;
72
+ }
73
+ get responseType() {
74
+ return this.info.responseType;
75
+ }
76
+ get mocking() {
77
+ return this.info.mocking;
78
+ }
79
+ get validation() {
80
+ return this.info.validation;
81
+ }
82
+ get middleware() {
83
+ return this.info.middleware || [];
84
+ }
85
+ async submit(config) {
86
+ let mock = false;
87
+ const apiMocking = this.api.mocking;
88
+ if (apiMocking) {
89
+ const mockingEnabled = (typeof apiMocking.enabled === "function" ? apiMocking.enabled() : apiMocking.enabled) ?? false;
90
+ if (mockingEnabled) {
91
+ if (!this.mocking?.handler) {
92
+ throw new Error(`[api-def] Endpoint for '${this.path}' has no mocking`);
152
93
  }
153
- return [2 /*return*/, Requester.submit(this, config, mock ? this.mocking : null)];
154
- });
155
- });
156
- };
157
- Endpoint.prototype.resolveUrl = function (options) {
158
- var query = options.query;
159
- var url = this.api.resolveUrl({
94
+ mock = true;
95
+ }
96
+ }
97
+ return Requester.submit(this, config, mock ? this.mocking : null);
98
+ }
99
+ resolveUrl(options) {
100
+ const { query } = options;
101
+ const url = this.api.resolveUrl({
160
102
  path: this.resolvePath(options),
161
103
  baseUrl: options.baseUrl,
162
104
  });
@@ -164,32 +106,27 @@ var Endpoint = /** @class */ (function () {
164
106
  url.search = new URLSearchParams(query).toString();
165
107
  }
166
108
  return url;
167
- };
168
- Endpoint.prototype.resolvePath = function (options) {
169
- var params = options.params;
109
+ }
110
+ resolvePath(options) {
111
+ const { params } = options;
170
112
  return (0, ApiUtils_1.resolvePathParams)(this.path, params);
171
- };
172
- Object.defineProperty(Endpoint.prototype, "baseUrl", {
173
- get: function () {
174
- return this.api.baseUrl;
175
- },
176
- enumerable: false,
177
- configurable: true
178
- });
113
+ }
114
+ get baseUrl() {
115
+ return this.api.baseUrl;
116
+ }
179
117
  /**
180
118
  * @deprecated Use `defaultRequestConfig` instead
181
119
  */
182
- Endpoint.prototype.computeConfig = function (config) {
120
+ computeConfig(config) {
183
121
  return this.computeRequestConfig(config);
184
- };
185
- Endpoint.prototype.computeRequestConfig = function (config) {
186
- var apiDefaults = this.api.computeRequestConfig();
187
- var endpointDefaults = this.defaultRequestConfig || {};
122
+ }
123
+ computeRequestConfig(config) {
124
+ const apiDefaults = this.api.computeRequestConfig();
125
+ const endpointDefaults = this.defaultRequestConfig || {};
188
126
  return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, endpointDefaults, config]);
189
- };
190
- Endpoint.prototype.getRequestBackend = function () {
127
+ }
128
+ getRequestBackend() {
191
129
  return this.api.requestBackend;
192
- };
193
- return Endpoint;
194
- }());
130
+ }
131
+ }
195
132
  exports.default = Endpoint;
@@ -3,6 +3,7 @@ import type { Api } from "./Api";
3
3
  import type { ResponseType } from "./ApiConstants";
4
4
  import type { Body, Params, Query, RawHeaders, State } from "./ApiTypes";
5
5
  import Endpoint, { type EndpointOptions } from "./Endpoint";
6
+ import type { BodyValidationOptions, ValidationOptions } from "./Validation";
6
7
  type DefaultResponseOf<T extends ResponseType | undefined> = T extends "text" ? string : T extends "arraybuffer" ? ArrayBuffer : "stream" extends T ? AsyncIterable<Uint8Array> : unknown;
7
8
  export type EndpointBuildOptions<TResponse = unknown, TParams extends Params | undefined = undefined, TQuery extends Query | undefined = undefined, TBody extends Body | undefined = undefined, TState extends State = State, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined, TPath extends string = string, TResponseType extends ResponseType | undefined = undefined> = Omit<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "validation"> & {
8
9
  responseType?: TResponseType;
@@ -11,10 +12,26 @@ export default class EndpointBuilder<TResponse = unknown, TParams extends Params
11
12
  private api;
12
13
  private readonly validation;
13
14
  constructor(api: Api);
15
+ queryOf<TNewQuery extends Query>(options?: ValidationOptions<TNewQuery>): EndpointBuilder<TResponse, TParams, TNewQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
16
+ /**
17
+ * @deprecated Pass `{ schema }` instead.
18
+ */
14
19
  queryOf<TNewQuery extends Query>(schema?: zod.Schema<TNewQuery>): EndpointBuilder<TResponse, TParams, TNewQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
15
20
  paramsOf<TNewParams extends Params>(): EndpointBuilder<TResponse, TNewParams, TQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
21
+ bodyOf<TNewBody extends Body>(options?: BodyValidationOptions<TNewBody>): EndpointBuilder<TResponse, TParams, TQuery, TNewBody, TState, TRequestHeaders, TResponseHeaders>;
22
+ /**
23
+ * @deprecated Pass `{ schema }` instead.
24
+ */
16
25
  bodyOf<TNewBody extends Body>(schema?: zod.Schema<TNewBody>): EndpointBuilder<TResponse, TParams, TQuery, TNewBody, TState, TRequestHeaders, TResponseHeaders>;
26
+ responseOf<TNewResponse>(options?: ValidationOptions<TNewResponse>): EndpointBuilder<TNewResponse, TParams, TQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
27
+ /**
28
+ * @deprecated Pass `{ schema }` instead.
29
+ */
17
30
  responseOf<TNewResponse>(schema?: zod.Schema<TNewResponse>): EndpointBuilder<TNewResponse, TParams, TQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
31
+ stateOf<TNewState extends State>(options?: ValidationOptions<TNewState>): EndpointBuilder<TResponse, TParams, TQuery, TBody, TNewState, TRequestHeaders, TResponseHeaders>;
32
+ /**
33
+ * @deprecated Pass `{ schema }` instead.
34
+ */
18
35
  stateOf<TNewState extends State>(schema?: zod.Schema<TNewState>): EndpointBuilder<TResponse, TParams, TQuery, TBody, TNewState, TRequestHeaders, TResponseHeaders>;
19
36
  requestHeadersOf<TNewRequestHeaders extends RawHeaders>(): EndpointBuilder<TResponse, TParams, TQuery, TBody, TState, TNewRequestHeaders, TResponseHeaders>;
20
37
  responseHeadersOf<TNewResponseHeaders extends RawHeaders>(): EndpointBuilder<TResponse, TParams, TQuery, TBody, TState, TRequestHeaders, TNewResponseHeaders>;
@@ -1,52 +1,71 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
12
4
  };
13
5
  Object.defineProperty(exports, "__esModule", { value: true });
14
- var Endpoint_1 = require("./Endpoint");
15
- var EndpointBuilder = /** @class */ (function () {
16
- function EndpointBuilder(api) {
6
+ const Endpoint_1 = __importDefault(require("./Endpoint"));
7
+ const isSchema = (value) => {
8
+ return value !== undefined && "parse" in value;
9
+ };
10
+ const warnDeprecatedSchemaArgument = (methodName) => {
11
+ console.warn(`[api-def] ${methodName}(schema) is deprecated. Use ${methodName}({ schema }) instead.`);
12
+ };
13
+ class EndpointBuilder {
14
+ constructor(api) {
17
15
  this.validation = {};
18
16
  this.api = api;
19
17
  }
20
- EndpointBuilder.prototype.queryOf = function (schema) {
21
- this.validation.query = schema;
18
+ queryOf(optionsOrSchema) {
19
+ if (optionsOrSchema && isSchema(optionsOrSchema)) {
20
+ warnDeprecatedSchemaArgument("queryOf");
21
+ this.validation.query = optionsOrSchema;
22
+ return this;
23
+ }
24
+ this.validation.query = optionsOrSchema?.schema;
22
25
  return this;
23
- };
24
- EndpointBuilder.prototype.paramsOf = function () {
26
+ }
27
+ paramsOf() {
25
28
  return this;
26
- };
27
- EndpointBuilder.prototype.bodyOf = function (schema) {
28
- this.validation.body = schema;
29
+ }
30
+ bodyOf(optionsOrSchema) {
31
+ if (optionsOrSchema && isSchema(optionsOrSchema)) {
32
+ warnDeprecatedSchemaArgument("bodyOf");
33
+ this.validation.body = optionsOrSchema;
34
+ this.validation.bodyEncoding = "application/json";
35
+ return this;
36
+ }
37
+ this.validation.body = optionsOrSchema?.schema;
38
+ this.validation.bodyEncoding = optionsOrSchema?.encoding ?? "application/json";
29
39
  return this;
30
- };
31
- EndpointBuilder.prototype.responseOf = function (schema) {
32
- this.validation.response = schema;
40
+ }
41
+ responseOf(optionsOrSchema) {
42
+ if (optionsOrSchema && isSchema(optionsOrSchema)) {
43
+ warnDeprecatedSchemaArgument("responseOf");
44
+ this.validation.response = optionsOrSchema;
45
+ return this;
46
+ }
47
+ this.validation.response = optionsOrSchema?.schema;
33
48
  return this;
34
- };
35
- EndpointBuilder.prototype.stateOf = function (schema) {
36
- this.validation.state = schema;
49
+ }
50
+ stateOf(optionsOrSchema) {
51
+ if (optionsOrSchema && isSchema(optionsOrSchema)) {
52
+ warnDeprecatedSchemaArgument("stateOf");
53
+ this.validation.state = optionsOrSchema;
54
+ return this;
55
+ }
56
+ this.validation.state = optionsOrSchema?.schema;
37
57
  return this;
38
- };
39
- EndpointBuilder.prototype.requestHeadersOf = function () {
58
+ }
59
+ requestHeadersOf() {
40
60
  return this;
41
- };
42
- EndpointBuilder.prototype.responseHeadersOf = function () {
61
+ }
62
+ responseHeadersOf() {
43
63
  return this;
44
- };
45
- EndpointBuilder.prototype.build = function (options) {
46
- var endpoint = new Endpoint_1.default(this.api, __assign(__assign({}, options), { validation: this.validation }));
64
+ }
65
+ build(options) {
66
+ const endpoint = new Endpoint_1.default(this.api, { ...options, validation: this.validation });
47
67
  this.api.endpoints[endpoint.id] = endpoint;
48
68
  return endpoint;
49
- };
50
- return EndpointBuilder;
51
- }());
69
+ }
70
+ }
52
71
  exports.default = EndpointBuilder;
@@ -1,23 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEFAULT_QUERY_PARSE = exports.DEFAULT_QUERY_STRINGIFY = void 0;
4
- var DEFAULT_QUERY_STRINGIFY = function (query) {
5
- var _a, _b;
6
- var queryStrings = [];
7
- var queryKeys = Object.keys(query);
8
- for (var i = 0; i < queryKeys.length; i++) {
9
- var key = queryKeys[i];
10
- queryStrings.push("".concat(key, "=").concat((_b = (_a = query[key]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ""));
4
+ const DEFAULT_QUERY_STRINGIFY = (query) => {
5
+ const queryStrings = [];
6
+ const queryKeys = Object.keys(query);
7
+ for (let i = 0; i < queryKeys.length; i++) {
8
+ const key = queryKeys[i];
9
+ queryStrings.push(`${key}=${query[key]?.toString() ?? ""}`);
11
10
  }
12
11
  return queryStrings.join("&");
13
12
  };
14
13
  exports.DEFAULT_QUERY_STRINGIFY = DEFAULT_QUERY_STRINGIFY;
15
- var DEFAULT_QUERY_PARSE = function (queryString) {
16
- var query = {};
17
- var queryStrings = queryString.split("&");
18
- for (var i = 0; i < queryStrings.length; i++) {
19
- var _a = queryStrings[i].split("="), key = _a[0], value = _a[1];
20
- query[key] = !(value === null || value === void 0 ? void 0 : value.length) ? true : value;
14
+ const DEFAULT_QUERY_PARSE = (queryString) => {
15
+ const query = {};
16
+ const queryStrings = queryString.split("&");
17
+ for (let i = 0; i < queryStrings.length; i++) {
18
+ const [key, value] = queryStrings[i].split("=");
19
+ query[key] = !value?.length ? true : value;
21
20
  }
22
21
  return query;
23
22
  };
@@ -1,46 +1,64 @@
1
1
  "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
7
  }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
11
35
  Object.defineProperty(exports, "__esModule", { value: true });
12
36
  exports.processRequestConfigs = void 0;
13
- var ApiTypes_1 = require("./ApiTypes");
14
- var QueryHandling_1 = require("./QueryHandling");
15
- var Utils = require("./Utils");
16
- var MERGED_CONFIG_KEYS = ["headers"];
17
- var processRequestConfigs = function (configs) {
18
- var _a;
19
- var _b, _c;
20
- var computedConfig = Utils.assign.apply(Utils, __spreadArray([(_a = {},
21
- _a[ApiTypes_1.COMPUTED_CONFIG_SYMBOL] = true,
22
- _a)], configs, false));
23
- var _loop_1 = function (key) {
24
- computedConfig[key] = Utils.assign.apply(Utils, __spreadArray([{}], configs.reduce(function (acc, config) {
25
- if (config === null || config === void 0 ? void 0 : config[key]) {
37
+ const ApiTypes_1 = require("./ApiTypes");
38
+ const QueryHandling_1 = require("./QueryHandling");
39
+ const Utils = __importStar(require("./Utils"));
40
+ const MERGED_CONFIG_KEYS = ["headers"];
41
+ const processRequestConfigs = (configs) => {
42
+ const computedConfig = Utils.assign({
43
+ [ApiTypes_1.COMPUTED_CONFIG_SYMBOL]: true,
44
+ }, ...configs);
45
+ // merge other values
46
+ for (const key of MERGED_CONFIG_KEYS) {
47
+ computedConfig[key] = Utils.assign({}, ...configs.reduce((acc, config) => {
48
+ if (config?.[key]) {
26
49
  acc.push(config[key]);
27
50
  }
28
51
  return acc;
29
- }, []), false));
30
- };
31
- // merge other values
32
- for (var _i = 0, MERGED_CONFIG_KEYS_1 = MERGED_CONFIG_KEYS; _i < MERGED_CONFIG_KEYS_1.length; _i++) {
33
- var key = MERGED_CONFIG_KEYS_1[_i];
34
- _loop_1(key);
52
+ }, []));
35
53
  }
36
54
  computedConfig.queryHandling = {
37
- parse: ((_b = computedConfig.queryHandling) === null || _b === void 0 ? void 0 : _b.parse) || QueryHandling_1.DEFAULT_QUERY_PARSE,
38
- stringify: ((_c = computedConfig.queryHandling) === null || _c === void 0 ? void 0 : _c.stringify) || computedConfig.queryParser || QueryHandling_1.DEFAULT_QUERY_STRINGIFY,
55
+ parse: computedConfig.queryHandling?.parse || QueryHandling_1.DEFAULT_QUERY_PARSE,
56
+ stringify: computedConfig.queryHandling?.stringify || computedConfig.queryParser || QueryHandling_1.DEFAULT_QUERY_STRINGIFY,
39
57
  };
40
58
  computedConfig.queryParser = undefined;
41
- var query = computedConfig.query;
42
- var queryString = "";
43
- var queryObject = {};
59
+ const query = computedConfig.query;
60
+ let queryString = "";
61
+ let queryObject = {};
44
62
  if (query) {
45
63
  if (typeof query === "string") {
46
64
  queryString = query;
@@ -52,19 +70,19 @@ var processRequestConfigs = function (configs) {
52
70
  }
53
71
  }
54
72
  Object.defineProperty(computedConfig, "queryString", {
55
- get: function () {
73
+ get() {
56
74
  return queryString;
57
75
  },
58
- set: function (value) {
76
+ set(value) {
59
77
  queryString = value;
60
78
  queryObject = computedConfig.queryHandling.parse(value);
61
79
  },
62
80
  });
63
81
  Object.defineProperty(computedConfig, "queryObject", {
64
- get: function () {
82
+ get() {
65
83
  return queryObject;
66
84
  },
67
- set: function (value) {
85
+ set(value) {
68
86
  queryObject = value;
69
87
  queryString = computedConfig.queryHandling.stringify(value);
70
88
  },
@@ -1,10 +1,10 @@
1
1
  import type { Api } from "./Api";
2
2
  import type { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
3
3
  import type { ApiResponse, Body, ComputedRequestConfig, EventResult, Params, Query, RawHeaders, RequestCacheInfo, RequestEventHandlers, RequestHost, RequestStats, State } from "./ApiTypes";
4
+ import type RequestBackend from "./backend/RequestBackend";
4
5
  import type { EndpointMockingConfig } from "./MockingTypes";
5
6
  import type { RequestError } from "./RequestError";
6
7
  import type { Validation } from "./Validation";
7
- import type RequestBackend from "./backend/RequestBackend";
8
8
  export default class RequestContext<TResponse = any, TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State> {
9
9
  readonly id: number;
10
10
  readonly key: string;
@@ -42,6 +42,7 @@ export default class RequestContext<TResponse = any, TParams extends Params | un
42
42
  updateParams(params: Partial<Record<string, string>>): this;
43
43
  private parseRequestBody;
44
44
  getParsedBody(): any;
45
+ parseBody(): void;
45
46
  updateQuery(newQuery: Partial<TQuery>): this;
46
47
  triggerEvent(eventType: RequestEvent): Promise<EventResult<TResponse> | undefined>;
47
48
  addCanceller(canceler: () => void): void;