api-def 0.12.0-alpha.54 → 0.12.0-alpha.55

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/ApiUtils.d.ts CHANGED
@@ -8,5 +8,5 @@ export declare const isCancelledError: (error: Error) => error is CancelledReque
8
8
  export declare const isNetworkError: (error: Error) => boolean;
9
9
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
10
10
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
11
- export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined) => string;
11
+ export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined, allowMissing?: boolean) => string;
12
12
  export declare const resolveUrl: (options: ResolveUrlOptions) => URL;
package/cjs/ApiUtils.js CHANGED
@@ -55,7 +55,7 @@ var inferResponseType = function (contentType) {
55
55
  return "text";
56
56
  };
57
57
  exports.inferResponseType = inferResponseType;
58
- var resolvePathParams = function (path, params) {
58
+ var resolvePathParams = function (path, params, allowMissing) {
59
59
  var computedPath = path.startsWith("/") ? path : "/".concat(path);
60
60
  if (params) {
61
61
  var computedPathParts = computedPath.split("/");
@@ -72,13 +72,16 @@ var resolvePathParams = function (path, params) {
72
72
  if (paramKey) {
73
73
  var paramValue = params[paramKey];
74
74
  if (!paramValue) {
75
+ if (allowMissing) {
76
+ continue;
77
+ }
75
78
  throw new Error("[api-def] Missing param '".concat(paramKey, "'"));
76
79
  }
77
80
  computedPathParts[i] = paramValue;
78
81
  unusedKeys.delete(paramKey);
79
82
  }
80
83
  }
81
- if (unusedKeys.size > 0) {
84
+ if (unusedKeys.size > 0 && !allowMissing) {
82
85
  throw new Error("[api-def] Missing param '".concat(Array.from(unusedKeys)[0], "'"));
83
86
  }
84
87
  return computedPathParts.join("/");
@@ -8,6 +8,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;
11
+ private readonly rawPath;
11
12
  private computedPath;
12
13
  private computedBaseUrl;
13
14
  private computedMethod;
@@ -25,7 +26,8 @@ export default class RequestContext<TResponse = any, TParams extends Params | un
25
26
  readonly mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined;
26
27
  private parsedBody;
27
28
  readonly validation: Validation<TResponse, TParams, TQuery, TBody, TState>;
28
- constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<TParams, TQuery, TBody, TState>, computedPath: string, mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined);
29
+ constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<TParams, TQuery, TBody, TState>, rawPath: string, mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined);
30
+ validatePath(): void;
29
31
  /**
30
32
  * @deprecated Use `requestConfig` instead
31
33
  */
@@ -37,6 +39,7 @@ export default class RequestContext<TResponse = any, TParams extends Params | un
37
39
  private initMiddleware;
38
40
  private generateKey;
39
41
  updateHeaders(newHeaders: RawHeaders): this;
42
+ updateParams(params: Partial<Record<string, string>>): this;
40
43
  private parseRequestBody;
41
44
  getParsedBody(): any;
42
45
  updateQuery(newQuery: Partial<TQuery>): this;
@@ -49,7 +49,7 @@ var ApiUtils_1 = require("./ApiUtils");
49
49
  var Utils = require("./Utils");
50
50
  var contextIdCounter = 0;
51
51
  var RequestContext = /** @class */ (function () {
52
- function RequestContext(backend, host, config, computedPath, mocking) {
52
+ function RequestContext(backend, host, config, rawPath, mocking) {
53
53
  this.canceler = null;
54
54
  this.response = undefined;
55
55
  this.error = null;
@@ -60,7 +60,8 @@ var RequestContext = /** @class */ (function () {
60
60
  this.host = host;
61
61
  this.requestConfig = config;
62
62
  Utils.assign({}, this.requestConfig.headers);
63
- this.computedPath = computedPath;
63
+ this.rawPath = rawPath;
64
+ this.computedPath = (0, ApiUtils_1.resolvePathParams)(host.path, config.params, true);
64
65
  this.computedBaseUrl = host.baseUrl;
65
66
  this.computedMethod = host.method;
66
67
  this.key = this.generateKey();
@@ -77,6 +78,9 @@ var RequestContext = /** @class */ (function () {
77
78
  this.parseRequestBody();
78
79
  this.computedRequestUrl = this.resolveRequestUrl();
79
80
  }
81
+ RequestContext.prototype.validatePath = function () {
82
+ this.computedPath = (0, ApiUtils_1.resolvePathParams)(this.rawPath, this.requestConfig.params);
83
+ };
80
84
  Object.defineProperty(RequestContext.prototype, "computedConfig", {
81
85
  /**
82
86
  * @deprecated Use `requestConfig` instead
@@ -144,6 +148,12 @@ var RequestContext = /** @class */ (function () {
144
148
  this.parseRequestBody();
145
149
  return this;
146
150
  };
151
+ RequestContext.prototype.updateParams = function (params) {
152
+ this.requestConfig.params = Utils.assign({}, this.requestConfig.params, params);
153
+ this.computedPath = (0, ApiUtils_1.resolvePathParams)(this.rawPath, this.requestConfig.params);
154
+ this.computedRequestUrl = this.resolveRequestUrl();
155
+ return this;
156
+ };
147
157
  RequestContext.prototype.parseRequestBody = function () {
148
158
  var _a;
149
159
  if (this.requestConfig.body && this.requestConfig.headers) {
package/cjs/Requester.js CHANGED
@@ -65,7 +65,7 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
65
65
  case 0:
66
66
  computedConfig = host.computeConfig(config);
67
67
  backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
68
- context = new RequestContext_1.default(backend, host, computedConfig, (0, ApiUtils_1.resolvePathParams)(host.path, config.params), mocking);
68
+ context = new RequestContext_1.default(backend, host, computedConfig, host.path, mocking);
69
69
  key = context.key;
70
70
  lock = (context.requestConfig || {}).lock;
71
71
  if (typeof lock === "string") {
@@ -123,6 +123,7 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
123
123
  if (beforeSendEventResult && beforeSendEventResult.type === ApiConstants_1.EventResultType.RESPOND) {
124
124
  return [2 /*return*/, (context.response = beforeSendEventResult.response)];
125
125
  }
126
+ context.validatePath();
126
127
  // validation
127
128
  if (context.validation.query) {
128
129
  try {
package/esm/ApiUtils.d.ts CHANGED
@@ -8,5 +8,5 @@ export declare const isCancelledError: (error: Error) => error is CancelledReque
8
8
  export declare const isNetworkError: (error: Error) => boolean;
9
9
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
10
10
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
11
- export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined) => string;
11
+ export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined, allowMissing?: boolean) => string;
12
12
  export declare const resolveUrl: (options: ResolveUrlOptions) => URL;
package/esm/ApiUtils.js CHANGED
@@ -47,7 +47,7 @@ export const inferResponseType = (contentType) => {
47
47
  }
48
48
  return "text";
49
49
  };
50
- export const resolvePathParams = (path, params) => {
50
+ export const resolvePathParams = (path, params, allowMissing) => {
51
51
  const computedPath = path.startsWith("/") ? path : `/${path}`;
52
52
  if (params) {
53
53
  const computedPathParts = computedPath.split("/");
@@ -64,13 +64,16 @@ export const resolvePathParams = (path, params) => {
64
64
  if (paramKey) {
65
65
  const paramValue = params[paramKey];
66
66
  if (!paramValue) {
67
+ if (allowMissing) {
68
+ continue;
69
+ }
67
70
  throw new Error(`[api-def] Missing param '${paramKey}'`);
68
71
  }
69
72
  computedPathParts[i] = paramValue;
70
73
  unusedKeys.delete(paramKey);
71
74
  }
72
75
  }
73
- if (unusedKeys.size > 0) {
76
+ if (unusedKeys.size > 0 && !allowMissing) {
74
77
  throw new Error(`[api-def] Missing param '${Array.from(unusedKeys)[0]}'`);
75
78
  }
76
79
  return computedPathParts.join("/");
@@ -8,6 +8,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;
11
+ private readonly rawPath;
11
12
  private computedPath;
12
13
  private computedBaseUrl;
13
14
  private computedMethod;
@@ -25,7 +26,8 @@ export default class RequestContext<TResponse = any, TParams extends Params | un
25
26
  readonly mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined;
26
27
  private parsedBody;
27
28
  readonly validation: Validation<TResponse, TParams, TQuery, TBody, TState>;
28
- constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<TParams, TQuery, TBody, TState>, computedPath: string, mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined);
29
+ constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<TParams, TQuery, TBody, TState>, rawPath: string, mocking: EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | null | undefined);
30
+ validatePath(): void;
29
31
  /**
30
32
  * @deprecated Use `requestConfig` instead
31
33
  */
@@ -37,6 +39,7 @@ export default class RequestContext<TResponse = any, TParams extends Params | un
37
39
  private initMiddleware;
38
40
  private generateKey;
39
41
  updateHeaders(newHeaders: RawHeaders): this;
42
+ updateParams(params: Partial<Record<string, string>>): this;
40
43
  private parseRequestBody;
41
44
  getParsedBody(): any;
42
45
  updateQuery(newQuery: Partial<TQuery>): this;
@@ -7,11 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { resolveUrl } from "./ApiUtils";
10
+ import { resolvePathParams, resolveUrl } from "./ApiUtils";
11
11
  import * as Utils from "./Utils";
12
12
  let contextIdCounter = 0;
13
13
  export default class RequestContext {
14
- constructor(backend, host, config, computedPath, mocking) {
14
+ constructor(backend, host, config, rawPath, mocking) {
15
15
  this.canceler = null;
16
16
  this.response = undefined;
17
17
  this.error = null;
@@ -22,7 +22,8 @@ export default class RequestContext {
22
22
  this.host = host;
23
23
  this.requestConfig = config;
24
24
  Utils.assign({}, this.requestConfig.headers);
25
- this.computedPath = computedPath;
25
+ this.rawPath = rawPath;
26
+ this.computedPath = resolvePathParams(host.path, config.params, true);
26
27
  this.computedBaseUrl = host.baseUrl;
27
28
  this.computedMethod = host.method;
28
29
  this.key = this.generateKey();
@@ -39,6 +40,9 @@ export default class RequestContext {
39
40
  this.parseRequestBody();
40
41
  this.computedRequestUrl = this.resolveRequestUrl();
41
42
  }
43
+ validatePath() {
44
+ this.computedPath = resolvePathParams(this.rawPath, this.requestConfig.params);
45
+ }
42
46
  /**
43
47
  * @deprecated Use `requestConfig` instead
44
48
  */
@@ -89,6 +93,12 @@ export default class RequestContext {
89
93
  this.parseRequestBody();
90
94
  return this;
91
95
  }
96
+ updateParams(params) {
97
+ this.requestConfig.params = Utils.assign({}, this.requestConfig.params, params);
98
+ this.computedPath = resolvePathParams(this.rawPath, this.requestConfig.params);
99
+ this.computedRequestUrl = this.resolveRequestUrl();
100
+ return this;
101
+ }
92
102
  parseRequestBody() {
93
103
  var _a;
94
104
  if (this.requestConfig.body && this.requestConfig.headers) {
package/esm/Requester.js CHANGED
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { EventResultType, RequestEvent } from "./ApiConstants";
11
- import { inferResponseType, isAcceptableStatus, isNetworkError, resolvePathParams } from "./ApiUtils";
11
+ import { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
12
12
  import RequestContext from "./RequestContext";
13
13
  import { RequestErrorCode, convertToRequestError, isRequestError } from "./RequestError";
14
14
  import { textDecode } from "./TextDecoding";
@@ -20,7 +20,7 @@ const MOCK_REQUEST_BACKEND = new MockRequestBackend();
20
20
  export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
21
21
  const computedConfig = host.computeConfig(config);
22
22
  const backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
23
- const context = new RequestContext(backend, host, computedConfig, resolvePathParams(host.path, config.params), mocking);
23
+ const context = new RequestContext(backend, host, computedConfig, host.path, mocking);
24
24
  const { key } = context;
25
25
  // don't do this -- should only be for GET requests anyway and should be opt-in
26
26
  /*
@@ -72,6 +72,7 @@ const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* ()
72
72
  if (beforeSendEventResult && beforeSendEventResult.type === EventResultType.RESPOND) {
73
73
  return (context.response = beforeSendEventResult.response);
74
74
  }
75
+ context.validatePath();
75
76
  // validation
76
77
  if (context.validation.query) {
77
78
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.12.0-alpha.54",
3
+ "version": "0.12.0-alpha.55",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",