@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.
@@ -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;
@@ -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 response = await fetch(this.url, {
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")) {
@@ -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
  }
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meowinc/meow-sdk",
3
- "version": "0.14.3",
3
+ "version": "0.15.0",
4
4
  "description": "Meow SDK",
5
5
  "keywords": [
6
6
  "sdk"