@koine/api 2.0.0-beta.2 → 2.0.0-beta.200
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/ApiError.cjs.default.js +1 -0
- package/ApiError.cjs.js +8 -0
- package/ApiError.cjs.mjs +2 -0
- package/ApiError.d.ts +3 -2
- package/ApiError.esm.js +3 -0
- package/README.md +0 -0
- package/createApi.cjs.default.js +1 -0
- package/createApi.cjs.js +10 -0
- package/createApi.cjs.mjs +2 -0
- package/createApi.d.ts +3 -2
- package/createApi.esm.js +5 -0
- package/createApiResultFail.cjs.default.js +1 -0
- package/createApiResultFail.cjs.js +8 -0
- package/createApiResultFail.cjs.mjs +2 -0
- package/createApiResultFail.d.ts +3 -0
- package/createApiResultFail.esm.js +3 -0
- package/createApiResultOk.cjs.default.js +1 -0
- package/createApiResultOk.cjs.js +8 -0
- package/createApiResultOk.cjs.mjs +2 -0
- package/createApiResultOk.d.ts +3 -0
- package/createApiResultOk.esm.js +3 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +13 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +3 -2
- package/index.esm.js +4 -0
- package/next/nextApiResponse.cjs.js +12 -0
- package/next/nextApiResponse.d.ts +6 -0
- package/next/nextApiResponse.esm.js +7 -0
- package/next/nextApiResponse12.cjs.js +11 -0
- package/next/nextApiResponse12.d.ts +6 -0
- package/next/nextApiResponse12.esm.js +6 -0
- package/next.cjs.default.js +1 -0
- package/next.cjs.js +9 -0
- package/next.cjs.mjs +2 -0
- package/next.d.ts +2 -0
- package/next.esm.js +2 -0
- package/package.json +84 -13
- package/swr/createSwrApi.cjs.js +13 -0
- package/swr/createSwrApi.d.ts +41 -0
- package/swr/createSwrApi.esm.js +7 -0
- package/swr-mutation/createSwrMutationApi.cjs.js +12 -0
- package/swr-mutation/createSwrMutationApi.d.ts +19 -0
- package/swr-mutation/createSwrMutationApi.esm.js +7 -0
- package/swr-mutation.cjs.default.js +1 -0
- package/swr-mutation.cjs.js +7 -0
- package/swr-mutation.cjs.mjs +2 -0
- package/swr-mutation.d.ts +1 -0
- package/swr-mutation.esm.js +1 -0
- package/swr.cjs.default.js +1 -0
- package/swr.cjs.js +7 -0
- package/swr.cjs.mjs +2 -0
- package/swr.d.ts +1 -0
- package/swr.esm.js +1 -0
- package/types.cjs.default.js +1 -0
- package/types.cjs.js +2 -0
- package/types.cjs.mjs +2 -0
- package/types.d.ts +313 -0
- package/types.esm.js +1 -0
- package/typings.d.ts +11 -508
- package/vite.config.d.ts +2 -0
- package/ApiError.js +0 -23
- package/ApiError.mjs +0 -20
- package/createApi.js +0 -148
- package/createApi.mjs +0 -144
- package/createSwrApi.d.ts +0 -33
- package/createSwrApi.js +0 -91
- package/createSwrApi.mjs +0 -87
- package/index.js +0 -11
- package/index.mjs +0 -4
- package/nextApiResponse.d.ts +0 -3
- package/nextApiResponse.js +0 -9
- package/nextApiResponse.mjs +0 -5
package/createApi.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createApi = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var buildUrlQueryString_1 = require("@koine/utils/buildUrlQueryString");
|
|
6
|
-
var isFullObject_1 = require("@koine/utils/isFullObject");
|
|
7
|
-
var errorToString_1 = require("@koine/utils/errorToString");
|
|
8
|
-
/**
|
|
9
|
-
* Create api client
|
|
10
|
-
*
|
|
11
|
-
* @param apiName Short name to use in debug logs
|
|
12
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
13
|
-
*/
|
|
14
|
-
var createApi = function (apiName, baseUrl, options) {
|
|
15
|
-
var _a = options || {}, _b = _a.headers, headersBase = _b === void 0 ? {} : _b, _c = _a.request, requestBase = _c === void 0 ? {
|
|
16
|
-
referrerPolicy: "no-referrer",
|
|
17
|
-
// credentials: "include",
|
|
18
|
-
// mode: "cors",
|
|
19
|
-
// redirect: "follow",
|
|
20
|
-
// cache: "no-cache",
|
|
21
|
-
} : _c, throwErrBase = _a.throwErr, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes, processErrBase = _a.processErr;
|
|
22
|
-
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
23
|
-
// @ts-expect-error FIXME: type
|
|
24
|
-
api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
25
|
-
var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, processErr, _g, throwErr, _h, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, result, msg, e_1, e_2, logMsg;
|
|
26
|
-
return tslib_1.__generator(this, function (_j) {
|
|
27
|
-
switch (_j.label) {
|
|
28
|
-
case 0:
|
|
29
|
-
_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.processErr, processErr = _f === void 0 ? processErrBase : _f, _g = _a.throwErr, throwErr = _g === void 0 ? throwErrBase : _g;
|
|
30
|
-
_h = options || {}, params = _h.params, json = _h.json, query = _h.query;
|
|
31
|
-
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
32
|
-
requestInit = tslib_1.__assign(tslib_1.__assign({ method: method.toUpperCase() }, request), { headers: tslib_1.__assign({ "content-type": "application/json" }, headers) });
|
|
33
|
-
if (processReqBase) {
|
|
34
|
-
transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
35
|
-
url = transformed[0];
|
|
36
|
-
query = transformed[1];
|
|
37
|
-
json = transformed[2];
|
|
38
|
-
params = transformed[3];
|
|
39
|
-
requestInit = transformed[4];
|
|
40
|
-
}
|
|
41
|
-
if (processReq) {
|
|
42
|
-
transformed = processReq(method, url, query, json, params, requestInit);
|
|
43
|
-
url = transformed[0];
|
|
44
|
-
query = transformed[1];
|
|
45
|
-
json = transformed[2];
|
|
46
|
-
params = transformed[3];
|
|
47
|
-
requestInit = transformed[4];
|
|
48
|
-
}
|
|
49
|
-
if ((0, isFullObject_1.default)(params)) {
|
|
50
|
-
for (key in params) {
|
|
51
|
-
url = url.replace("{".concat(key, "}"), params[key].toString());
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
timeoutNumber = Number(timeout);
|
|
55
|
-
if (json) {
|
|
56
|
-
requestInit.body = JSON.stringify(json);
|
|
57
|
-
}
|
|
58
|
-
if (timeoutNumber > 0) {
|
|
59
|
-
controller = new AbortController();
|
|
60
|
-
timeoutId = setTimeout(function () { return controller.abort(); }, timeoutNumber);
|
|
61
|
-
requestInit.signal = controller.signal;
|
|
62
|
-
}
|
|
63
|
-
if (query) {
|
|
64
|
-
// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
65
|
-
url += (0, buildUrlQueryString_1.default)(query);
|
|
66
|
-
}
|
|
67
|
-
response = null;
|
|
68
|
-
result = null;
|
|
69
|
-
msg = "";
|
|
70
|
-
_j.label = 1;
|
|
71
|
-
case 1:
|
|
72
|
-
_j.trys.push([1, 3, , 4]);
|
|
73
|
-
return [4 /*yield*/, fetch(url, requestInit)];
|
|
74
|
-
case 2:
|
|
75
|
-
response = _j.sent();
|
|
76
|
-
return [3 /*break*/, 4];
|
|
77
|
-
case 3:
|
|
78
|
-
e_1 = _j.sent();
|
|
79
|
-
msg = (0, errorToString_1.default)(e_1);
|
|
80
|
-
return [3 /*break*/, 4];
|
|
81
|
-
case 4:
|
|
82
|
-
if (timeoutId) {
|
|
83
|
-
clearTimeout(timeoutId);
|
|
84
|
-
}
|
|
85
|
-
if (!response) return [3 /*break*/, 11];
|
|
86
|
-
_j.label = 5;
|
|
87
|
-
case 5:
|
|
88
|
-
_j.trys.push([5, 10, , 11]);
|
|
89
|
-
if (!processRes) return [3 /*break*/, 7];
|
|
90
|
-
return [4 /*yield*/, processRes(response, options || {})];
|
|
91
|
-
case 6:
|
|
92
|
-
result = _j.sent();
|
|
93
|
-
return [3 /*break*/, 9];
|
|
94
|
-
case 7: return [4 /*yield*/, response.json()];
|
|
95
|
-
case 8:
|
|
96
|
-
result = _j.sent();
|
|
97
|
-
_j.label = 9;
|
|
98
|
-
case 9: return [3 /*break*/, 11];
|
|
99
|
-
case 10:
|
|
100
|
-
e_2 = _j.sent();
|
|
101
|
-
msg = (0, errorToString_1.default)(e_2);
|
|
102
|
-
return [3 /*break*/, 11];
|
|
103
|
-
case 11:
|
|
104
|
-
if (!(result === null)) return [3 /*break*/, 14];
|
|
105
|
-
if (!processErr) return [3 /*break*/, 13];
|
|
106
|
-
return [4 /*yield*/, processErr(msg, options || {})];
|
|
107
|
-
case 12:
|
|
108
|
-
result = _j.sent();
|
|
109
|
-
return [3 /*break*/, 14];
|
|
110
|
-
case 13:
|
|
111
|
-
// this error should only happen on network errors or wrong API urls
|
|
112
|
-
// there is no specific HTTP error for this, we can consider these
|
|
113
|
-
// two statuses though:
|
|
114
|
-
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
115
|
-
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
116
|
-
result = {
|
|
117
|
-
data: null,
|
|
118
|
-
msg: msg,
|
|
119
|
-
status: 100,
|
|
120
|
-
fail: true,
|
|
121
|
-
ok: false,
|
|
122
|
-
};
|
|
123
|
-
_j.label = 14;
|
|
124
|
-
case 14:
|
|
125
|
-
if (throwErr && (result === null || result === void 0 ? void 0 : result.fail)) {
|
|
126
|
-
// throw new ApiError<Failed>(result);
|
|
127
|
-
// I prefer to throw an object literal despite what eslint says
|
|
128
|
-
// eslint-disable-next-line no-throw-literal
|
|
129
|
-
throw result;
|
|
130
|
-
}
|
|
131
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
132
|
-
logMsg = "".concat(result === null || result === void 0 ? void 0 : result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
133
|
-
if (result === null || result === void 0 ? void 0 : result.ok) {
|
|
134
|
-
console.info("\uD83D\uDFE2 ".concat(logMsg));
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
console.info("\uD83D\uDD34 ".concat(logMsg));
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return [2 /*return*/, result];
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}); };
|
|
144
|
-
return api;
|
|
145
|
-
}, {});
|
|
146
|
-
};
|
|
147
|
-
exports.createApi = createApi;
|
|
148
|
-
exports.default = exports.createApi;
|
package/createApi.mjs
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator } from "tslib";
|
|
2
|
-
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
3
|
-
import isFullObject from "@koine/utils/isFullObject";
|
|
4
|
-
import errorToString from "@koine/utils/errorToString";
|
|
5
|
-
/**
|
|
6
|
-
* Create api client
|
|
7
|
-
*
|
|
8
|
-
* @param apiName Short name to use in debug logs
|
|
9
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
10
|
-
*/
|
|
11
|
-
export var createApi = function (apiName, baseUrl, options) {
|
|
12
|
-
var _a = options || {}, _b = _a.headers, headersBase = _b === void 0 ? {} : _b, _c = _a.request, requestBase = _c === void 0 ? {
|
|
13
|
-
referrerPolicy: "no-referrer",
|
|
14
|
-
// credentials: "include",
|
|
15
|
-
// mode: "cors",
|
|
16
|
-
// redirect: "follow",
|
|
17
|
-
// cache: "no-cache",
|
|
18
|
-
} : _c, throwErrBase = _a.throwErr, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes, processErrBase = _a.processErr;
|
|
19
|
-
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
20
|
-
// @ts-expect-error FIXME: type
|
|
21
|
-
api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
22
|
-
var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, processErr, _g, throwErr, _h, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, result, msg, e_1, e_2, logMsg;
|
|
23
|
-
return __generator(this, function (_j) {
|
|
24
|
-
switch (_j.label) {
|
|
25
|
-
case 0:
|
|
26
|
-
_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.processErr, processErr = _f === void 0 ? processErrBase : _f, _g = _a.throwErr, throwErr = _g === void 0 ? throwErrBase : _g;
|
|
27
|
-
_h = options || {}, params = _h.params, json = _h.json, query = _h.query;
|
|
28
|
-
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
29
|
-
requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
|
|
30
|
-
if (processReqBase) {
|
|
31
|
-
transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
32
|
-
url = transformed[0];
|
|
33
|
-
query = transformed[1];
|
|
34
|
-
json = transformed[2];
|
|
35
|
-
params = transformed[3];
|
|
36
|
-
requestInit = transformed[4];
|
|
37
|
-
}
|
|
38
|
-
if (processReq) {
|
|
39
|
-
transformed = processReq(method, url, query, json, params, requestInit);
|
|
40
|
-
url = transformed[0];
|
|
41
|
-
query = transformed[1];
|
|
42
|
-
json = transformed[2];
|
|
43
|
-
params = transformed[3];
|
|
44
|
-
requestInit = transformed[4];
|
|
45
|
-
}
|
|
46
|
-
if (isFullObject(params)) {
|
|
47
|
-
for (key in params) {
|
|
48
|
-
url = url.replace("{".concat(key, "}"), params[key].toString());
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
timeoutNumber = Number(timeout);
|
|
52
|
-
if (json) {
|
|
53
|
-
requestInit.body = JSON.stringify(json);
|
|
54
|
-
}
|
|
55
|
-
if (timeoutNumber > 0) {
|
|
56
|
-
controller = new AbortController();
|
|
57
|
-
timeoutId = setTimeout(function () { return controller.abort(); }, timeoutNumber);
|
|
58
|
-
requestInit.signal = controller.signal;
|
|
59
|
-
}
|
|
60
|
-
if (query) {
|
|
61
|
-
// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
62
|
-
url += buildUrlQueryString(query);
|
|
63
|
-
}
|
|
64
|
-
response = null;
|
|
65
|
-
result = null;
|
|
66
|
-
msg = "";
|
|
67
|
-
_j.label = 1;
|
|
68
|
-
case 1:
|
|
69
|
-
_j.trys.push([1, 3, , 4]);
|
|
70
|
-
return [4 /*yield*/, fetch(url, requestInit)];
|
|
71
|
-
case 2:
|
|
72
|
-
response = _j.sent();
|
|
73
|
-
return [3 /*break*/, 4];
|
|
74
|
-
case 3:
|
|
75
|
-
e_1 = _j.sent();
|
|
76
|
-
msg = errorToString(e_1);
|
|
77
|
-
return [3 /*break*/, 4];
|
|
78
|
-
case 4:
|
|
79
|
-
if (timeoutId) {
|
|
80
|
-
clearTimeout(timeoutId);
|
|
81
|
-
}
|
|
82
|
-
if (!response) return [3 /*break*/, 11];
|
|
83
|
-
_j.label = 5;
|
|
84
|
-
case 5:
|
|
85
|
-
_j.trys.push([5, 10, , 11]);
|
|
86
|
-
if (!processRes) return [3 /*break*/, 7];
|
|
87
|
-
return [4 /*yield*/, processRes(response, options || {})];
|
|
88
|
-
case 6:
|
|
89
|
-
result = _j.sent();
|
|
90
|
-
return [3 /*break*/, 9];
|
|
91
|
-
case 7: return [4 /*yield*/, response.json()];
|
|
92
|
-
case 8:
|
|
93
|
-
result = _j.sent();
|
|
94
|
-
_j.label = 9;
|
|
95
|
-
case 9: return [3 /*break*/, 11];
|
|
96
|
-
case 10:
|
|
97
|
-
e_2 = _j.sent();
|
|
98
|
-
msg = errorToString(e_2);
|
|
99
|
-
return [3 /*break*/, 11];
|
|
100
|
-
case 11:
|
|
101
|
-
if (!(result === null)) return [3 /*break*/, 14];
|
|
102
|
-
if (!processErr) return [3 /*break*/, 13];
|
|
103
|
-
return [4 /*yield*/, processErr(msg, options || {})];
|
|
104
|
-
case 12:
|
|
105
|
-
result = _j.sent();
|
|
106
|
-
return [3 /*break*/, 14];
|
|
107
|
-
case 13:
|
|
108
|
-
// this error should only happen on network errors or wrong API urls
|
|
109
|
-
// there is no specific HTTP error for this, we can consider these
|
|
110
|
-
// two statuses though:
|
|
111
|
-
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
112
|
-
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
113
|
-
result = {
|
|
114
|
-
data: null,
|
|
115
|
-
msg: msg,
|
|
116
|
-
status: 100,
|
|
117
|
-
fail: true,
|
|
118
|
-
ok: false,
|
|
119
|
-
};
|
|
120
|
-
_j.label = 14;
|
|
121
|
-
case 14:
|
|
122
|
-
if (throwErr && (result === null || result === void 0 ? void 0 : result.fail)) {
|
|
123
|
-
// throw new ApiError<Failed>(result);
|
|
124
|
-
// I prefer to throw an object literal despite what eslint says
|
|
125
|
-
// eslint-disable-next-line no-throw-literal
|
|
126
|
-
throw result;
|
|
127
|
-
}
|
|
128
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
129
|
-
logMsg = "".concat(result === null || result === void 0 ? void 0 : result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
130
|
-
if (result === null || result === void 0 ? void 0 : result.ok) {
|
|
131
|
-
console.info("\uD83D\uDFE2 ".concat(logMsg));
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
console.info("\uD83D\uDD34 ".concat(logMsg));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return [2 /*return*/, result];
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}); };
|
|
141
|
-
return api;
|
|
142
|
-
}, {});
|
|
143
|
-
};
|
|
144
|
-
export default createApi;
|
package/createSwrApi.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type SWRConfiguration, type SWRResponse, type BareFetcher } from "swr";
|
|
2
|
-
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
3
|
-
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
|
-
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>;
|
|
23
|
-
/**
|
|
24
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
25
|
-
*/
|
|
26
|
-
export declare const createSwrApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions | undefined) => Koine.Api.Client<TEndpoints> & {
|
|
27
|
-
useGet: KoineApiMethodHookSWR<"useGet", TEndpoints>;
|
|
28
|
-
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
29
|
-
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
30
|
-
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
31
|
-
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
32
|
-
};
|
|
33
|
-
export default createSwrApi;
|
package/createSwrApi.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
"use client";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.createSwrApi = void 0;
|
|
5
|
-
var tslib_1 = require("tslib");
|
|
6
|
-
var isFunction_1 = require("@koine/utils/isFunction");
|
|
7
|
-
var swr_1 = require("swr");
|
|
8
|
-
var mutation_1 = require("swr/mutation");
|
|
9
|
-
var createApi_1 = require("./createApi");
|
|
10
|
-
function createUseApi(api, method) {
|
|
11
|
-
return function useApi(endpoint, options, _config) {
|
|
12
|
-
var _this = this;
|
|
13
|
-
if (method === "get") {
|
|
14
|
-
// const fetcher = async (_endpoint: TEndpoint) => {
|
|
15
|
-
// try {
|
|
16
|
-
// const { ok, data } = await api[method](_endpoint, {
|
|
17
|
-
// ...(options || {}),
|
|
18
|
-
// throwErr: true,
|
|
19
|
-
// });
|
|
20
|
-
// if (ok) {
|
|
21
|
-
// return data;
|
|
22
|
-
// }
|
|
23
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
24
|
-
// } catch(e) {
|
|
25
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
26
|
-
// }
|
|
27
|
-
// };
|
|
28
|
-
// }
|
|
29
|
-
var fetcher = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
30
|
-
var data;
|
|
31
|
-
return tslib_1.__generator(this, function (_a) {
|
|
32
|
-
switch (_a.label) {
|
|
33
|
-
case 0: return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign({}, (options || {})), { throwErr: true }))];
|
|
34
|
-
case 1:
|
|
35
|
-
data = (_a.sent()).data;
|
|
36
|
-
return [2 /*return*/, data];
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}); };
|
|
40
|
-
var config_1 = _config;
|
|
41
|
-
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
42
|
-
((0, isFunction_1.default)(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);
|
|
43
|
-
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
44
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
45
|
-
return (0, swr_1.default)(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
46
|
-
}
|
|
47
|
-
var config = _config;
|
|
48
|
-
var sender = function (
|
|
49
|
-
// if the first argument is an array the second tem are the base options
|
|
50
|
-
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
51
|
-
// by the _options just here below
|
|
52
|
-
_endpoint,
|
|
53
|
-
// these are the options arriving when calling `trigger({ json, query, etc... })
|
|
54
|
-
_options) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
55
|
-
var endpoint, options, _a, ok, data;
|
|
56
|
-
return tslib_1.__generator(this, function (_b) {
|
|
57
|
-
switch (_b.label) {
|
|
58
|
-
case 0:
|
|
59
|
-
endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
60
|
-
options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
61
|
-
return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, options), (_options.arg || {})), { throwErr: true }))];
|
|
62
|
-
case 1:
|
|
63
|
-
_a = _b.sent(), ok = _a.ok, data = _a.data;
|
|
64
|
-
return [2 /*return*/, ok ? data : data];
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}); };
|
|
68
|
-
// config.fetcher = sender;
|
|
69
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
70
|
-
return (0, mutation_1.default)(
|
|
71
|
-
// @ts-expect-error FIXME: I can't get it...
|
|
72
|
-
options ? [endpoint, options] : endpoint, sender, config);
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
77
|
-
*/
|
|
78
|
-
var createSwrApi = function () {
|
|
79
|
-
var args = [];
|
|
80
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
81
|
-
args[_i] = arguments[_i];
|
|
82
|
-
}
|
|
83
|
-
var api = createApi_1.default.apply(void 0, args);
|
|
84
|
-
["get", "post", "put", "patch", "delete"].forEach(function (method) {
|
|
85
|
-
var hookName = "use".concat(method.charAt(0).toUpperCase() + method.slice(1));
|
|
86
|
-
api[hookName] = createUseApi(api, method);
|
|
87
|
-
});
|
|
88
|
-
return api;
|
|
89
|
-
};
|
|
90
|
-
exports.createSwrApi = createSwrApi;
|
|
91
|
-
exports.default = exports.createSwrApi;
|
package/createSwrApi.mjs
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { __assign, __awaiter, __generator } from "tslib";
|
|
3
|
-
import isFunction from "@koine/utils/isFunction";
|
|
4
|
-
import useSWR from "swr";
|
|
5
|
-
import useSWRMutation from "swr/mutation";
|
|
6
|
-
import createApi from "./createApi";
|
|
7
|
-
function createUseApi(api, method) {
|
|
8
|
-
return function useApi(endpoint, options, _config) {
|
|
9
|
-
var _this = this;
|
|
10
|
-
if (method === "get") {
|
|
11
|
-
// const fetcher = async (_endpoint: TEndpoint) => {
|
|
12
|
-
// try {
|
|
13
|
-
// const { ok, data } = await api[method](_endpoint, {
|
|
14
|
-
// ...(options || {}),
|
|
15
|
-
// throwErr: true,
|
|
16
|
-
// });
|
|
17
|
-
// if (ok) {
|
|
18
|
-
// return data;
|
|
19
|
-
// }
|
|
20
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
21
|
-
// } catch(e) {
|
|
22
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
23
|
-
// }
|
|
24
|
-
// };
|
|
25
|
-
// }
|
|
26
|
-
var fetcher = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
27
|
-
var data;
|
|
28
|
-
return __generator(this, function (_a) {
|
|
29
|
-
switch (_a.label) {
|
|
30
|
-
case 0: return [4 /*yield*/, api[method](endpoint, __assign(__assign({}, (options || {})), { throwErr: true }))];
|
|
31
|
-
case 1:
|
|
32
|
-
data = (_a.sent()).data;
|
|
33
|
-
return [2 /*return*/, data];
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}); };
|
|
37
|
-
var config_1 = _config;
|
|
38
|
-
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
39
|
-
(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);
|
|
40
|
-
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
41
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
42
|
-
return useSWR(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
43
|
-
}
|
|
44
|
-
var config = _config;
|
|
45
|
-
var sender = function (
|
|
46
|
-
// if the first argument is an array the second tem are the base options
|
|
47
|
-
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
48
|
-
// by the _options just here below
|
|
49
|
-
_endpoint,
|
|
50
|
-
// these are the options arriving when calling `trigger({ json, query, etc... })
|
|
51
|
-
_options) { return __awaiter(_this, void 0, void 0, function () {
|
|
52
|
-
var endpoint, options, _a, ok, data;
|
|
53
|
-
return __generator(this, function (_b) {
|
|
54
|
-
switch (_b.label) {
|
|
55
|
-
case 0:
|
|
56
|
-
endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
57
|
-
options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
58
|
-
return [4 /*yield*/, api[method](endpoint, __assign(__assign(__assign({}, options), (_options.arg || {})), { throwErr: true }))];
|
|
59
|
-
case 1:
|
|
60
|
-
_a = _b.sent(), ok = _a.ok, data = _a.data;
|
|
61
|
-
return [2 /*return*/, ok ? data : data];
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}); };
|
|
65
|
-
// config.fetcher = sender;
|
|
66
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
67
|
-
return useSWRMutation(
|
|
68
|
-
// @ts-expect-error FIXME: I can't get it...
|
|
69
|
-
options ? [endpoint, options] : endpoint, sender, config);
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
74
|
-
*/
|
|
75
|
-
export var createSwrApi = function () {
|
|
76
|
-
var args = [];
|
|
77
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
78
|
-
args[_i] = arguments[_i];
|
|
79
|
-
}
|
|
80
|
-
var api = createApi.apply(void 0, args);
|
|
81
|
-
["get", "post", "put", "patch", "delete"].forEach(function (method) {
|
|
82
|
-
var hookName = "use".concat(method.charAt(0).toUpperCase() + method.slice(1));
|
|
83
|
-
api[hookName] = createUseApi(api, method);
|
|
84
|
-
});
|
|
85
|
-
return api;
|
|
86
|
-
};
|
|
87
|
-
export default createSwrApi;
|
package/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.nextApiResponse = exports.createSwrApi = exports.createApi = exports.ApiError = void 0;
|
|
4
|
-
var ApiError_1 = require("./ApiError");
|
|
5
|
-
Object.defineProperty(exports, "ApiError", { enumerable: true, get: function () { return ApiError_1.ApiError; } });
|
|
6
|
-
var createApi_1 = require("./createApi");
|
|
7
|
-
Object.defineProperty(exports, "createApi", { enumerable: true, get: function () { return createApi_1.createApi; } });
|
|
8
|
-
var createSwrApi_1 = require("./createSwrApi");
|
|
9
|
-
Object.defineProperty(exports, "createSwrApi", { enumerable: true, get: function () { return createSwrApi_1.createSwrApi; } });
|
|
10
|
-
var nextApiResponse_1 = require("./nextApiResponse");
|
|
11
|
-
Object.defineProperty(exports, "nextApiResponse", { enumerable: true, get: function () { return nextApiResponse_1.nextApiResponse; } });
|
package/index.mjs
DELETED
package/nextApiResponse.d.ts
DELETED
package/nextApiResponse.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.nextApiResponse = void 0;
|
|
4
|
-
var nextApiResponse = function (nextRes, result) {
|
|
5
|
-
// nextRes.status(result.status).json(result.data || result.msg);
|
|
6
|
-
nextRes.status(result.status).json(result);
|
|
7
|
-
};
|
|
8
|
-
exports.nextApiResponse = nextApiResponse;
|
|
9
|
-
exports.default = exports.nextApiResponse;
|