api-def 0.12.0-alpha.44 → 0.12.0-alpha.45

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/cjs/Endpoint.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Api } from "./Api";
2
2
  import type { RequestMethod, ResponseType } from "./ApiConstants";
3
- import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, State } from "./ApiTypes";
3
+ import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, RequestMiddleware, State } from "./ApiTypes";
4
4
  import type * as Mocking from "./MockingTypes";
5
5
  import type { Validation } from "./Validation";
6
6
  import type RequestBackend from "./backend/RequestBackend";
@@ -24,7 +24,12 @@ export interface EndpointOptions<TResponse, TParams extends Params | undefined,
24
24
  * Describe your endpoint to help with debugging and documentation
25
25
  */
26
26
  readonly description?: string;
27
+ /** @deprecated use `defaultRequestConfig` instead */
27
28
  readonly config?: BaseRequestConfig;
29
+ /**
30
+ * Default request configuration for this endpoint
31
+ */
32
+ readonly defaultRequestConfig?: BaseRequestConfig;
28
33
  /**
29
34
  * Let the backend requestor (fetch, axios, etc.) know what type of response
30
35
  * you are expecting from this endpoint
@@ -37,9 +42,13 @@ export interface EndpointOptions<TResponse, TParams extends Params | undefined,
37
42
  * Enable/disable mocked returns for all endpoints on your API object.
38
43
  */
39
44
  readonly mocking?: Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState>;
45
+ /**
46
+ * Middleware specific to this endpoint. These will be executed after API-level middleware.
47
+ */
48
+ readonly middleware?: RequestMiddleware[];
40
49
  readonly validation?: Validation<TResponse, TParams, TQuery, TBody, TState>;
41
50
  }
42
- export type EndpointInfo<TResponse, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders> & Required<Pick<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "name" | "validation">>;
51
+ export type EndpointInfo<TResponse, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders> & Omit<Required<Pick<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "name" | "validation" | "defaultRequestConfig">>, "config">;
43
52
  /**
44
53
  * @deprecated Use `EndpointInfo` instead
45
54
  */
@@ -53,16 +62,19 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
53
62
  get path(): TPath;
54
63
  get name(): string;
55
64
  get description(): string | undefined;
65
+ /** @deprecated Use `defaultRequestConfig` instead */
56
66
  get config(): BaseRequestConfig;
67
+ get defaultRequestConfig(): BaseRequestConfig;
57
68
  get responseType(): ResponseType | undefined;
58
69
  get mocking(): Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | undefined;
59
70
  get validation(): Validation<TResponse, TParams, TQuery, TBody, TState>;
71
+ get middleware(): RequestMiddleware[];
60
72
  submit(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): Promise<ApiResponse<TResponse>>;
61
73
  resolveUrl(options: EndpointResolveUrlOptions<TParams, TQuery>): URL;
62
74
  resolvePath(options: EndpointResolvePathOptions<TParams>): string;
63
75
  get baseUrl(): string;
64
76
  /**
65
- * @deprecated Use `computeRequestConfig` instead
77
+ * @deprecated Use `defaultRequestConfig` instead
66
78
  */
67
79
  computeConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
68
80
  computeRequestConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
package/cjs/Endpoint.js CHANGED
@@ -52,8 +52,9 @@ var ApiUtils_1 = require("./ApiUtils");
52
52
  var RequestConfig_1 = require("./RequestConfig");
53
53
  var Endpoint = /** @class */ (function () {
54
54
  function Endpoint(api, options) {
55
+ var _a, _b;
55
56
  this.api = api;
56
- this.info = __assign(__assign({}, options), { name: options.name || options.id, validation: options.validation || {} });
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 : {} });
57
58
  }
