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.
- package/README.md +3 -0
- package/bin/index.js +312 -313
- package/cjs/Api.d.ts +6 -6
- package/cjs/Api.js +99 -143
- package/cjs/ApiTypes.d.ts +1 -1
- package/cjs/ApiUtils.d.ts +2 -1
- package/cjs/ApiUtils.js +41 -33
- package/cjs/Endpoint.d.ts +1 -1
- package/cjs/Endpoint.js +112 -175
- package/cjs/EndpointBuilder.js +23 -32
- package/cjs/QueryHandling.js +12 -13
- package/cjs/RequestConfig.js +54 -36
- package/cjs/RequestContext.d.ts +1 -1
- package/cjs/RequestContext.js +125 -180
- package/cjs/RequestError.js +16 -19
- package/cjs/Requester.js +230 -312
- package/cjs/TextDecoding.js +31 -31
- package/cjs/Utils.js +20 -23
- package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
- package/cjs/backend/AxiosRequestBackend.js +65 -172
- package/cjs/backend/FetchRequestBackend.d.ts +1 -1
- package/cjs/backend/FetchRequestBackend.js +108 -154
- package/cjs/backend/MockRequestBackend.d.ts +1 -1
- package/cjs/backend/MockRequestBackend.js +146 -208
- package/cjs/cache/ClientCaching.js +26 -74
- package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
- package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
- package/cjs/index.d.ts +11 -11
- package/cjs/index.js +24 -21
- package/cjs/middleware/ClientCacheMiddleware.js +80 -103
- package/cjs/middleware/LoggingMiddleware.js +71 -42
- package/cjs/util/retry/index.js +42 -9
- package/cjs/util/retry/lib/retry.js +25 -27
- package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
- package/cjs/util/retry/lib/retryOperation.js +21 -23
- package/esm/Api.d.ts +11 -11
- package/esm/Api.js +12 -23
- package/esm/ApiConstants.d.ts +1 -1
- package/esm/ApiTypes.d.ts +5 -5
- package/esm/ApiUtils.d.ts +4 -3
- package/esm/ApiUtils.js +15 -7
- package/esm/Endpoint.d.ts +6 -6
- package/esm/Endpoint.js +22 -28
- package/esm/EndpointBuilder.d.ts +4 -4
- package/esm/EndpointBuilder.js +2 -2
- package/esm/MockingTypes.d.ts +1 -1
- package/esm/QueryHandling.d.ts +1 -1
- package/esm/QueryHandling.js +2 -3
- package/esm/RequestConfig.d.ts +1 -1
- package/esm/RequestConfig.js +6 -7
- package/esm/RequestContext.d.ts +7 -7
- package/esm/RequestContext.js +15 -27
- package/esm/RequestError.d.ts +3 -3
- package/esm/RequestError.js +2 -3
- package/esm/Requester.d.ts +2 -2
- package/esm/Requester.js +44 -50
- package/esm/UtilTypes.d.ts +1 -1
- package/esm/Utils.js +2 -1
- package/esm/Validation.d.ts +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +4 -4
- package/esm/backend/AxiosRequestBackend.js +47 -84
- package/esm/backend/FetchRequestBackend.d.ts +5 -5
- package/esm/backend/FetchRequestBackend.js +49 -63
- package/esm/backend/MockRequestBackend.d.ts +4 -4
- package/esm/backend/MockRequestBackend.js +105 -136
- package/esm/backend/RequestBackend.d.ts +2 -2
- package/esm/cache/ClientCaching.d.ts +1 -1
- package/esm/cache/ClientCaching.js +8 -17
- package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalForageClientCacheBackend.js +8 -25
- package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
- package/esm/index.d.ts +16 -16
- package/esm/index.js +15 -15
- package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
- package/esm/middleware/ClientCacheMiddleware.js +8 -17
- package/esm/middleware/LoggingMiddleware.d.ts +1 -1
- package/esm/middleware/LoggingMiddleware.js +5 -6
- package/esm/util/retry/index.js +1 -1
- package/esm/util/retry/lib/retry.d.ts +1 -1
- package/esm/util/retry/lib/retry.js +13 -7
- package/esm/util/retry/lib/retryOperation.d.ts +1 -26
- package/esm/util/retry/lib/retryOperation.js +1 -1
- package/package.json +66 -28
package/cjs/Api.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
2
|
import { type ResolveUrlOptions } from "./ApiUtils";
|
|
3
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
3
4
|
import type Endpoint from "./Endpoint";
|
|
4
5
|
import EndpointBuilder from "./EndpointBuilder";
|
|
5
6
|
import type { ApiMockingConfig } from "./MockingTypes";
|
|
6
|
-
import type RequestBackend from "./backend/RequestBackend";
|
|
7
7
|
export declare const getRequestBackend: () => RequestBackend | null;
|
|
8
8
|
export declare const isRequestBackendDefault: () => boolean;
|
|
9
9
|
export declare const setRequestBackend: (backend: RequestBackend) => void;
|
|
@@ -48,10 +48,10 @@ export declare class Api implements ApiInfo {
|
|
|
48
48
|
getConfig(): BaseRequestConfig;
|
|
49
49
|
computeRequestConfig(): BaseRequestConfig;
|
|
50
50
|
private hotRequest;
|
|
51
|
-
get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
52
|
-
post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
53
|
-
put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
54
|
-
delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
55
|
-
patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
51
|
+
get: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
52
|
+
post: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
53
|
+
put: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
54
|
+
delete: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
55
|
+
patch: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
56
56
|
resolveUrl(options: ApiResolveUrlOptions): URL;
|
|
57
57
|
}
|
package/cjs/Api.js
CHANGED
|
@@ -1,95 +1,88 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
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);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
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]; } };
|
|
36
7
|
}
|
|
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
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.Api = exports.setRequestBackend = exports.isRequestBackendDefault = exports.getRequestBackend = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
const ApiConstants_1 = require("./ApiConstants");
|
|
41
|
+
const ApiUtils_1 = require("./ApiUtils");
|
|
42
|
+
const FetchRequestBackend_1 = __importDefault(require("./backend/FetchRequestBackend"));
|
|
43
|
+
const EndpointBuilder_1 = __importDefault(require("./EndpointBuilder"));
|
|
44
|
+
const RequestConfig_1 = require("./RequestConfig");
|
|
45
|
+
const Requester = __importStar(require("./Requester"));
|
|
46
|
+
const Utils = __importStar(require("./Utils"));
|
|
47
47
|
// use fetch as default if it is present
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
let requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend_1.default() : null;
|
|
49
|
+
let requestBackendIsDefault = true;
|
|
50
|
+
const getRequestBackend = () => {
|
|
51
51
|
return requestBackend;
|
|
52
52
|
};
|
|
53
53
|
exports.getRequestBackend = getRequestBackend;
|
|
54
|
-
|
|
54
|
+
const isRequestBackendDefault = () => {
|
|
55
55
|
return requestBackendIsDefault;
|
|
56
56
|
};
|
|
57
57
|
exports.isRequestBackendDefault = isRequestBackendDefault;
|
|
58
|
-
|
|
58
|
+
const setRequestBackend = (backend) => {
|
|
59
59
|
requestBackendIsDefault = false;
|
|
60
60
|
requestBackend = backend;
|
|
61
61
|
};
|
|
62
62
|
exports.setRequestBackend = setRequestBackend;
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
class HotRequestHost {
|
|
64
|
+
constructor(api, path, method) {
|
|
65
65
|
this.responseType = undefined;
|
|
66
66
|
this.validation = {};
|
|
67
67
|
this.api = api;
|
|
68
68
|
this.method = method;
|
|
69
69
|
this.path = path;
|
|
70
70
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
configurable: true
|
|
77
|
-
});
|
|
78
|
-
HotRequestHost.prototype.computeConfig = function (config) {
|
|
79
|
-
var apiDefaults = this.api.computeRequestConfig();
|
|
71
|
+
get baseUrl() {
|
|
72
|
+
return this.api.baseUrl;
|
|
73
|
+
}
|
|
74
|
+
computeConfig(config) {
|
|
75
|
+
const apiDefaults = this.api.computeRequestConfig();
|
|
80
76
|
return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, config]);
|
|
81
|
-
}
|
|
82
|
-
|
|
77
|
+
}
|
|
78
|
+
getRequestBackend() {
|
|
83
79
|
return this.api.requestBackend;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
var Api = /** @class */ (function () {
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
class Api {
|
|
88
83
|
//private mutable = false;
|
|
89
84
|
//private wasMutable = false;
|
|
90
|
-
|
|
91
|
-
var _this = this;
|
|
92
|
-
var _a, _b, _c, _d;
|
|
85
|
+
constructor(options) {
|
|
93
86
|
this.endpoints = {};
|
|
94
87
|
/*configure(info: Partial<ApiInfo>): void {
|
|
95
88
|
if (!this.mutable) {
|
|
@@ -101,20 +94,13 @@ var Api = /** @class */ (function () {
|
|
|
101
94
|
Object.assign(this.info, info);
|
|
102
95
|
this.mutable = false;
|
|
103
96
|
}*/
|
|
104
|
-
this.hotRequest =
|
|
105
|
-
return function (path, config) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
106
|
-
switch (_a.label) {
|
|
107
|
-
case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path, requestMethod), config, null)];
|
|
108
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
109
|
-
}
|
|
110
|
-
}); }); };
|
|
111
|
-
};
|
|
97
|
+
this.hotRequest = (requestMethod) => async (path, config) => await Requester.submit(new HotRequestHost(this, path instanceof URL ? path.href : path, requestMethod), config, null);
|
|
112
98
|
this.get = this.hotRequest(ApiConstants_1.RequestMethod.GET);
|
|
113
99
|
this.post = this.hotRequest(ApiConstants_1.RequestMethod.POST);
|
|
114
100
|
this.put = this.hotRequest(ApiConstants_1.RequestMethod.PUT);
|
|
115
101
|
this.delete = this.hotRequest(ApiConstants_1.RequestMethod.DELETE);
|
|
116
102
|
this.patch = this.hotRequest(ApiConstants_1.RequestMethod.PATCH);
|
|
117
|
-
|
|
103
|
+
const requestBackend = options.requestBackend ?? (0, exports.getRequestBackend)();
|
|
118
104
|
if (!requestBackend) {
|
|
119
105
|
throw new Error("[api-def] No request backend provided in either Api options or globally, use `setRequestBackend()` to set one or pass one via `requestBackend`");
|
|
120
106
|
}
|
|
@@ -122,91 +108,61 @@ var Api = /** @class */ (function () {
|
|
|
122
108
|
name: options.name,
|
|
123
109
|
baseUrl: options.baseUrl,
|
|
124
110
|
middleware: options.middleware || [],
|
|
125
|
-
defaultRequestConfig:
|
|
126
|
-
mocking:
|
|
111
|
+
defaultRequestConfig: options.defaultRequestConfig ?? options.config ?? undefined,
|
|
112
|
+
mocking: options.mocking ?? undefined,
|
|
127
113
|
requestBackend: requestBackend,
|
|
128
114
|
};
|
|
129
115
|
//this.mutable = options.mutable ?? false;
|
|
130
116
|
//this.wasMutable = this.mutable;
|
|
131
117
|
this.endpoints = {};
|
|
132
118
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
get: function () {
|
|
159
|
-
return this.info.defaultRequestConfig;
|
|
160
|
-
},
|
|
161
|
-
enumerable: false,
|
|
162
|
-
configurable: true
|
|
163
|
-
});
|
|
164
|
-
Object.defineProperty(Api.prototype, "middleware", {
|
|
165
|
-
get: function () {
|
|
166
|
-
return this.info.middleware;
|
|
167
|
-
},
|
|
168
|
-
enumerable: false,
|
|
169
|
-
configurable: true
|
|
170
|
-
});
|
|
171
|
-
Object.defineProperty(Api.prototype, "mocking", {
|
|
172
|
-
get: function () {
|
|
173
|
-
return this.info.mocking;
|
|
174
|
-
},
|
|
175
|
-
enumerable: false,
|
|
176
|
-
configurable: true
|
|
177
|
-
});
|
|
178
|
-
Object.defineProperty(Api.prototype, "name", {
|
|
179
|
-
get: function () {
|
|
180
|
-
return this.info.name;
|
|
181
|
-
},
|
|
182
|
-
enumerable: false,
|
|
183
|
-
configurable: true
|
|
184
|
-
});
|
|
185
|
-
Api.prototype.endpoint = function () {
|
|
119
|
+
get requestBackend() {
|
|
120
|
+
return this.info.requestBackend;
|
|
121
|
+
}
|
|
122
|
+
get baseUrl() {
|
|
123
|
+
return this.info.baseUrl;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @deprecated use `defaultRequestConfig` instead
|
|
127
|
+
*/
|
|
128
|
+
get config() {
|
|
129
|
+
return this.info.defaultRequestConfig;
|
|
130
|
+
}
|
|
131
|
+
get defaultRequestConfig() {
|
|
132
|
+
return this.info.defaultRequestConfig;
|
|
133
|
+
}
|
|
134
|
+
get middleware() {
|
|
135
|
+
return this.info.middleware;
|
|
136
|
+
}
|
|
137
|
+
get mocking() {
|
|
138
|
+
return this.info.mocking;
|
|
139
|
+
}
|
|
140
|
+
get name() {
|
|
141
|
+
return this.info.name;
|
|
142
|
+
}
|
|
143
|
+
endpoint() {
|
|
186
144
|
return new EndpointBuilder_1.default(this);
|
|
187
|
-
}
|
|
145
|
+
}
|
|
188
146
|
/**
|
|
189
147
|
* @deprecated use `requestBackend` instead
|
|
190
148
|
*/
|
|
191
|
-
|
|
149
|
+
getRequestBackend() {
|
|
192
150
|
return this.requestBackend;
|
|
193
|
-
}
|
|
151
|
+
}
|
|
194
152
|
/**
|
|
195
153
|
* @deprecated use `computeRequestConfig()` instead
|
|
196
154
|
*/
|
|
197
|
-
|
|
155
|
+
getConfig() {
|
|
198
156
|
return this.computeRequestConfig();
|
|
199
|
-
}
|
|
200
|
-
|
|
157
|
+
}
|
|
158
|
+
computeRequestConfig() {
|
|
201
159
|
return ((typeof this.defaultRequestConfig === "function" ? this.defaultRequestConfig() : this.defaultRequestConfig) || {});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
var _a;
|
|
160
|
+
}
|
|
161
|
+
resolveUrl(options) {
|
|
205
162
|
return (0, ApiUtils_1.resolveUrl)({
|
|
206
|
-
baseUrl:
|
|
163
|
+
baseUrl: options.baseUrl ?? this.baseUrl,
|
|
207
164
|
path: options.path,
|
|
208
165
|
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
}());
|
|
166
|
+
}
|
|
167
|
+
}
|
|
212
168
|
exports.Api = Api;
|
package/cjs/ApiTypes.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Api } from "./Api";
|
|
2
2
|
import type { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
3
4
|
import type RequestContext from "./RequestContext";
|
|
4
5
|
import type { Validation } from "./Validation";
|
|
5
|
-
import type RequestBackend from "./backend/RequestBackend";
|
|
6
6
|
export type AcceptableStatus = number | [min: number, max: number];
|
|
7
7
|
export type RawHeaders = Record<string, string | number | boolean | null | undefined>;
|
|
8
8
|
export type Params = string;
|
package/cjs/ApiUtils.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { ResponseType } from "./ApiConstants";
|
|
2
2
|
import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
3
3
|
export interface ResolveUrlOptions {
|
|
4
|
-
path: string;
|
|
4
|
+
path: string | URL;
|
|
5
5
|
baseUrl: string;
|
|
6
6
|
}
|
|
7
7
|
export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
|
|
8
8
|
export declare const isNetworkError: (error: Error) => boolean;
|
|
9
|
+
export declare const isAbsoluteUrl: (url: string) => boolean;
|
|
9
10
|
export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
|
|
10
11
|
export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
|
|
11
12
|
export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined, allowMissing?: boolean) => string;
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveUrl = exports.resolvePathParams = exports.inferResponseType = exports.isAcceptableStatus = exports.isNetworkError = exports.isCancelledError = void 0;
|
|
4
|
-
|
|
3
|
+
exports.resolveUrl = exports.resolvePathParams = exports.inferResponseType = exports.isAcceptableStatus = exports.isAbsoluteUrl = exports.isNetworkError = exports.isCancelledError = void 0;
|
|
4
|
+
const isCancelledError = (error) => {
|
|
5
5
|
return "isCancelledRequest" in error;
|
|
6
6
|
};
|
|
7
7
|
exports.isCancelledError = isCancelledError;
|
|
8
|
-
|
|
9
|
-
var _a;
|
|
8
|
+
const isNetworkError = (error) => {
|
|
10
9
|
return (error.name === "NetworkError" ||
|
|
11
10
|
error.message === "Network Error" ||
|
|
12
|
-
|
|
11
|
+
error.constructor?.name === "NetworkError");
|
|
13
12
|
};
|
|
14
13
|
exports.isNetworkError = isNetworkError;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
const isAbsoluteUrl = (url) => {
|
|
15
|
+
return /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
|
|
16
|
+
};
|
|
17
|
+
exports.isAbsoluteUrl = isAbsoluteUrl;
|
|
18
|
+
const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
|
|
19
|
+
const isAcceptableStatus = (status, acceptableStatus) => {
|
|
20
|
+
const acceptable = acceptableStatus ?? DEFAULT_ACCEPTABLE_STATUS;
|
|
21
|
+
for (const cmpStatus of acceptable) {
|
|
20
22
|
if (Array.isArray(cmpStatus)) {
|
|
21
|
-
|
|
23
|
+
const [min, max] = cmpStatus;
|
|
22
24
|
if (status >= min && status <= max) {
|
|
23
25
|
return true;
|
|
24
26
|
}
|
|
@@ -32,12 +34,12 @@ var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
32
34
|
return false;
|
|
33
35
|
};
|
|
34
36
|
exports.isAcceptableStatus = isAcceptableStatus;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
const TEXT_CONTENT_TYPES = ["text/plain", "text/html", "text/xml", "application/xml"];
|
|
38
|
+
const JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
39
|
+
const ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
|
|
40
|
+
const STREAM_CONTENT_TYPES = ["text/event-stream", "application/x-ndjson"];
|
|
41
|
+
const inferResponseType = (contentType) => {
|
|
42
|
+
const contentTypePart = contentType?.split(";")[0].trim();
|
|
41
43
|
if (contentTypePart) {
|
|
42
44
|
if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
|
|
43
45
|
return "text";
|
|
@@ -55,14 +57,14 @@ var inferResponseType = function (contentType) {
|
|
|
55
57
|
return "text";
|
|
56
58
|
};
|
|
57
59
|
exports.inferResponseType = inferResponseType;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
const resolvePathParams = (path, params, allowMissing) => {
|
|
61
|
+
const computedPath = path.startsWith("/") || (0, exports.isAbsoluteUrl)(path) ? path : `/${path}`;
|
|
60
62
|
if (params) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
const computedPathParts = computedPath.split("/");
|
|
64
|
+
const unusedKeys = new Set(Object.keys(params));
|
|
65
|
+
for (let i = 0; i < computedPathParts.length; i++) {
|
|
66
|
+
const part = computedPathParts[i];
|
|
67
|
+
let paramKey;
|
|
66
68
|
if (part.startsWith(":")) {
|
|
67
69
|
paramKey = part.substring(1);
|
|
68
70
|
}
|
|
@@ -70,36 +72,42 @@ var resolvePathParams = function (path, params, allowMissing) {
|
|
|
70
72
|
paramKey = part.substring(1, part.length - 1);
|
|
71
73
|
}
|
|
72
74
|
if (paramKey) {
|
|
73
|
-
|
|
75
|
+
const paramValue = params[paramKey];
|
|
74
76
|
if (!paramValue) {
|
|
75
77
|
if (allowMissing) {
|
|
76
78
|
continue;
|
|
77
79
|
}
|
|
78
|
-
throw new Error(
|
|
80
|
+
throw new Error(`[api-def] Missing param '${paramKey}'`);
|
|
79
81
|
}
|
|
80
82
|
computedPathParts[i] = paramValue;
|
|
81
83
|
unusedKeys.delete(paramKey);
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
if (unusedKeys.size > 0 && !allowMissing) {
|
|
85
|
-
throw new Error(
|
|
87
|
+
throw new Error(`[api-def] Missing param '${Array.from(unusedKeys)[0]}'`);
|
|
86
88
|
}
|
|
87
89
|
return computedPathParts.join("/");
|
|
88
90
|
}
|
|
89
91
|
return computedPath;
|
|
90
92
|
};
|
|
91
93
|
exports.resolvePathParams = resolvePathParams;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
const resolveUrl = (options) => {
|
|
95
|
+
const { baseUrl, path } = options;
|
|
96
|
+
if (path instanceof URL) {
|
|
97
|
+
return new URL(path.href);
|
|
98
|
+
}
|
|
99
|
+
if ((0, exports.isAbsoluteUrl)(path)) {
|
|
100
|
+
return new URL(path);
|
|
101
|
+
}
|
|
102
|
+
let result = !baseUrl.endsWith("/") ? `${baseUrl}/` : baseUrl;
|
|
95
103
|
result += path.startsWith("/") ? path.substring(1) : path;
|
|
96
|
-
|
|
104
|
+
let origin;
|
|
97
105
|
if (typeof window !== "undefined") {
|
|
98
106
|
origin = window.origin;
|
|
99
107
|
}
|
|
100
108
|
if (!origin) {
|
|
101
|
-
if (
|
|
102
|
-
result =
|
|
109
|
+
if (!(0, exports.isAbsoluteUrl)(result)) {
|
|
110
|
+
result = `https://${result}`;
|
|
103
111
|
}
|
|
104
112
|
}
|
|
105
113
|
return new URL(result, origin);
|
package/cjs/Endpoint.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Api } from "./Api";
|
|
2
2
|
import type { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
3
|
import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, RequestMiddleware, State } from "./ApiTypes";
|
|
4
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
4
5
|
import type * as Mocking from "./MockingTypes";
|
|
5
6
|
import type { Validation } from "./Validation";
|
|
6
|
-
import type RequestBackend from "./backend/RequestBackend";
|
|
7
7
|
export interface EndpointResolveUrlOptions<TParams extends Params | undefined, TQuery extends Query | undefined> {
|
|
8
8
|
baseUrl?: string;
|
|
9
9
|
params?: (TParams extends string | symbol | number ? Record<TParams, string> : never) | undefined;
|