api-def 0.9.1 → 0.9.2

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 (121) hide show
  1. package/cjs/Api.d.ts +35 -35
  2. package/cjs/Api.js +140 -144
  3. package/cjs/ApiConstants.d.ts +58 -58
  4. package/cjs/ApiConstants.js +55 -55
  5. package/cjs/ApiTypes.d.ts +103 -103
  6. package/cjs/ApiTypes.js +4 -4
  7. package/cjs/ApiUtils.d.ts +6 -6
  8. package/cjs/ApiUtils.js +51 -53
  9. package/cjs/Endpoint.d.ts +52 -52
  10. package/cjs/Endpoint.js +104 -108
  11. package/cjs/EndpointBuilder.d.ts +14 -14
  12. package/cjs/EndpointBuilder.js +46 -46
  13. package/cjs/MockingTypes.d.ts +30 -30
  14. package/cjs/MockingTypes.js +2 -2
  15. package/cjs/QueryHandling.d.ts +3 -3
  16. package/cjs/QueryHandling.js +24 -24
  17. package/cjs/RequestConfig.d.ts +2 -2
  18. package/cjs/RequestConfig.js +74 -74
  19. package/cjs/RequestContext.d.ts +48 -48
  20. package/cjs/RequestContext.js +235 -238
  21. package/cjs/RequestError.d.ts +30 -30
  22. package/cjs/RequestError.js +47 -47
  23. package/cjs/Requester.d.ts +3 -3
  24. package/cjs/Requester.js +335 -336
  25. package/cjs/TextDecoding.d.ts +1 -1
  26. package/cjs/TextDecoding.js +152 -139
  27. package/cjs/UtilTypes.d.ts +5 -5
  28. package/cjs/UtilTypes.js +2 -2
  29. package/cjs/Utils.d.ts +15 -15
  30. package/cjs/Utils.js +79 -77
  31. package/cjs/Validation.d.ts +8 -8
  32. package/cjs/Validation.js +2 -2
  33. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  34. package/cjs/backend/AxiosRequestBackend.js +99 -99
  35. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  36. package/cjs/backend/FetchRequestBackend.js +202 -202
  37. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  38. package/cjs/backend/MockRequestBackend.js +164 -164
  39. package/cjs/backend/RequestBackend.d.ts +19 -19
  40. package/cjs/backend/RequestBackend.js +2 -2
  41. package/cjs/cache/CacheBackend.d.ts +6 -6
  42. package/cjs/cache/CacheBackend.js +2 -2
  43. package/cjs/cache/Caching.d.ts +10 -10
  44. package/cjs/cache/Caching.js +88 -88
  45. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  46. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  47. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  48. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  49. package/cjs/index.d.ts +13 -13
  50. package/cjs/index.js +41 -41
  51. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  52. package/cjs/middleware/CacheMiddleware.js +107 -107
  53. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  54. package/cjs/middleware/LoggingMiddleware.js +82 -88
  55. package/cjs/util/retry/index.d.ts +3 -3
  56. package/cjs/util/retry/index.js +52 -52
  57. package/cjs/util/retry/lib/retry.d.ts +3 -3
  58. package/cjs/util/retry/lib/retry.js +50 -54
  59. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  60. package/cjs/util/retry/lib/retryOperation.js +130 -134
  61. package/esm/Api.d.ts +35 -35
  62. package/esm/Api.js +92 -98
  63. package/esm/ApiConstants.d.ts +58 -58
  64. package/esm/ApiConstants.js +52 -52
  65. package/esm/ApiTypes.d.ts +103 -103
  66. package/esm/ApiTypes.js +1 -1
  67. package/esm/ApiUtils.d.ts +6 -6
  68. package/esm/ApiUtils.js +43 -45
  69. package/esm/Endpoint.d.ts +52 -52
  70. package/esm/Endpoint.js +66 -70
  71. package/esm/EndpointBuilder.d.ts +14 -14
  72. package/esm/EndpointBuilder.js +31 -31
  73. package/esm/MockingTypes.d.ts +30 -30
  74. package/esm/MockingTypes.js +1 -1
  75. package/esm/QueryHandling.d.ts +3 -3
  76. package/esm/QueryHandling.js +19 -19
  77. package/esm/RequestConfig.d.ts +2 -2
  78. package/esm/RequestConfig.js +56 -56
  79. package/esm/RequestContext.d.ts +48 -48
  80. package/esm/RequestContext.js +167 -170
  81. package/esm/RequestError.d.ts +30 -30
  82. package/esm/RequestError.js +42 -42
  83. package/esm/Requester.d.ts +3 -3
  84. package/esm/Requester.js +252 -253
  85. package/esm/TextDecoding.d.ts +1 -1
  86. package/esm/TextDecoding.js +148 -135
  87. package/esm/UtilTypes.d.ts +5 -5
  88. package/esm/UtilTypes.js +1 -1
  89. package/esm/Utils.d.ts +15 -15
  90. package/esm/Utils.js +66 -68
  91. package/esm/Validation.d.ts +8 -8
  92. package/esm/Validation.js +1 -1
  93. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  94. package/esm/backend/AxiosRequestBackend.js +62 -62
  95. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  96. package/esm/backend/FetchRequestBackend.js +136 -136
  97. package/esm/backend/MockRequestBackend.d.ts +11 -11
  98. package/esm/backend/MockRequestBackend.js +116 -116
  99. package/esm/backend/RequestBackend.d.ts +19 -19
  100. package/esm/backend/RequestBackend.js +1 -1
  101. package/esm/cache/CacheBackend.d.ts +6 -6
  102. package/esm/cache/CacheBackend.js +1 -1
  103. package/esm/cache/Caching.d.ts +10 -10
  104. package/esm/cache/Caching.js +39 -39
  105. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  106. package/esm/cache/LocalForageCacheBackend.js +20 -20
  107. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  108. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  109. package/esm/index.d.ts +13 -13
  110. package/esm/index.js +14 -15
  111. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  112. package/esm/middleware/CacheMiddleware.js +58 -58
  113. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  114. package/esm/middleware/LoggingMiddleware.js +77 -83
  115. package/esm/util/retry/index.d.ts +3 -3
  116. package/esm/util/retry/index.js +50 -50
  117. package/esm/util/retry/lib/retry.d.ts +3 -3
  118. package/esm/util/retry/lib/retry.js +35 -39
  119. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  120. package/esm/util/retry/lib/retryOperation.js +126 -132
  121. package/package.json +12 -32
