@koine/api 1.0.62 → 1.0.65

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/createApi.js CHANGED
@@ -13,16 +13,16 @@ export var createApi = function (apiName, baseUrl, options) {
13
13
  // mode: "cors",
14
14
  // redirect: "follow",
15
15
  // cache: "no-cache",
16
- } : _c, exceptionBase = _a.exception, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes;
16
+ } : _c, throwErrBase = _a.throwErr, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes, processErrBase = _a.processErr;
17
17
  return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
18
18
  // @ts-expect-error FIXME: type
19
19
  api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
20
- var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, exception, _g, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, e_1, result, e_2, msg;
21
- return __generator(this, function (_h) {
22
- switch (_h.label) {
20
+ 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;
21
+ return __generator(this, function (_j) {
22
+ switch (_j.label) {
23
23
  case 0:
24
- _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.exception, exception = _f === void 0 ? exceptionBase : _f;
25
- _g = options || {}, params = _g.params, json = _g.json, query = _g.query;
24
+ _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;
25
+ _h = options || {}, params = _h.params, json = _h.json, query = _h.query;
26
26
  url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
27
27
  requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
28
28
  if (processReqBase) {
@@ -59,70 +59,77 @@ export var createApi = function (apiName, baseUrl, options) {
59
59
  // FIXME: ts-expect-error this assertion is not the best, but nevermind for now
60
60
  url += buildUrlQueryString(query);
61
61
  }
62
- if (!exception) return [3 /*break*/, 5];
63
- _h.label = 1;
62
+ response = null;
63
+ result = null;
64
+ msg = "";
65
+ _j.label = 1;
64
66
  case 1:
65
- _h.trys.push([1, 3, , 4]);
67
+ _j.trys.push([1, 3, , 4]);
66
68
  return [4 /*yield*/, fetch(url, requestInit)];
67
69
  case 2:
68
- response = _h.sent();
70
+ response = _j.sent();
69
71
  return [3 /*break*/, 4];
70
72
  case 3:
71
- e_1 = _h.sent();
72
- // eslint-disable-next-line no-throw-literal
73
- throw { e: e_1 };
74
- case 4: return [3 /*break*/, 7];
75
- case 5: return [4 /*yield*/, fetch(url, requestInit)];
76
- case 6:
77
- response = _h.sent();
78
- _h.label = 7;
79
- case 7:
73
+ e_1 = _j.sent();
74
+ msg = e_1;
75
+ return [3 /*break*/, 4];
76
+ case 4:
80
77
  if (timeoutId) {
81
78
  clearTimeout(timeoutId);
82
79
  }
83
- if (!exception) return [3 /*break*/, 15];
84
- _h.label = 8;
85
- case 8:
86
- _h.trys.push([8, 13, , 14]);
87
- if (!processRes) return [3 /*break*/, 10];
80
+ if (!response) return [3 /*break*/, 11];
81
+ _j.label = 5;
82
+ case 5:
83
+ _j.trys.push([5, 10, , 11]);
84
+ if (!processRes) return [3 /*break*/, 7];
88
85
  return [4 /*yield*/, processRes(response, options || {})];
89
- case 9:
90
- result = _h.sent();
91
- return [3 /*break*/, 12];
92
- case 10: return [4 /*yield*/, response.json()];
86
+ case 6:
87
+ result = _j.sent();
88
+ return [3 /*break*/, 9];
89
+ case 7: return [4 /*yield*/, response.json()];
90
+ case 8:
91
+ result = _j.sent();
92
+ _j.label = 9;
93
+ case 9: return [3 /*break*/, 11];
94
+ case 10:
95
+ e_2 = _j.sent();
96
+ msg = e_2;
97
+ return [3 /*break*/, 11];
93
98
  case 11:
94
- result = _h.sent();
95
- _h.label = 12;
96
- case 12: return [3 /*break*/, 14];
99
+ if (!(result === null)) return [3 /*break*/, 14];
100
+ if (!processErr) return [3 /*break*/, 13];
101
+ return [4 /*yield*/, processErr(msg, options || {})];
102
+ case 12:
103
+ result = _j.sent();
104
+ return [3 /*break*/, 14];
97
105
  case 13:
98
- e_2 = _h.sent();
99
- // eslint-disable-next-line no-throw-literal
100
- throw { e: e_2 };
101
- case 14: return [3 /*break*/, 19];
102
- case 15:
103
- if (!processRes) return [3 /*break*/, 17];
104
- return [4 /*yield*/, processRes(response, options || {})];
105
- case 16:
106
- result = _h.sent();
107
- return [3 /*break*/, 19];
108
- case 17: return [4 /*yield*/, response.json()];
109
- case 18:
110
- result = _h.sent();
111
- _h.label = 19;
112
- case 19:
113
- if (exception && result.fail) {
106
+ // this error should only happen on network errors or wrong API urls
107
+ // there is no specific HTTP error for this, we can consider these
108
+ // two statuses though:
109
+ // - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
110
+ // - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
111
+ result = {
112
+ data: null,
113
+ msg: msg,
114
+ status: 100,
115
+ fail: true,
116
+ ok: false,
117
+ };
118
+ _j.label = 14;
119
+ case 14:
120
+ if (throwErr && (result === null || result === void 0 ? void 0 : result.fail)) {
114
121
  // throw new ApiError<Failed>(result);
115
122
  // I prefer to throw an object literal despite what eslint says
116
123
  // eslint-disable-next-line no-throw-literal
117
124
  throw result;
118
125
  }
119
126
  if (process.env["NODE_ENV"] !== "production") {
120
- msg = "".concat(result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
121
- if (result.ok) {
122
- console.log("\uD83D\uDFE2 ".concat(msg));
127
+ logMsg = "".concat(result === null || result === void 0 ? void 0 : result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
128
+ if (result === null || result === void 0 ? void 0 : result.ok) {
129
+ console.log("\uD83D\uDFE2 ".concat(logMsg));
123
130
  }
124
131
  else {
125
- console.log("\uD83D\uDD34 ".concat(msg));
132
+ console.log("\uD83D\uDD34 ".concat(logMsg));
126
133
  }
127
134
  }
128
135
  return [2 /*return*/, result];
package/createSwrApi.js CHANGED
@@ -11,7 +11,7 @@ function createUseApi(api, method) {
11
11
  // try {
12
12
  // const { ok, data } = await api[method](_endpoint, {
13
13
  // ...(options || {}),
14
- // exception: true,
14
+ // throwErr: true,
15
15
  // });
16
16
  // if (ok) {
17
17
  // return data;
@@ -26,7 +26,7 @@ function createUseApi(api, method) {
26
26
  var data;
27
27
  return __generator(this, function (_a) {
28
28
  switch (_a.label) {
29
- case 0: return [4 /*yield*/, api[method](endpoint, __assign(__assign({}, (options || {})), { exception: true }))];
29
+ case 0: return [4 /*yield*/, api[method](endpoint, __assign(__assign({}, (options || {})), { throwErr: true }))];
30
30
  case 1:
31
31
  data = (_a.sent()).data;
32
32
  return [2 /*return*/, data];
@@ -54,7 +54,7 @@ function createUseApi(api, method) {
54
54
  case 0:
55
55
  endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
56
56
  options = Array.isArray(_endpoint) ? _endpoint[1] : {};
57
- return [4 /*yield*/, api[method](endpoint, __assign(__assign(__assign({}, options), (_options.arg || {})), { exception: true }))];
57
+ return [4 /*yield*/, api[method](endpoint, __assign(__assign(__assign({}, options), (_options.arg || {})), { throwErr: true }))];
58
58
  case 1:
59
59
  _a = _b.sent(), ok = _a.ok, data = _a.data;
60
60
  return [2 /*return*/, ok ? data : data];
package/node/createApi.js CHANGED
@@ -16,16 +16,16 @@ var createApi = function (apiName, baseUrl, options) {
16
16
  // mode: "cors",
17
17
  // redirect: "follow",
18
18
  // cache: "no-cache",
19
- } : _c, exceptionBase = _a.exception, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes;
19
+ } : _c, throwErrBase = _a.throwErr, _d = _a.timeout, timeoutBase = _d === void 0 ? 10000 : _d, processReqBase = _a.processReq, processResBase = _a.processRes, processErrBase = _a.processErr;
20
20
  return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
21
21
  // @ts-expect-error FIXME: type
22
22
  api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
23
- var _a, _b, request, _c, headers, _d, timeout, processReq, _e, processRes, _f, exception, _g, params, json, query, url, requestInit, transformed, transformed, key, timeoutNumber, controller, timeoutId, response, e_1, result, e_2, msg;
24
- return tslib_1.__generator(this, function (_h) {
25
- switch (_h.label) {
23
+ 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;
24
+ return tslib_1.__generator(this, function (_j) {
25
+ switch (_j.label) {
26
26
  case 0:
27
- _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.exception, exception = _f === void 0 ? exceptionBase : _f;
28
- _g = options || {}, params = _g.params, json = _g.json, query = _g.query;
27
+ _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;
28
+ _h = options || {}, params = _h.params, json = _h.json, query = _h.query;
29
29
  url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
30
30
  requestInit = tslib_1.__assign(tslib_1.__assign({ method: method.toUpperCase() }, request), { headers: tslib_1.__assign({ "content-type": "application/json" }, headers) });
31
31
  if (processReqBase) {
@@ -62,70 +62,77 @@ var createApi = function (apiName, baseUrl, options) {
62
62
  // FIXME: ts-expect-error this assertion is not the best, but nevermind for now
63
63
  url += (0, utils_1.buildUrlQueryString)(query);
64
64
  }
65
- if (!exception) return [3 /*break*/, 5];
66
- _h.label = 1;
65
+ response = null;
66
+ result = null;
67
+ msg = "";
68
+ _j.label = 1;
67
69
  case 1:
68
- _h.trys.push([1, 3, , 4]);
70
+ _j.trys.push([1, 3, , 4]);
69
71
  return [4 /*yield*/, fetch(url, requestInit)];
70
72
  case 2:
71
- response = _h.sent();
73
+ response = _j.sent();
72
74
  return [3 /*break*/, 4];
73
75
  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:
76
+ e_1 = _j.sent();
77
+ msg = e_1;
78
+ return [3 /*break*/, 4];
79
+ case 4:
83
80
  if (timeoutId) {
84
81
  clearTimeout(timeoutId);
85
82
  }
86
- if (!exception) return [3 /*break*/, 15];
87
- _h.label = 8;
88
- case 8:
89
- _h.trys.push([8, 13, , 14]);
90
- if (!processRes) return [3 /*break*/, 10];
83
+ if (!response) return [3 /*break*/, 11];
84
+ _j.label = 5;
85
+ case 5:
86
+ _j.trys.push([5, 10, , 11]);
87
+ if (!processRes) return [3 /*break*/, 7];
91
88
  return [4 /*yield*/, processRes(response, options || {})];
92
- case 9:
93
- result = _h.sent();
94
- return [3 /*break*/, 12];
95
- case 10: return [4 /*yield*/, response.json()];
89
+ case 6:
90
+ result = _j.sent();
91
+ return [3 /*break*/, 9];
92
+ case 7: return [4 /*yield*/, response.json()];
93
+ case 8:
94
+ result = _j.sent();
95
+ _j.label = 9;
96
+ case 9: return [3 /*break*/, 11];
97
+ case 10:
98
+ e_2 = _j.sent();
99
+ msg = e_2;
100
+ return [3 /*break*/, 11];
96
101
  case 11:
97
- result = _h.sent();
98
- _h.label = 12;
99
- case 12: return [3 /*break*/, 14];
102
+ if (!(result === null)) return [3 /*break*/, 14];
103
+ if (!processErr) return [3 /*break*/, 13];
104
+ return [4 /*yield*/, processErr(msg, options || {})];
105
+ case 12:
106
+ result = _j.sent();
107
+ return [3 /*break*/, 14];
100
108
  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 (!processRes) return [3 /*break*/, 17];
107
- return [4 /*yield*/, processRes(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 (exception && result.fail) {
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: msg,
117
+ status: 100,
118
+ fail: true,
119
+ ok: false,
120
+ };
121
+ _j.label = 14;
122
+ case 14:
123
+ if (throwErr && (result === null || result === void 0 ? void 0 : result.fail)) {
117
124
  // throw new ApiError<Failed>(result);
118
125
  // I prefer to throw an object literal despite what eslint says
119
126
  // eslint-disable-next-line no-throw-literal
120
127
  throw result;
121
128
  }
122
129
  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));
130
+ logMsg = "".concat(result === null || result === void 0 ? void 0 : result.status, ": api[").concat(apiName, "] ").concat(method.toUpperCase(), " ").concat(url);
131
+ if (result === null || result === void 0 ? void 0 : result.ok) {
132
+ console.log("\uD83D\uDFE2 ".concat(logMsg));
126
133
  }
127
134
  else {
128
- console.log("\uD83D\uDD34 ".concat(msg));
135
+ console.log("\uD83D\uDD34 ".concat(logMsg));
129
136
  }
130
137
  }
131
138
  return [2 /*return*/, result];
@@ -14,7 +14,7 @@ function createUseApi(api, method) {
14
14
  // try {
15
15
  // const { ok, data } = await api[method](_endpoint, {
16
16
  // ...(options || {}),
17
- // exception: true,
17
+ // throwErr: true,
18
18
  // });
19
19
  // if (ok) {
20
20
  // return data;
@@ -29,7 +29,7 @@ function createUseApi(api, method) {
29
29
  var data;
30
30
  return tslib_1.__generator(this, function (_a) {
31
31
  switch (_a.label) {
32
- case 0: return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign({}, (options || {})), { exception: true }))];
32
+ case 0: return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign({}, (options || {})), { throwErr: true }))];
33
33
  case 1:
34
34
  data = (_a.sent()).data;
35
35
  return [2 /*return*/, data];
@@ -57,7 +57,7 @@ function createUseApi(api, method) {
57
57
  case 0:
58
58
  endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
59
59
  options = Array.isArray(_endpoint) ? _endpoint[1] : {};
60
- return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, options), (_options.arg || {})), { exception: true }))];
60
+ return [4 /*yield*/, api[method](endpoint, tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, options), (_options.arg || {})), { throwErr: true }))];
61
61
  case 1:
62
62
  _a = _b.sent(), ok = _a.ok, data = _a.data;
63
63
  return [2 /*return*/, ok ? data : data];
package/package.json CHANGED
@@ -5,12 +5,12 @@
5
5
  "typings": "./index.d.ts",
6
6
  "dependencies": {},
7
7
  "peerDependencies": {
8
- "@koine/utils": "1.0.62",
8
+ "@koine/utils": "1.0.65",
9
9
  "swr": "^2.0.0-beta.6",
10
10
  "next": "^12.2.3",
11
11
  "tslib": "^2.4.0"
12
12
  },
13
- "version": "1.0.62",
13
+ "version": "1.0.65",
14
14
  "module": "./index.js",
15
15
  "types": "./index.d.ts"
16
16
  }
package/typings.d.ts CHANGED
@@ -50,11 +50,12 @@ declare namespace Koine.Api {
50
50
  */
51
51
  request?: Omit<RequestInit, "body" | "headers" | "method">;
52
52
  /**
53
- * Flag to throw error with try/catch
53
+ * Flag to throw error within the catch block, by default we return a
54
+ * normalised error result {@link ResultFail}
54
55
  *
55
56
  * @default false
56
57
  */
57
- exception?: boolean;
58
+ throwErr?: boolean;
58
59
  /**
59
60
  * Timeout in `ms`, if `falsy` there is no timeout
60
61
  *
@@ -68,11 +69,17 @@ declare namespace Koine.Api {
68
69
  */
69
70
  processReq?: RequestProcessor;
70
71
  /**
71
- * Process response just after http response
72
+ * Process ok response just after http response
72
73
  *
73
74
  * @default undefined
74
75
  */
75
- processRes?: ResponseProcessor;
76
+ processRes?: ResponseProcessorOk;
77
+ /**
78
+ * Process failed response just after http response
79
+ *
80
+ * @default undefined
81
+ */
82
+ processErr?: ResponseProcessorFail;
76
83
  };
77
84
 
78
85
  type ClientMethod<
@@ -372,16 +379,24 @@ declare namespace Koine.Api {
372
379
  ];
373
380
 
374
381
  /**
375
- * The response processor at the request level, this is meant to apply
376
- * transformations to a single endpoint response
382
+ * The ok response processor at the request level, this is meant to apply
383
+ * transformations to a single or all endpoint responses
384
+ */
385
+ type ResponseProcessorOk = <TResponseOk extends ResponseOk = ResponseOk>(
386
+ response: _Response,
387
+ options: TOptions
388
+ ) => Promise<Koine.Api.Result<TResponseOk>>;
389
+
390
+ /**
391
+ * The fail response processor at the request level, this is meant to apply
392
+ * transformations to a single or all endpoint responses
377
393
  */
378
- type ResponseProcessor = <
379
- TResponseOk extends ResponseOk = ResponseOk,
394
+ type ResponseProcessorFail = <
380
395
  TResponseFail extends ResponseFailed = ResponseFailed
381
396
  >(
382
- response: _Response,
397
+ msg: string,
383
398
  options: TOptions
384
- ) => Promise<Koine.Api.Result<TResponseOk, TResponseFail>>;
399
+ ) => Promise<Koine.Api.Result<TResponseFail>>;
385
400
 
386
401
  //////////////////////////////////////////////////////////////////////////////
387
402
  //