58
59
  Object.defineProperty(Endpoint.prototype, "id", {
59
60
  get: function () {
@@ -91,8 +92,16 @@ var Endpoint = /** @class */ (function () {
91
92
  configurable: true
92
93
  });
93
94
  Object.defineProperty(Endpoint.prototype, "config", {
95
+ /** @deprecated Use `defaultRequestConfig` instead */
94
96
  get: function () {
95
- return this.info.config || {};
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;
96
105
  },
97
106
  enumerable: false,
98
107
  configurable: true
@@ -118,6 +127,13 @@ var Endpoint = /** @class */ (function () {
118
127
  enumerable: false,
119
128
  configurable: true
120
129
  });
130
+ Object.defineProperty(Endpoint.prototype, "middleware", {
131
+ get: function () {
132
+ return this.info.middleware || [];
133
+ },
134
+ enumerable: false,
135
+ configurable: true
136
+ });
121
137
  Endpoint.prototype.submit = function (config) {
122
138
  return __awaiter(this, void 0, void 0, function () {
123
139
  var mock, apiMocking, mockingEnabled;
@@ -158,14 +174,15 @@ var Endpoint = /** @class */ (function () {
158
174
  configurable: true
159
175
  });
160
176
  /**
161
- * @deprecated Use `computeRequestConfig` instead
177
+ * @deprecated Use `defaultRequestConfig` instead
162
178
  */
163
179
  Endpoint.prototype.computeConfig = function (config) {
164
180
  return this.computeRequestConfig(config);
165
181
  };
166
182
  Endpoint.prototype.computeRequestConfig = function (config) {
167
183
  var apiDefaults = this.api.computeRequestConfig();
168
- return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, this.config, config]);
184
+ var endpointDefaults = this.defaultRequestConfig || {};
185
+ return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, endpointDefaults, config]);
169
186
  };
170
187
  Endpoint.prototype.getRequestBackend = function () {
171
188
  return this.api.requestBackend;
@@ -35,6 +35,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
38
47
  Object.defineProperty(exports, "__esModule", { value: true });
39
48
  var ApiUtils_1 = require("./ApiUtils");
40
49
  var Utils = require("./Utils");
@@ -106,9 +115,11 @@ var RequestContext = /** @class */ (function () {
106
115
  configurable: true
107
116
  });
108
117
  RequestContext.prototype.initMiddleware = function () {
109
- var middleware = this.api.middleware;
110
- for (var i = 0; i < middleware.length; i++) {
111
- var events = middleware[i];
118
+ // Merge API-level and endpoint-level middleware
119
+ var allMiddleware = __spreadArray(__spreadArray([], this.api.middleware, true), ("middleware" in this.host && Array.isArray(this.host.middleware) ? this.host.middleware : []), true);
120
+ // Process all middleware in one pass
121
+ for (var i = 0; i < allMiddleware.length; i++) {
122
+ var events = allMiddleware[i];
112
123
  var eventTypes = Object.keys(events);
113
124
  for (var n = 0; n < eventTypes.length; n++) {
114
125
  var eventType = eventTypes[n];
package/esm/Endpoint.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Api } from "./Api";
2
2
  import type { RequestMethod, ResponseType } from "./ApiConstants";
3
- import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, State } from "./ApiTypes";
3
+ import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, RequestMiddleware, State } from "./ApiTypes";
4
4
  import type * as Mocking from "./MockingTypes";
5
5
  import type { Validation } from "./Validation";
6
6
  import type RequestBackend from "./backend/RequestBackend";
@@ -24,7 +24,12 @@ export interface EndpointOptions<TResponse, TParams extends Params | undefined,
24
24
  * Describe your endpoint to help with debugging and documentation
25
25
  */
26
26
  readonly description?: string;
27
+ /** @deprecated use `defaultRequestConfig` instead */
27
28
  readonly config?: BaseRequestConfig;
29
+ /**
30
+ * Default request configuration for this endpoint
31
+ */
32
+ readonly defaultRequestConfig?: BaseRequestConfig;
28
33
  /**
29
34
  * Let the backend requestor (fetch, axios, etc.) know what type of response
30
35
  * you are expecting from this endpoint
@@ -37,9 +42,13 @@ export interface EndpointOptions<TResponse, TParams extends Params | undefined,
37
42
  * Enable/disable mocked returns for all endpoints on your API object.
38
43
  */
39
44
  readonly mocking?: Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState>;
45
+ /**
46
+ * Middleware specific to this endpoint. These will be executed after API-level middleware.
47
+ */
48
+ readonly middleware?: RequestMiddleware[];
40
49
  readonly validation?: Validation<TResponse, TParams, TQuery, TBody, TState>;
41
50
  }
42
- export type EndpointInfo<TResponse, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders> & Required<Pick<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "name" | "validation">>;
51
+ export type EndpointInfo<TResponse, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders> & Omit<Required<Pick<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "name" | "validation" | "defaultRequestConfig">>, "config">;
43
52
  /**
44
53
  * @deprecated Use `EndpointInfo` instead
45
54
  */
@@ -53,16 +62,19 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
53
62
  get path(): TPath;
54
63
  get name(): string;
55
64
  get description(): string | undefined;
65
+ /** @deprecated Use `defaultRequestConfig` instead */
56
66
  get config(): BaseRequestConfig;
67
+ get defaultRequestConfig(): BaseRequestConfig;
57
68
  get responseType(): ResponseType | undefined;
58
69
  get mocking(): Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | undefined;
59
70
  get validation(): Validation<TResponse, TParams, TQuery, TBody, TState>;
71
+ get middleware(): RequestMiddleware[];
60
72
  submit(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): Promise<ApiResponse<TResponse>>;
61
73
  resolveUrl(options: EndpointResolveUrlOptions<TParams, TQuery>): URL;
62
74
  resolvePath(options: EndpointResolvePathOptions<TParams>): string;
63
75
  get baseUrl(): string;
64
76
  /**
65
- * @deprecated Use `computeRequestConfig` instead
77
+ * @deprecated Use `defaultRequestConfig` instead
66
78
  */
67
79
  computeConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
68
80
  computeRequestConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
package/esm/Endpoint.js CHANGED
@@ -12,8 +12,9 @@ import { resolvePathParams } from "./ApiUtils";
12
12
  import { processRequestConfigs } from "./RequestConfig";
13
13
  export default class Endpoint {
14
14
  constructor(api, options) {
15
+ var _a, _b;
15
16
  this.api = api;
16
- this.info = Object.assign(Object.assign({}, options), { name: options.name || options.id, validation: options.validation || {} });
17
+ this.info = Object.assign(Object.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 : {} });
17
18
  }
18
19
  get id() {
19
20
  return this.info.id;
@@ -30,8 +31,12 @@ export default class Endpoint {
30
31
  get description() {
31
32
  return this.info.description;
32
33
  }
34
+ /** @deprecated Use `defaultRequestConfig` instead */
33
35
  get config() {
34
- return this.info.config || {};
36
+ return this.defaultRequestConfig;
37
+ }
38
+ get defaultRequestConfig() {
39
+ return this.info.defaultRequestConfig;
35
40
  }
36
41
  get responseType() {
37
42
  return this.info.responseType;
@@ -42,6 +47,9 @@ export default class Endpoint {
42
47
  get validation() {
43
48
  return this.info.validation;
44
49
  }
50
+ get middleware() {
51
+ return this.info.middleware || [];
52
+ }
45
53
  submit(config) {
46
54
  return __awaiter(this, void 0, void 0, function* () {
47
55
  var _a, _b;
@@ -75,14 +83,15 @@ export default class Endpoint {
75
83
  return this.api.baseUrl;
76
84
  }
77
85
  /**
78
- * @deprecated Use `computeRequestConfig` instead
86
+ * @deprecated Use `defaultRequestConfig` instead
79
87
  */
80
88
  computeConfig(config) {
81
89
  return this.computeRequestConfig(config);
82
90
  }
83
91
  computeRequestConfig(config) {
84
92
  const apiDefaults = this.api.computeRequestConfig();
85
- return processRequestConfigs([apiDefaults, this.config, config]);
93
+ const endpointDefaults = this.defaultRequestConfig || {};
94
+ return processRequestConfigs([apiDefaults, endpointDefaults, config]);
86
95
  }
87
96
  getRequestBackend() {
88
97
  return this.api.requestBackend;
@@ -57,9 +57,14 @@ export default class RequestContext {
57
57
  return this.host.responseType;
58
58
  }
59
59
  initMiddleware() {
60
- const { middleware } = this.api;
61
- for (let i = 0; i < middleware.length; i++) {
62
- const events = middleware[i];
60
+ // Merge API-level and endpoint-level middleware
61
+ const allMiddleware = [
62
+ ...this.api.middleware,
63
+ ...("middleware" in this.host && Array.isArray(this.host.middleware) ? this.host.middleware : []),
64
+ ];
65
+ // Process all middleware in one pass
66
+ for (let i = 0; i < allMiddleware.length; i++) {
67
+ const events = allMiddleware[i];
63
68
  const eventTypes = Object.keys(events);
64
69
  for (let n = 0; n < eventTypes.length; n++) {
65
70
  const eventType = eventTypes[n];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.12.0-alpha.44",
3
+ "version": "0.12.0-alpha.45",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",