@koine/api 2.0.0-beta.1 → 2.0.0-beta.11

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