api-def 0.12.0 → 0.12.1

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/cjs/Api.d.ts CHANGED
@@ -48,10 +48,10 @@ export declare class Api implements ApiInfo {
48
48
  getConfig(): BaseRequestConfig;
49
49
  computeRequestConfig(): BaseRequestConfig;
50
50
  private hotRequest;
51
- get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
52
- post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
53
- put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
54
- delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
55
- patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
51
+ get: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
52
+ post: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
53
+ put: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
54
+ delete: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
55
+ patch: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
56
56
  resolveUrl(options: ApiResolveUrlOptions): URL;
57
57
  }
package/cjs/Api.js CHANGED
@@ -102,12 +102,14 @@ var Api = /** @class */ (function () {
102
102
  this.mutable = false;
103
103
  }*/
104
104
  this.hotRequest = function (requestMethod) {
105
- return function (path, config) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
106
- switch (_a.label) {
107
- case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path, requestMethod), config, null)];
108
- case 1: return [2 /*return*/, _a.sent()];
109
- }
110
- }); }); };
105
+ return function (path, config) { return __awaiter(_this, void 0, void 0, function () {
106
+ return __generator(this, function (_a) {
107
+ switch (_a.label) {
108
+ case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path instanceof URL ? path.href : path, requestMethod), config, null)];
109
+ case 1: return [2 /*return*/, _a.sent()];
110
+ }
111
+ });
112
+ }); };
111
113
  };
112
114
  this.get = this.hotRequest(ApiConstants_1.RequestMethod.GET);
113
115
  this.post = this.hotRequest(ApiConstants_1.RequestMethod.POST);
package/cjs/ApiUtils.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { ResponseType } from "./ApiConstants";
2
2
  import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
3
3
  export interface ResolveUrlOptions {
4
- path: string;
4
+ path: string | URL;
5
5
  baseUrl: string;
6
6
  }
7
7
  export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
8
8
  export declare const isNetworkError: (error: Error) => boolean;
9
+ export declare const isAbsoluteUrl: (url: string) => boolean;
9
10
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
10
11
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
11
12
  export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined, allowMissing?: boolean) => string;
package/cjs/ApiUtils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveUrl = exports.resolvePathParams = exports.inferResponseType = exports.isAcceptableStatus = exports.isNetworkError = exports.isCancelledError = void 0;
3
+ exports.resolveUrl = exports.resolvePathParams = exports.inferResponseType = exports.isAcceptableStatus = exports.isAbsoluteUrl = exports.isNetworkError = exports.isCancelledError = void 0;
4
4
  var isCancelledError = function (error) {
5
5
  return "isCancelledRequest" in error;
6
6
  };
@@ -12,6 +12,10 @@ var isNetworkError = function (error) {
12
12
  ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
13
13
  };
14
14
  exports.isNetworkError = isNetworkError;
15
+ var isAbsoluteUrl = function (url) {
16
+ return /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
17
+ };
18
+ exports.isAbsoluteUrl = isAbsoluteUrl;
15
19
  var DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
