@meowinc/meow-sdk 0.9.2 → 0.10.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.
@@ -5,6 +5,7 @@ export interface Response {
5
5
  name: string;
6
6
  role: UserRole;
7
7
  status: string;
8
+ avatar?: string;
8
9
  registrationDate: Date;
9
10
  }
10
11
  export declare function request(login: string): Promise<import("@meowinc/result").Result<Response, string>>;
@@ -8,8 +8,8 @@ export interface AuthorizationProvider {
8
8
  updateTokens(accessToken?: string, refreshToken?: string): void;
9
9
  onUpdate(callback: AuthorizationUpdateHandler): void;
10
10
  }
11
- type StorageType = "localStorage" | "sessionStorage";
12
- export declare class BrowserAuthhorizationProvider implements AuthorizationProvider {
11
+ type StorageType = "localStorage" | "sessionStorage" | "cookie";
12
+ export declare class BrowserAuthorizationProvider implements AuthorizationProvider {
13
13
  private readonly storageType;
14
14
  private readonly accessTokenKey;
15
15
  private readonly refreshTokenKey;
@@ -1,4 +1,5 @@
1
- export class BrowserAuthhorizationProvider {
1
+ import { CookieStorage } from "./utils/browser";
2
+ export class BrowserAuthorizationProvider {
2
3
  storageType;
3
4
  accessTokenKey;
4
5
  refreshTokenKey;
@@ -44,6 +45,8 @@ export class BrowserAuthhorizationProvider {
44
45
  return localStorage;
45
46
  case "sessionStorage":
46
47
  return sessionStorage;
48
+ case "cookie":
49
+ return new CookieStorage();
47
50
  }
48
51
  }
49
52
  }
@@ -0,0 +1,28 @@
1
+ import { LogInResponse } from "../account/auth/log-in";
2
+ import { MeowResult, UndefinedString } from "../types";
3
+ export declare class AuthorizationSession {
4
+ private login;
5
+ private password;
6
+ constructor(login: string, password: string);
7
+ startAuth(): Promise<BaseAuthorizationResult>;
8
+ }
9
+ export declare class BaseAuthorizationResult {
10
+ private content?;
11
+ private error?;
12
+ constructor(result: MeowResult<LogInResponse>);
13
+ hasError(): boolean;
14
+ getError(): UndefinedString;
15
+ isRequireTwoFactor(): boolean;
16
+ startTwoFactor(twoFactorId: number): Promise<TwoFactorAuthorizationResult>;
17
+ private _startTwoFactor;
18
+ }
19
+ export declare class TwoFactorAuthorizationResult {
20
+ private sessionId?;
21
+ private error?;
22
+ constructor(result: MeowResult<string>);
23
+ hasError(): boolean;
24
+ passCode(code: string): Promise<MeowResult<{
25
+ accessToken: string;
26
+ refreshToken: string;
27
+ }>>;
28
+ }
@@ -0,0 +1,85 @@
1
+ import { MeowSdkClient } from "..";
2
+ import { EmptyAuthorizationProvider } from "../auth-provider";
3
+ import { MeowResult } from "../types";
4
+ export class AuthorizationSession {
5
+ login;
6
+ password;
7
+ constructor(login, password) {
8
+ this.login = login;
9
+ this.password = password;
10
+ }
11
+ async startAuth() {
12
+ const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
13
+ const result = await client.account.auth.baseLogIn(this.login, this.password);
14
+ return new BaseAuthorizationResult(result);
15
+ }
16
+ }
17
+ export class BaseAuthorizationResult {
18
+ content;
19
+ error;
20
+ constructor(result) {
21
+ if (result.hasError()) {
22
+ this.error = result.getError();
23
+ }
24
+ else {
25
+ this.content = result.unwrap();
26
+ }
27
+ }
28
+ hasError() {
29
+ return this.error !== undefined;
30
+ }
31
+ getError() {
32
+ return this.error;
33
+ }
34
+ isRequireTwoFactor() {
35
+ return this.content ? this.content.type === "requireTwoFactor" : false;
36
+ }
37
+ async startTwoFactor(twoFactorId) {
38
+ return new TwoFactorAuthorizationResult(await this._startTwoFactor(twoFactorId));
39
+ }
40
+ async _startTwoFactor(twoFactorId) {
41
+ if (!this.content) {
42
+ return MeowResult.withError("Content not provided. Handle error!");
43
+ }
44
+ if (this.content.type !== "requireTwoFactor") {
45
+ return MeowResult.withError("Two factor auth not required");
46
+ }
47
+ const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
48
+ const result = await client.account.auth.setTwoFactorSession(this.content.sessionId, twoFactorId);
49
+ if (result.hasError()) {
50
+ return MeowResult.withError(result.getError());
51
+ }
52
+ return MeowResult.withOk(this.content.sessionId);
53
+ }
54
+ }
55
+ export class TwoFactorAuthorizationResult {
56
+ sessionId;
57
+ error;
58
+ constructor(result) {
59
+ if (result.hasError()) {
60
+ this.error = result.getError();
61
+ }
62
+ else {
63
+ this.sessionId = result.unwrap();
64
+ }
65
+ }
66
+ hasError() {
67
+ return this.error !== undefined;
68
+ }
69
+ async passCode(code) {
70
+ if (!this.sessionId) {
71
+ return MeowResult.withError("Session Id not provided. Handle error!");
72
+ }
73
+ const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
74
+ const result = await client.account.auth.submitTwoFactorSession(code, this.sessionId);
75
+ if (result.hasError()) {
76
+ return MeowResult.withError(result.getError());
77
+ }
78
+ const content = result.unwrap();
79
+ // imposible
80
+ if (content.type === "requireTwoFactor") {
81
+ return MeowResult.withError("Returned 'requireTwoFactor' response");
82
+ }
83
+ return MeowResult.withOk(content);
84
+ }
85
+ }
@@ -0,0 +1,15 @@
1
+ import { UndefinedString } from "../types";
2
+ export declare class BrowserUtils {
3
+ static getCookie(name: string): UndefinedString;
4
+ static setCookie(name: string, value: string, days: number): void;
5
+ }
6
+ export declare class CookieStorage implements Storage {
7
+ [name: string]: any;
8
+ length: number;
9
+ constructor();
10
+ clear(): void;
11
+ getItem(key: string): string | null;
12
+ key(_: number): string | null;
13
+ removeItem(key: string): void;
14
+ setItem(key: string, value: string, days?: number): void;
15
+ }
@@ -0,0 +1,50 @@
1
+ export class BrowserUtils {
2
+ static getCookie(name) {
3
+ if (!window.document) {
4
+ throw new Error("Window not exits, you execute code not in browser?");
5
+ }
6
+ const nameEQ = name + "=";
7
+ const ca = document.cookie.split(";");
8
+ for (let i = 0; i < ca.length; i++) {
9
+ let c = ca[i];
10
+ while (c.charAt(0) === " ")
11
+ c = c.substring(1, c.length);
12
+ if (c.indexOf(nameEQ) === 0) {
13
+ return c.substring(nameEQ.length, c.length);
14
+ }
15
+ }
16
+ return undefined;
17
+ }
18
+ static setCookie(name, value, days) {
19
+ if (!window.document) {
20
+ throw new Error("Window not exits, you execute code not in browser?");
21
+ }
22
+ let expires = "";
23
+ const date = new Date();
24
+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
25
+ expires = date.toUTCString();
26
+ document.cookie = `${name}=${value}; expires=${expires}; path=/; secure`;
27
+ }
28
+ }
29
+ export class CookieStorage {
30
+ length;
31
+ constructor() {
32
+ console.warn("used untested unstable feature `CookieStorage`");
33
+ this.length = 0;
34
+ }
35
+ clear() {
36
+ throw new Error("Method not implemented.");
37
+ }
38
+ getItem(key) {
39
+ return BrowserUtils.getCookie(key) ?? null;
40
+ }
41
+ key(_) {
42
+ throw new Error("Method not implemented.");
43
+ }
44
+ removeItem(key) {
45
+ this.setItem(key, "", -1);
46
+ }
47
+ setItem(key, value, days = 30) {
48
+ BrowserUtils.setCookie(key, value, days);
49
+ }
50
+ }
@@ -1,31 +1,5 @@
1
- import { MeowResult, UndefinedString } from "../types";
2
- import { Response as LogInResponse } from "./../account/auth/log-in/log-in";
3
- export declare class AuthorizationSession {
4
- private login;
5
- private password;
6
- constructor(login: string, password: string);
7
- startAuth(): Promise<BaseAuthorizationResult>;
8
- }
9
- export declare class BaseAuthorizationResult {
10
- private content?;
11
- private error?;
12
- constructor(result: MeowResult<LogInResponse>);
13
- hasError(): boolean;
14
- getError(): UndefinedString;
15
- isRequireTwoFactor(): boolean;
16
- startTwoFactor(twoFactorId: number): Promise<TwoFactorAuthorizationResult>;
17
- private _startTwoFactor;
18
- }
19
- export declare class TwoFactorAuthorizationResult {
20
- private sessionId?;
21
- private error?;
22
- constructor(result: MeowResult<string>);
23
- hasError(): boolean;
24
- passCode(code: string): Promise<MeowResult<{
25
- accessToken: string;
26
- refreshToken: string;
27
- }>>;
28
- }
1
+ import { AuthorizationSession } from "./auth";
2
+ export { BrowserUtils, CookieStorage } from "./browser";
29
3
  export declare class MeowUtils {
30
4
  static createAuthorizationSession(login: string, password: string): AuthorizationSession;
31
5
  static checkRequiredPermissions(expected: string[], permissions: string[]): boolean;
@@ -1,88 +1,5 @@
1
- import { MeowSdkClient } from "..";
2
- import { EmptyAuthorizationProvider } from "../auth-provider";
3
- import { MeowResult } from "../types";
4
- export class AuthorizationSession {
5
- login;
6
- password;
7
- constructor(login, password) {
8
- this.login = login;
9
- this.password = password;
10
- }
11
- async startAuth() {
12
- const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
13
- const result = await client.account.auth.baseLogIn(this.login, this.password);
14
- return new BaseAuthorizationResult(result);
15
- }
16
- }
17
- export class BaseAuthorizationResult {
18
- content;
19
- error;
20
- constructor(result) {
21
- if (result.hasError()) {
22
- this.error = result.getError();
23
- }
24
- else {
25
- this.content = result.unwrap();
26
- }
27
- }
28
- hasError() {
29
- return this.error !== undefined;
30
- }
31
- getError() {
32
- return this.error;
33
- }
34
- isRequireTwoFactor() {
35
- return this.content ? this.content.type === "requireTwoFactor" : false;
36
- }
37
- async startTwoFactor(twoFactorId) {
38
- return new TwoFactorAuthorizationResult(await this._startTwoFactor(twoFactorId));
39
- }
40
- async _startTwoFactor(twoFactorId) {
41
- if (!this.content) {
42
- return MeowResult.withError("Content not provided. Handle error!");
43
- }
44
- if (this.content.type !== "requireTwoFactor") {
45
- return MeowResult.withError("Two factor auth not required");
46
- }
47
- const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
48
- const result = await client.account.auth.setTwoFactorSession(this.content.sessionId, twoFactorId);
49
- if (result.hasError()) {
50
- return MeowResult.withError(result.getError());
51
- }
52
- return MeowResult.withOk(this.content.sessionId);
53
- }
54
- }
55
- export class TwoFactorAuthorizationResult {
56
- sessionId;
57
- error;
58
- constructor(result) {
59
- if (result.hasError()) {
60
- this.error = result.getError();
61
- }
62
- else {
63
- this.sessionId = result.unwrap();
64
- }
65
- }
66
- hasError() {
67
- return this.error !== undefined;
68
- }
69
- async passCode(code) {
70
- if (!this.sessionId) {
71
- return MeowResult.withError("Session Id not provided. Handle error!");
72
- }
73
- const client = MeowSdkClient.fromAuthorizationProvider(new EmptyAuthorizationProvider());
74
- const result = await client.account.auth.submitTwoFactorSession(code, this.sessionId);
75
- if (result.hasError()) {
76
- return MeowResult.withError(result.getError());
77
- }
78
- const content = result.unwrap();
79
- // imposible
80
- if (content.type === "requireTwoFactor") {
81
- return MeowResult.withError("Returned 'requireTwoFactor' response");
82
- }
83
- return MeowResult.withOk(content);
84
- }
85
- }
1
+ import { AuthorizationSession } from "./auth";
2
+ export { BrowserUtils, CookieStorage } from "./browser";
86
3
  export class MeowUtils {
87
4
  static createAuthorizationSession(login, password) {
88
5
  return new AuthorizationSession(login, password);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meowinc/meow-sdk",
3
- "version": "0.9.2",
3
+ "version": "0.10.0",
4
4
  "description": "Meow SDK",
5
5
  "keywords": [
6
6
  "sdk"