@koine/api 1.0.56 → 1.0.59
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/createApi.js +28 -20
- package/createSwrApi.d.ts +21 -2
- package/createSwrApi.js +5 -2
- package/node/createApi.js +28 -20
- package/node/createSwrApi.js +5 -2
- package/package.json +2 -2
- package/typings.d.ts +4 -1
package/createApi.js
CHANGED
|
@@ -17,14 +17,22 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
17
17
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
18
18
|
// @ts-expect-error FIXME: type
|
|
19
19
|
api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
20
|
-
var _a, _b, request, _c, headers, _d, timeout,
|
|
21
|
-
return __generator(this, function (
|
|
22
|
-
switch (
|
|
20
|
+
var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, exception, _g, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, e_1, result, e_2, msg;
|
|
21
|
+
return __generator(this, function (_h) {
|
|
22
|
+
switch (_h.label) {
|
|
23
23
|
case 0:
|
|
24
|
-
_a = options || {}, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? headersBase : _c, _d = _a.timeout, timeout = _d === void 0 ? timeoutBase : _d,
|
|
25
|
-
|
|
24
|
+
_a = options || {}, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? headersBase : _c, _d = _a.timeout, timeout = _d === void 0 ? timeoutBase : _d, processReq = _a.processReq, _e = _a.processRes, processRes = _e === void 0 ? processResBase : _e, _f = _a.exception, exception = _f === void 0 ? exceptionBase : _f;
|
|
25
|
+
_g = options || {}, params = _g.params, json = _g.json, query = _g.query;
|
|
26
26
|
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
27
27
|
requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
|
|
28
|
+
if (processReqBase) {
|
|
29
|
+
transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
30
|
+
url = transformed[0];
|
|
31
|
+
query = transformed[1];
|
|
32
|
+
json = transformed[2];
|
|
33
|
+
params = transformed[3];
|
|
34
|
+
requestInit = transformed[4];
|
|
35
|
+
}
|
|
28
36
|
if (processReq) {
|
|
29
37
|
transformed = processReq(method, url, query, json, params, requestInit);
|
|
30
38
|
url = transformed[0];
|
|
@@ -52,42 +60,42 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
52
60
|
url += buildUrlQueryString(query);
|
|
53
61
|
}
|
|
54
62
|
if (!exception) return [3 /*break*/, 5];
|
|
55
|
-
|
|
63
|
+
_h.label = 1;
|
|
56
64
|
case 1:
|
|
57
|
-
|
|
65
|
+
_h.trys.push([1, 3, , 4]);
|
|
58
66
|
return [4 /*yield*/, fetch(url, requestInit)];
|
|
59
67
|
case 2:
|
|
60
|
-
response =
|
|
68
|
+
response = _h.sent();
|
|
61
69
|
return [3 /*break*/, 4];
|
|
62
70
|
case 3:
|
|
63
|
-
e_1 =
|
|
71
|
+
e_1 = _h.sent();
|
|
64
72
|
// eslint-disable-next-line no-throw-literal
|
|
65
73
|
throw { e: e_1 };
|
|
66
74
|
case 4: return [3 /*break*/, 7];
|
|
67
75
|
case 5: return [4 /*yield*/, fetch(url, requestInit)];
|
|
68
76
|
case 6:
|
|
69
|
-
response =
|
|
70
|
-
|
|
77
|
+
response = _h.sent();
|
|
78
|
+
_h.label = 7;
|
|
71
79
|
case 7:
|
|
72
80
|
if (timeoutId) {
|
|
73
81
|
clearTimeout(timeoutId);
|
|
74
82
|
}
|
|
75
83
|
if (!exception) return [3 /*break*/, 15];
|
|
76
|
-
|
|
84
|
+
_h.label = 8;
|
|
77
85
|
case 8:
|
|
78
|
-
|
|
86
|
+
_h.trys.push([8, 13, , 14]);
|
|
79
87
|
if (!processRes) return [3 /*break*/, 10];
|
|
80
88
|
return [4 /*yield*/, processRes(response, options || {})];
|
|
81
89
|
case 9:
|
|
82
|
-
result =
|
|
90
|
+
result = _h.sent();
|
|
83
91
|
return [3 /*break*/, 12];
|
|
84
92
|
case 10: return [4 /*yield*/, response.json()];
|
|
85
93
|
case 11:
|
|
86
|
-
result =
|
|
87
|
-
|
|
94
|
+
result = _h.sent();
|
|
95
|
+
_h.label = 12;
|
|
88
96
|
case 12: return [3 /*break*/, 14];
|
|
89
97
|
case 13:
|
|
90
|
-
e_2 =
|
|
98
|
+
e_2 = _h.sent();
|
|
91
99
|
// eslint-disable-next-line no-throw-literal
|
|
92
100
|
throw { e: e_2 };
|
|
93
101
|
case 14: return [3 /*break*/, 19];
|
|
@@ -95,12 +103,12 @@ export var createApi = function (apiName, baseUrl, options) {
|
|
|
95
103
|
if (!processRes) return [3 /*break*/, 17];
|
|
96
104
|
return [4 /*yield*/, processRes(response, options || {})];
|
|
97
105
|
case 16:
|
|
98
|
-
result =
|
|
106
|
+
result = _h.sent();
|
|
99
107
|
return [3 /*break*/, 19];
|
|
100
108
|
case 17: return [4 /*yield*/, response.json()];
|
|
101
109
|
case 18:
|
|
102
|
-
result =
|
|
103
|
-
|
|
110
|
+
result = _h.sent();
|
|
111
|
+
_h.label = 19;
|
|
104
112
|
case 19:
|
|
105
113
|
if (exception && result.fail) {
|
|
106
114
|
// throw new ApiError<Failed>(result);
|
package/createSwrApi.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
-
import { type SWRConfiguration, type SWRResponse } from "swr";
|
|
1
|
+
import { type SWRConfiguration, type SWRResponse, type BareFetcher } from "swr";
|
|
2
2
|
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
3
|
-
declare type
|
|
3
|
+
declare type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
4
|
+
/**
|
|
5
|
+
* Conditional fetching as option
|
|
6
|
+
*
|
|
7
|
+
* Moving this to an option allows us to keep the endpoints typed dictionary,
|
|
8
|
+
* e.g. we can write:
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const { data, mutate } = myApi.useGet("User/{id}",
|
|
12
|
+
* { params: { id: aVariableMaybeContainingAnId || "" }, },
|
|
13
|
+
* { when: !!aVariableMaybeContainingAnId }
|
|
14
|
+
* );
|
|
15
|
+
*
|
|
16
|
+
* // we still have typed `data`, `mutate`
|
|
17
|
+
* ```
|
|
18
|
+
* @see https://swr.vercel.app/docs/conditional-fetching
|
|
19
|
+
*/
|
|
20
|
+
when?: boolean | (() => boolean);
|
|
21
|
+
};
|
|
22
|
+
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.EndpointOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfigurationExtended<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationConfiguration<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>) => THookName extends "useGet" ? SWRResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>;
|
|
4
23
|
/**
|
|
5
24
|
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
6
25
|
*/
|
package/createSwrApi.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { __assign, __awaiter, __generator } from "tslib";
|
|
2
|
+
import { isFunction } from "@koine/utils";
|
|
2
3
|
import useSWR from "swr";
|
|
3
4
|
import useSWRMutation from "swr/mutation";
|
|
4
5
|
import { createApi } from "./createApi";
|
|
@@ -33,9 +34,11 @@ function createUseApi(api, method) {
|
|
|
33
34
|
});
|
|
34
35
|
}); };
|
|
35
36
|
var config_1 = _config;
|
|
36
|
-
|
|
37
|
+
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
38
|
+
(isFunction(config_1 === null || config_1 === void 0 ? void 0 : config_1.when) && (config_1 === null || config_1 === void 0 ? void 0 : config_1.when()) === false);
|
|
39
|
+
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
37
40
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
38
|
-
return useSWR(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
41
|
+
return useSWR(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
39
42
|
}
|
|
40
43
|
var config = _config;
|
|
41
44
|
var sender = function (
|
package/node/createApi.js
CHANGED
|
@@ -20,14 +20,22 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
20
20
|
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
21
21
|
// @ts-expect-error FIXME: type
|
|
22
22
|
api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
23
|
-
var _a, _b, request, _c, headers, _d, timeout,
|
|
24
|
-
return tslib_1.__generator(this, function (
|
|
25
|
-
switch (
|
|
23
|
+
var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, exception, _g, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, e_1, result, e_2, msg;
|
|
24
|
+
return tslib_1.__generator(this, function (_h) {
|
|
25
|
+
switch (_h.label) {
|
|
26
26
|
case 0:
|
|
27
|
-
_a = options || {}, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? headersBase : _c, _d = _a.timeout, timeout = _d === void 0 ? timeoutBase : _d,
|
|
28
|
-
|
|
27
|
+
_a = options || {}, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? headersBase : _c, _d = _a.timeout, timeout = _d === void 0 ? timeoutBase : _d, processReq = _a.processReq, _e = _a.processRes, processRes = _e === void 0 ? processResBase : _e, _f = _a.exception, exception = _f === void 0 ? exceptionBase : _f;
|
|
28
|
+
_g = options || {}, params = _g.params, json = _g.json, query = _g.query;
|
|
29
29
|
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
30
30
|
requestInit = tslib_1.__assign(tslib_1.__assign({ method: method.toUpperCase() }, request), { headers: tslib_1.__assign({ "content-type": "application/json" }, headers) });
|
|
31
|
+
if (processReqBase) {
|
|
32
|
+
transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
33
|
+
url = transformed[0];
|
|
34
|
+
query = transformed[1];
|
|
35
|
+
json = transformed[2];
|
|
36
|
+
params = transformed[3];
|
|
37
|
+
requestInit = transformed[4];
|
|
38
|
+
}
|
|
31
39
|
if (processReq) {
|
|
32
40
|
transformed = processReq(method, url, query, json, params, requestInit);
|
|
33
41
|
url = transformed[0];
|
|
@@ -55,42 +63,42 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
55
63
|
url += (0, utils_1.buildUrlQueryString)(query);
|
|
56
64
|
}
|
|
57
65
|
if (!exception) return [3 /*break*/, 5];
|
|
58
|
-
|
|
66
|
+
_h.label = 1;
|
|
59
67
|
case 1:
|
|
60
|
-
|
|
68
|
+
_h.trys.push([1, 3, , 4]);
|
|
61
69
|
return [4 /*yield*/, fetch(url, requestInit)];
|
|
62
70
|
case 2:
|
|
63
|
-
response =
|
|
71
|
+
response = _h.sent();
|
|
64
72
|
return [3 /*break*/, 4];
|
|
65
73
|
case 3:
|
|
66
|
-
e_1 =
|
|
74
|
+
e_1 = _h.sent();
|
|
67
75
|
// eslint-disable-next-line no-throw-literal
|
|
68
76
|
throw { e: e_1 };
|
|
69
77
|
case 4: return [3 /*break*/, 7];
|
|
70
78
|
case 5: return [4 /*yield*/, fetch(url, requestInit)];
|
|
71
79
|
case 6:
|
|
72
|
-
response =
|
|
73
|
-
|
|
80
|
+
response = _h.sent();
|
|
81
|
+
_h.label = 7;
|
|
74
82
|
case 7:
|
|
75
83
|
if (timeoutId) {
|
|
76
84
|
clearTimeout(timeoutId);
|
|
77
85
|
}
|
|
78
86
|
if (!exception) return [3 /*break*/, 15];
|
|
79
|
-
|
|
87
|
+
_h.label = 8;
|
|
80
88
|
case 8:
|
|
81
|
-
|
|
89
|
+
_h.trys.push([8, 13, , 14]);
|
|
82
90
|
if (!processRes) return [3 /*break*/, 10];
|
|
83
91
|
return [4 /*yield*/, processRes(response, options || {})];
|
|
84
92
|
case 9:
|
|
85
|
-
result =
|
|
93
|
+
result = _h.sent();
|
|
86
94
|
return [3 /*break*/, 12];
|
|
87
95
|
case 10: return [4 /*yield*/, response.json()];
|
|
88
96
|
case 11:
|
|
89
|
-
result =
|
|
90
|
-
|
|
97
|
+
result = _h.sent();
|
|
98
|
+
_h.label = 12;
|
|
91
99
|
case 12: return [3 /*break*/, 14];
|
|
92
100
|
case 13:
|
|
93
|
-
e_2 =
|
|
101
|
+
e_2 = _h.sent();
|
|
94
102
|
// eslint-disable-next-line no-throw-literal
|
|
95
103
|
throw { e: e_2 };
|
|
96
104
|
case 14: return [3 /*break*/, 19];
|
|
@@ -98,12 +106,12 @@ var createApi = function (apiName, baseUrl, options) {
|
|
|
98
106
|
if (!processRes) return [3 /*break*/, 17];
|
|
99
107
|
return [4 /*yield*/, processRes(response, options || {})];
|
|
100
108
|
case 16:
|
|
101
|
-
result =
|
|
109
|
+
result = _h.sent();
|
|
102
110
|
return [3 /*break*/, 19];
|
|
103
111
|
case 17: return [4 /*yield*/, response.json()];
|
|
104
112
|
case 18:
|
|
105
|
-
result =
|
|
106
|
-
|
|
113
|
+
result = _h.sent();
|
|
114
|
+
_h.label = 19;
|
|
107
115
|
case 19:
|
|
108
116
|
if (exception && result.fail) {
|
|
109
117
|
// throw new ApiError<Failed>(result);
|
package/node/createSwrApi.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSwrApi = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
+
var utils_1 = require("@koine/utils");
|
|
5
6
|
var swr_1 = require("swr");
|
|
6
7
|
var mutation_1 = require("swr/mutation");
|
|
7
8
|
var createApi_1 = require("./createApi");
|
|
@@ -36,9 +37,11 @@ function createUseApi(api, method) {
|
|
|
36
37
|
});
|
|
37
38
|
}); };
|
|
38
39
|
var config_1 = _config;
|
|
39
|
-
|
|
40
|
+
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
41
|
+
((0, utils_1.isFunction)(config_1 === null || config_1 === void 0 ? void 0 : config_1.when) && (config_1 === null || config_1 === void 0 ? void 0 : config_1.when()) === false);
|
|
42
|
+
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
40
43
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
41
|
-
return (0, swr_1.default)(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
44
|
+
return (0, swr_1.default)(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
42
45
|
}
|
|
43
46
|
var config = _config;
|
|
44
47
|
var sender = function (
|
package/package.json
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
"typings": "./index.d.ts",
|
|
6
6
|
"dependencies": {},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@koine/utils": "1.0.
|
|
8
|
+
"@koine/utils": "1.0.59",
|
|
9
9
|
"swr": "^2.0.0-beta.6",
|
|
10
10
|
"next": "^12.2.0",
|
|
11
11
|
"tslib": "^2.4.0"
|
|
12
12
|
},
|
|
13
|
-
"version": "1.0.
|
|
13
|
+
"version": "1.0.59",
|
|
14
14
|
"module": "./index.js",
|
|
15
15
|
"types": "./index.d.ts"
|
|
16
16
|
}
|
package/typings.d.ts
CHANGED
|
@@ -347,7 +347,10 @@ declare namespace Koine.Api {
|
|
|
347
347
|
|
|
348
348
|
/**
|
|
349
349
|
* The request processor at the request level, this is meant to apply
|
|
350
|
-
* transformations to a single endpoint request
|
|
350
|
+
* transformations to a single endpoint request. Request processor applied at
|
|
351
|
+
* the whole client level is still applied just before this one, hence one
|
|
352
|
+
* might set some global processing and override it or undo it at the single
|
|
353
|
+
* request level.
|
|
351
354
|
*/
|
|
352
355
|
type EndpointRequestProcessor<
|
|
353
356
|
TEndpoints extends Endpoints,
|