@meowinc/meow-sdk 0.3.0 → 0.4.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,10 @@
1
+ import { AuthorizationProvider } from "../../..";
2
+ import { MeowResult } from "../../../types";
3
+ export type Response = {
4
+ id: number;
5
+ createdAt: string;
6
+ } & {
7
+ type: "email";
8
+ mailbox: string;
9
+ };
10
+ export declare function request(auth: AuthorizationProvider): Promise<MeowResult<Response>>;
@@ -0,0 +1,11 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(auth) {
5
+ return await new HttpClient()
6
+ .withMethodGet()
7
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/my`)
8
+ .withAuthorization(auth)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -0,0 +1,8 @@
1
+ import { GetTwoFactorResponse } from ".";
2
+ import { MeowResult } from "../../../types";
3
+ export interface Payload {
4
+ login: string;
5
+ password: string;
6
+ }
7
+ export type Response = GetTwoFactorResponse;
8
+ export declare function request(payload: Payload): Promise<MeowResult<Response>>;
@@ -0,0 +1,11 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(payload) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/my/login-password`)
8
+ .withJsonBody(payload)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -0,0 +1,7 @@
1
+ export type GetTwoFactorResponse = {
2
+ id: number;
3
+ createdAt: string;
4
+ } & {
5
+ type: "email";
6
+ mailbox: string;
7
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { AuthorizationProvider } from "../../..";
2
+ import { MeowResult } from "../../../types";
3
+ export interface Response {
4
+ sessionId: string;
5
+ }
6
+ export declare function request(mailbox: string, authorizationProvider: AuthorizationProvider): Promise<MeowResult<Response>>;
@@ -0,0 +1,14 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(mailbox, authorizationProvider) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/email`)
7
+ .withAuthorization(authorizationProvider)
8
+ .withMethodPost()
9
+ .withJsonBody({
10
+ mailbox,
11
+ })
12
+ .send()
13
+ .then((r) => r.mapError(ErrorToMessage));
14
+ }
@@ -0,0 +1,3 @@
1
+ import { AuthorizationProvider } from "../../..";
2
+ import { MeowResult } from "../../../types";
3
+ export declare function request(sessionId: string, code: string, authorizationProvider: AuthorizationProvider): Promise<MeowResult<void>>;
@@ -0,0 +1,15 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(sessionId, code, authorizationProvider) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/email/submit`)
7
+ .withMethodPost()
8
+ .withAuthorization(authorizationProvider)
9
+ .withJsonBody({
10
+ sessionId,
11
+ code,
12
+ })
13
+ .send()
14
+ .then((r) => r.mapError(ErrorToMessage));
15
+ }
@@ -3,9 +3,12 @@ import { AuthorizationProvider } from "../../auth-provider";
3
3
  export declare class AuthRequests {
4
4
  private readonly authProvider;
5
5
  constructor(authProvider: AuthorizationProvider);
6
- baseLogIn(login: string, password: string): Promise<import("../../types").MeowResult<import("./log-in").Response>>;
7
- twoFactorLogIn(code: string, requestId: string): Promise<import("../../types").MeowResult<import("./log-in").Response>>;
6
+ baseLogIn(login: string, password: string): Promise<import("../../types").MeowResult<import("./log-in").LogInResponse>>;
8
7
  signIn(login: string, password: string, verificationType: VerificationType): Promise<import("../../types").MeowResult<void>>;
9
- addTwoFactorEmail(email: string): Promise<import("../../types").MeowResult<import("./add-email").Response>>;
8
+ addTwoFactorEmail(email: string): Promise<import("../../types").MeowResult<import("./email/add-email").Response>>;
10
9
  submitTwoFactorEmail(sessionId: string, code: string): Promise<import("../../types").MeowResult<void>>;
10
+ setTwoFactorSession(sessionId: string, twoFactorId: number): Promise<import("../../types").MeowResult<void>>;
11
+ submitTwoFactorSession(code: string, sessionId: string): Promise<import("../../types").MeowResult<import("./log-in").LogInResponse>>;
12
+ getMyTwoFactor(): Promise<import("../../types").MeowResult<import("./2FA/get-my-two-factor").Response>>;
13
+ getTwoFactorByLoginPassword(login: string, password: string): Promise<import("../../types").MeowResult<import("./2FA").GetTwoFactorResponse>>;
11
14
  }
@@ -1,7 +1,11 @@
1
1
  import { request as signIn } from "./sign-in";
