@koine/api 2.0.0-beta.8 → 2.0.0-beta.80
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.d.ts +3 -9
- package/ApiError.esm.js +9 -0
- package/createApi.cjs.d.ts +2 -0
- package/createApi.cjs.default.js +1 -0
- package/{createApi.js → createApi.cjs.js} +41 -61
- package/createApi.cjs.mjs +2 -0
- package/createApi.d.ts +2 -7
- package/{createApi.mjs → createApi.esm.js} +32 -43
- 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.d.ts +4 -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.d.ts +3 -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.d.ts +3 -2
- package/index.esm.js +5 -0
- package/next/nextApiResponse.d.ts +6 -0
- package/next/nextApiResponse12.d.ts +6 -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.d.ts +2 -0
- package/next.esm.js +23 -0
- package/package.json +53 -12
- package/swr/createSwrApi.d.ts +9 -0
- package/swr-mutation/createSwrMutationApi.d.ts +19 -0
- 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.d.ts +1 -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.d.ts +1 -0
- package/swr.esm.js +4 -0
- package/types.d.ts +126 -0
- package/typings.d.ts +11 -508
- package/ApiError.js +0 -32
- package/ApiError.mjs +0 -14
- package/createSwrApi.d.ts +0 -34
- package/createSwrApi.js +0 -99
- package/createSwrApi.mjs +0 -82
- package/index.js +0 -28
- package/index.mjs +0 -4
- package/nextApiResponse.d.ts +0 -3
- package/nextApiResponse.js +0 -23
- package/nextApiResponse.mjs +0 -5
|
@@ -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.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @see https://eslint.org/docs/rules/no-throw-literal
|
|
5
|
-
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
export declare class ApiError<TResponseFail extends Koine.Api.ResponseFail = unknown> extends Error {
|
|
9
|
-
constructor(result: Koine.Api.ResultFail<TResponseFail>);
|
|
1
|
+
import type { Api } from "./types";
|
|
2
|
+
export declare class ApiError<TResponseFail extends Api.ResponseFail = unknown> extends Error {
|
|
3
|
+
constructor(result: Api.ResultFail<TResponseFail>);
|
|
10
4
|
}
|
|
11
5
|
export default ApiError;
|
package/ApiError.esm.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApi.cjs.js').default;
|
|
@@ -1,39 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
20
|
-
const _buildUrlQueryString = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/buildUrlQueryString"));
|
|
21
|
-
const _errorToString = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/errorToString"));
|
|
22
|
-
const _isFullObject = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/isFullObject"));
|
|
23
|
-
const createApi = (apiName, baseUrl, options)=>{
|
|
24
|
-
const { headers: headersBase = {}, request: requestBase = {
|
|
25
|
-
referrerPolicy: "no-referrer"
|
|
26
|
-
}, throwErr: throwErrBase, timeout: timeoutBase = 10000, processReq: processReqBase, processRes: processResBase, processErr: processErrBase } = options || {};
|
|
27
|
-
return [
|
|
28
|
-
"get",
|
|
29
|
-
"post",
|
|
30
|
-
"put",
|
|
31
|
-
"patch",
|
|
32
|
-
"delete"
|
|
33
|
-
].reduce((api, method)=>{
|
|
34
|
-
// @ts-expect-error FIXME: type
|
|
35
|
-
api[method] = async (endpoint, options)=>{
|
|
36
|
-
const { request = requestBase, headers = headersBase, timeout = timeoutBase, processReq, processRes = processResBase, processErr = processErrBase, throwErr = throwErrBase } = options || {};
|
|
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 || {};
|
|
37
19
|
let { params, json, query } = options || {};
|
|
38
20
|
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
39
21
|
let requestInit = {
|
|
@@ -41,8 +23,8 @@ const createApi = (apiName, baseUrl, options)=>{
|
|
|
41
23
|
...request,
|
|
42
24
|
headers: {
|
|
43
25
|
"content-type": "application/json",
|
|
44
|
-
...headers
|
|
45
|
-
}
|
|
26
|
+
...headers,
|
|
27
|
+
},
|
|
46
28
|
};
|
|
47
29
|
if (processReqBase) {
|
|
48
30
|
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
@@ -60,8 +42,8 @@ const createApi = (apiName, baseUrl, options)=>{
|
|
|
60
42
|
params = transformed[3];
|
|
61
43
|
requestInit = transformed[4];
|
|
62
44
|
}
|
|
63
|
-
if (
|
|
64
|
-
for(const key in params){
|
|
45
|
+
if (utils.isFullObject(params)) {
|
|
46
|
+
for (const key in params) {
|
|
65
47
|
url = url.replace(`{${key}}`, params[key].toString());
|
|
66
48
|
}
|
|
67
49
|
}
|
|
@@ -73,20 +55,20 @@ const createApi = (apiName, baseUrl, options)=>{
|
|
|
73
55
|
}
|
|
74
56
|
if (timeoutNumber > 0) {
|
|
75
57
|
controller = new AbortController();
|
|
76
|
-
timeoutId = setTimeout(()=>controller.abort(), timeoutNumber);
|
|
58
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutNumber);
|
|
77
59
|
requestInit.signal = controller.signal;
|
|
78
60
|
}
|
|
79
61
|
if (query) {
|
|
80
|
-
|
|
81
|
-
url += (0, _buildUrlQueryString.default)(query);
|
|
62
|
+
url += utils.buildUrlQueryString(query);
|
|
82
63
|
}
|
|
83
64
|
let response = null;
|
|
84
65
|
let result = null;
|
|
85
66
|
let msg = "";
|
|
86
67
|
try {
|
|
87
68
|
response = await fetch(url, requestInit);
|
|
88
|
-
}
|
|
89
|
-
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
msg = utils.errorToString(e);
|
|
90
72
|
}
|
|
91
73
|
if (timeoutId) {
|
|
92
74
|
clearTimeout(timeoutId);
|
|
@@ -95,42 +77,38 @@ const createApi = (apiName, baseUrl, options)=>{
|
|
|
95
77
|
try {
|
|
96
78
|
if (processRes) {
|
|
97
79
|
result = await processRes(response, options || {});
|
|
98
|
-
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
99
82
|
result = await response.json();
|
|
100
83
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
msg = utils.errorToString(e);
|
|
103
87
|
}
|
|
104
88
|
}
|
|
105
89
|
if (result === null) {
|
|
106
90
|
if (processErr) {
|
|
107
91
|
result = await processErr(msg, options || {});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// there is no specific HTTP error for this, we can consider these
|
|
111
|
-
// two statuses though:
|
|
112
|
-
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
113
|
-
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
114
94
|
result = {
|
|
115
95
|
data: null,
|
|
116
96
|
msg,
|
|
117
97
|
status: 100,
|
|
118
98
|
fail: true,
|
|
119
|
-
ok: false
|
|
99
|
+
ok: false,
|
|
120
100
|
};
|
|
121
101
|
}
|
|
122
102
|
}
|
|
123
103
|
if (throwErr && result?.fail) {
|
|
124
|
-
// throw new ApiError<Failed>(result);
|
|
125
|
-
// I prefer to throw an object literal despite what eslint says
|
|
126
|
-
// eslint-disable-next-line no-throw-literal
|
|
127
104
|
throw result;
|
|
128
105
|
}
|
|
129
|
-
if (process.env["NODE_ENV"]
|
|
106
|
+
if (process.env["NODE_ENV"] === "development") {
|
|
130
107
|
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
131
108
|
if (result?.ok) {
|
|
132
109
|
console.info(`🟢 ${logMsg}`);
|
|
133
|
-
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
134
112
|
console.info(`🔴 ${logMsg}`);
|
|
135
113
|
}
|
|
136
114
|
}
|
|
@@ -139,4 +117,6 @@ const createApi = (apiName, baseUrl, options)=>{
|
|
|
139
117
|
return api;
|
|
140
118
|
}, {});
|
|
141
119
|
};
|
|
142
|
-
|
|
120
|
+
|
|
121
|
+
exports.createApi = createApi;
|
|
122
|
+
exports["default"] = createApi;
|
package/createApi.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @param apiName Short name to use in debug logs
|
|
5
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
6
|
-
*/
|
|
7
|
-
export declare const createApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions) => Koine.Api.Client<TEndpoints>;
|
|
1
|
+
import type { Api } from "./types";
|
|
2
|
+
export declare let createApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions) => Api.Client<TEndpoints>;
|
|
8
3
|
export default createApi;
|
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
import buildUrlQueryString from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"post",
|
|
16
|
-
"put",
|
|
17
|
-
"patch",
|
|
18
|
-
"delete"
|
|
19
|
-
].reduce((api, method)=>{
|
|
20
|
-
// @ts-expect-error FIXME: type
|
|
21
|
-
api[method] = async (endpoint, options)=>{
|
|
22
|
-
const { request = requestBase, headers = headersBase, timeout = timeoutBase, processReq, processRes = processResBase, processErr = processErrBase, throwErr = throwErrBase } = options || {};
|
|
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 || {};
|
|
23
15
|
let { params, json, query } = options || {};
|
|
24
16
|
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
25
17
|
let requestInit = {
|
|
@@ -27,8 +19,8 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
27
19
|
...request,
|
|
28
20
|
headers: {
|
|
29
21
|
"content-type": "application/json",
|
|
30
|
-
...headers
|
|
31
|
-
}
|
|
22
|
+
...headers,
|
|
23
|
+
},
|
|
32
24
|
};
|
|
33
25
|
if (processReqBase) {
|
|
34
26
|
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
@@ -47,7 +39,7 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
47
39
|
requestInit = transformed[4];
|
|
48
40
|
}
|
|
49
41
|
if (isFullObject(params)) {
|
|
50
|
-
for(const key in params){
|
|
42
|
+
for (const key in params) {
|
|
51
43
|
url = url.replace(`{${key}}`, params[key].toString());
|
|
52
44
|
}
|
|
53
45
|
}
|
|
@@ -59,11 +51,10 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
59
51
|
}
|
|
60
52
|
if (timeoutNumber > 0) {
|
|
61
53
|
controller = new AbortController();
|
|
62
|
-
timeoutId = setTimeout(()=>controller.abort(), timeoutNumber);
|
|
54
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutNumber);
|
|
63
55
|
requestInit.signal = controller.signal;
|
|
64
56
|
}
|
|
65
57
|
if (query) {
|
|
66
|
-
// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
67
58
|
url += buildUrlQueryString(query);
|
|
68
59
|
}
|
|
69
60
|
let response = null;
|
|
@@ -71,7 +62,8 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
71
62
|
let msg = "";
|
|
72
63
|
try {
|
|
73
64
|
response = await fetch(url, requestInit);
|
|
74
|
-
}
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
75
67
|
msg = errorToString(e);
|
|
76
68
|
}
|
|
77
69
|
if (timeoutId) {
|
|
@@ -81,42 +73,38 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
81
73
|
try {
|
|
82
74
|
if (processRes) {
|
|
83
75
|
result = await processRes(response, options || {});
|
|
84
|
-
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
85
78
|
result = await response.json();
|
|
86
79
|
}
|
|
87
|
-
}
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
88
82
|
msg = errorToString(e);
|
|
89
83
|
}
|
|
90
84
|
}
|
|
91
85
|
if (result === null) {
|
|
92
86
|
if (processErr) {
|
|
93
87
|
result = await processErr(msg, options || {});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// there is no specific HTTP error for this, we can consider these
|
|
97
|
-
// two statuses though:
|
|
98
|
-
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
99
|
-
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
100
90
|
result = {
|
|
101
91
|
data: null,
|
|
102
92
|
msg,
|
|
103
93
|
status: 100,
|
|
104
94
|
fail: true,
|
|
105
|
-
ok: false
|
|
95
|
+
ok: false,
|
|
106
96
|
};
|
|
107
97
|
}
|
|
108
98
|
}
|
|
109
99
|
if (throwErr && result?.fail) {
|
|
110
|
-
// throw new ApiError<Failed>(result);
|
|
111
|
-
// I prefer to throw an object literal despite what eslint says
|
|
112
|
-
// eslint-disable-next-line no-throw-literal
|
|
113
100
|
throw result;
|
|
114
101
|
}
|
|
115
|
-
if (process.env["NODE_ENV"]
|
|
102
|
+
if (process.env["NODE_ENV"] === "development") {
|
|
116
103
|
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
117
104
|
if (result?.ok) {
|
|
118
105
|
console.info(`🟢 ${logMsg}`);
|
|
119
|
-
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
120
108
|
console.info(`🔴 ${logMsg}`);
|
|
121
109
|
}
|
|
122
110
|
}
|
|
@@ -125,4 +113,5 @@ import isFullObject from "@koine/utils/isFullObject";
|
|
|
125
113
|
return api;
|
|
126
114
|
}, {});
|
|
127
115
|
};
|
|
128
|
-
|
|
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.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { ApiError } from "./ApiError";
|
|
2
2
|
export { createApi } from "./createApi";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
+
export { createApiResultOk } from "./createApiResultOk";
|
|
5
|
+
export type { Api } from "./types";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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;
|
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.d.ts
ADDED