@koine/api 1.0.17 → 1.0.21
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.d.ts +2 -2
- package/core/index.js +30 -32
- package/next/index.d.ts +1 -1
- package/next/index.js +3 -3
- package/node/core/index.js +30 -32
- package/node/next/index.js +3 -3
- package/node/swr/index.js +15 -6
- package/package.json +2 -2
- package/swr/index.d.ts +1 -1
- package/swr/index.js +15 -6
- package/typings.d.ts +151 -31
package/core/index.d.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
export declare class ApiError<
|
|
9
|
-
constructor(result: Koine.Api.
|
|
8
|
+
export declare class ApiError<TResponseFail extends Koine.Api.ResponseFail = unknown> extends Error {
|
|
9
|
+
constructor(result: Koine.Api.ResultFail<TResponseFail>);
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* Create api client
|
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
|
+
credentials: "include",
|
|
30
|
+
referrerPolicy: "no-referrer",
|
|
31
|
+
// mode: "cors",
|
|
32
|
+
// redirect: "follow",
|
|
33
|
+
// cache: "no-cache",
|
|
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, msg;
|
|
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);
|
|
@@ -115,12 +113,12 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
115
113
|
throw result;
|
|
116
114
|
}
|
|
117
115
|
if (process.env["NODE_ENV"] !== "production") {
|
|
118
|
-
|
|
116
|
+
msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
119
117
|
if (result.ok) {
|
|
120
|
-
console.log("".concat(
|
|
118
|
+
console.log("[@koine] \uD83D\uDFE2 ".concat(msg));
|
|
121
119
|
}
|
|
122
120
|
else {
|
|
123
|
-
console.log("".concat(
|
|
121
|
+
console.log("[@koine] \uD83D\uDD34 ".concat(msg));
|
|
124
122
|
}
|
|
125
123
|
}
|
|
126
124
|
return [2 /*return*/, result];
|
package/next/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { NextApiResponse } from "next";
|
|
2
|
-
export declare const nextApiResponse: (nextRes: NextApiResponse,
|
|
2
|
+
export declare const nextApiResponse: (nextRes: NextApiResponse, result: Koine.Api.ResultOk | Koine.Api.ResultFail) => void;
|
package/next/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export var nextApiResponse = function (nextRes,
|
|
2
|
-
// nextRes.status(
|
|
3
|
-
nextRes.status(
|
|
1
|
+
export var nextApiResponse = function (nextRes, result) {
|
|
2
|
+
// nextRes.status(result.status).json(result.data || result.msg);
|
|
3
|
+
nextRes.status(result.status).json(result);
|
|
4
4
|
};
|
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
|
+
credentials: "include",
|
|
33
|
+
referrerPolicy: "no-referrer",
|
|
34
|
+
// mode: "cors",
|
|
35
|
+
// redirect: "follow",
|
|
36
|
+
// cache: "no-cache",
|
|
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, msg;
|
|
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);
|
|
@@ -118,12 +116,12 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
118
116
|
throw result;
|
|
119
117
|
}
|
|
120
118
|
if (process.env["NODE_ENV"] !== "production") {
|
|
121
|
-
|
|
119
|
+
msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
122
120
|
if (result.ok) {
|
|
123
|
-
console.log("".concat(
|
|
121
|
+
console.log("[@koine] \uD83D\uDFE2 ".concat(msg));
|
|
124
122
|
}
|
|
125
123
|
else {
|
|
126
|
-
console.log("".concat(
|
|
124
|
+
console.log("[@koine] \uD83D\uDD34 ".concat(msg));
|
|
127
125
|
}
|
|
128
126
|
}
|
|
129
127
|
return [2 /*return*/, result];
|
package/node/next/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.nextApiResponse = void 0;
|
|
4
|
-
var nextApiResponse = function (nextRes,
|
|
5
|
-
// nextRes.status(
|
|
6
|
-
nextRes.status(
|
|
4
|
+
var nextApiResponse = function (nextRes, result) {
|
|
5
|
+
// nextRes.status(result.status).json(result.data || result.msg);
|
|
6
|
+
nextRes.status(result.status).json(result);
|
|
7
7
|
};
|
|
8
8
|
exports.nextApiResponse = nextApiResponse;
|
package/node/swr/index.js
CHANGED
|
@@ -18,9 +18,9 @@ function createUseApi(api, method) {
|
|
|
18
18
|
// if (ok) {
|
|
19
19
|
// return data;
|
|
20
20
|
// }
|
|
21
|
-
// throw new Error() as unknown as Koine.Api.
|
|
21
|
+
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
22
22
|
// } catch(e) {
|
|
23
|
-
// throw new Error() as unknown as Koine.Api.
|
|
23
|
+
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
24
24
|
// }
|
|
25
25
|
// };
|
|
26
26
|
// }
|
|
@@ -41,11 +41,20 @@ function createUseApi(api, method) {
|
|
|
41
41
|
return (0, swr_1.default)(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
42
42
|
}
|
|
43
43
|
var config = _config;
|
|
44
|
-
var sender = function (
|
|
45
|
-
|
|
44
|
+
var sender = function (
|
|
45
|
+
// if the first argument is an array the second tem are the base options
|
|
46
|
+
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
47
|
+
// by the _options just here below
|
|
48
|
+
_endpoint,
|
|
49
|
+
// these are the options arriving when calling `trigger({ json, params, etc... })
|
|
50
|
+
_options) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
51
|
+
var endpoint, options, _a, ok, data;
|
|
46
52
|
return tslib_1.__generator(this, function (_b) {
|
|
47
53
|
switch (_b.label) {
|
|
48
|
-
case 0:
|
|
54
|
+
case 0:
|
|
55
|
+
endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
56
|
+
options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
57
|
+
return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, options), (_options.arg || {})), { shouldThrow: true }))];
|
|
49
58
|
case 1:
|
|
50
59
|
_a = _b.sent(), ok = _a.ok, data = _a.data;
|
|
51
60
|
return [2 /*return*/, ok ? data : data];
|
|
@@ -56,7 +65,7 @@ function createUseApi(api, method) {
|
|
|
56
65
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
57
66
|
return (0, mutation_1.default)(
|
|
58
67
|
// @ts-expect-error FIXME: I can't get it...
|
|
59
|
-
options ? [endpoint, options] :
|
|
68
|
+
options ? [endpoint, options] : endpoint, sender, config);
|
|
60
69
|
};
|
|
61
70
|
}
|
|
62
71
|
/**
|
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.21",
|
|
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.21",
|
|
14
14
|
"module": "./index.js",
|
|
15
15
|
"types": "./index.d.ts"
|
|
16
16
|
}
|
package/swr/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type SWRConfiguration, type SWRResponse } from "swr";
|
|
2
2
|
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
3
|
-
declare type KoineApiMethodHookSWR<THookName extends keyof Koine.Api.HooksMapsByName, TEndpoints extends Koine.Api.Endpoints> = <TEndpoint extends Koine.Api.EndpointUrl<TEndpoints>, TMethod extends Koine.Api.RequestMethod = Koine.Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Koine.Api.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfiguration<Koine.Api.
|
|
3
|
+
declare type KoineApiMethodHookSWR<THookName extends keyof Koine.Api.HooksMapsByName, TEndpoints extends Koine.Api.Endpoints> = <TEndpoint extends Koine.Api.EndpointUrl<TEndpoints>, TMethod extends Koine.Api.RequestMethod = Koine.Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Koine.Api.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfiguration<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationConfiguration<Koine.Api.EndpointResultOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResultFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>) => THookName extends "useGet" ? SWRResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationResponse<Koine.Api.EndpointResultOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResultFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>;
|
|
4
4
|
/**
|
|
5
5
|
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
6
6
|
*/
|
package/swr/index.js
CHANGED
|
@@ -15,9 +15,9 @@ function createUseApi(api, method) {
|
|
|
15
15
|
// if (ok) {
|
|
16
16
|
// return data;
|
|
17
17
|
// }
|
|
18
|
-
// throw new Error() as unknown as Koine.Api.
|
|
18
|
+
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
19
19
|
// } catch(e) {
|
|
20
|
-
// throw new Error() as unknown as Koine.Api.
|
|
20
|
+
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
21
21
|
// }
|
|
22
22
|
// };
|
|
23
23
|
// }
|
|
@@ -38,11 +38,20 @@ function createUseApi(api, method) {
|
|
|
38
38
|
return useSWR(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
39
39
|
}
|
|
40
40
|
var config = _config;
|
|
41
|
-
var sender = function (
|
|
42
|
-
|
|
41
|
+
var sender = function (
|
|
42
|
+
// if the first argument is an array the second tem are the base options
|
|
43
|
+
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
44
|
+
// by the _options just here below
|
|
45
|
+
_endpoint,
|
|
46
|
+
// these are the options arriving when calling `trigger({ json, params, etc... })
|
|
47
|
+
_options) { return __awaiter(_this, void 0, void 0, function () {
|
|
48
|
+
var endpoint, options, _a, ok, data;
|
|
43
49
|
return __generator(this, function (_b) {
|
|
44
50
|
switch (_b.label) {
|
|
45
|
-
case 0:
|
|
51
|
+
case 0:
|
|
52
|
+
endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
53
|
+
options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
54
|
+
return [4 /*yield*/, api[method](endpoint, __assign(__assign(__assign({}, options), (_options.arg || {})), { shouldThrow: true }))];
|
|
46
55
|
case 1:
|
|
47
56
|
_a = _b.sent(), ok = _a.ok, data = _a.data;
|
|
48
57
|
return [2 /*return*/, ok ? data : data];
|
|
@@ -53,7 +62,7 @@ function createUseApi(api, method) {
|
|
|
53
62
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
54
63
|
return useSWRMutation(
|
|
55
64
|
// @ts-expect-error FIXME: I can't get it...
|
|
56
|
-
options ? [endpoint, options] :
|
|
65
|
+
options ? [endpoint, options] : endpoint, sender, config);
|
|
57
66
|
};
|
|
58
67
|
}
|
|
59
68
|
/**
|
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
|
};
|
|
@@ -29,12 +37,12 @@ declare namespace Koine.Api {
|
|
|
29
37
|
> = <
|
|
30
38
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
31
39
|
TOptions extends EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>,
|
|
32
|
-
TSuccesfull extends Koine.Api.DataSuccesfull = Koine.Api.
|
|
40
|
+
TSuccesfull extends Koine.Api.DataSuccesfull = Koine.Api.EndpointResponseOk<
|
|
33
41
|
TEndpoints,
|
|
34
42
|
TEndpoint,
|
|
35
43
|
TMethod
|
|
36
44
|
>,
|
|
37
|
-
TFailed extends Koine.Api.DataFailed = Koine.Api.
|
|
45
|
+
TFailed extends Koine.Api.DataFailed = Koine.Api.EndpointResponseFail<
|
|
38
46
|
TEndpoints,
|
|
39
47
|
TEndpoint,
|
|
40
48
|
TMethod
|
|
@@ -64,29 +72,41 @@ declare namespace Koine.Api {
|
|
|
64
72
|
TMethod extends RequestMethod
|
|
65
73
|
> = RequestOptions<
|
|
66
74
|
TMethod,
|
|
67
|
-
TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
75
|
+
TEndpoints[TEndpoint][Uppercase<TMethod>]["json"],
|
|
68
76
|
TEndpoints[TEndpoint][Uppercase<TMethod>]["params"]
|
|
69
77
|
>;
|
|
70
78
|
|
|
71
|
-
type
|
|
79
|
+
type EndpointResultOk<
|
|
80
|
+
TEndpoints extends Endpoints,
|
|
81
|
+
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
82
|
+
TMethod extends RequestMethod
|
|
83
|
+
> = ResultOk<TEndpoints[TEndpoint][Uppercase<TMethod>]["ok"]>;
|
|
84
|
+
|
|
85
|
+
type EndpointResultFail<
|
|
72
86
|
TEndpoints extends Endpoints,
|
|
73
87
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
74
88
|
TMethod extends RequestMethod
|
|
75
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
89
|
+
> = ResultFail<TEndpoints[TEndpoint][Uppercase<TMethod>]["fail"]>;
|
|
76
90
|
|
|
77
|
-
type
|
|
91
|
+
type EndpointResponseOk<
|
|
78
92
|
TEndpoints extends Endpoints,
|
|
79
93
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
80
94
|
TMethod extends RequestMethod
|
|
81
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
95
|
+
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["ok"];
|
|
96
|
+
|
|
97
|
+
type EndpointResponseFail<
|
|
98
|
+
TEndpoints extends Endpoints,
|
|
99
|
+
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
100
|
+
TMethod extends RequestMethod
|
|
101
|
+
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["fail"];
|
|
82
102
|
|
|
83
103
|
type EndpointResponse<
|
|
84
104
|
TEndpoints extends Endpoints,
|
|
85
105
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
86
106
|
TMethod extends RequestMethod
|
|
87
107
|
> = Response<
|
|
88
|
-
|
|
89
|
-
|
|
108
|
+
EndpointResponseOk<TEndpoints, TEndpoint, TMethod>,
|
|
109
|
+
EndpointResponseFail<TEndpoints, TEndpoint, TMethod>
|
|
90
110
|
>;
|
|
91
111
|
|
|
92
112
|
//////////////////////////////////////////////////////////////////////////////
|
|
@@ -130,20 +150,20 @@ declare namespace Koine.Api {
|
|
|
130
150
|
/**
|
|
131
151
|
* The request body of a non-GET request
|
|
132
152
|
*/
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The JSON response data returned by the request in case of success
|
|
136
|
-
*/
|
|
137
|
-
response?: null | unknown;
|
|
153
|
+
json?: RequestJson;
|
|
138
154
|
/**
|
|
139
155
|
* The parameters to encode in the URL of the request
|
|
140
156
|
*/
|
|
141
157
|
params?: RequestParams;
|
|
158
|
+
/**
|
|
159
|
+
* The JSON response data returned by the request in case of success
|
|
160
|
+
*/
|
|
161
|
+
ok?: null | unknown;
|
|
142
162
|
/**
|
|
143
163
|
* The shape of the error data returned by the request in case of
|
|
144
164
|
* failure
|
|
145
165
|
*/
|
|
146
|
-
|
|
166
|
+
fail?: null | unknown;
|
|
147
167
|
};
|
|
148
168
|
|
|
149
169
|
//////////////////////////////////////////////////////////////////////////////
|
|
@@ -175,6 +195,14 @@ declare namespace Koine.Api {
|
|
|
175
195
|
* @default 10000
|
|
176
196
|
*/
|
|
177
197
|
timeout?: number | false | null;
|
|
198
|
+
/**
|
|
199
|
+
* Basic request options to supply to `fetch`
|
|
200
|
+
*
|
|
201
|
+
* @see RequestInit
|
|
202
|
+
*
|
|
203
|
+
* @default { credentials: "include", referrerPolicy: "no-referrer" }
|
|
204
|
+
*/
|
|
205
|
+
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
178
206
|
};
|
|
179
207
|
|
|
180
208
|
/**
|
|
@@ -215,65 +243,65 @@ declare namespace Koine.Api {
|
|
|
215
243
|
|
|
216
244
|
//////////////////////////////////////////////////////////////////////////////
|
|
217
245
|
//
|
|
218
|
-
// Response
|
|
246
|
+
// Response/Result
|
|
219
247
|
//
|
|
220
248
|
//////////////////////////////////////////////////////////////////////////////
|
|
221
249
|
|
|
222
|
-
type
|
|
250
|
+
type ResponseOk = unknown;
|
|
223
251
|
|
|
224
|
-
type
|
|
252
|
+
type ResponseFail = unknown;
|
|
225
253
|
|
|
226
|
-
type
|
|
254
|
+
type ResultShared<
|
|
227
255
|
T extends Record<string, unknown> = Record<string, unknown>
|
|
228
256
|
> = T & {
|
|
229
257
|
status: _Response["status"];
|
|
230
258
|
msg: _Response["statusText"];
|
|
231
259
|
};
|
|
232
260
|
|
|
233
|
-
type
|
|
261
|
+
type ResultOk<TResponse extends ResponseOk = ResponseOk> = {
|
|
234
262
|
status: _Response["status"];
|
|
235
263
|
msg: _Response["statusText"];
|
|
236
264
|
ok: true;
|
|
237
265
|
fail?: false;
|
|
238
|
-
data:
|
|
266
|
+
data: TResponse;
|
|
239
267
|
};
|
|
240
268
|
|
|
241
|
-
type
|
|
269
|
+
type ResultFail<TResponse extends ResponseFail = ResponseFail> = {
|
|
242
270
|
status: _Response["status"];
|
|
243
271
|
msg: _Response["statusText"];
|
|
244
272
|
ok?: false;
|
|
245
273
|
fail: true;
|
|
246
|
-
data:
|
|
274
|
+
data: TResponse;
|
|
247
275
|
};
|
|
248
276
|
|
|
249
|
-
type
|
|
277
|
+
type Result<TResponseOk extends ResponseOk, TResponseFail extends ResponseFail> =
|
|
250
278
|
// FIXME: without the type duplication below the following two lines do not
|
|
251
279
|
// work as they do not narrow the type when checking for the boolean values
|
|
252
280
|
// truthiness
|
|
253
|
-
// |
|
|
254
|
-
// |
|
|
281
|
+
// | ResultOk<TOk>
|
|
282
|
+
// | ResultFail<TOk>;
|
|
255
283
|
| {
|
|
256
284
|
status: _Response["status"];
|
|
257
285
|
msg: _Response["statusText"];
|
|
258
286
|
ok: true;
|
|
259
287
|
fail?: false;
|
|
260
|
-
data:
|
|
288
|
+
data: TResponseOk;
|
|
261
289
|
}
|
|
262
290
|
| {
|
|
263
291
|
status: _Response["status"];
|
|
264
292
|
msg: _Response["statusText"];
|
|
265
293
|
ok?: false;
|
|
266
294
|
fail: true;
|
|
267
|
-
data:
|
|
295
|
+
data: TResponseFail;
|
|
268
296
|
};
|
|
269
297
|
|
|
270
298
|
type ResponseAdapter = <
|
|
271
|
-
|
|
272
|
-
|
|
299
|
+
TResponseOk extends ResponseOk = ResponseOk,
|
|
300
|
+
TResponseFail extends ResponseFailed = ResponseFailed
|
|
273
301
|
>(
|
|
274
302
|
response: _Response,
|
|
275
303
|
options: TOptions
|
|
276
|
-
) => Promise<Koine.Api.
|
|
304
|
+
) => Promise<Koine.Api.Result<TResponseOk, TResponseFail>>;
|
|
277
305
|
|
|
278
306
|
//////////////////////////////////////////////////////////////////////////////
|
|
279
307
|
//
|
|
@@ -293,4 +321,96 @@ declare namespace Koine.Api {
|
|
|
293
321
|
};
|
|
294
322
|
|
|
295
323
|
type HooksMapsByName = { [K in keyof HooksMaps as HooksMaps[K]]: K };
|
|
324
|
+
|
|
325
|
+
//////////////////////////////////////////////////////////////////////////////
|
|
326
|
+
//
|
|
327
|
+
// Generate shortcuts
|
|
328
|
+
//
|
|
329
|
+
//////////////////////////////////////////////////////////////////////////////
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* To generate all available helpers use in your `API` types:
|
|
333
|
+
*
|
|
334
|
+
* ```ts
|
|
335
|
+
* type Response = Koine.Api.GenerateResponseHelpers<Endpoints>;
|
|
336
|
+
* type Request = Koine.Api.GenerateRequestHelpers<Endpoints>;
|
|
337
|
+
* type Get = Koine.Api.GenerateGetHelpers<Endpoints>;
|
|
338
|
+
* type Post = Koine.Api.GeneratePostHelpers<Endpoints>;
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
type Generate = "here just to read the example usage";
|
|
342
|
+
|
|
343
|
+
type _ShortcutsMaps = {
|
|
344
|
+
[TMethod in RequestMethod]: Capitalize<TMethod>;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
type _ShortcutsMapsByMethod = {
|
|
348
|
+
[K in keyof _ShortcutsMaps as _ShortcutsMaps[K]]: K;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* @example
|
|
353
|
+
* ```ts
|
|
354
|
+
* // define the type on your `API` types:
|
|
355
|
+
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
356
|
+
*
|
|
357
|
+
* // consume the type wherever in your app:
|
|
358
|
+
* type MyData = API.Response["get"]["my/endpoint"];
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
type GenerateResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
362
|
+
[TMethod in RequestMethod]: {
|
|
363
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["ok"];
|
|
364
|
+
};
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @example
|
|
369
|
+
* ```ts
|
|
370
|
+
* // define the type on your `API` types:
|
|
371
|
+
* type Get = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
372
|
+
*
|
|
373
|
+
* // consume the type wherever in your app:
|
|
374
|
+
* type MyData = API.Get["my/endpoint"];
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
type GenerateGetShortcuts<TEndpoints extends Endpoints> = {
|
|
378
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["ok"];
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* // define the type on your `API` types:
|
|
385
|
+
* type Post = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
386
|
+
*
|
|
387
|
+
* // consume the type wherever in your app:
|
|
388
|
+
* type MyData = API.Post["my/endpoint"];
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
type GeneratePostShortcuts<TEndpoints extends Endpoints> = {
|
|
392
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["ok"];
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* This is not useful as it is the same as doing
|
|
397
|
+
* `API.Endpoints["my/endpoint"]["GET"]["ok"];`
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* // define the type on your `API` types:
|
|
402
|
+
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
403
|
+
*
|
|
404
|
+
* // consume the type wherever in your app:
|
|
405
|
+
* type MyData = API.$["my/endpoint"]["get"]["ok"];
|
|
406
|
+
* ```
|
|
407
|
+
* @deprecated
|
|
408
|
+
*/
|
|
409
|
+
// type GenerateAllShortcuts<TEndpoints extends Endpoints> = {
|
|
410
|
+
// [TEndpointUrl in keyof TEndpoints]: {
|
|
411
|
+
// [TMethod in RequestMethod]: {
|
|
412
|
+
// [DataType in EndpointDataType]: TEndpoints[TEndpointUrl][Uppercase<TMethod>][DataType];
|
|
413
|
+
// }
|
|
414
|
+
// }
|
|
415
|
+
// }
|
|
296
416
|
}
|