@koine/api 1.0.16 → 1.0.20
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/core/index.js +27 -29
- package/node/core/index.js +27 -29
- package/package.json +2 -2
- package/typings.d.ts +109 -1
package/core/index.js
CHANGED
|
@@ -25,23 +25,21 @@ export { ApiError };
|
|
|
25
25
|
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
26
26
|
*/
|
|
27
27
|
export var createApi = function (apiName, baseUrl, options) {
|
|
28
|
-
var _a = options || {}, adapterBase = _a.adapter,
|
|
28
|
+
var _a = options || {}, adapterBase = _a.adapter, _b = _a.request, requestBase = _b === void 0 ? {
|
|
29
|
+
// mode: "cors",
|
|
30
|
+
// redirect: "follow",
|
|
31
|
+
credentials: "include",
|
|
32
|
+
// cache: "no-cache",
|
|
33
|
+
referrerPolicy: "no-referrer",
|
|
34
|
+
} : _b, shouldThrowBase = _a.shouldThrow;
|
|
29
35
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
30
36
|
api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
31
|
-
var _a, json, params, _b,
|
|
32
|
-
return __generator(this, function (
|
|
33
|
-
switch (
|
|
37
|
+
var _a, json, params, _b, request, _c, headers, _d, timeout, _e, adapter, _f, shouldThrow, requestInit, timeoutNumber, controller, timeoutId, url, response, e_1, result, e_2, pre;
|
|
38
|
+
return __generator(this, function (_g) {
|
|
39
|
+
switch (_g.label) {
|
|
34
40
|
case 0:
|
|
35
|
-
_a = options || {}, json = _a.json, params = _a.params, _b = _a.headers, headers =
|
|
36
|
-
requestInit = {
|
|
37
|
-
method: method.toUpperCase(),
|
|
38
|
-
// mode: "cors",
|
|
39
|
-
// redirect: "follow",
|
|
40
|
-
credentials: "include",
|
|
41
|
-
// cache: "no-cache",
|
|
42
|
-
referrerPolicy: "no-referrer",
|
|
43
|
-
headers: __assign({ "content-type": "application/json" }, headers),
|
|
44
|
-
};
|
|
41
|
+
_a = options || {}, json = _a.json, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _d, _e = _a.adapter, adapter = _e === void 0 ? adapterBase : _e, _f = _a.shouldThrow, shouldThrow = _f === void 0 ? shouldThrowBase : _f;
|
|
42
|
+
requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
|
|
45
43
|
timeoutNumber = Number(timeout);
|
|
46
44
|
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
47
45
|
if (method !== "get" && json) {
|
|
@@ -58,42 +56,42 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
58
56
|
url += buildUrlQueryString(params);
|
|
59
57
|
}
|
|
60
58
|
if (!shouldThrow) return [3 /*break*/, 5];
|
|
61
|
-
|
|
59
|
+
_g.label = 1;
|
|
62
60
|
case 1:
|
|
63
|
-
|
|
61
|
+
_g.trys.push([1, 3, , 4]);
|
|
64
62
|
return [4 /*yield*/, fetch(url, requestInit)];
|
|
65
63
|
case 2:
|
|
66
|
-
response =
|
|
64
|
+
response = _g.sent();
|
|
67
65
|
return [3 /*break*/, 4];
|
|
68
66
|
case 3:
|
|
69
|
-
e_1 =
|
|
67
|
+
e_1 = _g.sent();
|
|
70
68
|
// eslint-disable-next-line no-throw-literal
|
|
71
69
|
throw { e: e_1 };
|
|
72
70
|
case 4: return [3 /*break*/, 7];
|
|
73
71
|
case 5: return [4 /*yield*/, fetch(url, requestInit)];
|
|
74
72
|
case 6:
|
|
75
|
-
response =
|
|
76
|
-
|
|
73
|
+
response = _g.sent();
|
|
74
|
+
_g.label = 7;
|
|
77
75
|
case 7:
|
|
78
76
|
if (timeoutId) {
|
|
79
77
|
clearTimeout(timeoutId);
|
|
80
78
|
}
|
|
81
79
|
if (!shouldThrow) return [3 /*break*/, 15];
|
|
82
|
-
|
|
80
|
+
_g.label = 8;
|
|
83
81
|
case 8:
|
|
84
|
-
|
|
82
|
+
_g.trys.push([8, 13, , 14]);
|
|
85
83
|
if (!adapter) return [3 /*break*/, 10];
|
|
86
84
|
return [4 /*yield*/, adapter(response, options || {})];
|
|
87
85
|
case 9:
|
|
88
|
-
result =
|
|
86
|
+
result = _g.sent();
|
|
89
87
|
return [3 /*break*/, 12];
|
|
90
88
|
case 10: return [4 /*yield*/, response.json()];
|
|
91
89
|
case 11:
|
|
92
|
-
result =
|
|
93
|
-
|
|
90
|
+
result = _g.sent();
|
|
91
|
+
_g.label = 12;
|
|
94
92
|
case 12: return [3 /*break*/, 14];
|
|
95
93
|
case 13:
|
|
96
|
-
e_2 =
|
|
94
|
+
e_2 = _g.sent();
|
|
97
95
|
// eslint-disable-next-line no-throw-literal
|
|
98
96
|
throw { e: e_2 };
|
|
99
97
|
case 14: return [3 /*break*/, 19];
|
|
@@ -101,12 +99,12 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
101
99
|
if (!adapter) return [3 /*break*/, 17];
|
|
102
100
|
return [4 /*yield*/, adapter(response, options || {})];
|
|
103
101
|
case 16:
|
|
104
|
-
result =
|
|
102
|
+
result = _g.sent();
|
|
105
103
|
return [3 /*break*/, 19];
|
|
106
104
|
case 17: return [4 /*yield*/, response.json()];
|
|
107
105
|
case 18:
|
|
108
|
-
result =
|
|
109
|
-
|
|
106
|
+
result = _g.sent();
|
|
107
|
+
_g.label = 19;
|
|
110
108
|
case 19:
|
|
111
109
|
if (shouldThrow && result.fail) {
|
|
112
110
|
// throw new ApiError<Failed>(result);
|
package/node/core/index.js
CHANGED
|
@@ -28,23 +28,21 @@ exports.ApiError = ApiError;
|
|
|
28
28
|
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
29
29
|
*/
|
|
30
30
|
var createApi = function (apiName, baseUrl, options) {
|
|
31
|
-
var _a = options || {}, adapterBase = _a.adapter,
|
|
31
|
+
var _a = options || {}, adapterBase = _a.adapter, _b = _a.request, requestBase = _b === void 0 ? {
|
|
32
|
+
// mode: "cors",
|
|
33
|
+
// redirect: "follow",
|
|
34
|
+
credentials: "include",
|
|
35
|
+
// cache: "no-cache",
|
|
36
|
+
referrerPolicy: "no-referrer",
|
|
37
|
+
} : _b, shouldThrowBase = _a.shouldThrow;
|
|
32
38
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
33
39
|
api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
34
|
-
var _a, json, params, _b,
|
|
35
|
-
return tslib_1.__generator(this, function (
|
|
36
|
-
switch (
|
|
40
|
+
var _a, json, params, _b, request, _c, headers, _d, timeout, _e, adapter, _f, shouldThrow, requestInit, timeoutNumber, controller, timeoutId, url, response, e_1, result, e_2, pre;
|
|
41
|
+
return tslib_1.__generator(this, function (_g) {
|
|
42
|
+
switch (_g.label) {
|
|
37
43
|
case 0:
|
|
38
|
-
_a = options || {}, json = _a.json, params = _a.params, _b = _a.headers, headers =
|
|
39
|
-
requestInit = {
|
|
40
|
-
method: method.toUpperCase(),
|
|
41
|
-
// mode: "cors",
|
|
42
|
-
// redirect: "follow",
|
|
43
|
-
credentials: "include",
|
|
44
|
-
// cache: "no-cache",
|
|
45
|
-
referrerPolicy: "no-referrer",
|
|
46
|
-
headers: tslib_1.__assign({ "content-type": "application/json" }, headers),
|
|
47
|
-
};
|
|
44
|
+
_a = options || {}, json = _a.json, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _d, _e = _a.adapter, adapter = _e === void 0 ? adapterBase : _e, _f = _a.shouldThrow, shouldThrow = _f === void 0 ? shouldThrowBase : _f;
|
|
45
|
+
requestInit = tslib_1.__assign(tslib_1.__assign({ method: method.toUpperCase() }, request), { headers: tslib_1.__assign({ "content-type": "application/json" }, headers) });
|
|
48
46
|
timeoutNumber = Number(timeout);
|
|
49
47
|
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
50
48
|
if (method !== "get" && json) {
|
|
@@ -61,42 +59,42 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
61
59
|
url += (0, utils_1.buildUrlQueryString)(params);
|
|
62
60
|
}
|
|
63
61
|
if (!shouldThrow) return [3 /*break*/, 5];
|
|
64
|
-
|
|
62
|
+
_g.label = 1;
|
|
65
63
|
case 1:
|
|
66
|
-
|
|
64
|
+
_g.trys.push([1, 3, , 4]);
|
|
67
65
|
return [4 /*yield*/, fetch(url, requestInit)];
|
|
68
66
|
case 2:
|
|
69
|
-
response =
|
|
67
|
+
response = _g.sent();
|
|
70
68
|
return [3 /*break*/, 4];
|
|
71
69
|
case 3:
|
|
72
|
-
e_1 =
|
|
70
|
+
e_1 = _g.sent();
|
|
73
71
|
// eslint-disable-next-line no-throw-literal
|
|
74
72
|
throw { e: e_1 };
|
|
75
73
|
case 4: return [3 /*break*/, 7];
|
|
76
74
|
case 5: return [4 /*yield*/, fetch(url, requestInit)];
|
|
77
75
|
case 6:
|
|
78
|
-
response =
|
|
79
|
-
|
|
76
|
+
response = _g.sent();
|
|
77
|
+
_g.label = 7;
|
|
80
78
|
case 7:
|
|
81
79
|
if (timeoutId) {
|
|
82
80
|
clearTimeout(timeoutId);
|
|
83
81
|
}
|
|
84
82
|
if (!shouldThrow) return [3 /*break*/, 15];
|
|
85
|
-
|
|
83
|
+
_g.label = 8;
|
|
86
84
|
case 8:
|
|
87
|
-
|
|
85
|
+
_g.trys.push([8, 13, , 14]);
|
|
88
86
|
if (!adapter) return [3 /*break*/, 10];
|
|
89
87
|
return [4 /*yield*/, adapter(response, options || {})];
|
|
90
88
|
case 9:
|
|
91
|
-
result =
|
|
89
|
+
result = _g.sent();
|
|
92
90
|
return [3 /*break*/, 12];
|
|
93
91
|
case 10: return [4 /*yield*/, response.json()];
|
|
94
92
|
case 11:
|
|
95
|
-
result =
|
|
96
|
-
|
|
93
|
+
result = _g.sent();
|
|
94
|
+
_g.label = 12;
|
|
97
95
|
case 12: return [3 /*break*/, 14];
|
|
98
96
|
case 13:
|
|
99
|
-
e_2 =
|
|
97
|
+
e_2 = _g.sent();
|
|
100
98
|
// eslint-disable-next-line no-throw-literal
|
|
101
99
|
throw { e: e_2 };
|
|
102
100
|
case 14: return [3 /*break*/, 19];
|
|
@@ -104,12 +102,12 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
104
102
|
if (!adapter) return [3 /*break*/, 17];
|
|
105
103
|
return [4 /*yield*/, adapter(response, options || {})];
|
|
106
104
|
case 16:
|
|
107
|
-
result =
|
|
105
|
+
result = _g.sent();
|
|
108
106
|
return [3 /*break*/, 19];
|
|
109
107
|
case 17: return [4 /*yield*/, response.json()];
|
|
110
108
|
case 18:
|
|
111
|
-
result =
|
|
112
|
-
|
|
109
|
+
result = _g.sent();
|
|
110
|
+
_g.label = 19;
|
|
113
111
|
case 19:
|
|
114
112
|
if (shouldThrow && result.fail) {
|
|
115
113
|
// throw new ApiError<Failed>(result);
|
package/package.json
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
"main": "./node/index.js",
|
|
5
5
|
"typings": "./index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@koine/utils": "1.0.
|
|
7
|
+
"@koine/utils": "1.0.20",
|
|
8
8
|
"next": "^12.1.6",
|
|
9
9
|
"swr": "^2.0.0-beta.3",
|
|
10
10
|
"tslib": "^2.4.0"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {},
|
|
13
|
-
"version": "1.0.
|
|
13
|
+
"version": "1.0.20",
|
|
14
14
|
"module": "./index.js",
|
|
15
15
|
"types": "./index.d.ts"
|
|
16
16
|
}
|
package/typings.d.ts
CHANGED
|
@@ -19,6 +19,14 @@ declare namespace Koine.Api {
|
|
|
19
19
|
) => Client<TEndpoints>;
|
|
20
20
|
|
|
21
21
|
type ClientOptions = {
|
|
22
|
+
/**
|
|
23
|
+
* Basic request options to supply to `fetch`
|
|
24
|
+
*
|
|
25
|
+
* @see RequestInit
|
|
26
|
+
*
|
|
27
|
+
* @default { credentials: "include", referrerPolicy: "no-referrer" }
|
|
28
|
+
*/
|
|
29
|
+
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
22
30
|
adapter?: ResponseAdapter;
|
|
23
31
|
shouldThrow?: boolean;
|
|
24
32
|
};
|
|
@@ -175,6 +183,14 @@ declare namespace Koine.Api {
|
|
|
175
183
|
* @default 10000
|
|
176
184
|
*/
|
|
177
185
|
timeout?: number | false | null;
|
|
186
|
+
/**
|
|
187
|
+
* Basic request options to supply to `fetch`
|
|
188
|
+
*
|
|
189
|
+
* @see RequestInit
|
|
190
|
+
*
|
|
191
|
+
* @default { credentials: "include", referrerPolicy: "no-referrer" }
|
|
192
|
+
*/
|
|
193
|
+
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
178
194
|
};
|
|
179
195
|
|
|
180
196
|
/**
|
|
@@ -269,7 +285,7 @@ declare namespace Koine.Api {
|
|
|
269
285
|
|
|
270
286
|
type ResponseAdapter = <
|
|
271
287
|
Succesfull extends DataSuccesfull = DataSuccesfull,
|
|
272
|
-
Failed extends DataFailed = DataFailed
|
|
288
|
+
Failed extends DataFailed = DataFailed
|
|
273
289
|
>(
|
|
274
290
|
response: _Response,
|
|
275
291
|
options: TOptions
|
|
@@ -293,4 +309,96 @@ declare namespace Koine.Api {
|
|
|
293
309
|
};
|
|
294
310
|
|
|
295
311
|
type HooksMapsByName = { [K in keyof HooksMaps as HooksMaps[K]]: K };
|
|
312
|
+
|
|
313
|
+
//////////////////////////////////////////////////////////////////////////////
|
|
314
|
+
//
|
|
315
|
+
// Generate shortcuts
|
|
316
|
+
//
|
|
317
|
+
//////////////////////////////////////////////////////////////////////////////
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* To generate all available helpers use in your `API` types:
|
|
321
|
+
*
|
|
322
|
+
* ```ts
|
|
323
|
+
* type Response = Koine.Api.GenerateResponseHelpers<Endpoints>;
|
|
324
|
+
* type Request = Koine.Api.GenerateRequestHelpers<Endpoints>;
|
|
325
|
+
* type Get = Koine.Api.GenerateGetHelpers<Endpoints>;
|
|
326
|
+
* type Post = Koine.Api.GeneratePostHelpers<Endpoints>;
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
type Generate = "here just to read the example usage";
|
|
330
|
+
|
|
331
|
+
type _ShortcutsMaps = {
|
|
332
|
+
[TMethod in RequestMethod]: Capitalize<TMethod>;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
type _ShortcutsMapsByMethod = {
|
|
336
|
+
[K in keyof _ShortcutsMaps as _ShortcutsMaps[K]]: K;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* // define the type on your `API` types:
|
|
343
|
+
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
344
|
+
*
|
|
345
|
+
* // consume the type wherever in your app:
|
|
346
|
+
* type MyData = API.Response["get"]["my/endpoint"];
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
type GenerateResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
350
|
+
[TMethod in RequestMethod]: {
|
|
351
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["response"];
|
|
352
|
+
};
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* // define the type on your `API` types:
|
|
359
|
+
* type Get = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
360
|
+
*
|
|
361
|
+
* // consume the type wherever in your app:
|
|
362
|
+
* type MyData = API.Get["my/endpoint"];
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
type GenerateGetShortcuts<TEndpoints extends Endpoints> = {
|
|
366
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["response"];
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* @example
|
|
371
|
+
* ```ts
|
|
372
|
+
* // define the type on your `API` types:
|
|
373
|
+
* type Post = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
374
|
+
*
|
|
375
|
+
* // consume the type wherever in your app:
|
|
376
|
+
* type MyData = API.Post["my/endpoint"];
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
type GeneratePostShortcuts<TEndpoints extends Endpoints> = {
|
|
380
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["response"];
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* This is not useful as it is the same as doing
|
|
385
|
+
* `API.Endpoints["my/endpoint"]["GET"]["response"];`
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* // define the type on your `API` types:
|
|
390
|
+
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
391
|
+
*
|
|
392
|
+
* // consume the type wherever in your app:
|
|
393
|
+
* type MyData = API.$["my/endpoint"]["get"]["response"];
|
|
394
|
+
* ```
|
|
395
|
+
* @deprecated
|
|
396
|
+
*/
|
|
397
|
+
// type GenerateAllShortcuts<TEndpoints extends Endpoints> = {
|
|
398
|
+
// [TEndpointUrl in keyof TEndpoints]: {
|
|
399
|
+
// [TMethod in RequestMethod]: {
|
|
400
|
+
// [DataType in EndpointDataType]: TEndpoints[TEndpointUrl][Uppercase<TMethod>][DataType];
|
|
401
|
+
// }
|
|
402
|
+
// }
|
|
403
|
+
// }
|
|
296
404
|
}
|