@koine/api 1.0.20 → 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 +6 -6
- package/next/index.d.ts +1 -1
- package/next/index.js +3 -3
- package/node/core/index.js +6 -6
- 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 +48 -36
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
|
@@ -26,15 +26,15 @@ export { ApiError };
|
|
|
26
26
|
*/
|
|
27
27
|
export var createApi = function (apiName, baseUrl, options) {
|
|
28
28
|
var _a = options || {}, adapterBase = _a.adapter, _b = _a.request, requestBase = _b === void 0 ? {
|
|
29
|
+
credentials: "include",
|
|
30
|
+
referrerPolicy: "no-referrer",
|
|
29
31
|
// mode: "cors",
|
|
30
32
|
// redirect: "follow",
|
|
31
|
-
credentials: "include",
|
|
32
33
|
// cache: "no-cache",
|
|
33
|
-
referrerPolicy: "no-referrer",
|
|
34
34
|
} : _b, shouldThrowBase = _a.shouldThrow;
|
|
35
35
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
36
36
|
api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
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,
|
|
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
38
|
return __generator(this, function (_g) {
|
|
39
39
|
switch (_g.label) {
|
|
40
40
|
case 0:
|
|
@@ -113,12 +113,12 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
113
113
|
throw result;
|
|
114
114
|
}
|
|
115
115
|
if (process.env["NODE_ENV"] !== "production") {
|
|
116
|
-
|
|
116
|
+
msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
117
117
|
if (result.ok) {
|
|
118
|
-
console.log("".concat(
|
|
118
|
+
console.log("[@koine] \uD83D\uDFE2 ".concat(msg));
|
|
119
119
|
}
|
|
120
120
|
else {
|
|
121
|
-
console.log("".concat(
|
|
121
|
+
console.log("[@koine] \uD83D\uDD34 ".concat(msg));
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
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
|
@@ -29,15 +29,15 @@ exports.ApiError = ApiError;
|
|
|
29
29
|
*/
|
|
30
30
|
var createApi = function (apiName, baseUrl, options) {
|
|
31
31
|
var _a = options || {}, adapterBase = _a.adapter, _b = _a.request, requestBase = _b === void 0 ? {
|
|
32
|
+
credentials: "include",
|
|
33
|
+
referrerPolicy: "no-referrer",
|
|
32
34
|
// mode: "cors",
|
|
33
35
|
// redirect: "follow",
|
|
34
|
-
credentials: "include",
|
|
35
36
|
// cache: "no-cache",
|
|
36
|
-
referrerPolicy: "no-referrer",
|
|
37
37
|
} : _b, shouldThrowBase = _a.shouldThrow;
|
|
38
38
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
39
39
|
api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
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,
|
|
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
41
|
return tslib_1.__generator(this, function (_g) {
|
|
42
42
|
switch (_g.label) {
|
|
43
43
|
case 0:
|
|
@@ -116,12 +116,12 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
116
116
|
throw result;
|
|
117
117
|
}
|
|
118
118
|
if (process.env["NODE_ENV"] !== "production") {
|
|
119
|
-
|
|
119
|
+
msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
120
120
|
if (result.ok) {
|
|
121
|
-
console.log("".concat(
|
|
121
|
+
console.log("[@koine] \uD83D\uDFE2 ".concat(msg));
|
|
122
122
|
}
|
|
123
123
|
else {
|
|
124
|
-
console.log("".concat(
|
|
124
|
+
console.log("[@koine] \uD83D\uDD34 ".concat(msg));
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
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
|
@@ -37,12 +37,12 @@ declare namespace Koine.Api {
|
|
|
37
37
|
> = <
|
|
38
38
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
39
39
|
TOptions extends EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>,
|
|
40
|
-
TSuccesfull extends Koine.Api.DataSuccesfull = Koine.Api.
|
|
40
|
+
TSuccesfull extends Koine.Api.DataSuccesfull = Koine.Api.EndpointResponseOk<
|
|
41
41
|
TEndpoints,
|
|
42
42
|
TEndpoint,
|
|
43
43
|
TMethod
|
|
44
44
|
>,
|
|
45
|
-
TFailed extends Koine.Api.DataFailed = Koine.Api.
|
|
45
|
+
TFailed extends Koine.Api.DataFailed = Koine.Api.EndpointResponseFail<
|
|
46
46
|
TEndpoints,
|
|
47
47
|
TEndpoint,
|
|
48
48
|
TMethod
|
|
@@ -72,29 +72,41 @@ declare namespace Koine.Api {
|
|
|
72
72
|
TMethod extends RequestMethod
|
|
73
73
|
> = RequestOptions<
|
|
74
74
|
TMethod,
|
|
75
|
-
TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
75
|
+
TEndpoints[TEndpoint][Uppercase<TMethod>]["json"],
|
|
76
76
|
TEndpoints[TEndpoint][Uppercase<TMethod>]["params"]
|
|
77
77
|
>;
|
|
78
78
|
|
|
79
|
-
type
|
|
79
|
+
type EndpointResultOk<
|
|
80
80
|
TEndpoints extends Endpoints,
|
|
81
81
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
82
82
|
TMethod extends RequestMethod
|
|
83
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
83
|
+
> = ResultOk<TEndpoints[TEndpoint][Uppercase<TMethod>]["ok"]>;
|
|
84
84
|
|
|
85
|
-
type
|
|
85
|
+
type EndpointResultFail<
|
|
86
86
|
TEndpoints extends Endpoints,
|
|
87
87
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
88
88
|
TMethod extends RequestMethod
|
|
89
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["
|
|
89
|
+
> = ResultFail<TEndpoints[TEndpoint][Uppercase<TMethod>]["fail"]>;
|
|
90
|
+
|
|
91
|
+
type EndpointResponseOk<
|
|
92
|
+
TEndpoints extends Endpoints,
|
|
93
|
+
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
94
|
+
TMethod extends RequestMethod
|
|
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"];
|
|
90
102
|
|
|
91
103
|
type EndpointResponse<
|
|
92
104
|
TEndpoints extends Endpoints,
|
|
93
105
|
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
94
106
|
TMethod extends RequestMethod
|
|
95
107
|
> = Response<
|
|
96
|
-
|
|
97
|
-
|
|
108
|
+
EndpointResponseOk<TEndpoints, TEndpoint, TMethod>,
|
|
109
|
+
EndpointResponseFail<TEndpoints, TEndpoint, TMethod>
|
|
98
110
|
>;
|
|
99
111
|
|
|
100
112
|
//////////////////////////////////////////////////////////////////////////////
|
|
@@ -138,20 +150,20 @@ declare namespace Koine.Api {
|
|
|
138
150
|
/**
|
|
139
151
|
* The request body of a non-GET request
|
|
140
152
|
*/
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* The JSON response data returned by the request in case of success
|
|
144
|
-
*/
|
|
145
|
-
response?: null | unknown;
|
|
153
|
+
json?: RequestJson;
|
|
146
154
|
/**
|
|
147
155
|
* The parameters to encode in the URL of the request
|
|
148
156
|
*/
|
|
149
157
|
params?: RequestParams;
|
|
158
|
+
/**
|
|
159
|
+
* The JSON response data returned by the request in case of success
|
|
160
|
+
*/
|
|
161
|
+
ok?: null | unknown;
|
|
150
162
|
/**
|
|
151
163
|
* The shape of the error data returned by the request in case of
|
|
152
164
|
* failure
|
|
153
165
|
*/
|
|
154
|
-
|
|
166
|
+
fail?: null | unknown;
|
|
155
167
|
};
|
|
156
168
|
|
|
157
169
|
//////////////////////////////////////////////////////////////////////////////
|
|
@@ -231,65 +243,65 @@ declare namespace Koine.Api {
|
|
|
231
243
|
|
|
232
244
|
//////////////////////////////////////////////////////////////////////////////
|
|
233
245
|
//
|
|
234
|
-
// Response
|
|
246
|
+
// Response/Result
|
|
235
247
|
//
|
|
236
248
|
//////////////////////////////////////////////////////////////////////////////
|
|
237
249
|
|
|
238
|
-
type
|
|
250
|
+
type ResponseOk = unknown;
|
|
239
251
|
|
|
240
|
-
type
|
|
252
|
+
type ResponseFail = unknown;
|
|
241
253
|
|
|
242
|
-
type
|
|
254
|
+
type ResultShared<
|
|
243
255
|
T extends Record<string, unknown> = Record<string, unknown>
|
|
244
256
|
> = T & {
|
|
245
257
|
status: _Response["status"];
|
|
246
258
|
msg: _Response["statusText"];
|
|
247
259
|
};
|
|
248
260
|
|
|
249
|
-
type
|
|
261
|
+
type ResultOk<TResponse extends ResponseOk = ResponseOk> = {
|
|
250
262
|
status: _Response["status"];
|
|
251
263
|
msg: _Response["statusText"];
|
|
252
264
|
ok: true;
|
|
253
265
|
fail?: false;
|
|
254
|
-
data:
|
|
266
|
+
data: TResponse;
|
|
255
267
|
};
|
|
256
268
|
|
|
257
|
-
type
|
|
269
|
+
type ResultFail<TResponse extends ResponseFail = ResponseFail> = {
|
|
258
270
|
status: _Response["status"];
|
|
259
271
|
msg: _Response["statusText"];
|
|
260
272
|
ok?: false;
|
|
261
273
|
fail: true;
|
|
262
|
-
data:
|
|
274
|
+
data: TResponse;
|
|
263
275
|
};
|
|
264
276
|
|
|
265
|
-
type
|
|
277
|
+
type Result<TResponseOk extends ResponseOk, TResponseFail extends ResponseFail> =
|
|
266
278
|
// FIXME: without the type duplication below the following two lines do not
|
|
267
279
|
// work as they do not narrow the type when checking for the boolean values
|
|
268
280
|
// truthiness
|
|
269
|
-
// |
|
|
270
|
-
// |
|
|
281
|
+
// | ResultOk<TOk>
|
|
282
|
+
// | ResultFail<TOk>;
|
|
271
283
|
| {
|
|
272
284
|
status: _Response["status"];
|
|
273
285
|
msg: _Response["statusText"];
|
|
274
286
|
ok: true;
|
|
275
287
|
fail?: false;
|
|
276
|
-
data:
|
|
288
|
+
data: TResponseOk;
|
|
277
289
|
}
|
|
278
290
|
| {
|
|
279
291
|
status: _Response["status"];
|
|
280
292
|
msg: _Response["statusText"];
|
|
281
293
|
ok?: false;
|
|
282
294
|
fail: true;
|
|
283
|
-
data:
|
|
295
|
+
data: TResponseFail;
|
|
284
296
|
};
|
|
285
297
|
|
|
286
298
|
type ResponseAdapter = <
|
|
287
|
-
|
|
288
|
-
|
|
299
|
+
TResponseOk extends ResponseOk = ResponseOk,
|
|
300
|
+
TResponseFail extends ResponseFailed = ResponseFailed
|
|
289
301
|
>(
|
|
290
302
|
response: _Response,
|
|
291
303
|
options: TOptions
|
|
292
|
-
) => Promise<Koine.Api.
|
|
304
|
+
) => Promise<Koine.Api.Result<TResponseOk, TResponseFail>>;
|
|
293
305
|
|
|
294
306
|
//////////////////////////////////////////////////////////////////////////////
|
|
295
307
|
//
|
|
@@ -348,7 +360,7 @@ declare namespace Koine.Api {
|
|
|
348
360
|
*/
|
|
349
361
|
type GenerateResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
350
362
|
[TMethod in RequestMethod]: {
|
|
351
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["
|
|
363
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["ok"];
|
|
352
364
|
};
|
|
353
365
|
};
|
|
354
366
|
|
|
@@ -363,7 +375,7 @@ declare namespace Koine.Api {
|
|
|
363
375
|
* ```
|
|
364
376
|
*/
|
|
365
377
|
type GenerateGetShortcuts<TEndpoints extends Endpoints> = {
|
|
366
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["
|
|
378
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["ok"];
|
|
367
379
|
};
|
|
368
380
|
|
|
369
381
|
/**
|
|
@@ -377,12 +389,12 @@ declare namespace Koine.Api {
|
|
|
377
389
|
* ```
|
|
378
390
|
*/
|
|
379
391
|
type GeneratePostShortcuts<TEndpoints extends Endpoints> = {
|
|
380
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["
|
|
392
|
+
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["ok"];
|
|
381
393
|
};
|
|
382
394
|
|
|
383
395
|
/**
|
|
384
396
|
* This is not useful as it is the same as doing
|
|
385
|
-
* `API.Endpoints["my/endpoint"]["GET"]["
|
|
397
|
+
* `API.Endpoints["my/endpoint"]["GET"]["ok"];`
|
|
386
398
|
*
|
|
387
399
|
* @example
|
|
388
400
|
* ```ts
|
|
@@ -390,7 +402,7 @@ declare namespace Koine.Api {
|
|
|
390
402
|
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
391
403
|
*
|
|
392
404
|
* // consume the type wherever in your app:
|
|
393
|
-
* type MyData = API.$["my/endpoint"]["get"]["
|
|
405
|
+
* type MyData = API.$["my/endpoint"]["get"]["ok"];
|
|
394
406
|
* ```
|
|
395
407
|
* @deprecated
|
|
396
408
|
*/
|