2
- import { request as logIn } from "./log-in";
3
- import { request as addEmail } from "./add-email";
4
- import { request as submitAddEmail } from "./submit-add-email";
2
+ import { request as logIn } from "./log-in/log-in";
3
+ import { request as addEmail } from "./email/add-email";
4
+ import { request as submitAddEmail } from "./email/submit-add-email";
5
+ import { request as setTwoFactorSession } from "./log-in/set-two-factor-session";
6
+ import { request as submitTwoFactorSession } from "./log-in/submit-two-factor-session";
7
+ import { request as getMyTwoFactor } from "./2FA/get-my-two-factor";
8
+ import { request as getTwoFactorByLoginPassword } from "./2FA/get-two-factor-by-login-password";
5
9
  export class AuthRequests {
6
10
  authProvider;
7
11
  constructor(authProvider) {
@@ -11,14 +15,6 @@ export class AuthRequests {
11
15
  return await logIn({
12
16
  login,
13
17
  password,
14
- type: "base",
15
- });
16
- }
17
- async twoFactorLogIn(code, requestId) {
18
- return await logIn({
19
- code,
20
- requestId,
21
- type: "twoFactor",
22
18
  });
23
19
  }
24
20
  async signIn(login, password, verificationType) {
@@ -34,4 +30,16 @@ export class AuthRequests {
34
30
  async submitTwoFactorEmail(sessionId, code) {
35
31
  return await submitAddEmail(sessionId, code, this.authProvider);
36
32
  }
33
+ async setTwoFactorSession(sessionId, twoFactorId) {
34
+ return await setTwoFactorSession({ sessionId, twoFactorId });
35
+ }
36
+ async submitTwoFactorSession(code, sessionId) {
37
+ return await submitTwoFactorSession({ code, sessionId });
38
+ }
39
+ async getMyTwoFactor() {
40
+ return await getMyTwoFactor(this.authProvider);
41
+ }
42
+ async getTwoFactorByLoginPassword(login, password) {
43
+ return await getTwoFactorByLoginPassword({ login, password });
44
+ }
37
45
  }
@@ -0,0 +1,8 @@
1
+ export type LogInResponse = {
2
+ type: "success";
3
+ accessToken: string;
4
+ refreshToken: string;
5
+ } | {
6
+ type: "requireTwoFactor";
7
+ sessionId: string;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { LogInResponse } from ".";
2
+ import { MeowResult } from "../../../types";
3
+ export interface Payload {
4
+ login: string;
5
+ password: string;
6
+ }
7
+ export type Response = LogInResponse;
8
+ export declare function request(payload: Payload): Promise<MeowResult<Response>>;
@@ -0,0 +1,11 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(payload) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/auth/log-in`)
8
+ .withJsonBody(payload)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -0,0 +1,6 @@
1
+ import { MeowResult } from "../../../types";
2
+ export interface Payload {
3
+ sessionId: string;
4
+ twoFactorId: number;
5
+ }
6
+ export declare function request(payload: Payload): Promise<MeowResult<void>>;
@@ -0,0 +1,11 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(payload) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/session/set-up`)
8
+ .withJsonBody(payload)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -0,0 +1,8 @@
1
+ import { LogInResponse } from ".";
2
+ import { MeowResult } from "../../../types";
3
+ export interface Payload {
4
+ code: string;
5
+ sessionId: string;
6
+ }
7
+ export type Response = LogInResponse;
8
+ export declare function request(payload: Payload): Promise<MeowResult<Response>>;
@@ -0,0 +1,11 @@
1
+ import { ACCOUNT_API } from "../..";
2
+ import { ErrorToMessage } from "../../../error";
3
+ import { HttpClient } from "../../../http-client";
4
+ export async function request(payload) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/auth/2fa/session/submit`)
8
+ .withJsonBody(payload)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -35,4 +35,12 @@ export declare class InPlaceAuthorizationProvider implements AuthorizationProvid
35
35
  updateTokens(accessToken?: string, refreshToken?: string): void;
36
36
  onUpdate(callback: AuthorizationUpdateHandler): void;
37
37
  }
38
+ export declare class EmptyAuthorizationProvider implements AuthorizationProvider {
39
+ getAccessToken(): UndefinedString;
40
+ setAccessToken(): void;
41
+ getRefreshToken(): UndefinedString;
42
+ setRefreshToken(): void;
43
+ updateTokens(): void;
44
+ onUpdate(): void;
45
+ }
38
46
  export {};
@@ -83,3 +83,15 @@ export class InPlaceAuthorizationProvider {
83
83
  this.updateCallbacks.push(callback);
84
84
  }
85
85
  }
86
+ export class EmptyAuthorizationProvider {
87
+ getAccessToken() {
88
+ return undefined;
89
+ }
90
+ setAccessToken() { }
91
+ getRefreshToken() {
92
+ return undefined;
93
+ }
94
+ setRefreshToken() { }
95
+ updateTokens() { }
96
+ onUpdate() { }
97
+ }
@@ -0,0 +1,32 @@
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
+ }
29
+ export declare class MeowUtils {
30
+ static createAuthorizationSession(login: string, password: string): AuthorizationSession;
31
+ static checkRequiredPermissions(expected: string[], permissions: string[]): boolean;
32
+ }
@@ -0,0 +1,93 @@
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
+ }
86
+ export class MeowUtils {
87
+ static createAuthorizationSession(login, password) {
88
+ return new AuthorizationSession(login, password);
89
+ }
90
+ static checkRequiredPermissions(expected, permissions) {
91
+ return permissions.every((permission) => expected.includes(permission));
92
+ }
93
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meowinc/meow-sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Meow SDK",
5
5
  "keywords": [
6
6
  "sdk"