16
20
  var isAcceptableStatus = function (status, acceptableStatus) {
17
21
  var acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
@@ -56,7 +60,7 @@ var inferResponseType = function (contentType) {
56
60
  };
57
61
  exports.inferResponseType = inferResponseType;
58
62
  var resolvePathParams = function (path, params, allowMissing) {
59
- var computedPath = path.startsWith("/") ? path : "/".concat(path);
63
+ var computedPath = path.startsWith("/") || (0, exports.isAbsoluteUrl)(path) ? path : "/".concat(path);
60
64
  if (params) {
61
65
  var computedPathParts = computedPath.split("/");
62
66
  var unusedKeys = new Set(Object.keys(params));
@@ -91,6 +95,12 @@ var resolvePathParams = function (path, params, allowMissing) {
91
95
  exports.resolvePathParams = resolvePathParams;
92
96
  var resolveUrl = function (options) {
93
97
  var baseUrl = options.baseUrl, path = options.path;
98
+ if (path instanceof URL) {
99
+ return new URL(path.href);
100
+ }
101
+ if ((0, exports.isAbsoluteUrl)(path)) {
102
+ return new URL(path);
103
+ }
94
104
  var result = !baseUrl.endsWith("/") ? "".concat(baseUrl, "/") : baseUrl;
95
105
  result += path.startsWith("/") ? path.substring(1) : path;
96
106
  var origin = undefined;
@@ -98,7 +108,7 @@ var resolveUrl = function (options) {
98
108
  origin = window.origin;
99
109
  }
100
110
  if (!origin) {
101
- if (result.indexOf("://") === -1) {
111
+ if (!(0, exports.isAbsoluteUrl)(result)) {
102
112
  result = "https://".concat(result);
103
113
  }
104
114
  }
package/esm/Api.d.ts CHANGED
@@ -48,10 +48,10 @@ export declare class Api implements ApiInfo {
48
48
  getConfig(): BaseRequestConfig;
49
49
  computeRequestConfig(): BaseRequestConfig;
50
50
  private hotRequest;
51
- get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
52
- post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
53
- put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
54
- delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
55
- patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
51
+ get: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
52
+ post: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
53
+ put: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
54
+ delete: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
55
+ patch: <R = unknown>(path: string | URL, config: RequestConfig) => Promise<ApiResponse<R>>;
56
56
  resolveUrl(options: ApiResolveUrlOptions): URL;
57
57
  }
package/esm/Api.js CHANGED
@@ -62,7 +62,9 @@ export class Api {
62
62
  Object.assign(this.info, info);
63
63
  this.mutable = false;
64
64
  }*/
65
- this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () { return yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null); });
65
+ this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () {
66
+ return yield Requester.submit(new HotRequestHost(this, path instanceof URL ? path.href : path, requestMethod), config, null);
67
+ });
66
68
  this.get = this.hotRequest(RequestMethod.GET);
67
69
  this.post = this.hotRequest(RequestMethod.POST);
68
70
  this.put = this.hotRequest(RequestMethod.PUT);
package/esm/ApiUtils.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { ResponseType } from "./ApiConstants";
2
2
  import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
3
3
  export interface ResolveUrlOptions {
4
- path: string;
4
+ path: string | URL;
5
5
  baseUrl: string;
6
6
  }
7
7
  export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
8
8
  export declare const isNetworkError: (error: Error) => boolean;
9
+ export declare const isAbsoluteUrl: (url: string) => boolean;
9
10
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
10
11
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
11
12
  export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined, allowMissing?: boolean) => string;
package/esm/ApiUtils.js CHANGED
@@ -7,6 +7,9 @@ export const isNetworkError = (error) => {
7
7
  error.message === "Network Error" ||
8
8
  ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
9
9
  };
10
+ export const isAbsoluteUrl = (url) => {
11
+ return /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
12
+ };
10
13
  const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
11
14
  export const isAcceptableStatus = (status, acceptableStatus) => {
12
15
  const acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
@@ -48,7 +51,7 @@ export const inferResponseType = (contentType) => {
48
51
  return "text";
49
52
  };
50
53
  export const resolvePathParams = (path, params, allowMissing) => {
51
- const computedPath = path.startsWith("/") ? path : `/${path}`;
54
+ const computedPath = path.startsWith("/") || isAbsoluteUrl(path) ? path : `/${path}`;
52
55
  if (params) {
53
56
  const computedPathParts = computedPath.split("/");
54
57
  const unusedKeys = new Set(Object.keys(params));
@@ -82,6 +85,12 @@ export const resolvePathParams = (path, params, allowMissing) => {
82
85
  };
83
86
  export const resolveUrl = (options) => {
84
87
  const { baseUrl, path } = options;
88
+ if (path instanceof URL) {
89
+ return new URL(path.href);
90
+ }
91
+ if (isAbsoluteUrl(path)) {
92
+ return new URL(path);
93
+ }
85
94
  let result = !baseUrl.endsWith("/") ? `${baseUrl}/` : baseUrl;
86
95
  result += path.startsWith("/") ? path.substring(1) : path;
87
96
  let origin = undefined;
@@ -89,7 +98,7 @@ export const resolveUrl = (options) => {
89
98
  origin = window.origin;
90
99
  }
91
100
  if (!origin) {
92
- if (result.indexOf("://") === -1) {
101
+ if (!isAbsoluteUrl(result)) {
93
102
  result = `https://${result}`;
94
103
  }
95
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",