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.
- package/README.md +3 -0
- package/bin/index.js +312 -313
- package/cjs/Api.d.ts +1 -1
- package/cjs/Api.js +99 -145
- package/cjs/ApiTypes.d.ts +3 -2
- package/cjs/ApiUtils.js +30 -32
- package/cjs/Endpoint.d.ts +1 -1
- package/cjs/Endpoint.js +112 -175
- package/cjs/EndpointBuilder.d.ts +17 -0
- package/cjs/EndpointBuilder.js +55 -36
- package/cjs/QueryHandling.js +12 -13
- package/cjs/RequestConfig.js +54 -36
- package/cjs/RequestContext.d.ts +2 -1
- package/cjs/RequestContext.js +143 -180
- package/cjs/RequestError.js +16 -19
- package/cjs/Requester.js +231 -312
- package/cjs/TextDecoding.js +31 -31
- package/cjs/Utils.d.ts +1 -0
- package/cjs/Utils.js +74 -25
- package/cjs/Validation.d.ts +8 -1
- 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 +6 -6
- package/esm/Api.js +12 -25
- package/esm/ApiConstants.d.ts +1 -1
- package/esm/ApiTypes.d.ts +7 -6
- package/esm/ApiUtils.d.ts +2 -2
- package/esm/ApiUtils.js +4 -5
- package/esm/Endpoint.d.ts +6 -6
- package/esm/Endpoint.js +22 -28
- package/esm/EndpointBuilder.d.ts +21 -4
- package/esm/EndpointBuilder.js +38 -10
- 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 +8 -7
- package/esm/RequestContext.js +33 -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 +45 -50
- package/esm/UtilTypes.d.ts +1 -1
- package/esm/Utils.d.ts +1 -0
- package/esm/Utils.js +54 -2
- package/esm/Validation.d.ts +8 -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 +50 -64
- 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 +67 -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;
|
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,22 +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 () {
|
|
106
|
-
return __generator(this, function (_a) {
|
|
107
|
-
switch (_a.label) {
|
|
108
|
-
case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path instanceof URL ? path.href : path, requestMethod), config, null)];
|
|
109
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}); };
|
|
113
|
-
};
|
|
97
|
+
this.hotRequest = (requestMethod) => async (path, config) => await Requester.submit(new HotRequestHost(this, path instanceof URL ? path.href : path, requestMethod), config, null);
|
|
114
98
|
this.get = this.hotRequest(ApiConstants_1.RequestMethod.GET);
|
|
115
99
|
this.post = this.hotRequest(ApiConstants_1.RequestMethod.POST);
|
|
116
100
|
this.put = this.hotRequest(ApiConstants_1.RequestMethod.PUT);
|
|
117
101
|
this.delete = this.hotRequest(ApiConstants_1.RequestMethod.DELETE);
|
|
118
102
|
this.patch = this.hotRequest(ApiConstants_1.RequestMethod.PATCH);
|
|
119
|
-
|
|
103
|
+
const requestBackend = options.requestBackend ?? (0, exports.getRequestBackend)();
|
|
120
104
|
if (!requestBackend) {
|
|
121
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`");
|
|
122
106
|
}
|
|
@@ -124,91 +108,61 @@ var Api = /** @class */ (function () {
|
|
|
124
108
|
name: options.name,
|
|
125
109
|
baseUrl: options.baseUrl,
|
|
126
110
|
middleware: options.middleware || [],
|
|
127
|
-
defaultRequestConfig:
|
|
128
|
-
mocking:
|
|
111
|
+
defaultRequestConfig: options.defaultRequestConfig ?? options.config ?? undefined,
|
|
112
|
+
mocking: options.mocking ?? undefined,
|
|
129
113
|
requestBackend: requestBackend,
|
|
130
114
|
};
|
|
131
115
|
//this.mutable = options.mutable ?? false;
|
|
132
116
|
//this.wasMutable = this.mutable;
|
|
133
117
|
this.endpoints = {};
|
|
134
118
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
get: function () {
|
|
161
|
-
return this.info.defaultRequestConfig;
|
|
162
|
-
},
|
|
163
|
-
enumerable: false,
|
|
164
|
-
configurable: true
|
|
165
|
-
});
|
|
166
|
-
Object.defineProperty(Api.prototype, "middleware", {
|
|
167
|
-
get: function () {
|
|
168
|
-
return this.info.middleware;
|
|
169
|
-
},
|
|
170
|
-
enumerable: false,
|
|
171
|
-
configurable: true
|
|
172
|
-
});
|
|
173
|
-
Object.defineProperty(Api.prototype, "mocking", {
|
|
174
|
-
get: function () {
|
|
175
|
-
return this.info.mocking;
|
|
176
|
-
},
|
|
177
|
-
enumerable: false,
|
|
178
|
-
configurable: true
|
|
179
|
-
});
|
|
180
|
-
Object.defineProperty(Api.prototype, "name", {
|
|
181
|
-
get: function () {
|
|
182
|
-
return this.info.name;
|
|
183
|
-
},
|
|
184
|
-
enumerable: false,
|
|
185
|
-
configurable: true
|
|
186
|
-
});
|
|
187
|
-
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() {
|
|
188
144
|
return new EndpointBuilder_1.default(this);
|
|
189
|
-
}
|
|
145
|
+
}
|
|
190
146
|
/**
|
|
191
147
|
* @deprecated use `requestBackend` instead
|
|
192
148
|
*/
|
|
193
|
-
|
|
149
|
+
getRequestBackend() {
|
|
194
150
|
return this.requestBackend;
|
|
195
|
-
}
|
|
151
|
+
}
|
|
196
152
|
/**
|
|
197
153
|
* @deprecated use `computeRequestConfig()` instead
|
|
198
154
|
*/
|
|
199
|
-
|
|
155
|
+
getConfig() {
|
|
200
156
|
return this.computeRequestConfig();
|
|
201
|
-
}
|
|
202
|
-
|
|
157
|
+
}
|
|
158
|
+
computeRequestConfig() {
|
|
203
159
|
return ((typeof this.defaultRequestConfig === "function" ? this.defaultRequestConfig() : this.defaultRequestConfig) || {});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
var _a;
|
|
160
|
+
}
|
|
161
|
+
resolveUrl(options) {
|
|
207
162
|
return (0, ApiUtils_1.resolveUrl)({
|
|
208
|
-
baseUrl:
|
|
163
|
+
baseUrl: options.baseUrl ?? this.baseUrl,
|
|
209
164
|
path: options.path,
|
|
210
165
|
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
}());
|
|
166
|
+
}
|
|
167
|
+
}
|
|
214
168
|
exports.Api = Api;
|
package/cjs/ApiTypes.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
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;
|
|
9
9
|
export type Query = string | undefined | Record<string, any>;
|
|
10
|
-
export type Body = string | number | Record<string, any
|
|
10
|
+
export type Body = string | number | Record<string, any> | FormData;
|
|
11
|
+
export type RequestBodyEncoding = "application/json" | "multipart/form-data" | "application/x-www-form-urlencoded";
|
|
11
12
|
export type State = Record<string, any>;
|
|
12
13
|
export interface ApiResponse<T = any> {
|
|
13
14
|
readonly method: RequestMethod;
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.resolveUrl = exports.resolvePathParams = exports.inferResponseType = exports.isAcceptableStatus = exports.isAbsoluteUrl = exports.isNetworkError = exports.isCancelledError = void 0;
|
|
4
|
-
|
|
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
|
-
|
|
14
|
+
const isAbsoluteUrl = (url) => {
|
|
16
15
|
return /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
|
|
17
16
|
};
|
|
18
17
|
exports.isAbsoluteUrl = isAbsoluteUrl;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
for (
|
|
23
|
-
var cmpStatus = acceptable_1[_i];
|
|
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) {
|
|
24
22
|
if (Array.isArray(cmpStatus)) {
|
|
25
|
-
|
|
23
|
+
const [min, max] = cmpStatus;
|
|
26
24
|
if (status >= min && status <= max) {
|
|
27
25
|
return true;
|
|
28
26
|
}
|
|
@@ -36,12 +34,12 @@ var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
36
34
|
return false;
|
|
37
35
|
};
|
|
38
36
|
exports.isAcceptableStatus = isAcceptableStatus;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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();
|
|
45
43
|
if (contentTypePart) {
|
|
46
44
|
if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
|
|
47
45
|
return "text";
|
|
@@ -59,14 +57,14 @@ var inferResponseType = function (contentType) {
|
|
|
59
57
|
return "text";
|
|
60
58
|
};
|
|
61
59
|
exports.inferResponseType = inferResponseType;
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const resolvePathParams = (path, params, allowMissing) => {
|
|
61
|
+
const computedPath = path.startsWith("/") || (0, exports.isAbsoluteUrl)(path) ? path : `/${path}`;
|
|
64
62
|
if (params) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
for (
|
|
68
|
-
|
|
69
|
-
|
|
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;
|
|
70
68
|
if (part.startsWith(":")) {
|
|
71
69
|
paramKey = part.substring(1);
|
|
72
70
|
}
|
|
@@ -74,42 +72,42 @@ var resolvePathParams = function (path, params, allowMissing) {
|
|
|
74
72
|
paramKey = part.substring(1, part.length - 1);
|
|
75
73
|
}
|
|
76
74
|
if (paramKey) {
|
|
77
|
-
|
|
75
|
+
const paramValue = params[paramKey];
|
|
78
76
|
if (!paramValue) {
|
|
79
77
|
if (allowMissing) {
|
|
80
78
|
continue;
|
|
81
79
|
}
|
|
82
|
-
throw new Error(
|
|
80
|
+
throw new Error(`[api-def] Missing param '${paramKey}'`);
|
|
83
81
|
}
|
|
84
82
|
computedPathParts[i] = paramValue;
|
|
85
83
|
unusedKeys.delete(paramKey);
|
|
86
84
|
}
|
|
87
85
|
}
|
|
88
86
|
if (unusedKeys.size > 0 && !allowMissing) {
|
|
89
|
-
throw new Error(
|
|
87
|
+
throw new Error(`[api-def] Missing param '${Array.from(unusedKeys)[0]}'`);
|
|
90
88
|
}
|
|
91
89
|
return computedPathParts.join("/");
|
|
92
90
|
}
|
|
93
91
|
return computedPath;
|
|
94
92
|
};
|
|
95
93
|
exports.resolvePathParams = resolvePathParams;
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
const resolveUrl = (options) => {
|
|
95
|
+
const { baseUrl, path } = options;
|
|
98
96
|
if (path instanceof URL) {
|
|
99
97
|
return new URL(path.href);
|
|
100
98
|
}
|
|
101
99
|
if ((0, exports.isAbsoluteUrl)(path)) {
|
|
102
100
|
return new URL(path);
|
|
103
101
|
}
|
|
104
|
-
|
|
102
|
+
let result = !baseUrl.endsWith("/") ? `${baseUrl}/` : baseUrl;
|
|
105
103
|
result += path.startsWith("/") ? path.substring(1) : path;
|
|
106
|
-
|
|
104
|
+
let origin;
|
|
107
105
|
if (typeof window !== "undefined") {
|
|
108
106
|
origin = window.origin;
|
|
109
107
|
}
|
|
110
108
|
if (!origin) {
|
|
111
109
|
if (!(0, exports.isAbsoluteUrl)(result)) {
|
|
112
|
-
result =
|
|
110
|
+
result = `https://${result}`;
|
|
113
111
|
}
|
|
114
112
|
}
|
|
115
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;
|