@koine/api 1.0.39 → 1.0.42

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/core/index.js CHANGED
@@ -34,12 +34,17 @@ export var createApi = function (apiName, baseUrl, options) {
34
34
  } : _b, shouldThrowBase = _a.shouldThrow;
35
35
  return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
36
36
  api[method] = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () {
37
- var _a, json, params, _b, request, _c, headers, _d, timeout, _e, transformRequest, _f, transformResponse, _g, shouldThrow, requestInit, timeoutNumber, controller, timeoutId, url, response, e_1, result, e_2, msg;
37
+ var _a, json, path, params, _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
38
  return __generator(this, function (_h) {
39
39
  switch (_h.label) {
40
40
  case 0:
41
- _a = options || {}, json = _a.json, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _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;
41
+ _a = options || {}, json = _a.json, path = _a.path, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _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
42
  requestInit = __assign(__assign({ method: method.toUpperCase() }, request), { headers: __assign({ "content-type": "application/json" }, headers) });
43
+ if (path && Object.keys(path).length) {
44
+ for (key in path) {
45
+ endpoint = endpoint.replace("{".concat(key, "}"), path[key].toString());
46
+ }
47
+ }
43
48
  timeoutNumber = Number(timeout);
44
49
  url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
45
50
  if (method !== "get" && json) {
@@ -37,12 +37,17 @@ var createApi = function (apiName, baseUrl, options) {
37
37
  } : _b, shouldThrowBase = _a.shouldThrow;
38
38
  return ["get", "post", "put", "patch", "delete"].reduce(function (api, method) {
39
39
  api[method] = function (endpoint, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
40
- var _a, json, params, _b, request, _c, headers, _d, timeout, _e, transformRequest, _f, transformResponse, _g, shouldThrow, requestInit, timeoutNumber, controller, timeoutId, url, response, e_1, result, e_2, msg;
40
+ var _a, json, path, params, _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;
41
41
  return tslib_1.__generator(this, function (_h) {
42
42
  switch (_h.label) {
43
43
  case 0:
44
- _a = options || {}, json = _a.json, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _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;
44
+ _a = options || {}, json = _a.json, path = _a.path, params = _a.params, _b = _a.request, request = _b === void 0 ? requestBase : _b, _c = _a.headers, headers = _c === void 0 ? {} : _c, _d = _a.timeout, timeout = _d === void 0 ? 10000 : _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;
45
45
  requestInit = tslib_1.__assign(tslib_1.__assign({ method: method.toUpperCase() }, request), { headers: tslib_1.__assign({ "content-type": "application/json" }, headers) });
46
+ if (path && Object.keys(path).length) {
47
+ for (key in path) {
48
+ endpoint = endpoint.replace("{".concat(key, "}"), path[key].toString());
49
+ }
50
+ }
46
51
  timeoutNumber = Number(timeout);
47
52
  url = "".concat(baseUrl, "/").concat(endpoint + "".replace(/^\/*/, ""));
48
53
  if (method !== "get" && json) {
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.39",
8
+ "@koine/utils": "1.0.42",
9
9
  "next": "^12.1.6",
10
10
  "swr": "^2.0.0-beta.5",
11
11
  "tslib": "^2.4.0"
12
12
  },
13
- "version": "1.0.39",
13
+ "version": "1.0.42",
14
14
  "module": "./index.js",
15
15
  "types": "./index.d.ts"
16
16
  }
package/typings.d.ts CHANGED
@@ -204,6 +204,15 @@ declare namespace Koine.Api {
204
204
  * @default { credentials: "include", referrerPolicy: "no-referrer" }
205
205
  */
206
206
  request?: Omit<RequestInit, "body" | "headers" | "method">;
207
+ /**
208
+ * A dictionary to dynamically interpolate endpoint url values, e.g.:
209
+ *
210
+ * ```
211
+ * "user/{id}" endpoint with { path: { id: "12" }} results in a call
212
+ * to the endpoint "user/12"
213
+ * ```
214
+ */
215
+ path?: Record<string, string | number>;
207
216
  };
208
217
 
209
218
  /**