api-def 0.9.1 → 0.9.3

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 (122) hide show
  1. package/README.md +1 -1
  2. package/cjs/Api.d.ts +35 -35
  3. package/cjs/Api.js +140 -144
  4. package/cjs/ApiConstants.d.ts +58 -58
  5. package/cjs/ApiConstants.js +55 -55
  6. package/cjs/ApiTypes.d.ts +103 -103
  7. package/cjs/ApiTypes.js +4 -4
  8. package/cjs/ApiUtils.d.ts +6 -6
  9. package/cjs/ApiUtils.js +51 -53
  10. package/cjs/Endpoint.d.ts +52 -52
  11. package/cjs/Endpoint.js +104 -108
  12. package/cjs/EndpointBuilder.d.ts +14 -14
  13. package/cjs/EndpointBuilder.js +46 -46
  14. package/cjs/MockingTypes.d.ts +30 -30
  15. package/cjs/MockingTypes.js +2 -2
  16. package/cjs/QueryHandling.d.ts +3 -3
  17. package/cjs/QueryHandling.js +24 -24
  18. package/cjs/RequestConfig.d.ts +2 -2
  19. package/cjs/RequestConfig.js +74 -74
  20. package/cjs/RequestContext.d.ts +48 -48
  21. package/cjs/RequestContext.js +235 -238
  22. package/cjs/RequestError.d.ts +30 -30
  23. package/cjs/RequestError.js +47 -47
  24. package/cjs/Requester.d.ts +3 -3
  25. package/cjs/Requester.js +335 -336
  26. package/cjs/TextDecoding.d.ts +1 -1
  27. package/cjs/TextDecoding.js +152 -139
  28. package/cjs/UtilTypes.d.ts +5 -5
  29. package/cjs/UtilTypes.js +2 -2
  30. package/cjs/Utils.d.ts +15 -15
  31. package/cjs/Utils.js +79 -77
  32. package/cjs/Validation.d.ts +8 -8
  33. package/cjs/Validation.js +2 -2
  34. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  35. package/cjs/backend/AxiosRequestBackend.js +99 -99
  36. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  37. package/cjs/backend/FetchRequestBackend.js +203 -202
  38. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  39. package/cjs/backend/MockRequestBackend.js +164 -164
  40. package/cjs/backend/RequestBackend.d.ts +19 -19
  41. package/cjs/backend/RequestBackend.js +2 -2
  42. package/cjs/cache/CacheBackend.d.ts +6 -6
  43. package/cjs/cache/CacheBackend.js +2 -2
  44. package/cjs/cache/Caching.d.ts +10 -10
  45. package/cjs/cache/Caching.js +88 -88
  46. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  47. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  48. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  49. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  50. package/cjs/index.d.ts +13 -13
  51. package/cjs/index.js +41 -41
  52. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  53. package/cjs/middleware/CacheMiddleware.js +107 -107
  54. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  55. package/cjs/middleware/LoggingMiddleware.js +82 -88
  56. package/cjs/util/retry/index.d.ts +3 -3
  57. package/cjs/util/retry/index.js +52 -52
  58. package/cjs/util/retry/lib/retry.d.ts +3 -3
  59. package/cjs/util/retry/lib/retry.js +50 -54
  60. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  61. package/cjs/util/retry/lib/retryOperation.js +130 -134
  62. package/esm/Api.d.ts +35 -35
  63. package/esm/Api.js +92 -98
  64. package/esm/ApiConstants.d.ts +58 -58
  65. package/esm/ApiConstants.js +52 -52
  66. package/esm/ApiTypes.d.ts +103 -103
  67. package/esm/ApiTypes.js +1 -1
  68. package/esm/ApiUtils.d.ts +6 -6
  69. package/esm/ApiUtils.js +43 -45
  70. package/esm/Endpoint.d.ts +52 -52
  71. package/esm/Endpoint.js +66 -70
  72. package/esm/EndpointBuilder.d.ts +14 -14
  73. package/esm/EndpointBuilder.js +31 -31
  74. package/esm/MockingTypes.d.ts +30 -30
  75. package/esm/MockingTypes.js +1 -1
  76. package/esm/QueryHandling.d.ts +3 -3
  77. package/esm/QueryHandling.js +19 -19
  78. package/esm/RequestConfig.d.ts +2 -2
  79. package/esm/RequestConfig.js +56 -56
  80. package/esm/RequestContext.d.ts +48 -48
  81. package/esm/RequestContext.js +167 -170
  82. package/esm/RequestError.d.ts +30 -30
  83. package/esm/RequestError.js +42 -42
  84. package/esm/Requester.d.ts +3 -3
  85. package/esm/Requester.js +252 -253
  86. package/esm/TextDecoding.d.ts +1 -1
  87. package/esm/TextDecoding.js +148 -135
  88. package/esm/UtilTypes.d.ts +5 -5
  89. package/esm/UtilTypes.js +1 -1
  90. package/esm/Utils.d.ts +15 -15
  91. package/esm/Utils.js +66 -68
  92. package/esm/Validation.d.ts +8 -8
  93. package/esm/Validation.js +1 -1
  94. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  95. package/esm/backend/AxiosRequestBackend.js +62 -62
  96. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  97. package/esm/backend/FetchRequestBackend.js +136 -136
  98. package/esm/backend/MockRequestBackend.d.ts +11 -11
  99. package/esm/backend/MockRequestBackend.js +116 -116
  100. package/esm/backend/RequestBackend.d.ts +19 -19
  101. package/esm/backend/RequestBackend.js +1 -1
  102. package/esm/cache/CacheBackend.d.ts +6 -6
  103. package/esm/cache/CacheBackend.js +1 -1
  104. package/esm/cache/Caching.d.ts +10 -10
  105. package/esm/cache/Caching.js +39 -39
  106. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  107. package/esm/cache/LocalForageCacheBackend.js +20 -20
  108. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  109. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  110. package/esm/index.d.ts +13 -13
  111. package/esm/index.js +14 -15
  112. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  113. package/esm/middleware/CacheMiddleware.js +58 -58
  114. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  115. package/esm/middleware/LoggingMiddleware.js +77 -83
  116. package/esm/util/retry/index.d.ts +3 -3
  117. package/esm/util/retry/index.js +50 -50
  118. package/esm/util/retry/lib/retry.d.ts +3 -3
  119. package/esm/util/retry/lib/retry.js +35 -39
  120. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  121. package/esm/util/retry/lib/retryOperation.js +126 -132
  122. package/package.json +19 -34
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [api-def](https://github.com/Censkh/api-def/) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Censkh/api-def/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/api-def.svg?style=flat)](https://www.npmjs.com/package/api-def) [![build status](https://img.shields.io/github/workflow/status/censkh/api-def/Node.js%20CI)](https://github.com/Censkh/api-def/actions)
1
+ # [api-def](https://github.com/Censkh/api-def/) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Censkh/api-def/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/api-def.svg?style=flat)](https://www.npmjs.com/package/api-def) [![build status](https://img.shields.io/github/actions/workflow/status/censkh/api-def/workflow.yml)](https://github.com/Censkh/api-def/actions)
2
2
 
3
3
  Typed APIs with middleware support
4
4
 
package/cjs/Api.d.ts CHANGED
@@ -1,35 +1,35 @@
1
- import Endpoint from "./Endpoint";
2
- import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
3
- import RequestBackend from "./backend/RequestBackend";
4
- import EndpointBuilder from "./EndpointBuilder";
5
- import { ApiMockingConfig } from "./MockingTypes";
6
- export declare const getRequestBackend: () => RequestBackend | null;
7
- export declare const isRequestBackendDefault: () => boolean;
8
- export declare const setRequestBackend: (backend: RequestBackend) => void;
9
- export interface ApiInfo {
10
- readonly name: string;
11
- readonly baseUrl: string;
12
- readonly middleware?: RequestMiddleware[];
13
- readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
14
- readonly mocking?: ApiMockingConfig;
15
- readonly requestBackend?: RequestBackend;
16
- }
17
- export declare class Api implements ApiInfo {
18
- readonly baseUrl: string;
19
- readonly name: string;
20
- readonly middleware: RequestMiddleware[];
21
- readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
22
- readonly mocking?: ApiMockingConfig;
23
- readonly requestBackend: RequestBackend;
24
- protected readonly endpoints: Record<string, Endpoint>;
25
- constructor(info: ApiInfo);
26
- endpoint(): EndpointBuilder;
27
- getRequestBackend(): RequestBackend;
28
- getConfig(): BaseRequestConfig;
29
- private hotRequest;
30
- get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
31
- post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
32
- put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
33
- delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
34
- patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
35
- }
1
+ import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
2
+ import Endpoint from "./Endpoint";
3
+ import EndpointBuilder from "./EndpointBuilder";
4
+ import { ApiMockingConfig } from "./MockingTypes";
5
+ import RequestBackend from "./backend/RequestBackend";
6
+ export declare const getRequestBackend: () => RequestBackend | null;
7
+ export declare const isRequestBackendDefault: () => boolean;
8
+ export declare const setRequestBackend: (backend: RequestBackend) => void;
9
+ export interface ApiInfo {
10
+ readonly name: string;
11
+ readonly baseUrl: string;
12
+ readonly middleware?: RequestMiddleware[];
13
+ readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
14
+ readonly mocking?: ApiMockingConfig;
15
+ readonly requestBackend?: RequestBackend;
16
+ }
17
+ export declare class Api implements ApiInfo {
18
+ readonly baseUrl: string;
19
+ readonly name: string;
20
+ readonly middleware: RequestMiddleware[];
21
+ readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
22
+ readonly mocking?: ApiMockingConfig;
23
+ readonly requestBackend: RequestBackend;
24
+ protected readonly endpoints: Record<string, Endpoint>;
25
+ constructor(info: ApiInfo);
26
+ endpoint(): EndpointBuilder;
27
+ getRequestBackend(): RequestBackend;
28
+ getConfig(): BaseRequestConfig;
29
+ private hotRequest;
30
+ get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
31
+ post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
32
+ put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
33
+ delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
34
+ patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
35
+ }
package/cjs/Api.js CHANGED
@@ -1,144 +1,140 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
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;
13
- return g = { next: verb(0), "throw": verb(1), "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 (_) 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 };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.Api = exports.setRequestBackend = exports.isRequestBackendDefault = exports.getRequestBackend = void 0;
40
- var Requester = require("./Requester");
41
- var EndpointBuilder_1 = require("./EndpointBuilder");
42
- var Utils = require("./Utils");
43
- var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
44
- var ApiConstants_1 = require("./ApiConstants");
45
- var RequestConfig_1 = require("./RequestConfig");
46
- // use fetch as default if it is present
47
- var requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend_1.default() : null;
48
- var requestBackendIsDefault = true;
49
- var getRequestBackend = function () {
50
- return requestBackend;
51
- };
52
- exports.getRequestBackend = getRequestBackend;
53
- var isRequestBackendDefault = function () {
54
- return requestBackendIsDefault;
55
- };
56
- exports.isRequestBackendDefault = isRequestBackendDefault;
57
- var setRequestBackend = function (backend) {
58
- requestBackendIsDefault = false;
59
- requestBackend = backend;
60
- };
61
- exports.setRequestBackend = setRequestBackend;
62
- var HotRequestHost = /** @class */ (function () {
63
- function HotRequestHost(api, path, method) {
64
- this.responseType = undefined;
65
- this.validation = {};
66
- this.api = api;
67
- this.method = method;
68
- this.path = path;
69
- }
70
- Object.defineProperty(HotRequestHost.prototype, "baseUrl", {
71
- get: function () {
72
- return this.api.baseUrl;
73
- },
74
- enumerable: false,
75
- configurable: true
76
- });
77
- HotRequestHost.prototype.computeConfig = function (config) {
78
- var apiDefaults = this.api.getConfig();
79
- return (0, RequestConfig_1.computeRequestConfig)([
80
- apiDefaults,
81
- config,
82
- ]);
83
- };
84
- HotRequestHost.prototype.computePath = function (path, config) {
85
- return path.startsWith("/") ? path : "/".concat(path);
86
- };
87
- HotRequestHost.prototype.getRequestBackend = function () {
88
- return this.api.getRequestBackend();
89
- };
90
- return HotRequestHost;
91
- }());
92
- var defaultBackendMessageShown = false;
93
- var Api = /** @class */ (function () {
94
- function Api(info) {
95
- var _this = this;
96
- var _a, _b;
97
- this.endpoints = {};
98
- this.hotRequest = function (requestMethod) { return function (path, config) { return __awaiter(_this, void 0, void 0, function () {
99
- return __generator(this, function (_a) {
100
- switch (_a.label) {
101
- case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path, requestMethod), config, null)];
102
- case 1: return [2 /*return*/, (_a.sent())];
103
- }
104
- });
105
- }); }; };
106
- this.get = this.hotRequest(ApiConstants_1.RequestMethod.GET);
107
- this.post = this.hotRequest(ApiConstants_1.RequestMethod.POST);
108
- this.put = this.hotRequest(ApiConstants_1.RequestMethod.PUT);
109
- this.delete = this.hotRequest(ApiConstants_1.RequestMethod.DELETE);
110
- this.patch = this.hotRequest(ApiConstants_1.RequestMethod.PATCH);
111
- this.name = info.name;
112
- this.baseUrl = info.baseUrl;
113
- this.middleware = info.middleware || [];
114
- this.endpoints = {};
115
- this.config = info.config;
116
- this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
117
- var requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : (0, exports.getRequestBackend)();
118
- if (!requestBackend) {
119
- 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
- }
121
- this.requestBackend = requestBackend;
122
- if (!info.requestBackend) {
123
- if (process.env.NODE_ENV === "development") {
124
- if ((0, exports.isRequestBackendDefault)() && !defaultBackendMessageShown) {
125
- defaultBackendMessageShown = true;
126
- // eslint-disable-next-line
127
- console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
128
- }
129
- }
130
- }
131
- }
132
- Api.prototype.endpoint = function () {
133
- return new EndpointBuilder_1.default(this);
134
- };
135
- Api.prototype.getRequestBackend = function () {
136
- return this.requestBackend;
137
- };
138
- Api.prototype.getConfig = function () {
139
- return ((typeof this.config === "function" ? this.config() : this.config) ||
140
- {});
141
- };
142
- return Api;
143
- }());
144
- exports.Api = Api;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
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;
13
+ return g = { next: verb(0), "throw": verb(1), "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 };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Api = exports.setRequestBackend = exports.isRequestBackendDefault = exports.getRequestBackend = void 0;
40
+ var ApiConstants_1 = require("./ApiConstants");
41
+ var EndpointBuilder_1 = require("./EndpointBuilder");
42
+ var RequestConfig_1 = require("./RequestConfig");
43
+ var Requester = require("./Requester");
44
+ var Utils = require("./Utils");
45
+ var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
46
+ // use fetch as default if it is present
47
+ var requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend_1.default() : null;
48
+ var requestBackendIsDefault = true;
49
+ var getRequestBackend = function () {
50
+ return requestBackend;
51
+ };
52
+ exports.getRequestBackend = getRequestBackend;
53
+ var isRequestBackendDefault = function () {
54
+ return requestBackendIsDefault;
55
+ };
56
+ exports.isRequestBackendDefault = isRequestBackendDefault;
57
+ var setRequestBackend = function (backend) {
58
+ requestBackendIsDefault = false;
59
+ requestBackend = backend;
60
+ };
61
+ exports.setRequestBackend = setRequestBackend;
62
+ var HotRequestHost = /** @class */ (function () {
63
+ function HotRequestHost(api, path, method) {
64
+ this.responseType = undefined;
65
+ this.validation = {};
66
+ this.api = api;
67
+ this.method = method;
68
+ this.path = path;
69
+ }
70
+ Object.defineProperty(HotRequestHost.prototype, "baseUrl", {
71
+ get: function () {
72
+ return this.api.baseUrl;
73
+ },
74
+ enumerable: false,
75
+ configurable: true
76
+ });
77
+ HotRequestHost.prototype.computeConfig = function (config) {
78
+ var apiDefaults = this.api.getConfig();
79
+ return (0, RequestConfig_1.computeRequestConfig)([apiDefaults, config]);
80
+ };
81
+ HotRequestHost.prototype.computePath = function (path, config) {
82
+ return path.startsWith("/") ? path : "/".concat(path);
83
+ };
84
+ HotRequestHost.prototype.getRequestBackend = function () {
85
+ return this.api.getRequestBackend();
86
+ };
87
+ return HotRequestHost;
88
+ }());
89
+ var defaultBackendMessageShown = false;
90
+ var Api = /** @class */ (function () {
91
+ function Api(info) {
92
+ var _this = this;
93
+ var _a, _b;
94
+ this.endpoints = {};
95
+ this.hotRequest = function (requestMethod) {
96
+ return function (path, config) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path, requestMethod), config, null)];
99
+ case 1: return [2 /*return*/, _a.sent()];
100
+ }
101
+ }); }); };
102
+ };
103
+ this.get = this.hotRequest(ApiConstants_1.RequestMethod.GET);
104
+ this.post = this.hotRequest(ApiConstants_1.RequestMethod.POST);
105
+ this.put = this.hotRequest(ApiConstants_1.RequestMethod.PUT);
106
+ this.delete = this.hotRequest(ApiConstants_1.RequestMethod.DELETE);
107
+ this.patch = this.hotRequest(ApiConstants_1.RequestMethod.PATCH);
108
+ this.name = info.name;
109
+ this.baseUrl = info.baseUrl;
110
+ this.middleware = info.middleware || [];
111
+ this.endpoints = {};
112
+ this.config = info.config;
113
+ this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
114
+ var requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : (0, exports.getRequestBackend)();
115
+ if (!requestBackend) {
116
+ 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`");
117
+ }
118
+ this.requestBackend = requestBackend;
119
+ if (!info.requestBackend) {
120
+ if (process.env.NODE_ENV === "development") {
121
+ if ((0, exports.isRequestBackendDefault)() && !defaultBackendMessageShown) {
122
+ defaultBackendMessageShown = true;
123
+ // eslint-disable-next-line
124
+ console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
125
+ }
126
+ }
127
+ }
128
+ }
129
+ Api.prototype.endpoint = function () {
130
+ return new EndpointBuilder_1.default(this);
131
+ };
132
+ Api.prototype.getRequestBackend = function () {
133
+ return this.requestBackend;
134
+ };
135
+ Api.prototype.getConfig = function () {
136
+ return (typeof this.config === "function" ? this.config() : this.config) || {};
137
+ };
138
+ return Api;
139
+ }());
140
+ exports.Api = Api;
@@ -1,58 +1,58 @@
1
- import { EnumOf } from "./Utils";
2
- export declare const RequestMethod: {
3
- /** @deprecated use 'POST' */
4
- readonly Post: "post";
5
- /** @deprecated use 'GET' */
6
- readonly Get: "get";
7
- readonly POST: "post";
8
- readonly GET: "get";
9
- readonly PUT: "put";
10
- readonly DELETE: "delete";
11
- readonly PATCH: "patch";
12
- };
13
- export declare type RequestMethod = EnumOf<typeof RequestMethod>;
14
- export declare const RequestEvent: {
15
- /** @deprecated use 'BEFORE_SEND' */
16
- readonly BeforeSend: "beforeSend";
17
- /** @deprecated use 'SUCCESS' */
18
- readonly Success: "success";
19
- /** @deprecated use 'ERROR' */
20
- readonly Error: "error";
21
- /** @deprecated use 'UNRECOVERABLE_ERROR' */
22
- readonly UnrecoverableError: "unrecoverableError";
23
- readonly BEFORE_SEND: "beforeSend";
24
- readonly SUCCESS: "success";
25
- readonly ERROR: "error";
26
- readonly UNRECOVERABLE_ERROR: "unrecoverableError";
27
- };
28
- export declare type RequestEvent = EnumOf<typeof RequestEvent>;
29
- export declare const EventResultType: {
30
- /** @deprecated use 'RESPOND' */
31
- readonly Respond: "respond";
32
- /** @deprecated use 'RETRY' */
33
- readonly Retry: "retry";
34
- readonly RESPOND: "respond";
35
- readonly RETRY: "retry";
36
- };
37
- export declare type EventResultType = EnumOf<typeof EventResultType>;
38
- export declare const CacheSource: {
39
- /** @deprecated use 'API' */
40
- readonly Api: "api";
41
- /** @deprecated use 'LOCAL' */
42
- readonly Local: "local";
43
- readonly API: "api";
44
- readonly LOCAL: "local";
45
- };
46
- export declare type CacheSource = EnumOf<typeof CacheSource>;
47
- export declare const ResponseType: {
48
- /** @deprecated use 'JSON' */
49
- readonly Json: "json";
50
- /** @deprecated use 'TEXT' */
51
- readonly Text: "text";
52
- /** @deprecated use 'ARRAY_BUFFER' */
53
- readonly ArrayBuffer: "arraybuffer";
54
- readonly JSON: "json";
55
- readonly TEXT: "text";
56
- readonly ARRAY_BUFFER: "arraybuffer";
57
- };
58
- export declare type ResponseType = EnumOf<typeof ResponseType>;
1
+ import { EnumOf } from "./Utils";
2
+ export declare const RequestMethod: {
3
+ /** @deprecated use 'POST' */
4
+ readonly Post: "post";
5
+ /** @deprecated use 'GET' */
6
+ readonly Get: "get";
7
+ readonly POST: "post";
8
+ readonly GET: "get";
9
+ readonly PUT: "put";
10
+ readonly DELETE: "delete";
11
+ readonly PATCH: "patch";
12
+ };
13
+ export type RequestMethod = EnumOf<typeof RequestMethod>;
14
+ export declare const RequestEvent: {
15
+ /** @deprecated use 'BEFORE_SEND' */
16
+ readonly BeforeSend: "beforeSend";
17
+ /** @deprecated use 'SUCCESS' */
18
+ readonly Success: "success";
19
+ /** @deprecated use 'ERROR' */
20
+ readonly Error: "error";
21
+ /** @deprecated use 'UNRECOVERABLE_ERROR' */
22
+ readonly UnrecoverableError: "unrecoverableError";
23
+ readonly BEFORE_SEND: "beforeSend";
24
+ readonly SUCCESS: "success";
25
+ readonly ERROR: "error";
26
+ readonly UNRECOVERABLE_ERROR: "unrecoverableError";
27
+ };
28
+ export type RequestEvent = EnumOf<typeof RequestEvent>;
29
+ export declare const EventResultType: {
30
+ /** @deprecated use 'RESPOND' */
31
+ readonly Respond: "respond";
32
+ /** @deprecated use 'RETRY' */
33
+ readonly Retry: "retry";
34
+ readonly RESPOND: "respond";
35
+ readonly RETRY: "retry";
36
+ };
37
+ export type EventResultType = EnumOf<typeof EventResultType>;
38
+ export declare const CacheSource: {
39
+ /** @deprecated use 'API' */
40
+ readonly Api: "api";
41
+ /** @deprecated use 'LOCAL' */
42
+ readonly Local: "local";
43
+ readonly API: "api";
44
+ readonly LOCAL: "local";
45
+ };
46
+ export type CacheSource = EnumOf<typeof CacheSource>;
47
+ export declare const ResponseType: {
48
+ /** @deprecated use 'JSON' */
49
+ readonly Json: "json";
50
+ /** @deprecated use 'TEXT' */
51
+ readonly Text: "text";
52
+ /** @deprecated use 'ARRAY_BUFFER' */
53
+ readonly ArrayBuffer: "arraybuffer";
54
+ readonly JSON: "json";
55
+ readonly TEXT: "text";
56
+ readonly ARRAY_BUFFER: "arraybuffer";
57
+ };
58
+ export type ResponseType = EnumOf<typeof ResponseType>;
@@ -1,55 +1,55 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResponseType = exports.CacheSource = exports.EventResultType = exports.RequestEvent = exports.RequestMethod = void 0;
4
- exports.RequestMethod = {
5
- /** @deprecated use 'POST' */
6
- Post: "post",
7
- /** @deprecated use 'GET' */
8
- Get: "get",
9
- POST: "post",
10
- GET: "get",
11
- PUT: "put",
12
- DELETE: "delete",
13
- PATCH: "patch",
14
- };
15
- exports.RequestEvent = {
16
- /** @deprecated use 'BEFORE_SEND' */
17
- BeforeSend: "beforeSend",
18
- /** @deprecated use 'SUCCESS' */
19
- Success: "success",
20
- /** @deprecated use 'ERROR' */
21
- Error: "error",
22
- /** @deprecated use 'UNRECOVERABLE_ERROR' */
23
- UnrecoverableError: "unrecoverableError",
24
- BEFORE_SEND: "beforeSend",
25
- SUCCESS: "success",
26
- ERROR: "error",
27
- UNRECOVERABLE_ERROR: "unrecoverableError",
28
- };
29
- exports.EventResultType = {
30
- /** @deprecated use 'RESPOND' */
31
- Respond: "respond",
32
- /** @deprecated use 'RETRY' */
33
- Retry: "retry",
34
- RESPOND: "respond",
35
- RETRY: "retry",
36
- };
37
- exports.CacheSource = {
38
- /** @deprecated use 'API' */
39
- Api: "api",
40
- /** @deprecated use 'LOCAL' */
41
- Local: "local",
42
- API: "api",
43
- LOCAL: "local",
44
- };
45
- exports.ResponseType = {
46
- /** @deprecated use 'JSON' */
47
- Json: "json",
48
- /** @deprecated use 'TEXT' */
49
- Text: "text",
50
- /** @deprecated use 'ARRAY_BUFFER' */
51
- ArrayBuffer: "arraybuffer",
52
- JSON: "json",
53
- TEXT: "text",
54
- ARRAY_BUFFER: "arraybuffer",
55
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResponseType = exports.CacheSource = exports.EventResultType = exports.RequestEvent = exports.RequestMethod = void 0;
4
+ exports.RequestMethod = {
5
+ /** @deprecated use 'POST' */
6
+ Post: "post",
7
+ /** @deprecated use 'GET' */
8
+ Get: "get",
9
+ POST: "post",
10
+ GET: "get",
11
+ PUT: "put",
12
+ DELETE: "delete",
13
+ PATCH: "patch",
14
+ };
15
+ exports.RequestEvent = {
16
+ /** @deprecated use 'BEFORE_SEND' */
17
+ BeforeSend: "beforeSend",
18
+ /** @deprecated use 'SUCCESS' */
19
+ Success: "success",
20
+ /** @deprecated use 'ERROR' */
21
+ Error: "error",
22
+ /** @deprecated use 'UNRECOVERABLE_ERROR' */
23
+ UnrecoverableError: "unrecoverableError",
24
+ BEFORE_SEND: "beforeSend",
25
+ SUCCESS: "success",
26
+ ERROR: "error",
27
+ UNRECOVERABLE_ERROR: "unrecoverableError",
28
+ };
29
+ exports.EventResultType = {
30
+ /** @deprecated use 'RESPOND' */
31
+ Respond: "respond",
32
+ /** @deprecated use 'RETRY' */
33
+ Retry: "retry",
34
+ RESPOND: "respond",
35
+ RETRY: "retry",
36
+ };
37
+ exports.CacheSource = {
38
+ /** @deprecated use 'API' */
39
+ Api: "api",
40
+ /** @deprecated use 'LOCAL' */
41
+ Local: "local",
42
+ API: "api",
43
+ LOCAL: "local",
44
+ };
45
+ exports.ResponseType = {
46
+ /** @deprecated use 'JSON' */
47
+ Json: "json",
48
+ /** @deprecated use 'TEXT' */
49
+ Text: "text",
50
+ /** @deprecated use 'ARRAY_BUFFER' */
51
+ ArrayBuffer: "arraybuffer",
52
+ JSON: "json",
53
+ TEXT: "text",
54
+ ARRAY_BUFFER: "arraybuffer",
55
+ };