@meowinc/meow-sdk 0.14.2 → 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.
@@ -0,0 +1,13 @@
1
+ import { AuthorizationProvider } from "../..";
2
+ import { MeowResult } from "../../types";
3
+ export interface Payload {
4
+ appId?: string;
5
+ permissions: string[];
6
+ removeOldSessions: boolean;
7
+ isOneTime: true;
8
+ }
9
+ export interface Response {
10
+ affectedSessions?: number;
11
+ accessToken: string;
12
+ }
13
+ export declare function request(payload: Payload, auth: AuthorizationProvider): Promise<MeowResult<Response>>;
@@ -0,0 +1,12 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request(payload, auth) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/sessions/create`)
8
+ .withAuthorization(auth)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -1,11 +1,13 @@
1
1
  import { AuthorizationProvider } from "../../auth-provider";
2
2
  import { type Payload as CreateSessionPayload } from "./create";
3
3
  import { type Payload as RequestTokenPayload } from "./request-token";
4
+ import { type Payload as CreateOneTimeSessionPayload } from "./create-one-time";
4
5
  export declare class SessionsRequests {
5
6
  private readonly authProvider;
6
7
  constructor(authProvider: AuthorizationProvider);
7
8
  requestToken(payload: RequestTokenPayload): Promise<import("../../types").MeowResult<import("./request-token").Response>>;
8
9
  noVerificationRequtestToken(): Promise<import("../../types").MeowResult<import("./request-token").Response>>;
9
10
  createSession(payload: CreateSessionPayload): Promise<import("../../types").MeowResult<import("./create").Response>>;
11
+ createOneTimeSession(payload: CreateOneTimeSessionPayload): Promise<import("../../types").MeowResult<import("./create-one-time").Response>>;
10
12
  getCurrentSessions(): Promise<import("../../types").MeowResult<import("./get-current").Response>>;
11
13
  }
@@ -1,6 +1,7 @@
1
1
  import { request as createSession } from "./create";
2
2
  import { request as requestToken } from "./request-token";
3
3
  import { request as getCurrentSessions } from "./get-current";
4
+ import { request as createOneTimeSession } from "./create-one-time";
4
5
  export class SessionsRequests {
5
6
  authProvider;
6
7
  constructor(authProvider) {
@@ -17,6 +18,9 @@ export class SessionsRequests {
17
18
  async createSession(payload) {
18
19
  return await createSession(payload, this.authProvider);
19
20
  }
21
+ async createOneTimeSession(payload) {
22
+ return await createOneTimeSession(payload, this.authProvider);
23
+ }
20
24
  async getCurrentSessions() {
21
25
  return getCurrentSessions(this.authProvider);
22
26
  }
@@ -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.2",
3
+ "version": "0.15.0",
4
4
  "description": "Meow SDK",
5
5
  "keywords": [
6
6
  "sdk"