@meowinc/meow-sdk 0.14.3 → 0.15.0
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/dist/http-client.d.ts +3 -0
- package/dist/http-client.js +25 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/package.json +1 -1
package/dist/http-client.d.ts
CHANGED
|
@@ -2,13 +2,16 @@ import type { AuthorizationProvider } from "./auth-provider";
|
|
|
2
2
|
import { HttpError, type AppError } from "./error";
|
|
3
3
|
import { Result } from "@meowinc/result";
|
|
4
4
|
export declare class HttpClient {
|
|
5
|
+
private timeout?;
|
|
5
6
|
private url;
|
|
6
7
|
private headers;
|
|
7
8
|
private method;
|
|
8
9
|
private body;
|
|
9
10
|
private params;
|
|
10
11
|
private authorization?;
|
|
12
|
+
static setGlobalTimeout(timeout?: number): void;
|
|
11
13
|
withUrl(url: string): this;
|
|
14
|
+
withTimeout(timeout: number): this;
|
|
12
15
|
withParam(key: string, value?: string | number | boolean): this;
|
|
13
16
|
withMethodGet(): this;
|
|
14
17
|
withMethodPost(): this;
|
package/dist/http-client.js
CHANGED
|
@@ -2,17 +2,27 @@ import { HttpError } from "./error";
|
|
|
2
2
|
import { AccessToken } from "./token";
|
|
3
3
|
import { ACCOUNT_API } from "./account";
|
|
4
4
|
import { Result } from "@meowinc/result";
|
|
5
|
+
import { MeowUtils } from "./utils";
|
|
6
|
+
let MEOW_GLOBAL_TIMEOUT = undefined;
|
|
5
7
|
export class HttpClient {
|
|
8
|
+
timeout;
|
|
6
9
|
url = "";
|
|
7
10
|
headers = {};
|
|
8
11
|
method = "GET";
|
|
9
12
|
body = null;
|
|
10
13
|
params = {};
|
|
11
14
|
authorization;
|
|
15
|
+
static setGlobalTimeout(timeout) {
|
|
16
|
+
MEOW_GLOBAL_TIMEOUT = timeout;
|
|
17
|
+
}
|
|
12
18
|
withUrl(url) {
|
|
13
19
|
this.url = url;
|
|
14
20
|
return this;
|
|
15
21
|
}
|
|
22
|
+
withTimeout(timeout) {
|
|
23
|
+
this.timeout = timeout;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
16
26
|
withParam(key, value) {
|
|
17
27
|
if (value === undefined) {
|
|
18
28
|
return this;
|
|
@@ -82,11 +92,25 @@ export class HttpClient {
|
|
|
82
92
|
this.setHeader("Authorization", `Bearer ${accessToken}`);
|
|
83
93
|
}
|
|
84
94
|
}
|
|
85
|
-
const
|
|
95
|
+
const responsePromise = fetch(this.url, {
|
|
86
96
|
method: this.method,
|
|
87
97
|
headers: this.headers,
|
|
88
98
|
body: this.body,
|
|
89
99
|
});
|
|
100
|
+
let timeout;
|
|
101
|
+
if (this.timeout !== undefined) {
|
|
102
|
+
timeout = this.timeout;
|
|
103
|
+
}
|
|
104
|
+
else if (MEOW_GLOBAL_TIMEOUT !== undefined) {
|
|
105
|
+
timeout = MEOW_GLOBAL_TIMEOUT;
|
|
106
|
+
}
|
|
107
|
+
let response;
|
|
108
|
+
if (timeout !== undefined) {
|
|
109
|
+
response = await MeowUtils.timeout(responsePromise, timeout);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
response = await responsePromise;
|
|
113
|
+
}
|
|
90
114
|
if (response.ok) {
|
|
91
115
|
const contentType = response.headers.get("content-type");
|
|
92
116
|
if (contentType && contentType.includes("application/json")) {
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export { BrowserUtils, CookieStorage } from "./browser";
|
|
|
3
3
|
export declare class MeowUtils {
|
|
4
4
|
static createAuthorizationSession(login: string, password: string): AuthorizationSession;
|
|
5
5
|
static checkRequiredPermissions(expected: string[], permissions: string[]): boolean;
|
|
6
|
+
static timeout<T>(promise: Promise<T>, ms: number): Promise<T>;
|
|
6
7
|
}
|
package/dist/utils/index.js
CHANGED
|
@@ -7,4 +7,21 @@ export class MeowUtils {
|
|
|
7
7
|
static checkRequiredPermissions(expected, permissions) {
|
|
8
8
|
return permissions.every((permission) => expected.includes(permission));
|
|
9
9
|
}
|
|
10
|
+
static timeout(promise, ms) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const timer = setTimeout(() => {
|
|
13
|
+
reject(new Error(`Timeout after ${ms}ms`));
|
|
14
|
+
}, ms);
|
|
15
|
+
promise
|
|
16
|
+
.then((result) => {
|
|
17
|
+
resolve(result);
|
|
18
|
+
})
|
|
19
|
+
.catch((error) => {
|
|
20
|
+
reject(error);
|
|
21
|
+
})
|
|
22
|
+
.finally(() => {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
10
27
|
}
|