@@ -1,170 +1,167 @@
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 * as Utils from "./Utils";
11
- let contextIdCounter = 0;
12
- export default class RequestContext {
13
- constructor(backend, host, config, computedPath, mocking) {
14
- this.canceler = null;
15
- this.response = undefined;
16
- this.error = null;
17
- this.cacheInfo = { cached: false, source: null };
18
- this.cancelled = false;
19
- this.backend = backend;
20
- this.id = contextIdCounter++;
21
- this.host = host;
22
- this.computedConfig = config;
23
- Utils.assign({}, this.computedConfig.headers);
24
- this.computedPath = computedPath;
25
- this.computedBaseUrl = host.baseUrl;
26
- this.computedMethod = host.method;
27
- this.key = this.generateKey();
28
- this.stats = {
29
- attempt: 0,
30
- cached: false,
31
- };
32
- this.eventHandlers = {};
33
- this.mocking = mocking;
34
- this.validation = host.validation;
35
- this.initMiddleware();
36
- this.parseRequestBody();
37
- this.computedRequestUrl = this.generateRequestUrl();
38
- }
39
- get method() {
40
- return this.computedMethod;
41
- }
42
- get api() {
43
- return this.host.api;
44
- }
45
- get baseUrl() {
46
- return this.computedBaseUrl;
47
- }
48
- get responseType() {
49
- return this.host.responseType;
50
- }
51
- initMiddleware() {
52
- const { middleware } = this.api;
53
- for (let i = 0; i < middleware.length; i++) {
54
- const events = middleware[i];
55
- const eventTypes = Object.keys(events);
56
- for (let n = 0; n < eventTypes.length; n++) {
57
- const eventType = eventTypes[n];
58
- const eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
59
- const middlewareEventHandlers = events[eventType];
60
- if (middlewareEventHandlers) {
61
- eventHandlersForType.push(middlewareEventHandlers);
62
- }
63
- }
64
- }
65
- }
66
- generateKey() {
67
- let key = this.computedPath.trim();
68
- if (this.computedConfig.queryString) {
69
- key += "?" + this.computedConfig.queryString;
70
- }
71
- return key;
72
- }
73
- updateHeaders(newHeaders) {
74
- this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
75
- this.parseRequestBody();
76
- return this;
77
- }
78
- parseRequestBody() {
79
- var _a;
80
- if (this.computedConfig.body && this.computedConfig.headers) {
81
- const contentTypeKey = Object.keys(this.computedConfig.headers).find(key => key.toLowerCase() === "content-type");
82
- const contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
83
- if (contentType === "application/x-www-form-urlencoded") {
84
- const searchParams = new URLSearchParams();
85
- for (const key of Object.keys(this.computedConfig.body)) {
86
- const value = this.computedConfig.body[key];
87
- searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
88
- }
89
- this.parsedBody = searchParams;
90
- }
91
- else {
92
- this.parsedBody = this.computedConfig.body;
93
- }
94
- }
95
- else {
96
- this.parsedBody = undefined;
97
- }
98
- }
99
- getParsedBody() {
100
- return this.parsedBody;
101
- }
102
- updateQuery(newQuery) {
103
- this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
104
- return this;
105
- }
106
- triggerEvent(eventType) {
107
- return __awaiter(this, void 0, void 0, function* () {
108
- const eventHandlers = this.eventHandlers[eventType];
109
- if (eventHandlers) {
110
- for (let i = 0; i < eventHandlers.length; i++) {
111
- const eventHandler = eventHandlers[i];
112
- // eslint-disable-next-line no-await-in-loop
113
- const eventResult = yield eventHandler(this);
114
- if (eventResult) {
115
- return eventResult;
116
- }
117
- }
118
- }
119
- return undefined;
120
- });
121
- }
122
- addCanceller(canceler) {
123
- this.canceler = canceler;
124
- }
125
- cancel() {
126
- this.cancelled = true;
127
- if (this.canceler) {
128
- this.canceler();
129
- }
130
- }
131
- get requestUrl() {
132
- return this.computedRequestUrl;
133
- }
134
- get path() {
135
- return this.computedPath;
136
- }
137
- updatePath(path) {
138
- this.computedPath = path;
139
- this.computedRequestUrl = this.generateRequestUrl();
140
- }
141
- updateBaseUrl(baseUrl) {
142
- this.computedBaseUrl = baseUrl;
143
- this.computedRequestUrl = this.generateRequestUrl();
144
- }
145
- updateMethod(method) {
146
- this.computedMethod = method;
147
- }
148
- generateRequestUrl() {
149
- let path = !this.baseUrl.endsWith("/")
150
- ? this.baseUrl + "/"
151
- : this.baseUrl;
152
- path += this.computedPath.startsWith("/")
153
- ? this.computedPath.substring(1)
154
- : this.computedPath;
155
- let origin = undefined;
156
- if (typeof window !== "undefined") {
157
- origin = window.origin;
158
- }
159
- if (!origin) {
160
- if (path.indexOf("://") === -1) {
161
- path = `https://${path}`;
162
- }
163
- }
164
- const url = new URL(path, origin);
165
- if (this.computedConfig.queryString) {
166
- url.search = this.computedConfig.queryString;
167
- }
168
- return url;
169
- }
170
- }
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 * as Utils from "./Utils";
11
+ let contextIdCounter = 0;
12
+ export default class RequestContext {
13
+ constructor(backend, host, config, computedPath, mocking) {
14
+ this.canceler = null;
15
+ this.response = undefined;
16
+ this.error = null;
17
+ this.cacheInfo = { cached: false, source: null };
18
+ this.cancelled = false;
19
+ this.backend = backend;
20
+ this.id = contextIdCounter++;
21
+ this.host = host;
22
+ this.computedConfig = config;
23
+ Utils.assign({}, this.computedConfig.headers);
24
+ this.computedPath = computedPath;
25
+ this.computedBaseUrl = host.baseUrl;
26
+ this.computedMethod = host.method;
27
+ this.key = this.generateKey();
28
+ this.stats = {
29
+ attempt: 0,
30
+ cached: false,
31
+ };
32
+ this.eventHandlers = {};
33
+ this.mocking = mocking;
34
+ this.validation = host.validation;
35
+ this.initMiddleware();
36
+ this.parseRequestBody();
37
+ this.computedRequestUrl = this.generateRequestUrl();
38
+ }
39
+ get method() {
40
+ return this.computedMethod;
41
+ }
42
+ get api() {
43
+ return this.host.api;
44
+ }
45
+ get baseUrl() {
46
+ return this.computedBaseUrl;
47
+ }
48
+ get responseType() {
49
+ return this.host.responseType;
50
+ }
51
+ initMiddleware() {
52
+ const { middleware } = this.api;
53
+ for (let i = 0; i < middleware.length; i++) {
54
+ const events = middleware[i];
55
+ const eventTypes = Object.keys(events);
56
+ for (let n = 0; n < eventTypes.length; n++) {
57
+ const eventType = eventTypes[n];
58
+ const eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
59
+ const middlewareEventHandlers = events[eventType];
60
+ if (middlewareEventHandlers) {
61
+ eventHandlersForType.push(middlewareEventHandlers);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ generateKey() {
67
+ let key = this.computedPath.trim();
68
+ if (this.computedConfig.queryString) {
69
+ key += `?${this.computedConfig.queryString}`;
70
+ }
71
+ return key;
72
+ }
73
+ updateHeaders(newHeaders) {
74
+ this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
75
+ this.parseRequestBody();
76
+ return this;
77
+ }
78
+ parseRequestBody() {
79
+ var _a;
80
+ if (this.computedConfig.body && this.computedConfig.headers) {
81
+ const contentTypeKey = Object.keys(this.computedConfig.headers).find((key) => key.toLowerCase() === "content-type");
82
+ const contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
83
+ if (contentType === "application/x-www-form-urlencoded") {
84
+ const searchParams = new URLSearchParams();
85
+ for (const key of Object.keys(this.computedConfig.body)) {
86
+ const value = this.computedConfig.body[key];
87
+ searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
88
+ }
89
+ this.parsedBody = searchParams;
90
+ }
91
+ else {
92
+ this.parsedBody = this.computedConfig.body;
93
+ }
94
+ }
95
+ else {
96
+ this.parsedBody = undefined;
97
+ }
98
+ }
99
+ getParsedBody() {
100
+ return this.parsedBody;
101
+ }
102
+ updateQuery(newQuery) {
103
+ this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
104
+ this.computedRequestUrl = this.generateRequestUrl();
105
+ return this;
106
+ }
107
+ triggerEvent(eventType) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ const eventHandlers = this.eventHandlers[eventType];
110
+ if (eventHandlers) {
111
+ for (let i = 0; i < eventHandlers.length; i++) {
112
+ const eventHandler = eventHandlers[i];
113
+ // eslint-disable-next-line no-await-in-loop
114
+ const eventResult = yield eventHandler(this);
115
+ if (eventResult) {
116
+ return eventResult;
117
+ }
118
+ }
119
+ }
120
+ return undefined;
121
+ });
122
+ }
123
+ addCanceller(canceler) {
124
+ this.canceler = canceler;
125
+ }
126
+ cancel() {
127
+ this.cancelled = true;
128
+ if (this.canceler) {
129
+ this.canceler();
130
+ }
131
+ }
132
+ get requestUrl() {
133
+ return this.computedRequestUrl;
134
+ }
135
+ get path() {
136
+ return this.computedPath;
137
+ }
138
+ updatePath(path) {
139
+ this.computedPath = path;
140
+ this.computedRequestUrl = this.generateRequestUrl();
141
+ }
142
+ updateBaseUrl(baseUrl) {
143
+ this.computedBaseUrl = baseUrl;
144
+ this.computedRequestUrl = this.generateRequestUrl();
145
+ }
146
+ updateMethod(method) {
147
+ this.computedMethod = method;
148
+ }
149
+ generateRequestUrl() {
150
+ let path = !this.baseUrl.endsWith("/") ? `${this.baseUrl}/` : this.baseUrl;
151
+ path += this.computedPath.startsWith("/") ? this.computedPath.substring(1) : this.computedPath;
152
+ let origin = undefined;
153
+ if (typeof window !== "undefined") {
154
+ origin = window.origin;
155
+ }
156
+ if (!origin) {
157
+ if (path.indexOf("://") === -1) {
158
+ path = `https://${path}`;
159
+ }
160
+ }
161
+ const url = new URL(path, origin);
162
+ if (this.computedConfig.queryString) {
163
+ url.search = this.computedConfig.queryString;
164
+ }
165
+ return url;
166
+ }
167
+ }
@@ -1,30 +1,30 @@
1
- import { ApiResponse } from "./ApiTypes";
2
- import { EnumOf } from "./Utils";
3
- import RequestContext from "./RequestContext";
4
- export declare const RequestErrorCode: {
5
- readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
6
- readonly REQUEST_NETWORK_ERROR: "request/network-error";
7
- readonly REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found";
8
- readonly REQUEST_INVALID_STATUS: "request/invalid-status";
9
- readonly REQUEST_INVALID_CONFIG: "request/invalid-config";
10
- readonly REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type";
11
- readonly VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error";
12
- readonly VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error";
13
- readonly VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error";
14
- readonly VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error";
15
- };
16
- export declare type RequestErrorCode = EnumOf<typeof RequestErrorCode>;
17
- export interface RequestError extends Error {
18
- isRequestError: true;
19
- response: ApiResponse | undefined | null;
20
- code: string;
21
- attempts: number;
22
- }
23
- export declare const isRequestError: (error: Error) => error is RequestError;
24
- export interface RequestErrorConfig {
25
- error: Error;
26
- code: string;
27
- context: RequestContext;
28
- response?: ApiResponse | null;
29
- }
30
- export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
1
+ import { ApiResponse } from "./ApiTypes";
2
+ import RequestContext from "./RequestContext";
3
+ import { EnumOf } from "./Utils";
4
+ export declare const RequestErrorCode: {
5
+ readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
6
+ readonly REQUEST_NETWORK_ERROR: "request/network-error";
7
+ readonly REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found";
8
+ readonly REQUEST_INVALID_STATUS: "request/invalid-status";
9
+ readonly REQUEST_INVALID_CONFIG: "request/invalid-config";
10
+ readonly REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type";
11
+ readonly VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error";
12
+ readonly VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error";
13
+ readonly VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error";
14
+ readonly VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error";
15
+ };
16
+ export type RequestErrorCode = EnumOf<typeof RequestErrorCode>;
17
+ export interface RequestError extends Error {
18
+ isRequestError: true;
19
+ response: ApiResponse | undefined | null;
20
+ code: string;
21
+ attempts: number;
22
+ }
23
+ export declare const isRequestError: (error: Error) => error is RequestError;
24
+ export interface RequestErrorConfig {
25
+ error: Error;
26
+ code: string;
27
+ context: RequestContext;
28
+ response?: ApiResponse | null;
29
+ }
30
+ export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
@@ -1,42 +1,42 @@
1
- export const RequestErrorCode = {
2
- MISC_UNKNOWN_ERROR: "misc/unknown-error",
3
- REQUEST_NETWORK_ERROR: "request/network-error",
4
- REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found",
5
- REQUEST_INVALID_STATUS: "request/invalid-status",
6
- REQUEST_INVALID_CONFIG: "request/invalid-config",
7
- REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type",
8
- VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error",
9
- VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error",
10
- VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error",
11
- VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error",
12
- };
13
- export const isRequestError = (error) => {
14
- return "isRequestError" in error;
15
- };
16
- export const convertToRequestError = (config) => {
17
- const { error, context, response, code } = config;
18
- const body = context.getParsedBody();
19
- const resultError = Object.assign(error, {
20
- name: "RequestError",
21
- response: response,
22
- code: code,
23
- isRequestError: true,
24
- attempts: context.stats.attempt,
25
- request: {
26
- url: context.requestUrl.href,
27
- query: context.computedConfig.queryObject,
28
- headers: context.computedConfig.headers,
29
- body: body,
30
- },
31
- });
32
- try {
33
- Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}` });
34
- }
35
- catch (e) {
36
- // ignore
37
- }
38
- delete resultError.config;
39
- delete resultError.toJSON;
40
- Object.setPrototypeOf(error, Error);
41
- return resultError;
42
- };
1
+ export const RequestErrorCode = {
2
+ MISC_UNKNOWN_ERROR: "misc/unknown-error",
3
+ REQUEST_NETWORK_ERROR: "request/network-error",
4
+ REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found",
5
+ REQUEST_INVALID_STATUS: "request/invalid-status",
6
+ REQUEST_INVALID_CONFIG: "request/invalid-config",
7
+ REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type",
8
+ VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error",
9
+ VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error",
10
+ VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error",
11
+ VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error",
12
+ };
13
+ export const isRequestError = (error) => {
14
+ return "isRequestError" in error;
15
+ };
16
+ export const convertToRequestError = (config) => {
17
+ const { error, context, response, code } = config;
18
+ const body = context.getParsedBody();
19
+ const resultError = Object.assign(error, {
20
+ name: "RequestError",
21
+ response: response,
22
+ code: code,
23
+ isRequestError: true,
24
+ attempts: context.stats.attempt,
25
+ request: {
26
+ url: context.requestUrl.href,
27
+ query: context.computedConfig.queryObject,
28
+ headers: context.computedConfig.headers,
29
+ body: body,
30
+ },
31
+ });
32
+ try {
33
+ Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}` });
34
+ }
35
+ catch (e) {
36
+ // ignore
37
+ }
38
+ resultError.config = undefined;
39
+ resultError.toJSON = undefined;
40
+ Object.setPrototypeOf(error, Error);
41
+ return resultError;
42
+ };
@@ -1,3 +1,3 @@
1
- import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
2
- import { EndpointMockingConfig } from "./MockingTypes";
3
- export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;
1
+ import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
2
+ import { EndpointMockingConfig } from "./MockingTypes";
3
+ export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;