@koine/api 2.0.0-beta.10 → 2.0.0-beta.12
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/createApiResultFail.d.ts +2 -0
- package/createApiResultFail.mjs +7 -0
- package/createApiResultOk.d.ts +2 -0
- package/createApiResultOk.mjs +8 -0
- package/index.d.ts +3 -1
- package/index.mjs +3 -1
- package/nextApiResponse.d.ts +4 -2
- package/nextApiResponse.mjs +10 -3
- package/nextApiResponse12.d.ts +6 -0
- package/nextApiResponse12.mjs +13 -0
- package/package.json +4 -4
- package/typings.d.ts +24 -0
- package/ApiError.js +0 -32
- package/createApi.js +0 -142
- package/createSwrApi.js +0 -99
- package/index.js +0 -28
- package/nextApiResponse.js +0 -23
package/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { ApiError } from "./ApiError";
|
|
2
2
|
export { createApi } from "./createApi";
|
|
3
|
+
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
+
export { createApiResultOk } from "./createApiResultOk";
|
|
3
5
|
export { createSwrApi } from "./createSwrApi";
|
|
4
|
-
export {
|
|
6
|
+
export { nextApiResponse12 } from "./nextApiResponse12";
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { ApiError } from "./ApiError";
|
|
2
2
|
export { createApi } from "./createApi";
|
|
3
|
+
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
+
export { createApiResultOk } from "./createApiResultOk";
|
|
3
5
|
export { createSwrApi } from "./createSwrApi";
|
|
4
|
-
export {
|
|
6
|
+
export { nextApiResponse12 } from "./nextApiResponse12";
|
package/nextApiResponse.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export declare const nextApiResponse: {
|
|
2
|
+
ok<T>(data: T, msg?: string): void;
|
|
3
|
+
fail<T_1>(data: T_1, msg?: string, status?: number): void;
|
|
4
|
+
};
|
|
3
5
|
export default nextApiResponse;
|
package/nextApiResponse.mjs
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import createApiResultFail from "./createApiResultFail";
|
|
3
|
+
import createApiResultOk from "./createApiResultOk";
|
|
4
|
+
export const nextApiResponse = {
|
|
5
|
+
ok (data, msg) {
|
|
6
|
+
NextResponse.json(createApiResultOk(data, msg));
|
|
7
|
+
},
|
|
8
|
+
fail (data, msg, status) {
|
|
9
|
+
NextResponse.json(createApiResultFail(data, msg, status));
|
|
10
|
+
}
|
|
4
11
|
};
|
|
5
12
|
export default nextApiResponse;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import createApiResultFail from "./createApiResultFail";
|
|
2
|
+
import createApiResultOk from "./createApiResultOk";
|
|
3
|
+
export const nextApiResponse12 = (nextRes)=>{
|
|
4
|
+
return {
|
|
5
|
+
ok (data, msg) {
|
|
6
|
+
nextRes.status(200).json(createApiResultOk(data, msg));
|
|
7
|
+
},
|
|
8
|
+
fail (data, msg, status) {
|
|
9
|
+
nextRes.status(status || 404).json(createApiResultFail(data, msg, status));
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export default nextApiResponse12;
|
package/package.json
CHANGED
|
@@ -10,15 +10,15 @@
|
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@koine/utils": "2.0.0-beta.
|
|
13
|
+
"@koine/utils": "2.0.0-beta.12"
|
|
14
14
|
},
|
|
15
15
|
"module": "./index.mjs",
|
|
16
16
|
"main": "./index.js",
|
|
17
17
|
"types": "./index.d.ts",
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"next": "^13.4.
|
|
19
|
+
"next": "^13.4.12",
|
|
20
20
|
"swr": "^2.2.0",
|
|
21
|
-
"type-fest": "^
|
|
21
|
+
"type-fest": "^4.1.0"
|
|
22
22
|
},
|
|
23
|
-
"version": "2.0.0-beta.
|
|
23
|
+
"version": "2.0.0-beta.12"
|
|
24
24
|
}
|
package/typings.d.ts
CHANGED
|
@@ -424,6 +424,30 @@ declare namespace Koine.Api {
|
|
|
424
424
|
[K in keyof _ShortcutsMaps as _ShortcutsMaps[K]]: K;
|
|
425
425
|
};
|
|
426
426
|
|
|
427
|
+
/**
|
|
428
|
+
* @example
|
|
429
|
+
* ```ts
|
|
430
|
+
* // define the type on your `API` types:
|
|
431
|
+
* type Result = Koine.Api.GenerateResultShortcuts<Endpoints>;
|
|
432
|
+
*
|
|
433
|
+
* // consume the type wherever in your app:
|
|
434
|
+
* type MyResult = API.Result["get"]["my/endpoint"];
|
|
435
|
+
*
|
|
436
|
+
* MyResult["ok"];
|
|
437
|
+
* ^
|
|
438
|
+
* MyResult["fail"];
|
|
439
|
+
* ^
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
type GenerateResultShortcuts<TEndpoints extends Endpoints> = {
|
|
443
|
+
[TMethod in RequestMethod]: {
|
|
444
|
+
[TEndpointUrl in keyof TEndpoints]: {
|
|
445
|
+
ok: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["ok"];
|
|
446
|
+
fail: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["fail"];
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
};
|
|
450
|
+
|
|
427
451
|
/**
|
|
428
452
|
* @example
|
|
429
453
|
* ```ts
|
package/ApiError.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom `ApiError` class extending `Error` to throw in failed response.
|
|
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
|
-
*/ "use strict";
|
|
8
|
-
Object.defineProperty(exports, "__esModule", {
|
|
9
|
-
value: true
|
|
10
|
-
});
|
|
11
|
-
function _export(target, all) {
|
|
12
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: all[name]
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
_export(exports, {
|
|
18
|
-
ApiError: function() {
|
|
19
|
-
return ApiError;
|
|
20
|
-
},
|
|
21
|
-
default: function() {
|
|
22
|
-
return _default;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
let ApiError = class ApiError extends Error {
|
|
26
|
-
constructor(result){
|
|
27
|
-
super(`Request failed with ${result.status} ${result.msg}`);
|
|
28
|
-
this.name = "ApiError";
|
|
29
|
-
Object.assign(this, result);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const _default = ApiError;
|
package/createApi.js
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
createApi: function() {
|
|
13
|
-
return createApi;
|
|
14
|
-
},
|
|
15
|
-
default: function() {
|
|
16
|
-
return _default;
|
|
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 || {};
|
|
37
|
-
let { params, json, query } = options || {};
|
|
38
|
-
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
39
|
-
let requestInit = {
|
|
40
|
-
method: method.toUpperCase(),
|
|
41
|
-
...request,
|
|
42
|
-
headers: {
|
|
43
|
-
"content-type": "application/json",
|
|
44
|
-
...headers
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
if (processReqBase) {
|
|
48
|
-
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
49
|
-
url = transformed[0];
|
|
50
|
-
query = transformed[1];
|
|
51
|
-
json = transformed[2];
|
|
52
|
-
params = transformed[3];
|
|
53
|
-
requestInit = transformed[4];
|
|
54
|
-
}
|
|
55
|
-
if (processReq) {
|
|
56
|
-
const transformed = processReq(method, url, query, json, params, requestInit);
|
|
57
|
-
url = transformed[0];
|
|
58
|
-
query = transformed[1];
|
|
59
|
-
json = transformed[2];
|
|
60
|
-
params = transformed[3];
|
|
61
|
-
requestInit = transformed[4];
|
|
62
|
-
}
|
|
63
|
-
if ((0, _isFullObject.default)(params)) {
|
|
64
|
-
for(const key in params){
|
|
65
|
-
url = url.replace(`{${key}}`, params[key].toString());
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const timeoutNumber = Number(timeout);
|
|
69
|
-
let controller;
|
|
70
|
-
let timeoutId;
|
|
71
|
-
if (json) {
|
|
72
|
-
requestInit.body = JSON.stringify(json);
|
|
73
|
-
}
|
|
74
|
-
if (timeoutNumber > 0) {
|
|
75
|
-
controller = new AbortController();
|
|
76
|
-
timeoutId = setTimeout(()=>controller.abort(), timeoutNumber);
|
|
77
|
-
requestInit.signal = controller.signal;
|
|
78
|
-
}
|
|
79
|
-
if (query) {
|
|
80
|
-
// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
81
|
-
url += (0, _buildUrlQueryString.default)(query);
|
|
82
|
-
}
|
|
83
|
-
let response = null;
|
|
84
|
-
let result = null;
|
|
85
|
-
let msg = "";
|
|
86
|
-
try {
|
|
87
|
-
response = await fetch(url, requestInit);
|
|
88
|
-
} catch (e) {
|
|
89
|
-
msg = (0, _errorToString.default)(e);
|
|
90
|
-
}
|
|
91
|
-
if (timeoutId) {
|
|
92
|
-
clearTimeout(timeoutId);
|
|
93
|
-
}
|
|
94
|
-
if (response) {
|
|
95
|
-
try {
|
|
96
|
-
if (processRes) {
|
|
97
|
-
result = await processRes(response, options || {});
|
|
98
|
-
} else {
|
|
99
|
-
result = await response.json();
|
|
100
|
-
}
|
|
101
|
-
} catch (e) {
|
|
102
|
-
msg = (0, _errorToString.default)(e);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (result === null) {
|
|
106
|
-
if (processErr) {
|
|
107
|
-
result = await processErr(msg, options || {});
|
|
108
|
-
} else {
|
|
109
|
-
// this error should only happen on network errors or wrong API urls
|
|
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)
|
|
114
|
-
result = {
|
|
115
|
-
data: null,
|
|
116
|
-
msg,
|
|
117
|
-
status: 100,
|
|
118
|
-
fail: true,
|
|
119
|
-
ok: false
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
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
|
-
throw result;
|
|
128
|
-
}
|
|
129
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
130
|
-
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
131
|
-
if (result?.ok) {
|
|
132
|
-
console.info(`🟢 ${logMsg}`);
|
|
133
|
-
} else {
|
|
134
|
-
console.info(`🔴 ${logMsg}`);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return result;
|
|
138
|
-
};
|
|
139
|
-
return api;
|
|
140
|
-
}, {});
|
|
141
|
-
};
|
|
142
|
-
const _default = createApi;
|
package/createSwrApi.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
function _export(target, all) {
|
|
7
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: all[name]
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
_export(exports, {
|
|
13
|
-
createSwrApi: function() {
|
|
14
|
-
return createSwrApi;
|
|
15
|
-
},
|
|
16
|
-
default: function() {
|
|
17
|
-
return _default;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
21
|
-
const _swr = /*#__PURE__*/ _interop_require_default._(require("swr"));
|
|
22
|
-
const _mutation = /*#__PURE__*/ _interop_require_default._(require("swr/mutation"));
|
|
23
|
-
const _isFunction = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/isFunction"));
|
|
24
|
-
const _createApi = /*#__PURE__*/ _interop_require_default._(require("./createApi"));
|
|
25
|
-
function createUseApi(api, method) {
|
|
26
|
-
return function useApi(endpoint, options, _config) {
|
|
27
|
-
if (method === "get") {
|
|
28
|
-
// const fetcher = async (_endpoint: TEndpoint) => {
|
|
29
|
-
// try {
|
|
30
|
-
// const { ok, data } = await api[method](_endpoint, {
|
|
31
|
-
// ...(options || {}),
|
|
32
|
-
// throwErr: true,
|
|
33
|
-
// });
|
|
34
|
-
// if (ok) {
|
|
35
|
-
// return data;
|
|
36
|
-
// }
|
|
37
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
38
|
-
// } catch(e) {
|
|
39
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
40
|
-
// }
|
|
41
|
-
// };
|
|
42
|
-
// }
|
|
43
|
-
const fetcher = async ()=>{
|
|
44
|
-
const { data } = await api[method](endpoint, {
|
|
45
|
-
...options || {},
|
|
46
|
-
throwErr: true
|
|
47
|
-
});
|
|
48
|
-
return data;
|
|
49
|
-
};
|
|
50
|
-
const config = _config;
|
|
51
|
-
const shouldNotFetch = config?.when === false || (0, _isFunction.default)(config?.when) && config?.when() === false;
|
|
52
|
-
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
53
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
54
|
-
return (0, _swr.default)(shouldNotFetch ? null : options ? [
|
|
55
|
-
endpoint,
|
|
56
|
-
options
|
|
57
|
-
] : [
|
|
58
|
-
endpoint
|
|
59
|
-
], fetcher, config);
|
|
60
|
-
}
|
|
61
|
-
const config = _config;
|
|
62
|
-
const sender = async (// if the first argument is an array the second tem are the base options
|
|
63
|
-
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
64
|
-
// by the _options just here below
|
|
65
|
-
_endpoint, // these are the options arriving when calling `trigger({ json, query, etc... })
|
|
66
|
-
_options)=>{
|
|
67
|
-
const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
68
|
-
const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
69
|
-
const { ok, data } = await api[method](endpoint, {
|
|
70
|
-
...options,
|
|
71
|
-
..._options.arg || {},
|
|
72
|
-
throwErr: true
|
|
73
|
-
});
|
|
74
|
-
return ok ? data : data;
|
|
75
|
-
};
|
|
76
|
-
// config.fetcher = sender;
|
|
77
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
78
|
-
return (0, _mutation.default)(// @ts-expect-error FIXME: I can't get it...
|
|
79
|
-
options ? [
|
|
80
|
-
endpoint,
|
|
81
|
-
options
|
|
82
|
-
] : endpoint, sender, config);
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
const createSwrApi = (...args)=>{
|
|
86
|
-
const api = (0, _createApi.default)(...args);
|
|
87
|
-
[
|
|
88
|
-
"get",
|
|
89
|
-
"post",
|
|
90
|
-
"put",
|
|
91
|
-
"patch",
|
|
92
|
-
"delete"
|
|
93
|
-
].forEach((method)=>{
|
|
94
|
-
const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
|
|
95
|
-
api[hookName] = createUseApi(api, method);
|
|
96
|
-
});
|
|
97
|
-
return api;
|
|
98
|
-
};
|
|
99
|
-
const _default = createSwrApi;
|
package/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
ApiError: function() {
|
|
13
|
-
return _ApiError.ApiError;
|
|
14
|
-
},
|
|
15
|
-
createApi: function() {
|
|
16
|
-
return _createApi.createApi;
|
|
17
|
-
},
|
|
18
|
-
createSwrApi: function() {
|
|
19
|
-
return _createSwrApi.createSwrApi;
|
|
20
|
-
},
|
|
21
|
-
nextApiResponse: function() {
|
|
22
|
-
return _nextApiResponse.nextApiResponse;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
const _ApiError = require("./ApiError");
|
|
26
|
-
const _createApi = require("./createApi");
|
|
27
|
-
const _createSwrApi = require("./createSwrApi");
|
|
28
|
-
const _nextApiResponse = require("./nextApiResponse");
|
package/nextApiResponse.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
nextApiResponse: function() {
|
|
13
|
-
return nextApiResponse;
|
|
14
|
-
},
|
|
15
|
-
default: function() {
|
|
16
|
-
return _default;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const nextApiResponse = (nextRes, result)=>{
|
|
20
|
-
// nextRes.status(result.status).json(result.data || result.msg);
|
|
21
|
-
nextRes.status(result.status).json(result);
|
|
22
|
-
};
|
|
23
|
-
const _default = nextApiResponse;
|