api-def 0.8.0 → 0.8.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.
- package/cjs/Api.d.ts +3 -0
- package/cjs/Api.js +22 -1
- package/cjs/ApiTypes.d.ts +2 -0
- package/cjs/Endpoint.d.ts +2 -0
- package/cjs/Endpoint.js +3 -8
- package/cjs/Requester.js +2 -15
- package/cjs/Utils.d.ts +1 -0
- package/cjs/Utils.js +15 -7
- package/cjs/backend/FetchRequestBackend.js +4 -0
- package/esm/Api.d.ts +3 -0
- package/esm/Api.js +22 -1
- package/esm/ApiTypes.d.ts +2 -0
- package/esm/Endpoint.d.ts +2 -0
- package/esm/Endpoint.js +3 -7
- package/esm/Requester.js +1 -13
- package/esm/Utils.d.ts +1 -0
- package/esm/Utils.js +13 -6
- package/esm/backend/FetchRequestBackend.js +5 -1
- package/package.json +1 -1
package/cjs/Api.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface ApiInfo {
|
|
|
12
12
|
readonly middleware?: RequestMiddleware[];
|
|
13
13
|
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
14
14
|
readonly mocking?: ApiMockingConfig;
|
|
15
|
+
readonly requestBackend?: RequestBackend;
|
|
15
16
|
}
|
|
16
17
|
export declare class Api implements ApiInfo {
|
|
17
18
|
readonly baseUrl: string;
|
|
@@ -19,9 +20,11 @@ export declare class Api implements ApiInfo {
|
|
|
19
20
|
readonly middleware: RequestMiddleware[];
|
|
20
21
|
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
21
22
|
readonly mocking?: ApiMockingConfig;
|
|
23
|
+
readonly requestBackend: RequestBackend;
|
|
22
24
|
protected readonly endpoints: Record<string, Endpoint>;
|
|
23
25
|
constructor(info: ApiInfo);
|
|
24
26
|
endpoint(): EndpointBuilder;
|
|
27
|
+
getRequestBackend(): RequestBackend;
|
|
25
28
|
getConfig(): BaseRequestConfig;
|
|
26
29
|
private hotRequest;
|
|
27
30
|
get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
package/cjs/Api.js
CHANGED
|
@@ -83,12 +83,16 @@ var HotRequestHost = /** @class */ (function () {
|
|
|
83
83
|
HotRequestHost.prototype.computePath = function (path, config) {
|
|
84
84
|
return path.startsWith("/") ? path : "/".concat(path);
|
|
85
85
|
};
|
|
86
|
+
HotRequestHost.prototype.getRequestBackend = function () {
|
|
87
|
+
return this.api.getRequestBackend();
|
|
88
|
+
};
|
|
86
89
|
return HotRequestHost;
|
|
87
90
|
}());
|
|
91
|
+
var defaultBackendMessageShown = false;
|
|
88
92
|
var Api = /** @class */ (function () {
|
|
89
93
|
function Api(info) {
|
|
90
94
|
var _this = this;
|
|
91
|
-
var _a;
|
|
95
|
+
var _a, _b;
|
|
92
96
|
this.endpoints = {};
|
|
93
97
|
this.hotRequest = function (requestMethod) { return function (path, config) { return __awaiter(_this, void 0, void 0, function () {
|
|
94
98
|
return __generator(this, function (_a) {
|
|
@@ -109,10 +113,27 @@ var Api = /** @class */ (function () {
|
|
|
109
113
|
this.endpoints = {};
|
|
110
114
|
this.config = info.config;
|
|
111
115
|
this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
|
|
116
|
+
var requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : (0, exports.getRequestBackend)();
|
|
117
|
+
if (!requestBackend) {
|
|
118
|
+
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`");
|
|
119
|
+
}
|
|
120
|
+
this.requestBackend = requestBackend;
|
|
121
|
+
if (!info.requestBackend) {
|
|
122
|
+
if (process.env.NODE_ENV === "development") {
|
|
123
|
+
if ((0, exports.isRequestBackendDefault)() && !defaultBackendMessageShown) {
|
|
124
|
+
defaultBackendMessageShown = true;
|
|
125
|
+
// eslint-disable-next-line
|
|
126
|
+
console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
112
130
|
}
|
|
113
131
|
Api.prototype.endpoint = function () {
|
|
114
132
|
return new EndpointBuilder_1.default(this);
|
|
115
133
|
};
|
|
134
|
+
Api.prototype.getRequestBackend = function () {
|
|
135
|
+
return this.requestBackend;
|
|
136
|
+
};
|
|
116
137
|
Api.prototype.getConfig = function () {
|
|
117
138
|
return ((typeof this.config === "function" ? this.config() : this.config) ||
|
|
118
139
|
{});
|
package/cjs/ApiTypes.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import RequestContext from "./RequestContext";
|
|
2
2
|
import { Api } from "./Api";
|
|
3
3
|
import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
4
|
+
import RequestBackend from "./backend/RequestBackend";
|
|
4
5
|
export declare type AcceptableStatus = number | [min: number, max: number];
|
|
5
6
|
export declare type Headers = Record<string, string | number | boolean | null | undefined>;
|
|
6
7
|
export declare type Params = string;
|
|
@@ -82,6 +83,7 @@ export interface RequestHost {
|
|
|
82
83
|
readonly responseType: ResponseType | undefined;
|
|
83
84
|
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
84
85
|
computePath(path: string, config: RequestConfig): string;
|
|
86
|
+
getRequestBackend(): RequestBackend;
|
|
85
87
|
}
|
|
86
88
|
export interface CancelledRequestError extends Error {
|
|
87
89
|
isCancelledRequest: true;
|
package/cjs/Endpoint.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Api } from "./Api";
|
|
|
2
2
|
import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
3
3
|
import * as Mocking from "./MockingTypes";
|
|
4
4
|
import { RequestMethod, ResponseType } from "./ApiConstants";
|
|
5
|
+
import RequestBackend from "./backend/RequestBackend";
|
|
5
6
|
export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined> {
|
|
6
7
|
readonly id: string;
|
|
7
8
|
readonly method: RequestMethod;
|
|
@@ -44,4 +45,5 @@ export default class Endpoint<R = any, P extends Params | undefined = Params | u
|
|
|
44
45
|
computePath(path: string, request: RequestConfig): string;
|
|
45
46
|
get baseUrl(): string;
|
|
46
47
|
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
48
|
+
getRequestBackend(): RequestBackend;
|
|
47
49
|
}
|
package/cjs/Endpoint.js
CHANGED
|
@@ -37,8 +37,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
var Requester = require("./Requester");
|
|
40
|
-
var ApiTypes_1 = require("./ApiTypes");
|
|
41
|
-
var Utils = require("./Utils");
|
|
42
40
|
var RequestConfig_1 = require("./RequestConfig");
|
|
43
41
|
var Endpoint = /** @class */ (function () {
|
|
44
42
|
function Endpoint(api, info) {
|
|
@@ -94,18 +92,15 @@ var Endpoint = /** @class */ (function () {
|
|
|
94
92
|
configurable: true
|
|
95
93
|
});
|
|
96
94
|
Endpoint.prototype.computeConfig = function (config) {
|
|
97
|
-
var _a;
|
|
98
95
|
var apiDefaults = this.api.getConfig();
|
|
99
|
-
var computedConfig = Utils.assign((_a = {},
|
|
100
|
-
_a[ApiTypes_1.COMPUTED_CONFIG_SYMBOL] = true,
|
|
101
|
-
_a), apiDefaults, this.config, config);
|
|
102
96
|
return (0, RequestConfig_1.computeRequestConfig)([
|
|
103
97
|
apiDefaults,
|
|
104
98
|
this.config,
|
|
105
99
|
config,
|
|
106
100
|
]);
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
};
|
|
102
|
+
Endpoint.prototype.getRequestBackend = function () {
|
|
103
|
+
return this.api.getRequestBackend();
|
|
109
104
|
};
|
|
110
105
|
return Endpoint;
|
|
111
106
|
}());
|
package/cjs/Requester.js
CHANGED
|
@@ -51,7 +51,6 @@ exports.submit = void 0;
|
|
|
51
51
|
var ApiUtils = require("./ApiUtils");
|
|
52
52
|
var ApiUtils_1 = require("./ApiUtils");
|
|
53
53
|
var RequestContext_1 = require("./RequestContext");
|
|
54
|
-
var Api = require("./Api");
|
|
55
54
|
var ApiConstants_1 = require("./ApiConstants");
|
|
56
55
|
var retry_1 = require("./util/retry");
|
|
57
56
|
var MockRequestBackend_1 = require("./backend/MockRequestBackend");
|
|
@@ -66,10 +65,7 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
66
65
|
switch (_a.label) {
|
|
67
66
|
case 0:
|
|
68
67
|
computedConfig = host.computeConfig(config);
|
|
69
|
-
backend = mocking ? MOCK_REQUEST_BACKEND :
|
|
70
|
-
if (!backend) {
|
|
71
|
-
throw new Error("[api-def] Please specify a backend you wish to use, this can be done either with 'setRequestBackend()'");
|
|
72
|
-
}
|
|
68
|
+
backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
73
69
|
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
74
70
|
key = context.key;
|
|
75
71
|
lock = (context.computedConfig || {}).lock;
|
|
@@ -109,21 +105,12 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
109
105
|
});
|
|
110
106
|
}); };
|
|
111
107
|
exports.submit = submit;
|
|
112
|
-
var defaultBackendMessageShown = false;
|
|
113
108
|
var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0, function () {
|
|
114
109
|
var beforeSendEventResult, maxRetries, retryOpts, performRequest, response;
|
|
115
110
|
var _a;
|
|
116
111
|
return __generator(this, function (_b) {
|
|
117
112
|
switch (_b.label) {
|
|
118
|
-
case 0:
|
|
119
|
-
if (process.env.NODE_ENV === "development") {
|
|
120
|
-
if (Api.isRequestBackendDefault() && !defaultBackendMessageShown) {
|
|
121
|
-
defaultBackendMessageShown = true;
|
|
122
|
-
// eslint-disable-next-line
|
|
123
|
-
console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.BeforeSend)];
|
|
113
|
+
case 0: return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.BeforeSend)];
|
|
127
114
|
case 1:
|
|
128
115
|
beforeSendEventResult = _b.sent();
|
|
129
116
|
if (beforeSendEventResult &&
|
package/cjs/Utils.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export declare const assign: typeof Object["assign"];
|
|
|
2
2
|
export declare const padNumber: (stringOrNumber: string | number, maxLength: number) => string;
|
|
3
3
|
export declare type EnumOf<T extends Record<string, any>> = T[keyof T];
|
|
4
4
|
export declare type Fetch = typeof window.fetch;
|
|
5
|
+
export declare const getGlobal: () => any;
|
|
5
6
|
export declare const getGlobalFetch: () => Fetch | undefined;
|
|
6
7
|
export declare const noop: () => void;
|
|
7
8
|
/**
|
package/cjs/Utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.randInt = exports.delayThenReturn = exports.noop = exports.getGlobalFetch = exports.padNumber = exports.assign = void 0;
|
|
3
|
+
exports.randInt = exports.delayThenReturn = exports.noop = exports.getGlobalFetch = exports.getGlobal = exports.padNumber = exports.assign = void 0;
|
|
4
4
|
// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
5
5
|
exports.assign = Object.assign || function (target, varArgs) {
|
|
6
6
|
if (target === null || target === undefined) {
|
|
@@ -28,14 +28,22 @@ var padNumber = function (stringOrNumber, maxLength) {
|
|
|
28
28
|
: "0".repeat(maxLength - string.length) + string;
|
|
29
29
|
};
|
|
30
30
|
exports.padNumber = padNumber;
|
|
31
|
-
var
|
|
32
|
-
if (typeof global !== "undefined"
|
|
33
|
-
return global
|
|
31
|
+
var getGlobal = function () {
|
|
32
|
+
if (typeof global !== "undefined") {
|
|
33
|
+
return global;
|
|
34
|
+
}
|
|
35
|
+
if (typeof window !== "undefined") {
|
|
36
|
+
return window;
|
|
34
37
|
}
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
return undefined;
|
|
39
|
+
};
|
|
40
|
+
exports.getGlobal = getGlobal;
|
|
41
|
+
var getGlobalFetch = function () {
|
|
42
|
+
var global = (0, exports.getGlobal)();
|
|
43
|
+
if (global && typeof global.fetch === "function") {
|
|
44
|
+
return global.fetch.bind(global);
|
|
37
45
|
}
|
|
38
|
-
return
|
|
46
|
+
return undefined;
|
|
39
47
|
};
|
|
40
48
|
exports.getGlobalFetch = getGlobalFetch;
|
|
41
49
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
@@ -67,6 +67,10 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
67
67
|
this.id = "fetch";
|
|
68
68
|
if (fetchLibrary !== undefined) {
|
|
69
69
|
this.fetch = fetchLibrary;
|
|
70
|
+
// otherwise window throws illegal invocation
|
|
71
|
+
if (fetchLibrary === (0, Utils_1.getGlobalFetch)()) {
|
|
72
|
+
this.fetch = fetchLibrary.bind((0, Utils_1.getGlobal)());
|
|
73
|
+
}
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
76
|
FetchRequestBackend.prototype.extractResponseFromError = function (error) {
|
package/esm/Api.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export interface ApiInfo {
|
|
|
12
12
|
readonly middleware?: RequestMiddleware[];
|
|
13
13
|
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
14
14
|
readonly mocking?: ApiMockingConfig;
|
|
15
|
+
readonly requestBackend?: RequestBackend;
|
|
15
16
|
}
|
|
16
17
|
export declare class Api implements ApiInfo {
|
|
17
18
|
readonly baseUrl: string;
|
|
@@ -19,9 +20,11 @@ export declare class Api implements ApiInfo {
|
|
|
19
20
|
readonly middleware: RequestMiddleware[];
|
|
20
21
|
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
21
22
|
readonly mocking?: ApiMockingConfig;
|
|
23
|
+
readonly requestBackend: RequestBackend;
|
|
22
24
|
protected readonly endpoints: Record<string, Endpoint>;
|
|
23
25
|
constructor(info: ApiInfo);
|
|
24
26
|
endpoint(): EndpointBuilder;
|
|
27
|
+
getRequestBackend(): RequestBackend;
|
|
25
28
|
getConfig(): BaseRequestConfig;
|
|
26
29
|
private hotRequest;
|
|
27
30
|
get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
package/esm/Api.js
CHANGED
|
@@ -46,10 +46,14 @@ class HotRequestHost {
|
|
|
46
46
|
computePath(path, config) {
|
|
47
47
|
return path.startsWith("/") ? path : `/${path}`;
|
|
48
48
|
}
|
|
49
|
+
getRequestBackend() {
|
|
50
|
+
return this.api.getRequestBackend();
|
|
51
|
+
}
|
|
49
52
|
}
|
|
53
|
+
let defaultBackendMessageShown = false;
|
|
50
54
|
export class Api {
|
|
51
55
|
constructor(info) {
|
|
52
|
-
var _a;
|
|
56
|
+
var _a, _b;
|
|
53
57
|
this.endpoints = {};
|
|
54
58
|
this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () {
|
|
55
59
|
return (yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null));
|
|
@@ -65,10 +69,27 @@ export class Api {
|
|
|
65
69
|
this.endpoints = {};
|
|
66
70
|
this.config = info.config;
|
|
67
71
|
this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
|
|
72
|
+
const requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : getRequestBackend();
|
|
73
|
+
if (!requestBackend) {
|
|
74
|
+
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`");
|
|
75
|
+
}
|
|
76
|
+
this.requestBackend = requestBackend;
|
|
77
|
+
if (!info.requestBackend) {
|
|
78
|
+
if (process.env.NODE_ENV === "development") {
|
|
79
|
+
if (isRequestBackendDefault() && !defaultBackendMessageShown) {
|
|
80
|
+
defaultBackendMessageShown = true;
|
|
81
|
+
// eslint-disable-next-line
|
|
82
|
+
console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
68
86
|
}
|
|
69
87
|
endpoint() {
|
|
70
88
|
return new EndpointBuilder(this);
|
|
71
89
|
}
|
|
90
|
+
getRequestBackend() {
|
|
91
|
+
return this.requestBackend;
|
|
92
|
+
}
|
|
72
93
|
getConfig() {
|
|
73
94
|
return ((typeof this.config === "function" ? this.config() : this.config) ||
|
|
74
95
|
{});
|
package/esm/ApiTypes.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import RequestContext from "./RequestContext";
|
|
2
2
|
import { Api } from "./Api";
|
|
3
3
|
import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
4
|
+
import RequestBackend from "./backend/RequestBackend";
|
|
4
5
|
export declare type AcceptableStatus = number | [min: number, max: number];
|
|
5
6
|
export declare type Headers = Record<string, string | number | boolean | null | undefined>;
|
|
6
7
|
export declare type Params = string;
|
|
@@ -82,6 +83,7 @@ export interface RequestHost {
|
|
|
82
83
|
readonly responseType: ResponseType | undefined;
|
|
83
84
|
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
84
85
|
computePath(path: string, config: RequestConfig): string;
|
|
86
|
+
getRequestBackend(): RequestBackend;
|
|
85
87
|
}
|
|
86
88
|
export interface CancelledRequestError extends Error {
|
|
87
89
|
isCancelledRequest: true;
|
package/esm/Endpoint.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Api } from "./Api";
|
|
|
2
2
|
import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
3
3
|
import * as Mocking from "./MockingTypes";
|
|
4
4
|
import { RequestMethod, ResponseType } from "./ApiConstants";
|
|
5
|
+
import RequestBackend from "./backend/RequestBackend";
|
|
5
6
|
export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined> {
|
|
6
7
|
readonly id: string;
|
|
7
8
|
readonly method: RequestMethod;
|
|
@@ -44,4 +45,5 @@ export default class Endpoint<R = any, P extends Params | undefined = Params | u
|
|
|
44
45
|
computePath(path: string, request: RequestConfig): string;
|
|
45
46
|
get baseUrl(): string;
|
|
46
47
|
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
48
|
+
getRequestBackend(): RequestBackend;
|
|
47
49
|
}
|
package/esm/Endpoint.js
CHANGED
|
@@ -8,8 +8,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import * as Requester from "./Requester";
|
|
11
|
-
import { COMPUTED_CONFIG_SYMBOL, } from "./ApiTypes";
|
|
12
|
-
import * as Utils from "./Utils";
|
|
13
11
|
import { computeRequestConfig } from "./RequestConfig";
|
|
14
12
|
export default class Endpoint {
|
|
15
13
|
constructor(api, info) {
|
|
@@ -59,15 +57,13 @@ export default class Endpoint {
|
|
|
59
57
|
}
|
|
60
58
|
computeConfig(config) {
|
|
61
59
|
const apiDefaults = this.api.getConfig();
|
|
62
|
-
const computedConfig = Utils.assign({
|
|
63
|
-
[COMPUTED_CONFIG_SYMBOL]: true,
|
|
64
|
-
}, apiDefaults, this.config, config);
|
|
65
60
|
return computeRequestConfig([
|
|
66
61
|
apiDefaults,
|
|
67
62
|
this.config,
|
|
68
63
|
config,
|
|
69
64
|
]);
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
}
|
|
66
|
+
getRequestBackend() {
|
|
67
|
+
return this.api.getRequestBackend();
|
|
72
68
|
}
|
|
73
69
|
}
|
package/esm/Requester.js
CHANGED
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import * as ApiUtils from "./ApiUtils";
|
|
11
11
|
import { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
|
|
12
12
|
import RequestContext from "./RequestContext";
|
|
13
|
-
import * as Api from "./Api";
|
|
14
13
|
import { EventResultType, RequestEvent } from "./ApiConstants";
|
|
15
14
|
import retry from "./util/retry";
|
|
16
15
|
import MockRequestBackend from "./backend/MockRequestBackend";
|
|
@@ -21,10 +20,7 @@ const runningOperations = {};
|
|
|
21
20
|
const MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
22
21
|
export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
22
|
const computedConfig = host.computeConfig(config);
|
|
24
|
-
const backend = mocking ? MOCK_REQUEST_BACKEND :
|
|
25
|
-
if (!backend) {
|
|
26
|
-
throw new Error("[api-def] Please specify a backend you wish to use, this can be done either with 'setRequestBackend()'");
|
|
27
|
-
}
|
|
23
|
+
const backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
28
24
|
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
29
25
|
const { key } = context;
|
|
30
26
|
// don't do this -- should only be for GET requests anyway and should be opt-in
|
|
@@ -63,16 +59,8 @@ export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void
|
|
|
63
59
|
}
|
|
64
60
|
}
|
|
65
61
|
});
|
|
66
|
-
let defaultBackendMessageShown = false;
|
|
67
62
|
const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
63
|
var _a;
|
|
69
|
-
if (process.env.NODE_ENV === "development") {
|
|
70
|
-
if (Api.isRequestBackendDefault() && !defaultBackendMessageShown) {
|
|
71
|
-
defaultBackendMessageShown = true;
|
|
72
|
-
// eslint-disable-next-line
|
|
73
|
-
console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
64
|
const beforeSendEventResult = yield context.triggerEvent(RequestEvent.BeforeSend);
|
|
77
65
|
if (beforeSendEventResult &&
|
|
78
66
|
beforeSendEventResult.type === EventResultType.Respond) {
|
package/esm/Utils.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export declare const assign: typeof Object["assign"];
|
|
|
2
2
|
export declare const padNumber: (stringOrNumber: string | number, maxLength: number) => string;
|
|
3
3
|
export declare type EnumOf<T extends Record<string, any>> = T[keyof T];
|
|
4
4
|
export declare type Fetch = typeof window.fetch;
|
|
5
|
+
export declare const getGlobal: () => any;
|
|
5
6
|
export declare const getGlobalFetch: () => Fetch | undefined;
|
|
6
7
|
export declare const noop: () => void;
|
|
7
8
|
/**
|
package/esm/Utils.js
CHANGED
|
@@ -24,14 +24,21 @@ export const padNumber = (stringOrNumber, maxLength) => {
|
|
|
24
24
|
? string
|
|
25
25
|
: "0".repeat(maxLength - string.length) + string;
|
|
26
26
|
};
|
|
27
|
-
export const
|
|
28
|
-
if (typeof global !== "undefined"
|
|
29
|
-
return global
|
|
27
|
+
export const getGlobal = () => {
|
|
28
|
+
if (typeof global !== "undefined") {
|
|
29
|
+
return global;
|
|
30
|
+
}
|
|
31
|
+
if (typeof window !== "undefined") {
|
|
32
|
+
return window;
|
|
30
33
|
}
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
return undefined;
|
|
35
|
+
};
|
|
36
|
+
export const getGlobalFetch = () => {
|
|
37
|
+
const global = getGlobal();
|
|
38
|
+
if (global && typeof global.fetch === "function") {
|
|
39
|
+
return global.fetch.bind(global);
|
|
33
40
|
}
|
|
34
|
-
return
|
|
41
|
+
return undefined;
|
|
35
42
|
};
|
|
36
43
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
37
44
|
export const noop = () => {
|
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import * as Utils from "../Utils";
|
|
11
|
-
import { getGlobalFetch } from "../Utils";
|
|
11
|
+
import { getGlobal, getGlobalFetch } from "../Utils";
|
|
12
12
|
import { inferResponseType } from "../ApiUtils";
|
|
13
13
|
class FetchError extends Error {
|
|
14
14
|
}
|
|
@@ -18,6 +18,10 @@ export default class FetchRequestBackend {
|
|
|
18
18
|
this.id = "fetch";
|
|
19
19
|
if (fetchLibrary !== undefined) {
|
|
20
20
|
this.fetch = fetchLibrary;
|
|
21
|
+
// otherwise window throws illegal invocation
|
|
22
|
+
if (fetchLibrary === getGlobalFetch()) {
|
|
23
|
+
this.fetch = fetchLibrary.bind(getGlobal());
|
|
24
|
+
}
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
27
|
extractResponseFromError(error) {
|