@koine/api 1.0.78 → 1.0.82
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/package.json +3 -2
- package/core/index.d.ts +0 -17
- package/core/index.js +0 -137
- package/core/package.json +0 -6
- package/next/index.d.ts +0 -2
- package/next/index.js +0 -4
- package/next/package.json +0 -6
- package/swr/index.d.ts +0 -14
- package/swr/index.js +0 -82
- package/swr/package.json +0 -6
package/package.json
CHANGED
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
"main": "./node/index.js",
|
|
5
5
|
"typings": "./index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@koine/utils": "1.0.
|
|
7
|
+
"@koine/utils": "1.0.82",
|
|
8
|
+
"ts-debounce": "^4.0.0",
|
|
8
9
|
"date-fns-tz": "^1.3.7",
|
|
9
10
|
"swr": "^2.0.0-beta.6",
|
|
10
11
|
"next": "^12.2.5",
|
|
11
12
|
"tslib": "^2.4.0"
|
|
12
13
|
},
|
|
13
14
|
"peerDependencies": {},
|
|
14
|
-
"version": "1.0.
|
|
15
|
+
"version": "1.0.82",
|
|
15
16
|
"module": "./index.js",
|
|
16
17
|
"types": "./index.d.ts"
|
|
17
18
|
}
|
package/core/index.d.ts
DELETED
|
@@ -1,17 +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
|
-
*/
|
|
8
|
-
export declare class ApiError<TResponseFail extends Koine.Api.ResponseFail = unknown> extends Error {
|
|
9
|
-
constructor(result: Koine.Api.ResultFail<TResponseFail>);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Create api client
|
|
13
|
-
*
|
|
14
|
-
* @param apiName Short name to use in debug logs
|
|
15
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
16
|
-
*/
|
|
17
|
-
export declare const createApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions) => Koine.Api.Client<TEndpoints>;
|
package/core/index.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { __assign, __awaiter, __extends, __generator } from "tslib";
|
|
2
|
-
import { buildUrlQueryString, isFullObject } from "@koine/utils";
|
|
3
|
-
/**
|
|
4
|
-
* Custom `ApiError` class extending `Error` to throw in failed response.
|
|
5
|
-
*
|
|
6
|
-
* @see https://eslint.org/docs/rules/no-throw-literal
|
|
7
|
-
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
8
|
-
*
|
|
9
|
-
*/
|
|
10
|
-
var ApiError = /** @class */ (function (_super) {
|
|
11
|
-
__extends(ApiError, _super);
|
|
12
|
-
function ApiError(result) {
|
|
13
|
-
var _this = _super.call(this, "Request failed with ".concat(result.status, " ").concat(result.msg)) || this;
|
|
14
|
-
_this.name = "ApiError";
|
|
15
|
-
Object.assign(_this, result);
|
|
16
|
-
return _this;
|
|
17
|
-
}
|
|
18
|
-
return ApiError;
|
|
19
|
-
}(Error));
|
|
20
|
-
export { ApiError };
|
|
21
|
-
/**
|
|
22
|
-
* Create api client
|
|
23
|
-
*
|
|
24
|
-
* @param apiName Short name to use in debug logs
|
|
25
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
26
|
-
*/
|
|
27
|
-
export var createApi = function (apiName, baseUrl, options) {
|
|
28
|
-
var _a = options || {}, _b = _a.headers, headersBase = _b === void 0 ? {} : _b, _c = _a.request, requestBase = _c === void 0 ? {
|
|
29
|
-
credentials: "include",
|
|
30
|
-
referrerPolicy: "no-referrer",
|
|
31
|
-
// mode: "cors",
|
|
32
|
-
// redirect: "follow",
|
|
33
|
-
// cache: "no-cache",
|
|
34
|
-
} : _c, shouldThrowBase = _a.shouldThrow, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, transformRequestBase = _a.transformRequest, transformResponseBase = _a.transformResponse;
|
|
35
|
-
return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
|
|
36
|
-
api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
37
|
-
var _a, params, json, query, _b, request, _c, headers, _d, timeout, _e, transformRequest, _f, transformResponse, _g, shouldThrow, requestInit, key, timeoutNumber, controller, timeoutId, url, response, e_1, result, e_2, msg;
|
|
38
|
-
return __generator(this, function (_h) {
|
|
39
|
-
switch (_h.label) {
|
|
40
|
-
case 0:
|
|
41
|
-
_a = options || {}, params = _a.params, json = _a.json, query = _a.query, _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, _e = _a.transformRequest, transformRequest = _e === void 0 ? transformRequestBase : _e, _f = _a.transformResponse, transformResponse = _f === void 0 ? transformResponseBase : _f, _g = _a.shouldThrow, shouldThrow = _g === void 0 ? shouldThrowBase : _g;
|
|
42
|
-
requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
|
|
43
|
-
if (isFullObject(params)) {
|
|
44
|
-
for (key in params) {
|
|
45
|
-
endpoint = endpoint.replace("{".concat(key, "}"), params[key].toString());
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
timeoutNumber = Number(timeout);
|
|
49
|
-
url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
|
|
50
|
-
if (method !== "get" && json) {
|
|
51
|
-
requestInit.body = JSON.stringify(json);
|
|
52
|
-
}
|
|
53
|
-
if (timeoutNumber > 0) {
|
|
54
|
-
controller = new AbortController();
|
|
55
|
-
timeoutId = setTimeout(function () { return controller.abort(); }, timeoutNumber);
|
|
56
|
-
requestInit.signal = controller.signal;
|
|
57
|
-
}
|
|
58
|
-
if (transformRequest) {
|
|
59
|
-
requestInit = transformRequest(requestInit);
|
|
60
|
-
}
|
|
61
|
-
if (query) {
|
|
62
|
-
// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
63
|
-
url += buildUrlQueryString(query);
|
|
64
|
-
}
|
|
65
|
-
if (!shouldThrow) return [3 /*break*/, 5];
|
|
66
|
-
_h.label = 1;
|
|
67
|
-
case 1:
|
|
68
|
-
_h.trys.push([1, 3, , 4]);
|
|
69
|
-
return [4 /*yield*/, fetch(url, requestInit)];
|
|
70
|
-
case 2:
|
|
71
|
-
response = _h.sent();
|
|
72
|
-
return [3 /*break*/, 4];
|
|
73
|
-
case 3:
|
|
74
|
-
e_1 = _h.sent();
|
|
75
|
-
// eslint-disable-next-line no-throw-literal
|
|
76
|
-
throw { e: e_1 };
|
|
77
|
-
case 4: return [3 /*break*/, 7];
|
|
78
|
-
case 5: return [4 /*yield*/, fetch(url, requestInit)];
|
|
79
|
-
case 6:
|
|
80
|
-
response = _h.sent();
|
|
81
|
-
_h.label = 7;
|
|
82
|
-
case 7:
|
|
83
|
-
if (timeoutId) {
|
|
84
|
-
clearTimeout(timeoutId);
|
|
85
|
-
}
|
|
86
|
-
if (!shouldThrow) return [3 /*break*/, 15];
|
|
87
|
-
_h.label = 8;
|
|
88
|
-
case 8:
|
|
89
|
-
_h.trys.push([8, 13, , 14]);
|
|
90
|
-
if (!transformResponse) return [3 /*break*/, 10];
|
|
91
|
-
return [4 /*yield*/, transformResponse(response, options || {})];
|
|
92
|
-
case 9:
|
|
93
|
-
result = _h.sent();
|
|
94
|
-
return [3 /*break*/, 12];
|
|
95
|
-
case 10: return [4 /*yield*/, response.json()];
|
|
96
|
-
case 11:
|
|
97
|
-
result = _h.sent();
|
|
98
|
-
_h.label = 12;
|
|
99
|
-
case 12: return [3 /*break*/, 14];
|
|
100
|
-
case 13:
|
|
101
|
-
e_2 = _h.sent();
|
|
102
|
-
// eslint-disable-next-line no-throw-literal
|
|
103
|
-
throw { e: e_2 };
|
|
104
|
-
case 14: return [3 /*break*/, 19];
|
|
105
|
-
case 15:
|
|
106
|
-
if (!transformResponse) return [3 /*break*/, 17];
|
|
107
|
-
return [4 /*yield*/, transformResponse(response, options || {})];
|
|
108
|
-
case 16:
|
|
109
|
-
result = _h.sent();
|
|
110
|
-
return [3 /*break*/, 19];
|
|
111
|
-
case 17: return [4 /*yield*/, response.json()];
|
|
112
|
-
case 18:
|
|
113
|
-
result = _h.sent();
|
|
114
|
-
_h.label = 19;
|
|
115
|
-
case 19:
|
|
116
|
-
if (shouldThrow && result.fail) {
|
|
117
|
-
// throw new ApiError<Failed>(result);
|
|
118
|
-
// I prefer to throw an object literal despite what eslint says
|
|
119
|
-
// eslint-disable-next-line no-throw-literal
|
|
120
|
-
throw result;
|
|
121
|
-
}
|
|
122
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
123
|
-
msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
|
|
124
|
-
if (result.ok) {
|
|
125
|
-
console.log("\uD83D\uDFE2 ".concat(msg));
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
console.log("\uD83D\uDD34 ".concat(msg));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return [2 /*return*/, result];
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}); };
|
|
135
|
-
return api;
|
|
136
|
-
}, {});
|
|
137
|
-
};
|
package/core/package.json
DELETED
package/next/index.d.ts
DELETED
package/next/index.js
DELETED
package/next/package.json
DELETED
package/swr/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type SWRConfiguration, type SWRResponse } from "swr";
|
|
2
|
-
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
3
|
-
declare type KoineApiMethodHookSWR<THookName extends keyof Koine.Api.HooksMapsByName, TEndpoints extends Koine.Api.Endpoints> = <TEndpoint extends Koine.Api.EndpointUrl<TEndpoints>, TMethod extends Koine.Api.RequestMethod = Koine.Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Koine.Api.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfiguration<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.EndpointRequestOptions<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.EndpointRequestOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>;
|
|
4
|
-
/**
|
|
5
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
6
|
-
*/
|
|
7
|
-
export declare const createSwrApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions | undefined) => Koine.Api.Client<TEndpoints> & {
|
|
8
|
-
useGet: KoineApiMethodHookSWR<"useGet", TEndpoints>;
|
|
9
|
-
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
10
|
-
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
11
|
-
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
12
|
-
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
13
|
-
};
|
|
14
|
-
export {};
|
package/swr/index.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator } from "tslib";
|
|
2
|
-
import useSWR from "swr";
|
|
3
|
-
import useSWRMutation from "swr/mutation";
|
|
4
|
-
import { createApi } from "../core";
|
|
5
|
-
function createUseApi(api, method) {
|
|
6
|
-
return function useApi(endpoint, options, _config) {
|
|
7
|
-
var _this = this;
|
|
8
|
-
if (method === "get") {
|
|
9
|
-
// const fetcher = async (_endpoint: TEndpoint) => {
|
|
10
|
-
// try {
|
|
11
|
-
// const { ok, data } = await api[method](_endpoint, {
|
|
12
|
-
// ...(options || {}),
|
|
13
|
-
// shouldThrow: true,
|
|
14
|
-
// });
|
|
15
|
-
// if (ok) {
|
|
16
|
-
// return data;
|
|
17
|
-
// }
|
|
18
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
19
|
-
// } catch(e) {
|
|
20
|
-
// throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
21
|
-
// }
|
|
22
|
-
// };
|
|
23
|
-
// }
|
|
24
|
-
var fetcher = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
25
|
-
var data;
|
|
26
|
-
return __generator(this, function (_a) {
|
|
27
|
-
switch (_a.label) {
|
|
28
|
-
case 0: return [4 /*yield*/, api[method](endpoint, __assign(__assign({}, (options || {})), { shouldThrow: true }))];
|
|
29
|
-
case 1:
|
|
30
|
-
data = (_a.sent()).data;
|
|
31
|
-
return [2 /*return*/, data];
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}); };
|
|
35
|
-
var config_1 = _config;
|
|
36
|
-
// <Data = any, Error = any>(key: Key, config: SWRConfiguration<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
37
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
38
|
-
return useSWR(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
39
|
-
}
|
|
40
|
-
var config = _config;
|
|
41
|
-
var sender = function (
|
|
42
|
-
// if the first argument is an array the second tem are the base options
|
|
43
|
-
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
44
|
-
// by the _options just here below
|
|
45
|
-
_endpoint,
|
|
46
|
-
// these are the options arriving when calling `trigger({ json, query, etc... })
|
|
47
|
-
_options) { return __awaiter(_this, void 0, void 0, function () {
|
|
48
|
-
var endpoint, options, _a, ok, data;
|
|
49
|
-
return __generator(this, function (_b) {
|
|
50
|
-
switch (_b.label) {
|
|
51
|
-
case 0:
|
|
52
|
-
endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
53
|
-
options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
54
|
-
return [4 /*yield*/, api[method](endpoint, __assign(__assign(__assign({}, options), (_options.arg || {})), { shouldThrow: true }))];
|
|
55
|
-
case 1:
|
|
56
|
-
_a = _b.sent(), ok = _a.ok, data = _a.data;
|
|
57
|
-
return [2 /*return*/, ok ? data : data];
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}); };
|
|
61
|
-
// config.fetcher = sender;
|
|
62
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
63
|
-
return useSWRMutation(
|
|
64
|
-
// @ts-expect-error FIXME: I can't get it...
|
|
65
|
-
options ? [endpoint, options] : endpoint, sender, config);
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
70
|
-
*/
|
|
71
|
-
export var createSwrApi = function () {
|
|
72
|
-
var args = [];
|
|
73
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
74
|
-
args[_i] = arguments[_i];
|
|
75
|
-
}
|
|
76
|
-
var api = createApi.apply(void 0, args);
|
|
77
|
-
["get", "post", "put", "patch", "delete"].forEach(function (method) {
|
|
78
|
-
var hookName = "use".concat(method.charAt(0).toUpperCase() + method.slice(1));
|
|
79
|
-
api[hookName] = createUseApi(api, method);
|
|
80
|
-
});
|
|
81
|
-
return api;
|
|
82
|
-
};
|