@koine/api 2.0.0-beta.77 → 2.0.0-beta.79
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.d.ts +2 -0
- package/ApiError.cjs.default.js +1 -0
- package/ApiError.cjs.js +14 -0
- package/ApiError.cjs.mjs +2 -0
- package/ApiError.esm.js +9 -0
- package/README.md +1 -0
- package/createApi.cjs.d.ts +2 -0
- package/createApi.cjs.default.js +1 -0
- package/createApi.cjs.js +122 -0
- package/createApi.cjs.mjs +2 -0
- package/createApi.esm.js +117 -0
- package/createApiResultFail.cjs.d.ts +2 -0
- package/createApiResultFail.cjs.default.js +1 -0
- package/createApiResultFail.cjs.js +21 -0
- package/createApiResultFail.cjs.mjs +2 -0
- package/createApiResultFail.esm.js +15 -0
- package/createApiResultOk.cjs.d.ts +2 -0
- package/createApiResultOk.cjs.default.js +1 -0
- package/createApiResultOk.cjs.js +14 -0
- package/createApiResultOk.cjs.mjs +2 -0
- package/createApiResultOk.esm.js +9 -0
- package/createSwrApi.cjs.js +30 -0
- package/createSwrApi.esm.js +23 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +16 -0
- package/index.cjs.mjs +2 -0
- package/index.esm.js +5 -0
- package/next.cjs.d.ts +1 -0
- package/next.cjs.default.js +1 -0
- package/next.cjs.js +28 -0
- package/next.cjs.mjs +2 -0
- package/next.esm.js +23 -0
- package/package.json +30 -42
- package/swr-mutation.cjs.d.ts +1 -0
- package/swr-mutation.cjs.default.js +1 -0
- package/swr-mutation.cjs.js +38 -0
- package/swr-mutation.cjs.mjs +2 -0
- package/swr-mutation.esm.js +30 -0
- package/swr.cjs.d.ts +1 -0
- package/swr.cjs.default.js +1 -0
- package/swr.cjs.js +12 -0
- package/swr.cjs.mjs +2 -0
- package/swr.esm.js +4 -0
- package/typings.d.ts +13 -0
- package/ApiError.js +0 -1
- package/cjs/ApiError.d.ts +0 -5
- package/cjs/ApiError.js +0 -1
- package/cjs/createApi.d.ts +0 -3
- package/cjs/createApi.js +0 -1
- package/cjs/createApiResultFail.d.ts +0 -4
- package/cjs/createApiResultFail.js +0 -1
- package/cjs/createApiResultOk.d.ts +0 -3
- package/cjs/createApiResultOk.js +0 -1
- package/cjs/index.d.ts +0 -5
- package/cjs/index.js +0 -1
- package/cjs/next/nextApiResponse.d.ts +0 -6
- package/cjs/next/nextApiResponse.js +0 -1
- package/cjs/next/nextApiResponse12.d.ts +0 -6
- package/cjs/next/nextApiResponse12.js +0 -1
- package/cjs/next.d.ts +0 -2
- package/cjs/next.js +0 -1
- package/cjs/package.json +0 -22
- package/cjs/swr/createSwrApi.d.ts +0 -9
- package/cjs/swr/createSwrApi.js +0 -1
- package/cjs/swr-mutation/createSwrMutationApi.d.ts +0 -19
- package/cjs/swr-mutation/createSwrMutationApi.js +0 -1
- package/cjs/swr-mutation.d.ts +0 -1
- package/cjs/swr-mutation.js +0 -1
- package/cjs/swr.d.ts +0 -1
- package/cjs/swr.js +0 -1
- package/cjs/types.d.ts +0 -126
- package/cjs/types.js +0 -1
- package/createApi.js +0 -1
- package/createApiResultFail.js +0 -1
- package/createApiResultOk.js +0 -1
- package/index.js +0 -1
- package/next/nextApiResponse.js +0 -1
- package/next/nextApiResponse12.js +0 -1
- package/next.js +0 -1
- package/swr/createSwrApi.js +0 -1
- package/swr-mutation/createSwrMutationApi.js +0 -1
- package/swr-mutation.js +0 -1
- package/swr.js +0 -1
- package/types.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./ApiError.cjs.js').default;
|
package/ApiError.cjs.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
class ApiError extends Error {
|
|
6
|
+
constructor(result) {
|
|
7
|
+
super(`Request failed with ${result.status} ${result.msg}`);
|
|
8
|
+
this.name = "ApiError";
|
|
9
|
+
Object.assign(this, result);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.ApiError = ApiError;
|
|
14
|
+
exports["default"] = ApiError;
|
package/ApiError.cjs.mjs
ADDED
package/ApiError.esm.js
ADDED
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @koine/api
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApi.cjs.js').default;
|
package/createApi.cjs.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var utils = require('@koine/utils');
|
|
6
|
+
|
|
7
|
+
let apiMethods = [
|
|
8
|
+
"get",
|
|
9
|
+
"post",
|
|
10
|
+
"put",
|
|
11
|
+
"patch",
|
|
12
|
+
"delete",
|
|
13
|
+
];
|
|
14
|
+
let createApi = (apiName, baseUrl, options) => {
|
|
15
|
+
const { headers: headersBase = {}, request: requestBase = {}, throwErr: throwErrBase, timeout: timeoutBase = 10000, processReq: processReqBase, processRes: processResBase, processErr: processErrBase, } = options || {};
|
|
16
|
+
return apiMethods.reduce((api, method) => {
|
|
17
|
+
api[method] = async (endpoint, options) => {
|
|
18
|
+
const { request = requestBase, headers = headersBase, timeout = timeoutBase, processReq, processRes = processResBase, processErr = processErrBase, throwErr = throwErrBase, } = options || {};
|
|
19
|
+
let { params, json, query } = options || {};
|
|
20
|
+
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
21
|
+
let requestInit = {
|
|
22
|
+
method: method.toUpperCase(),
|
|
23
|
+
...request,
|
|
24
|
+
headers: {
|
|
25
|
+
"content-type": "application/json",
|
|
26
|
+
...headers,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
if (processReqBase) {
|
|
30
|
+
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
31
|
+
url = transformed[0];
|
|
32
|
+
query = transformed[1];
|
|
33
|
+
json = transformed[2];
|
|
34
|
+
params = transformed[3];
|
|
35
|
+
requestInit = transformed[4];
|
|
36
|
+
}
|
|
37
|
+
if (processReq) {
|
|
38
|
+
const transformed = processReq(method, url, query, json, params, requestInit);
|
|
39
|
+
url = transformed[0];
|
|
40
|
+
query = transformed[1];
|
|
41
|
+
json = transformed[2];
|
|
42
|
+
params = transformed[3];
|
|
43
|
+
requestInit = transformed[4];
|
|
44
|
+
}
|
|
45
|
+
if (utils.isFullObject(params)) {
|
|
46
|
+
for (const key in params) {
|
|
47
|
+
url = url.replace(`{${key}}`, params[key].toString());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const timeoutNumber = Number(timeout);
|
|
51
|
+
let controller;
|
|
52
|
+
let timeoutId;
|
|
53
|
+
if (json) {
|
|
54
|
+
requestInit.body = JSON.stringify(json);
|
|
55
|
+
}
|
|
56
|
+
if (timeoutNumber > 0) {
|
|
57
|
+
controller = new AbortController();
|
|
58
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutNumber);
|
|
59
|
+
requestInit.signal = controller.signal;
|
|
60
|
+
}
|
|
61
|
+
if (query) {
|
|
62
|
+
url += utils.buildUrlQueryString(query);
|
|
63
|
+
}
|
|
64
|
+
let response = null;
|
|
65
|
+
let result = null;
|
|
66
|
+
let msg = "";
|
|
67
|
+
try {
|
|
68
|
+
response = await fetch(url, requestInit);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
msg = utils.errorToString(e);
|
|
72
|
+
}
|
|
73
|
+
if (timeoutId) {
|
|
74
|
+
clearTimeout(timeoutId);
|
|
75
|
+
}
|
|
76
|
+
if (response) {
|
|
77
|
+
try {
|
|
78
|
+
if (processRes) {
|
|
79
|
+
result = await processRes(response, options || {});
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
result = await response.json();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
msg = utils.errorToString(e);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (result === null) {
|
|
90
|
+
if (processErr) {
|
|
91
|
+
result = await processErr(msg, options || {});
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
result = {
|
|
95
|
+
data: null,
|
|
96
|
+
msg,
|
|
97
|
+
status: 100,
|
|
98
|
+
fail: true,
|
|
99
|
+
ok: false,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (throwErr && result?.fail) {
|
|
104
|
+
throw result;
|
|
105
|
+
}
|
|
106
|
+
if (process.env["NODE_ENV"] === "development") {
|
|
107
|
+
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
108
|
+
if (result?.ok) {
|
|
109
|
+
console.info(`🟢 ${logMsg}`);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
console.info(`🔴 ${logMsg}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
};
|
|
117
|
+
return api;
|
|
118
|
+
}, {});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
exports.createApi = createApi;
|
|
122
|
+
exports["default"] = createApi;
|
package/createApi.esm.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { isFullObject, buildUrlQueryString, errorToString } from '@koine/utils';
|
|
2
|
+
|
|
3
|
+
let apiMethods = [
|
|
4
|
+
"get",
|
|
5
|
+
"post",
|
|
6
|
+
"put",
|
|
7
|
+
"patch",
|
|
8
|
+
"delete",
|
|
9
|
+
];
|
|
10
|
+
let createApi = (apiName, baseUrl, options) => {
|
|
11
|
+
const { headers: headersBase = {}, request: requestBase = {}, throwErr: throwErrBase, timeout: timeoutBase = 10000, processReq: processReqBase, processRes: processResBase, processErr: processErrBase, } = options || {};
|
|
12
|
+
return apiMethods.reduce((api, method) => {
|
|
13
|
+
api[method] = async (endpoint, options) => {
|
|
14
|
+
const { request = requestBase, headers = headersBase, timeout = timeoutBase, processReq, processRes = processResBase, processErr = processErrBase, throwErr = throwErrBase, } = options || {};
|
|
15
|
+
let { params, json, query } = options || {};
|
|
16
|
+
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
17
|
+
let requestInit = {
|
|
18
|
+
method: method.toUpperCase(),
|
|
19
|
+
...request,
|
|
20
|
+
headers: {
|
|
21
|
+
"content-type": "application/json",
|
|
22
|
+
...headers,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
if (processReqBase) {
|
|
26
|
+
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
27
|
+
url = transformed[0];
|
|
28
|
+
query = transformed[1];
|
|
29
|
+
json = transformed[2];
|
|
30
|
+
params = transformed[3];
|
|
31
|
+
requestInit = transformed[4];
|
|
32
|
+
}
|
|
33
|
+
if (processReq) {
|
|
34
|
+
const transformed = processReq(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 (isFullObject(params)) {
|
|
42
|
+
for (const key in params) {
|
|
43
|
+
url = url.replace(`{${key}}`, params[key].toString());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const timeoutNumber = Number(timeout);
|
|
47
|
+
let controller;
|
|
48
|
+
let timeoutId;
|
|
49
|
+
if (json) {
|
|
50
|
+
requestInit.body = JSON.stringify(json);
|
|
51
|
+
}
|
|
52
|
+
if (timeoutNumber > 0) {
|
|
53
|
+
controller = new AbortController();
|
|
54
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutNumber);
|
|
55
|
+
requestInit.signal = controller.signal;
|
|
56
|
+
}
|
|
57
|
+
if (query) {
|
|
58
|
+
url += buildUrlQueryString(query);
|
|
59
|
+
}
|
|
60
|
+
let response = null;
|
|
61
|
+
let result = null;
|
|
62
|
+
let msg = "";
|
|
63
|
+
try {
|
|
64
|
+
response = await fetch(url, requestInit);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
msg = errorToString(e);
|
|
68
|
+
}
|
|
69
|
+
if (timeoutId) {
|
|
70
|
+
clearTimeout(timeoutId);
|
|
71
|
+
}
|
|
72
|
+
if (response) {
|
|
73
|
+
try {
|
|
74
|
+
if (processRes) {
|
|
75
|
+
result = await processRes(response, options || {});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
result = await response.json();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
msg = errorToString(e);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (result === null) {
|
|
86
|
+
if (processErr) {
|
|
87
|
+
result = await processErr(msg, options || {});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
result = {
|
|
91
|
+
data: null,
|
|
92
|
+
msg,
|
|
93
|
+
status: 100,
|
|
94
|
+
fail: true,
|
|
95
|
+
ok: false,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (throwErr && result?.fail) {
|
|
100
|
+
throw result;
|
|
101
|
+
}
|
|
102
|
+
if (process.env["NODE_ENV"] === "development") {
|
|
103
|
+
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
104
|
+
if (result?.ok) {
|
|
105
|
+
console.info(`🟢 ${logMsg}`);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
console.info(`🔴 ${logMsg}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
return api;
|
|
114
|
+
}, {});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export { createApi, createApi as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApiResultFail.cjs.js').default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
let createApiResultOk = (data = {}, msg) => ({
|
|
6
|
+
ok: true,
|
|
7
|
+
fail: false,
|
|
8
|
+
data,
|
|
9
|
+
msg: msg || "",
|
|
10
|
+
status: 200,
|
|
11
|
+
});
|
|
12
|
+
let createApiResultFail = (data = {}, msg, status) => ({
|
|
13
|
+
fail: true,
|
|
14
|
+
data,
|
|
15
|
+
msg: msg || "",
|
|
16
|
+
status: status || 404,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
exports.createApiResultFail = createApiResultFail;
|
|
20
|
+
exports.createApiResultOk = createApiResultOk;
|
|
21
|
+
exports["default"] = createApiResultFail;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
let createApiResultOk = (data = {}, msg) => ({
|
|
2
|
+
ok: true,
|
|
3
|
+
fail: false,
|
|
4
|
+
data,
|
|
5
|
+
msg: msg || "",
|
|
6
|
+
status: 200,
|
|
7
|
+
});
|
|
8
|
+
let createApiResultFail = (data = {}, msg, status) => ({
|
|
9
|
+
fail: true,
|
|
10
|
+
data,
|
|
11
|
+
msg: msg || "",
|
|
12
|
+
status: status || 404,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export { createApiResultFail, createApiResultOk, createApiResultFail as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApiResultOk.cjs.js').default;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
let createApiResultOk = (data = {}, msg) => ({
|
|
6
|
+
ok: true,
|
|
7
|
+
fail: false,
|
|
8
|
+
data,
|
|
9
|
+
msg: msg || "",
|
|
10
|
+
status: 200,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
exports.createApiResultOk = createApiResultOk;
|
|
14
|
+
exports["default"] = createApiResultOk;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useSWR = require('swr');
|
|
4
|
+
var utils = require('@koine/utils');
|
|
5
|
+
var createApi = require('./createApi.cjs.js');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var useSWR__default = /*#__PURE__*/_interopDefaultLegacy(useSWR);
|
|
10
|
+
|
|
11
|
+
let createUseApi = (api) => (endpoint, options, config) => {
|
|
12
|
+
const fetcher = async () => {
|
|
13
|
+
const { data } = await api.get(endpoint, {
|
|
14
|
+
...(options || {}),
|
|
15
|
+
throwErr: true,
|
|
16
|
+
});
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
const shouldNotFetch = config?.when === false ||
|
|
20
|
+
(utils.isFunction(config?.when) && config?.when() === false);
|
|
21
|
+
return useSWR__default["default"](shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config);
|
|
22
|
+
};
|
|
23
|
+
let createSwrApi = (...args) => {
|
|
24
|
+
const api = createApi.createApi(...args);
|
|
25
|
+
api.use = createUseApi(api);
|
|
26
|
+
return api;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
exports.createSwrApi = createSwrApi;
|
|
30
|
+
exports.createUseApi = createUseApi;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import useSWR from 'swr';
|
|
2
|
+
import { isFunction } from '@koine/utils';
|
|
3
|
+
import { createApi } from './createApi.esm.js';
|
|
4
|
+
|
|
5
|
+
let createUseApi = (api) => (endpoint, options, config) => {
|
|
6
|
+
const fetcher = async () => {
|
|
7
|
+
const { data } = await api.get(endpoint, {
|
|
8
|
+
...(options || {}),
|
|
9
|
+
throwErr: true,
|
|
10
|
+
});
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
const shouldNotFetch = config?.when === false ||
|
|
14
|
+
(isFunction(config?.when) && config?.when() === false);
|
|
15
|
+
return useSWR(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config);
|
|
16
|
+
};
|
|
17
|
+
let createSwrApi = (...args) => {
|
|
18
|
+
const api = createApi(...args);
|
|
19
|
+
api.use = createUseApi(api);
|
|
20
|
+
return api;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { createUseApi as a, createSwrApi as c };
|
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var ApiError = require('./ApiError.cjs.js');
|
|
6
|
+
var createApi = require('./createApi.cjs.js');
|
|
7
|
+
var createApiResultFail = require('./createApiResultFail.cjs.js');
|
|
8
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
9
|
+
require('@koine/utils');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.ApiError = ApiError.ApiError;
|
|
14
|
+
exports.createApi = createApi.createApi;
|
|
15
|
+
exports.createApiResultFail = createApiResultFail.createApiResultFail;
|
|
16
|
+
exports.createApiResultOk = createApiResultOk.createApiResultOk;
|
package/index.cjs.mjs
ADDED
package/index.esm.js
ADDED
package/next.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./next";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./next.cjs.js').default;
|
package/next.cjs.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var server = require('next/server');
|
|
6
|
+
var createApiResultFail = require('./createApiResultFail.cjs.js');
|
|
7
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
8
|
+
|
|
9
|
+
let nextApiResponse = {
|
|
10
|
+
ok(data, msg) {
|
|
11
|
+
return server.NextResponse.json(createApiResultOk.createApiResultOk(data, msg));
|
|
12
|
+
},
|
|
13
|
+
fail(data, msg, status) {
|
|
14
|
+
return server.NextResponse.json(createApiResultFail.createApiResultFail(data, msg, status));
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let nextApiResponse12 = (nextRes) => ({
|
|
19
|
+
ok(data, msg) {
|
|
20
|
+
nextRes.status(200).json(createApiResultOk.createApiResultOk(data, msg));
|
|
21
|
+
},
|
|
22
|
+
fail(data, msg, status) {
|
|
23
|
+
nextRes.status(status || 404).json(createApiResultFail.createApiResultFail(data, msg, status));
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
exports.nextApiResponse = nextApiResponse;
|
|
28
|
+
exports.nextApiResponse12 = nextApiResponse12;
|
package/next.cjs.mjs
ADDED
package/next.esm.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { createApiResultFail } from './createApiResultFail.esm.js';
|
|
3
|
+
import { createApiResultOk } from './createApiResultOk.esm.js';
|
|
4
|
+
|
|
5
|
+
let nextApiResponse = {
|
|
6
|
+
ok(data, msg) {
|
|
7
|
+
return NextResponse.json(createApiResultOk(data, msg));
|
|
8
|
+
},
|
|
9
|
+
fail(data, msg, status) {
|
|
10
|
+
return NextResponse.json(createApiResultFail(data, msg, status));
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
let nextApiResponse12 = (nextRes) => ({
|
|
15
|
+
ok(data, msg) {
|
|
16
|
+
nextRes.status(200).json(createApiResultOk(data, msg));
|
|
17
|
+
},
|
|
18
|
+
fail(data, msg, status) {
|
|
19
|
+
nextRes.status(status || 404).json(createApiResultFail(data, msg, status));
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export { nextApiResponse, nextApiResponse12 };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@koine/api",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@koine/utils": "2.0.0-beta.
|
|
5
|
+
"@koine/utils": "2.0.0-beta.79"
|
|
6
6
|
},
|
|
7
7
|
"peerDependencies": {
|
|
8
8
|
"next": "^14.0.4",
|
|
@@ -16,62 +16,50 @@
|
|
|
16
16
|
"optional": true
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"module": "./index.js",
|
|
20
|
-
"type": "module",
|
|
21
|
-
"main": "./cjs/index.js",
|
|
22
19
|
"exports": {
|
|
20
|
+
"./package.json": "./package.json",
|
|
23
21
|
".": {
|
|
24
|
-
"
|
|
25
|
-
"import": "./index.
|
|
22
|
+
"module": "./index.esm.js",
|
|
23
|
+
"import": "./index.cjs.mjs",
|
|
24
|
+
"default": "./index.cjs.js"
|
|
26
25
|
},
|
|
27
26
|
"./ApiError": {
|
|
28
|
-
"
|
|
29
|
-
"import": "./ApiError.
|
|
27
|
+
"module": "./ApiError.esm.js",
|
|
28
|
+
"import": "./ApiError.cjs.mjs",
|
|
29
|
+
"default": "./ApiError.cjs.js"
|
|
30
30
|
},
|
|
31
31
|
"./createApi": {
|
|
32
|
-
"
|
|
33
|
-
"import": "./createApi.
|
|
32
|
+
"module": "./createApi.esm.js",
|
|
33
|
+
"import": "./createApi.cjs.mjs",
|
|
34
|
+
"default": "./createApi.cjs.js"
|
|
34
35
|
},
|
|
35
36
|
"./createApiResultFail": {
|
|
36
|
-
"
|
|
37
|
-
"import": "./createApiResultFail.
|
|
37
|
+
"module": "./createApiResultFail.esm.js",
|
|
38
|
+
"import": "./createApiResultFail.cjs.mjs",
|
|
39
|
+
"default": "./createApiResultFail.cjs.js"
|
|
38
40
|
},
|
|
39
41
|
"./createApiResultOk": {
|
|
40
|
-
"
|
|
41
|
-
"import": "./createApiResultOk.
|
|
42
|
+
"module": "./createApiResultOk.esm.js",
|
|
43
|
+
"import": "./createApiResultOk.cjs.mjs",
|
|
44
|
+
"default": "./createApiResultOk.cjs.js"
|
|
42
45
|
},
|
|
43
46
|
"./next": {
|
|
44
|
-
"
|
|
45
|
-
"import": "./next.
|
|
46
|
-
|
|
47
|
-
"./next/nextApiResponse": {
|
|
48
|
-
"require": "./cjs/next/nextApiResponse.js",
|
|
49
|
-
"import": "./next/nextApiResponse.js"
|
|
50
|
-
},
|
|
51
|
-
"./next/nextApiResponse12": {
|
|
52
|
-
"require": "./cjs/next/nextApiResponse12.js",
|
|
53
|
-
"import": "./next/nextApiResponse12.js"
|
|
54
|
-
},
|
|
55
|
-
"./swr-mutation": {
|
|
56
|
-
"require": "./cjs/swr-mutation.js",
|
|
57
|
-
"import": "./swr-mutation.js"
|
|
58
|
-
},
|
|
59
|
-
"./swr-mutation/createSwrMutationApi": {
|
|
60
|
-
"require": "./cjs/swr-mutation/createSwrMutationApi.js",
|
|
61
|
-
"import": "./swr-mutation/createSwrMutationApi.js"
|
|
47
|
+
"module": "./next.esm.js",
|
|
48
|
+
"import": "./next.cjs.mjs",
|
|
49
|
+
"default": "./next.cjs.js"
|
|
62
50
|
},
|
|
63
51
|
"./swr": {
|
|
64
|
-
"
|
|
65
|
-
"import": "./swr.
|
|
66
|
-
|
|
67
|
-
"./swr/createSwrApi": {
|
|
68
|
-
"require": "./cjs/swr/createSwrApi.js",
|
|
69
|
-
"import": "./swr/createSwrApi.js"
|
|
52
|
+
"module": "./swr.esm.js",
|
|
53
|
+
"import": "./swr.cjs.mjs",
|
|
54
|
+
"default": "./swr.cjs.js"
|
|
70
55
|
},
|
|
71
|
-
"./
|
|
72
|
-
"
|
|
73
|
-
"import": "./
|
|
56
|
+
"./swr-mutation": {
|
|
57
|
+
"module": "./swr-mutation.esm.js",
|
|
58
|
+
"import": "./swr-mutation.cjs.mjs",
|
|
59
|
+
"default": "./swr-mutation.cjs.js"
|
|
74
60
|
}
|
|
75
61
|
},
|
|
76
|
-
"
|
|
62
|
+
"module": "./index.esm.js",
|
|
63
|
+
"main": "./index.cjs.js",
|
|
64
|
+
"version": "2.0.0-beta.79"
|
|
77
65
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./swr-mutation";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./swr-mutation.cjs.js').default;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var useSWRMutation = require('swr/mutation');
|
|
6
|
+
var createApi = require('./createApi.cjs.js');
|
|
7
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
8
|
+
require('@koine/utils');
|
|
9
|
+
require('swr');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var useSWRMutation__default = /*#__PURE__*/_interopDefaultLegacy(useSWRMutation);
|
|
14
|
+
|
|
15
|
+
let createUseMutationApi = (api, method) => (endpoint, options, config) => {
|
|
16
|
+
const sender = async (_endpoint, _options) => {
|
|
17
|
+
const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
18
|
+
const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
19
|
+
const { ok, data } = await api[method](endpoint, {
|
|
20
|
+
...options,
|
|
21
|
+
...(_options.arg || {}),
|
|
22
|
+
throwErr: true,
|
|
23
|
+
});
|
|
24
|
+
return ok ? data : data;
|
|
25
|
+
};
|
|
26
|
+
return useSWRMutation__default["default"](options ? [endpoint, options] : endpoint, sender, config);
|
|
27
|
+
};
|
|
28
|
+
let createSwrMutationApi = (...args) => {
|
|
29
|
+
const api = createApi.createApi(...args);
|
|
30
|
+
api.use = createSwrApi.createUseApi(api);
|
|
31
|
+
["post", "put", "patch", "delete"].forEach((method) => {
|
|
32
|
+
const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
|
|
33
|
+
api[hookName] = createUseMutationApi(api, method);
|
|
34
|
+
});
|
|
35
|
+
return api;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.createSwrMutationApi = createSwrMutationApi;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import useSWRMutation from 'swr/mutation';
|
|
2
|
+
import { createApi } from './createApi.esm.js';
|
|
3
|
+
import { a as createUseApi } from './createSwrApi.esm.js';
|
|
4
|
+
import '@koine/utils';
|
|
5
|
+
import 'swr';
|
|
6
|
+
|
|
7
|
+
let createUseMutationApi = (api, method) => (endpoint, options, config) => {
|
|
8
|
+
const sender = async (_endpoint, _options) => {
|
|
9
|
+
const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
10
|
+
const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
11
|
+
const { ok, data } = await api[method](endpoint, {
|
|
12
|
+
...options,
|
|
13
|
+
...(_options.arg || {}),
|
|
14
|
+
throwErr: true,
|
|
15
|
+
});
|
|
16
|
+
return ok ? data : data;
|
|
17
|
+
};
|
|
18
|
+
return useSWRMutation(options ? [endpoint, options] : endpoint, sender, config);
|
|
19
|
+
};
|
|
20
|
+
let createSwrMutationApi = (...args) => {
|
|
21
|
+
const api = createApi(...args);
|
|
22
|
+
api.use = createUseApi(api);
|
|
23
|
+
["post", "put", "patch", "delete"].forEach((method) => {
|
|
24
|
+
const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
|
|
25
|
+
api[hookName] = createUseMutationApi(api, method);
|
|
26
|
+
});
|
|
27
|
+
return api;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { createSwrMutationApi };
|
package/swr.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./swr";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./swr.cjs.js').default;
|
package/swr.cjs.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
6
|
+
require('swr');
|
|
7
|
+
require('@koine/utils');
|
|
8
|
+
require('./createApi.cjs.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
exports.createSwrApi = createSwrApi.createSwrApi;
|
package/swr.cjs.mjs
ADDED
package/swr.esm.js
ADDED
package/typings.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare namespace Koine.Api {
|
|
2
|
+
type Endpoints = import("./types").Api.Endpoints;
|
|
3
|
+
type GenerateGetShortcuts<E extends Endpoints> =
|
|
4
|
+
import("./types").Api.Generate.GetShortcuts<E>;
|
|
5
|
+
type GeneratePostShortcuts<E extends Endpoints> =
|
|
6
|
+
import("./types").Api.Generate.PostShortcuts<E>;
|
|
7
|
+
type GenerateRequestShortcuts<E extends Endpoints> =
|
|
8
|
+
import("./types").Api.Generate.RequestShortcuts<E>;
|
|
9
|
+
type GenerateResponseShortcuts<E extends Endpoints> =
|
|
10
|
+
import("./types").Api.Generate.ResponseShortcuts<E>;
|
|
11
|
+
type GenerateResultShortcuts<E extends Endpoints> =
|
|
12
|
+
import("./types").Api.Generate.ResultShortcuts<E>;
|
|
13
|
+
}
|
package/ApiError.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r)}}export default ApiError;
|
package/cjs/ApiError.d.ts
DELETED
package/cjs/ApiError.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r)}}export default ApiError;
|
package/cjs/createApi.d.ts
DELETED
package/cjs/createApi.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{buildUrlQueryString as e,errorToString as t,isFullObject as r}from"@koine/utils";let o=["get","post","put","patch","delete"];export let createApi=(l,a,i)=>{let{headers:s={},request:n={},throwErr:p,timeout:c=1e4,processReq:u,processRes:f,processErr:d}=i||{};return o.reduce((o,i)=>(o[i]=async(o,h)=>{let $,m;let{request:w=n,headers:y=s,timeout:g=c,processReq:b,processRes:E=f,processErr:N=d,throwErr:k=p}=h||{},{params:A,json:C,query:j}=h||{},q=`${a}/${o+"".replace(/^\/*/,"")}`,v={method:i.toUpperCase(),...w,headers:{"content-type":"application/json",...y}};if(u){let e=u(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4]}if(b){let e=b(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4]}if(r(A))for(let e in A)q=q.replace(`{${e}}`,A[e].toString());let x=Number(g);C&&(v.body=JSON.stringify(C)),x>0&&($=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&(q+=e(j));let O=null,R=null,S="";try{O=await fetch(q,v)}catch(e){S=t(e)}if(m&&clearTimeout(m),O)try{R=E?await E(O,h||{}):await O.json()}catch(e){S=t(e)}if(null===R&&(R=N?await N(S,h||{}):{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)throw R;if("development"===process.env.NODE_ENV){let e=`${R?.status}: api[${l}] ${i.toUpperCase()} ${q}`;R?.ok?console.info(`🟢 ${e}`):console.info(`🔴 ${e}`)}return R},o),{})};export default createApi;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let createApiResultOk=(t={},e)=>({ok:!0,fail:!1,data:t,msg:e||"",status:200});export let createApiResultFail=(t={},e,a)=>({fail:!0,data:t,msg:e||"",status:a||404});export default createApiResultFail;
|
package/cjs/createApiResultOk.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let createApiResultOk=(e={},t)=>({ok:!0,fail:!1,data:e,msg:t||"",status:200});export default createApiResultOk;
|
package/cjs/index.d.ts
DELETED
package/cjs/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{ApiError}from"./ApiError";export{createApi}from"./createApi";export{createApiResultFail}from"./createApiResultFail";export{createApiResultOk}from"./createApiResultOk";
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
|
-
export declare let nextApiResponse: {
|
|
3
|
-
ok<T>(data: T, msg?: string): NextResponse<import("..").Api.ResultOk<T>>;
|
|
4
|
-
fail<T_1>(data: T_1, msg?: string, status?: number): NextResponse<import("..").Api.ResultFail<T_1>>;
|
|
5
|
-
};
|
|
6
|
-
export default nextApiResponse;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{NextResponse as e}from"next/server";import{createApiResultFail as t}from"../createApiResultFail";import{createApiResultOk as o}from"../createApiResultOk";export let nextApiResponse={ok:(t,r)=>e.json(o(t,r)),fail:(o,r,p)=>e.json(t(o,r,p))};export default nextApiResponse;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createApiResultFail as e}from"../createApiResultFail";import{createApiResultOk as t}from"../createApiResultOk";export let nextApiResponse12=s=>({ok(e,o){s.status(200).json(t(e,o))},fail(t,o,p){s.status(p||404).json(e(t,o,p))}});export default nextApiResponse12;
|
package/cjs/next.d.ts
DELETED
package/cjs/next.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{nextApiResponse}from"./next/nextApiResponse";export{nextApiResponse12}from"./next/nextApiResponse12";
|
package/cjs/package.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@koine/api",
|
|
3
|
-
"sideEffects": false,
|
|
4
|
-
"dependencies": {
|
|
5
|
-
"@koine/utils": "*"
|
|
6
|
-
},
|
|
7
|
-
"peerDependencies": {
|
|
8
|
-
"next": "^14.0.4",
|
|
9
|
-
"swr": "^2.2.4"
|
|
10
|
-
},
|
|
11
|
-
"peerDependenciesMeta": {
|
|
12
|
-
"next": {
|
|
13
|
-
"optional": true
|
|
14
|
-
},
|
|
15
|
-
"swr": {
|
|
16
|
-
"optional": true
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"module": "./index.js",
|
|
20
|
-
"type": "module",
|
|
21
|
-
"main": "./index.js"
|
|
22
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type BareFetcher, type SWRConfiguration, type SWRResponse } from "swr";
|
|
2
|
-
import type { Api } from "../types";
|
|
3
|
-
type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
4
|
-
when?: boolean | (() => boolean);
|
|
5
|
-
};
|
|
6
|
-
export declare let createSwrApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
7
|
-
use: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: SWRConfigurationExtended<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, BareFetcher<any>> | undefined) => SWRResponse<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, any>;
|
|
8
|
-
};
|
|
9
|
-
export default createSwrApi;
|
package/cjs/swr/createSwrApi.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import e from"swr";import{isFunction as r}from"@koine/utils";import{createApi as t}from"../createApi";export let createUseApi=t=>(i,o,p)=>{let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};return e(p?.when===!1||r(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};export let createSwrApi=(...e)=>{let r=t(...e);return r.use=createUseApi(r),r};export default createSwrApi;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
2
|
-
import { createUseApi } from "../swr/createSwrApi";
|
|
3
|
-
import type { Api } from "../types";
|
|
4
|
-
type MutationRequestMethod = Exclude<Api.RequestMethod, "get">;
|
|
5
|
-
type MutationHookName = Exclude<keyof Api.HooksMapsByName, "use">;
|
|
6
|
-
type KoineApiMethodHookSWR<THookName extends MutationHookName, TEndpoints extends Api.Endpoints> = <TEndpoint extends Api.EndpointUrl<TEndpoints>, TMethod extends MutationRequestMethod = Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, config?: SWRMutationConfiguration<Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, TEndpoint, Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>>) => SWRMutationResponse<Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, TEndpoint, Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>>;
|
|
7
|
-
export declare let createSwrMutationApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
8
|
-
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
9
|
-
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
10
|
-
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
11
|
-
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
12
|
-
} & {
|
|
13
|
-
use: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: (Partial<import("swr/dist/_internal").PublicConfiguration<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, import("swr/dist/_internal").BareFetcher<any>>> & Partial<import("swr/dist/_internal").ProviderConfiguration> & {
|
|
14
|
-
provider?: ((cache: Readonly<import("swr/dist/_internal").Cache<any>>) => import("swr/dist/_internal").Cache<any>) | undefined;
|
|
15
|
-
} & {
|
|
16
|
-
when?: boolean | (() => boolean) | undefined;
|
|
17
|
-
}) | undefined) => import("swr/dist/_internal").SWRResponse<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, any>;
|
|
18
|
-
};
|
|
19
|
-
export default createSwrMutationApi;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import r from"swr/mutation";import{createApi as t}from"../createApi";import{createUseApi as e}from"../swr/createSwrApi";let a=(t,e)=>(a,o,i)=>r(o?[a,o]:a,async(r,a)=>{let o=Array.isArray(r)?r[0]:r,i=Array.isArray(r)?r[1]:{},{ok:p,data:s}=await t[e](o,{...i,...a.arg||{},throwErr:!0});return s},i);export let createSwrMutationApi=(...r)=>{let o=t(...r);return o.use=e(o),["post","put","patch","delete"].forEach(r=>{o[`use${r.charAt(0).toUpperCase()+r.slice(1)}`]=a(o,r)}),o};export default createSwrMutationApi;
|
package/cjs/swr-mutation.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createSwrMutationApi } from "./swr-mutation/createSwrMutationApi";
|
package/cjs/swr-mutation.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{createSwrMutationApi}from"./swr-mutation/createSwrMutationApi";
|
package/cjs/swr.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createSwrApi } from "./swr/createSwrApi";
|
package/cjs/swr.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{createSwrApi}from"./swr/createSwrApi";
|
package/cjs/types.d.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
type _Response = Response;
|
|
2
|
-
type ExtractEndpointParams<T extends string> = string extends T ? Record<string, string> : T extends `${infer _Start}{${infer Param}}${infer Rest}` ? {
|
|
3
|
-
[k in Param | keyof ExtractEndpointParams<Rest>]: string | number;
|
|
4
|
-
} : T extends `${infer _Start}{${infer Param}}` ? {
|
|
5
|
-
[k in Param]: string | number;
|
|
6
|
-
} : never;
|
|
7
|
-
export declare namespace Api {
|
|
8
|
-
export type ClientOptions = {
|
|
9
|
-
headers?: RequestInit["headers"];
|
|
10
|
-
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
11
|
-
throwErr?: boolean;
|
|
12
|
-
timeout?: number | false | null;
|
|
13
|
-
processReq?: RequestProcessor;
|
|
14
|
-
processRes?: ResponseProcessorRes;
|
|
15
|
-
processErr?: ResponseProcessorErr;
|
|
16
|
-
};
|
|
17
|
-
type ClientMethod<TMethod extends RequestMethod, TEndpoints extends Endpoints> = <TEndpoint extends EndpointUrl<TEndpoints>, TOptions extends EndpointOptions<TEndpoints, TEndpoint, TMethod>, TOk extends ResponseOk = EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, TFail extends ResponseFail = EndpointResponseFail<TEndpoints, TEndpoint, TMethod>>(endpoint: TEndpoint, options?: TOptions) => Promise<EndpointResult<TEndpoints, TEndpoint, TMethod>>;
|
|
18
|
-
export type Client<TEndpoints extends Endpoints> = {
|
|
19
|
-
[TMethod in RequestMethod]: ClientMethod<TMethod, TEndpoints>;
|
|
20
|
-
};
|
|
21
|
-
export type GetDataType<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod, TDataType extends keyof DataTypes<Uppercase<TMethod>>> = TEndpoints[TEndpoint][Uppercase<TMethod>] extends DataTypes<Uppercase<TMethod>> ? TEndpoints[TEndpoint][Uppercase<TMethod>][TDataType] : never;
|
|
22
|
-
export type EndpointOptions<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = RequestOptions<TEndpoints, TEndpoint, TMethod, GetDataType<TEndpoints, TEndpoint, TMethod, "json">, GetDataType<TEndpoints, TEndpoint, TMethod, "query">>;
|
|
23
|
-
export type EndpointResponseOk<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
24
|
-
export type EndpointResponseFail<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = GetDataType<TEndpoints, TEndpoint, TMethod, "fail">;
|
|
25
|
-
type EndpointResult<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = Result<EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, EndpointResponseFail<TEndpoints, TEndpoint, TMethod>>;
|
|
26
|
-
export type Endpoints = Record<string, Endpoint>;
|
|
27
|
-
type Endpoint = {
|
|
28
|
-
[TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
|
|
29
|
-
};
|
|
30
|
-
export type EndpointUrl<TEndpoints extends Endpoints> = Extract<keyof TEndpoints, string>;
|
|
31
|
-
type DataTypes<TMethod extends Uppercase<RequestMethod>> = {
|
|
32
|
-
json?: RequestJson;
|
|
33
|
-
query?: RequestQuery;
|
|
34
|
-
ok?: null | unknown;
|
|
35
|
-
fail?: null | unknown;
|
|
36
|
-
};
|
|
37
|
-
export type RequestMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
38
|
-
type RequestJson = unknown;
|
|
39
|
-
type RequestQuery = unknown;
|
|
40
|
-
type RequestParams = unknown;
|
|
41
|
-
type RequestOptions<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod, TJson extends RequestJson, TQuery extends RequestQuery> = Omit<ClientOptions, "processReq"> & {
|
|
42
|
-
processReq?: EndpointRequestProcessor<TEndpoints, TEndpoint, TMethod>;
|
|
43
|
-
params?: ExtractEndpointParams<TEndpoint>;
|
|
44
|
-
query?: TQuery;
|
|
45
|
-
json?: TJson;
|
|
46
|
-
};
|
|
47
|
-
export type ResponseOk = unknown;
|
|
48
|
-
export type ResponseFail = unknown;
|
|
49
|
-
export type ResultOk<TResponse extends ResponseOk = ResponseOk> = {
|
|
50
|
-
status: _Response["status"];
|
|
51
|
-
msg: _Response["statusText"];
|
|
52
|
-
ok: true;
|
|
53
|
-
fail?: false;
|
|
54
|
-
data: TResponse;
|
|
55
|
-
};
|
|
56
|
-
export type ResultFail<TResponse extends ResponseFail = ResponseFail> = {
|
|
57
|
-
status: _Response["status"];
|
|
58
|
-
msg: Error["message"] | _Response["statusText"];
|
|
59
|
-
ok?: false;
|
|
60
|
-
fail: true;
|
|
61
|
-
data: TResponse;
|
|
62
|
-
};
|
|
63
|
-
export type Result<TResponseOk extends ResponseOk, TResponseFail extends ResponseFail> = {
|
|
64
|
-
status: _Response["status"];
|
|
65
|
-
msg: _Response["statusText"];
|
|
66
|
-
ok: true;
|
|
67
|
-
fail?: false;
|
|
68
|
-
data: TResponseOk;
|
|
69
|
-
} | {
|
|
70
|
-
status: _Response["status"];
|
|
71
|
-
msg: Error["message"] | _Response["statusText"];
|
|
72
|
-
ok?: false;
|
|
73
|
-
fail: true;
|
|
74
|
-
data: TResponseFail;
|
|
75
|
-
};
|
|
76
|
-
export type RequestProcessor = (method: RequestMethod, url: string, query: any, json: any, params: any, requestInit: RequestInit) => [
|
|
77
|
-
string,
|
|
78
|
-
RequestQuery,
|
|
79
|
-
RequestJson,
|
|
80
|
-
RequestParams,
|
|
81
|
-
RequestInit
|
|
82
|
-
];
|
|
83
|
-
export type EndpointRequestProcessor<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = (method: TMethod, url: string, query: EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"], json: EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"], params: EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"], requestInit: RequestInit) => [
|
|
84
|
-
string,
|
|
85
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"],
|
|
86
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"],
|
|
87
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"],
|
|
88
|
-
RequestInit
|
|
89
|
-
];
|
|
90
|
-
type ResponseProcessorRes = <TResponseOk extends ResponseOk = ResponseOk, TResponseFail extends ResponseFail = ResponseFail>(response: _Response, options: any) => Promise<Result<TResponseOk, TResponseFail>>;
|
|
91
|
-
type ResponseProcessorErr = <TResponseOk extends ResponseOk = ResponseOk, TResponseFail extends ResponseFail = ResponseFail>(msg: string, options: any) => Promise<Result<TResponseOk, TResponseFail>>;
|
|
92
|
-
type HooksMaps = {
|
|
93
|
-
[TMethod in RequestMethod]: `use${TMethod extends "get" ? "" : Capitalize<TMethod>}`;
|
|
94
|
-
};
|
|
95
|
-
export type HooksMapsByName = {
|
|
96
|
-
[K in keyof HooksMaps as HooksMaps[K]]: K;
|
|
97
|
-
};
|
|
98
|
-
export {};
|
|
99
|
-
}
|
|
100
|
-
export declare namespace Api.Generate {
|
|
101
|
-
type ResultShortcuts<TEndpoints extends Endpoints> = {
|
|
102
|
-
[TMethod in RequestMethod]: {
|
|
103
|
-
[TEndpoint in Extract<keyof TEndpoints, string>]: {
|
|
104
|
-
ok: GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
105
|
-
fail: GetDataType<TEndpoints, TEndpoint, TMethod, "fail">;
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
type ResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
110
|
-
[TMethod in RequestMethod]: {
|
|
111
|
-
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
type RequestShortcuts<TEndpoints extends Endpoints> = {
|
|
115
|
-
[TMethod in RequestMethod]: {
|
|
116
|
-
[TEndpoint in Extract<keyof TEndpoints, string>]: TMethod extends "get" ? GetDataType<TEndpoints, TEndpoint, TMethod, "query"> : GetDataType<TEndpoints, TEndpoint, TMethod, "json">;
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
type GetShortcuts<TEndpoints extends Endpoints> = {
|
|
120
|
-
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, "get", "ok">;
|
|
121
|
-
};
|
|
122
|
-
type PostShortcuts<TEndpoints extends Endpoints> = {
|
|
123
|
-
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, "post", "ok">;
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
export {};
|
package/cjs/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{};
|
package/createApi.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{buildUrlQueryString as e,errorToString as t,isFullObject as r}from"@koine/utils";let o=["get","post","put","patch","delete"];export let createApi=(l,a,i)=>{let{headers:s={},request:n={},throwErr:p,timeout:c=1e4,processReq:u,processRes:f,processErr:d}=i||{};return o.reduce((o,i)=>(o[i]=async(o,h)=>{let $,m;let{request:w=n,headers:y=s,timeout:g=c,processReq:b,processRes:E=f,processErr:N=d,throwErr:k=p}=h||{},{params:A,json:C,query:j}=h||{},q=`${a}/${o+"".replace(/^\/*/,"")}`,v={method:i.toUpperCase(),...w,headers:{"content-type":"application/json",...y}};if(u){let e=u(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4]}if(b){let e=b(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4]}if(r(A))for(let e in A)q=q.replace(`{${e}}`,A[e].toString());let x=Number(g);C&&(v.body=JSON.stringify(C)),x>0&&($=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&(q+=e(j));let O=null,R=null,S="";try{O=await fetch(q,v)}catch(e){S=t(e)}if(m&&clearTimeout(m),O)try{R=E?await E(O,h||{}):await O.json()}catch(e){S=t(e)}if(null===R&&(R=N?await N(S,h||{}):{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)throw R;if("development"===process.env.NODE_ENV){let e=`${R?.status}: api[${l}] ${i.toUpperCase()} ${q}`;R?.ok?console.info(`🟢 ${e}`):console.info(`🔴 ${e}`)}return R},o),{})};export default createApi;
|
package/createApiResultFail.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let createApiResultOk=(t={},e)=>({ok:!0,fail:!1,data:t,msg:e||"",status:200});export let createApiResultFail=(t={},e,a)=>({fail:!0,data:t,msg:e||"",status:a||404});export default createApiResultFail;
|
package/createApiResultOk.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let createApiResultOk=(e={},t)=>({ok:!0,fail:!1,data:e,msg:t||"",status:200});export default createApiResultOk;
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{ApiError}from"./ApiError";export{createApi}from"./createApi";export{createApiResultFail}from"./createApiResultFail";export{createApiResultOk}from"./createApiResultOk";
|
package/next/nextApiResponse.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{NextResponse as e}from"next/server";import{createApiResultFail as t}from"../createApiResultFail";import{createApiResultOk as o}from"../createApiResultOk";export let nextApiResponse={ok:(t,r)=>e.json(o(t,r)),fail:(o,r,p)=>e.json(t(o,r,p))};export default nextApiResponse;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createApiResultFail as e}from"../createApiResultFail";import{createApiResultOk as t}from"../createApiResultOk";export let nextApiResponse12=s=>({ok(e,o){s.status(200).json(t(e,o))},fail(t,o,p){s.status(p||404).json(e(t,o,p))}});export default nextApiResponse12;
|
package/next.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{nextApiResponse}from"./next/nextApiResponse";export{nextApiResponse12}from"./next/nextApiResponse12";
|
package/swr/createSwrApi.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import e from"swr";import{isFunction as r}from"@koine/utils";import{createApi as t}from"../createApi";export let createUseApi=t=>(i,o,p)=>{let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};return e(p?.when===!1||r(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};export let createSwrApi=(...e)=>{let r=t(...e);return r.use=createUseApi(r),r};export default createSwrApi;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import r from"swr/mutation";import{createApi as t}from"../createApi";import{createUseApi as e}from"../swr/createSwrApi";let a=(t,e)=>(a,o,i)=>r(o?[a,o]:a,async(r,a)=>{let o=Array.isArray(r)?r[0]:r,i=Array.isArray(r)?r[1]:{},{ok:p,data:s}=await t[e](o,{...i,...a.arg||{},throwErr:!0});return s},i);export let createSwrMutationApi=(...r)=>{let o=t(...r);return o.use=e(o),["post","put","patch","delete"].forEach(r=>{o[`use${r.charAt(0).toUpperCase()+r.slice(1)}`]=a(o,r)}),o};export default createSwrMutationApi;
|
package/swr-mutation.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{createSwrMutationApi}from"./swr-mutation/createSwrMutationApi";
|
package/swr.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{createSwrApi}from"./swr/createSwrApi";
|
package/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{};
|