@meowinc/meow-sdk 0.0.1

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.
Files changed (55) hide show
  1. package/.prettierrc +3 -0
  2. package/dist/account/app/create.d.ts +10 -0
  3. package/dist/account/app/create.js +12 -0
  4. package/dist/account/app/get-app-list.d.ts +7 -0
  5. package/dist/account/app/get-app-list.js +10 -0
  6. package/dist/account/app/get-by-display-id.d.ts +10 -0
  7. package/dist/account/app/get-by-display-id.js +10 -0
  8. package/dist/account/app/index.d.ts +8 -0
  9. package/dist/account/app/index.js +22 -0
  10. package/dist/account/auth/index.d.ts +9 -0
  11. package/dist/account/auth/index.js +29 -0
  12. package/dist/account/auth/log-in.d.ts +16 -0
  13. package/dist/account/auth/log-in.js +11 -0
  14. package/dist/account/auth/sign-in.d.ts +9 -0
  15. package/dist/account/auth/sign-in.js +11 -0
  16. package/dist/account/index.d.ts +18 -0
  17. package/dist/account/index.js +31 -0
  18. package/dist/account/service/create-service.d.ts +13 -0
  19. package/dist/account/service/create-service.js +12 -0
  20. package/dist/account/service/get-service-list.d.ts +11 -0
  21. package/dist/account/service/get-service-list.js +10 -0
  22. package/dist/account/service/index.d.ts +8 -0
  23. package/dist/account/service/index.js +14 -0
  24. package/dist/account/services-permissions/create-permission.d.ts +8 -0
  25. package/dist/account/services-permissions/create-permission.js +12 -0
  26. package/dist/account/services-permissions/get-service-permission.d.ts +10 -0
  27. package/dist/account/services-permissions/get-service-permission.js +10 -0
  28. package/dist/account/services-permissions/index.d.ts +10 -0
  29. package/dist/account/services-permissions/index.js +14 -0
  30. package/dist/account/sessions/create.d.ts +13 -0
  31. package/dist/account/sessions/create.js +12 -0
  32. package/dist/account/sessions/index.d.ts +8 -0
  33. package/dist/account/sessions/index.js +16 -0
  34. package/dist/account/sessions/request-token.d.ts +19 -0
  35. package/dist/account/sessions/request-token.js +12 -0
  36. package/dist/account/types.d.ts +1 -0
  37. package/dist/account/types.js +1 -0
  38. package/dist/account/user/get-user-by-login.d.ts +11 -0
  39. package/dist/account/user/get-user-by-login.js +10 -0
  40. package/dist/account/user/index.d.ts +6 -0
  41. package/dist/account/user/index.js +10 -0
  42. package/dist/auth-provider.d.ts +28 -0
  43. package/dist/auth-provider.js +62 -0
  44. package/dist/error.d.ts +30 -0
  45. package/dist/error.js +34 -0
  46. package/dist/http-client.d.ts +21 -0
  47. package/dist/http-client.js +130 -0
  48. package/dist/index.d.ts +13 -0
  49. package/dist/index.js +25 -0
  50. package/dist/token.d.ts +8 -0
  51. package/dist/token.js +20 -0
  52. package/dist/types.d.ts +7 -0
  53. package/dist/types.js +3 -0
  54. package/package.json +31 -0
  55. package/tsconfig.json +19 -0
package/.prettierrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "tabWidth": 4,
3
+ }
@@ -0,0 +1,10 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { MeowResult } from "../../types";
3
+ interface Payload {
4
+ displayId: string;
5
+ name: string;
6
+ type: AppType;
7
+ }
8
+ type AppType = "web" | "mobile";
9
+ export declare function request(payload: Payload, authProviver: AuthorizationProvider): Promise<MeowResult<void>>;
10
+ export {};
@@ -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, authProviver) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/app/`)
7
+ .withMethodPost()
8
+ .withAuthorization(authProviver)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -0,0 +1,7 @@
1
+ export interface Response {
2
+ items: {
3
+ id: string;
4
+ name: string;
5
+ }[];
6
+ }
7
+ export declare function request(): Promise<import("ts-result-meow").Result<Response, string>>;
@@ -0,0 +1,10 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request() {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/app`)
7
+ .withMethodGet()
8
+ .send()
9
+ .then((r) => r.mapError(ErrorToMessage));
10
+ }
@@ -0,0 +1,10 @@
1
+ import { MeowResult } from "../../types";
2
+ export interface Response {
3
+ id: string;
4
+ name: string;
5
+ owner: {
6
+ id: number;
7
+ name: string;
8
+ };
9
+ }
10
+ export declare function request(displayId: string): Promise<MeowResult<Response>>;
@@ -0,0 +1,10 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request(displayId) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/app/${displayId}`)
7
+ .withMethodGet()
8
+ .send()
9
+ .then((r) => r.mapError(ErrorToMessage));
10
+ }
@@ -0,0 +1,8 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ export declare class AppRequests {
3
+ private readonly authProvider;
4
+ constructor(authProvider: AuthorizationProvider);
5
+ create(displayId: string, name: string, type: "web" | "mobile"): Promise<import("../../types").MeowResult<void>>;
6
+ getAppList(): Promise<import("ts-result-meow").Result<import("./get-app-list").Response, string>>;
7
+ getByDisplayId(displayId: string): Promise<import("../../types").MeowResult<import("./get-by-display-id").Response>>;
8
+ }
@@ -0,0 +1,22 @@
1
+ import { request as create } from "./create";
2
+ import { request as getAppList } from "./get-app-list";
3
+ import { request as getByDisplayId } from "./get-by-display-id";
4
+ export class AppRequests {
5
+ authProvider;
6
+ constructor(authProvider) {
7
+ this.authProvider = authProvider;
8
+ }
9
+ async create(displayId, name, type) {
10
+ return create({
11
+ displayId,
12
+ name,
13
+ type,
14
+ }, this.authProvider);
15
+ }
16
+ async getAppList() {
17
+ return await getAppList();
18
+ }
19
+ async getByDisplayId(displayId) {
20
+ return await getByDisplayId(displayId);
21
+ }
22
+ }
@@ -0,0 +1,9 @@
1
+ import { VerificationType } from "..";
2
+ import { AuthorizationProvider } from "../../auth-provider";
3
+ export declare class AuthRequests {
4
+ private readonly authProvider;
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>>;
8
+ signIn(login: string, password: string, verificationType: VerificationType): Promise<import("../../types").MeowResult<void>>;
9
+ }
@@ -0,0 +1,29 @@
1
+ import { request as signIn } from "./sign-in";
2
+ import { request as logIn } from "./log-in";
3
+ export class AuthRequests {
4
+ authProvider;
5
+ constructor(authProvider) {
6
+ this.authProvider = authProvider;
7
+ }
8
+ async baseLogIn(login, password) {
9
+ return await logIn({
10
+ login,
11
+ password,
12
+ type: "base",
13
+ });
14
+ }
15
+ async twoFactorLogIn(code, requestId) {
16
+ return await logIn({
17
+ code,
18
+ requestId,
19
+ type: "twoFactor",
20
+ });
21
+ }
22
+ async signIn(login, password, verificationType) {
23
+ return await signIn({
24
+ login,
25
+ password,
26
+ verificationType,
27
+ });
28
+ }
29
+ }
@@ -0,0 +1,16 @@
1
+ import { MeowResult } from "../../types";
2
+ type Payload = {
3
+ login: string;
4
+ password: string;
5
+ type: "base";
6
+ } | {
7
+ requestId: string;
8
+ code: string;
9
+ type: "twoFactor";
10
+ };
11
+ export interface Response {
12
+ accessToken: string;
13
+ refreshToken: string;
14
+ }
15
+ export declare function request(payload: Payload): Promise<MeowResult<Response>>;
16
+ export {};
@@ -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,9 @@
1
+ import { MeowResult } from "../../types";
2
+ import { VerificationType } from "../types";
3
+ interface Payload {
4
+ login: string;
5
+ password: string;
6
+ verificationType: VerificationType;
7
+ }
8
+ export declare function request(payload: Payload): Promise<MeowResult<void>>;
9
+ export {};
@@ -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/sign-in`)
8
+ .withJsonBody(payload)
9
+ .send()
10
+ .then((r) => r.mapError(ErrorToMessage));
11
+ }
@@ -0,0 +1,18 @@
1
+ import { AuthRequests } from "./auth";
2
+ import { AppRequests } from "./app";
3
+ import type { AuthorizationProvider } from "../auth-provider";
4
+ import { SessionsRequests } from "./sessions";
5
+ import { UserRequests } from "./user";
6
+ import { ServiceRequests } from "./service";
7
+ import { ServicesPermissionsRequests } from "./services-permissions";
8
+ export { type VerificationType } from "./types";
9
+ export declare const ACCOUNT_API = "https://account.meowinc.tech/api";
10
+ export declare class AccountClient {
11
+ app: AppRequests;
12
+ auth: AuthRequests;
13
+ user: UserRequests;
14
+ service: ServiceRequests;
15
+ servicePermissions: ServicesPermissionsRequests;
16
+ sessions: SessionsRequests;
17
+ constructor(authorizationProvider: AuthorizationProvider);
18
+ }
@@ -0,0 +1,31 @@
1
+ import { AuthRequests } from "./auth";
2
+ import { AppRequests } from "./app";
3
+ import { SessionsRequests } from "./sessions";
4
+ import { UserRequests } from "./user";
5
+ import { ServiceRequests } from "./service";
6
+ import { ServicesPermissionsRequests } from "./services-permissions";
7
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
8
+ const permissions = [
9
+ "account.getAccountInformation",
10
+ "account.getSecurityAccountInformation",
11
+ "account.requestPermissions",
12
+ "account.changePassword",
13
+ "account.appsManagement",
14
+ ];
15
+ export const ACCOUNT_API = "https://account.meowinc.tech/api";
16
+ export class AccountClient {
17
+ app;
18
+ auth;
19
+ user;
20
+ service;
21
+ servicePermissions;
22
+ sessions;
23
+ constructor(authorizationProvider) {
24
+ this.auth = new AuthRequests(authorizationProvider);
25
+ this.app = new AppRequests(authorizationProvider);
26
+ this.user = new UserRequests(authorizationProvider);
27
+ this.service = new ServiceRequests(authorizationProvider);
28
+ this.servicePermissions = new ServicesPermissionsRequests(authorizationProvider);
29
+ this.sessions = new SessionsRequests(authorizationProvider);
30
+ }
31
+ }
@@ -0,0 +1,13 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { MeowResult } from "../../types";
3
+ export interface Response {
4
+ displayId: number;
5
+ name: string;
6
+ description: string;
7
+ }
8
+ export interface Payload {
9
+ displayId: string;
10
+ name: string;
11
+ description: string;
12
+ }
13
+ export declare function request(payload: Payload, authProvider: 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, authProvider) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/services/`)
7
+ .withMethodPost()
8
+ .withAuthorization(authProvider)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -0,0 +1,11 @@
1
+ import { MeowResult } from "../../types";
2
+ export interface Response {
3
+ items: {
4
+ createdAt: Date;
5
+ description: string;
6
+ displayId: string;
7
+ id: number;
8
+ name: string;
9
+ }[];
10
+ }
11
+ export declare function request(): Promise<MeowResult<Response>>;
@@ -0,0 +1,10 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request() {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/services`)
7
+ .withMethodGet()
8
+ .send()
9
+ .then((r) => r.mapError(ErrorToMessage));
10
+ }
@@ -0,0 +1,8 @@
1
+ import { Payload as CreateServicePayload } from "./create-service";
2
+ import { AuthorizationProvider } from "../../auth-provider";
3
+ export declare class ServiceRequests {
4
+ private readonly authProvider;
5
+ constructor(authProvider: AuthorizationProvider);
6
+ getServiceList(): Promise<import("../../types").MeowResult<import("./get-service-list").Response>>;
7
+ createService(payload: CreateServicePayload): Promise<import("../../types").MeowResult<import("./create-service").Response>>;
8
+ }
@@ -0,0 +1,14 @@
1
+ import { request as getServiceList } from "./get-service-list";
2
+ import { request as createService, } from "./create-service";
3
+ export class ServiceRequests {
4
+ authProvider;
5
+ constructor(authProvider) {
6
+ this.authProvider = authProvider;
7
+ }
8
+ async getServiceList() {
9
+ return await getServiceList();
10
+ }
11
+ async createService(payload) {
12
+ return await createService(payload, this.authProvider);
13
+ }
14
+ }
@@ -0,0 +1,8 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { MeowResult, UserRole } from "../../types";
3
+ export interface Payload {
4
+ code: string;
5
+ description: string;
6
+ roles: UserRole[];
7
+ }
8
+ export declare function request(serviceId: string, payload: Payload, authProvider: AuthorizationProvider): Promise<MeowResult<void>>;
@@ -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(serviceId, payload, authProvider) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/services/${serviceId}/permissions`)
7
+ .withMethodPost()
8
+ .withAuthorization(authProvider)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -0,0 +1,10 @@
1
+ import { MeowResult, UserRole } from "../../types";
2
+ export interface Response {
3
+ items: {
4
+ id: number;
5
+ code: string;
6
+ description: string;
7
+ availableRoles: UserRole[];
8
+ }[];
9
+ }
10
+ export declare function request(serviceId: string): Promise<MeowResult<Response>>;
@@ -0,0 +1,10 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request(serviceId) {
5
+ return await new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/services/${serviceId}/permissions`)
7
+ .withMethodGet()
8
+ .send()
9
+ .then((r) => r.mapError(ErrorToMessage));
10
+ }
@@ -0,0 +1,10 @@
1
+ import { Response as GetServicePermissionsResponse } from "./get-service-permission";
2
+ import { Payload as CreateServicePermissionPayload } from "./create-permission";
3
+ import { AuthorizationProvider } from "../../auth-provider";
4
+ import { MeowResult } from "../../types";
5
+ export declare class ServicesPermissionsRequests {
6
+ authProvider: AuthorizationProvider;
7
+ constructor(authProvider: AuthorizationProvider);
8
+ getServicePermissions(serviceId: string): Promise<MeowResult<GetServicePermissionsResponse>>;
9
+ createServicePermissions(serviceId: string, payload: CreateServicePermissionPayload): Promise<MeowResult<void>>;
10
+ }
@@ -0,0 +1,14 @@
1
+ import { request as getServicePermissions, } from "./get-service-permission";
2
+ import { request as createServicePermission, } from "./create-permission";
3
+ export class ServicesPermissionsRequests {
4
+ authProvider;
5
+ constructor(authProvider) {
6
+ this.authProvider = authProvider;
7
+ }
8
+ async getServicePermissions(serviceId) {
9
+ return await getServicePermissions(serviceId);
10
+ }
11
+ async createServicePermissions(serviceId, payload) {
12
+ return await createServicePermission(serviceId, payload, this.authProvider);
13
+ }
14
+ }
@@ -0,0 +1,13 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { MeowResult } from "../../types";
3
+ export interface Payload {
4
+ appId?: string;
5
+ permissions: string[];
6
+ removeOldSessions: boolean;
7
+ }
8
+ export interface Response {
9
+ affectedSessions?: number;
10
+ accessToken: string;
11
+ refreshToken: string;
12
+ }
13
+ export declare function request(payload: Payload, authProvider: 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, authProvider) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/sessions/create`)
8
+ .withAuthorization(authProvider)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -0,0 +1,8 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { type Payload as CreateSessionPayload } from "./create";
3
+ export declare class SessionsRequests {
4
+ private readonly authProvider;
5
+ constructor(authProvider: AuthorizationProvider);
6
+ noVerificationRequtestToken(): Promise<import("../../types").MeowResult<import("./request-token").Response>>;
7
+ createSession(payload: CreateSessionPayload): Promise<import("../../types").MeowResult<import("./create").Response>>;
8
+ }
@@ -0,0 +1,16 @@
1
+ import { request as createSession, } from "./create";
2
+ import { request as requestToken } from "./request-token";
3
+ export class SessionsRequests {
4
+ authProvider;
5
+ constructor(authProvider) {
6
+ this.authProvider = authProvider;
7
+ }
8
+ async noVerificationRequtestToken() {
9
+ return await requestToken({
10
+ type: "noVerification",
11
+ }, this.authProvider);
12
+ }
13
+ async createSession(payload) {
14
+ return await createSession(payload, this.authProvider);
15
+ }
16
+ }
@@ -0,0 +1,19 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ import { MeowResult } from "../../types";
3
+ type Payload = {
4
+ type: "noVerification";
5
+ } | {
6
+ type: "loginPassword";
7
+ login: string;
8
+ password: string;
9
+ } | {
10
+ type: "twoFactor";
11
+ requestId: string;
12
+ code: string;
13
+ };
14
+ export interface Response {
15
+ type: "success";
16
+ token: string;
17
+ }
18
+ export declare function request(payload: Payload, authProvider: AuthorizationProvider): Promise<MeowResult<Response>>;
19
+ export {};
@@ -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, authProvider) {
5
+ return await new HttpClient()
6
+ .withMethodPost()
7
+ .withUrl(`${ACCOUNT_API}/v1/sessions/request-token`)
8
+ .withAuthorization(authProvider)
9
+ .withJsonBody(payload)
10
+ .send()
11
+ .then((r) => r.mapError(ErrorToMessage));
12
+ }
@@ -0,0 +1 @@
1
+ export type VerificationType = "noVerification" | "loginPassword";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ type UserRole = "root" | "admin" | "user" | "inActive";
2
+ export interface Response {
3
+ id: number;
4
+ login: string;
5
+ name: string;
6
+ role: UserRole;
7
+ status: string;
8
+ registrationDate: Date;
9
+ }
10
+ export declare function request(login: string): Promise<import("ts-result-meow").Result<Response, string>>;
11
+ export {};
@@ -0,0 +1,10 @@
1
+ import { ACCOUNT_API } from "..";
2
+ import { ErrorToMessage } from "../../error";
3
+ import { HttpClient } from "../../http-client";
4
+ export async function request(login) {
5
+ return new HttpClient()
6
+ .withUrl(`${ACCOUNT_API}/v1/user/${login}`)
7
+ .withMethodGet()
8
+ .send()
9
+ .then((r) => r.mapError(ErrorToMessage));
10
+ }
@@ -0,0 +1,6 @@
1
+ import { AuthorizationProvider } from "../../auth-provider";
2
+ export declare class UserRequests {
3
+ private readonly authProvider;
4
+ constructor(authProvider: AuthorizationProvider);
5
+ getUserByLogin(login: string): Promise<import("ts-result-meow").Result<import("./get-user-by-login").Response, string>>;
6
+ }
@@ -0,0 +1,10 @@
1
+ import { request as getUserByLoginRequest } from "./get-user-by-login";
2
+ export class UserRequests {
3
+ authProvider;
4
+ constructor(authProvider) {
5
+ this.authProvider = authProvider;
6
+ }
7
+ async getUserByLogin(login) {
8
+ return await getUserByLoginRequest(login);
9
+ }
10
+ }
@@ -0,0 +1,28 @@
1
+ import type { UndefinedString } from "./types";
2
+ export interface AuthorizationProvider {
3
+ getAccessToken(): UndefinedString;
4
+ setAccessToken(token: string): void;
5
+ getRefreshToken(): UndefinedString;
6
+ setRefreshToken(token: string): void;
7
+ }
8
+ type StorageType = "localStorage" | "sessionStorage";
9
+ export declare class BrowserAuthhorizationProvider implements AuthorizationProvider {
10
+ private readonly storageType;
11
+ private readonly accessTokenKey;
12
+ private readonly refreshTokenKey;
13
+ constructor(storageType: StorageType, accessTokenKey: string, refreshTokenKey: string);
14
+ getAccessToken(): UndefinedString;
15
+ setAccessToken(token: string): void;
16
+ getRefreshToken(): UndefinedString;
17
+ setRefreshToken(token: string): void;
18
+ }
19
+ export declare class InPlaceAuthorizationProvider implements AuthorizationProvider {
20
+ private accessToken;
21
+ private refreshToken;
22
+ constructor(accessToken: UndefinedString, refreshToken: UndefinedString);
23
+ getAccessToken(): UndefinedString;
24
+ setAccessToken(token: string): void;
25
+ getRefreshToken(): UndefinedString;
26
+ setRefreshToken(token: string): void;
27
+ }
28
+ export {};
@@ -0,0 +1,62 @@
1
+ export class BrowserAuthhorizationProvider {
2
+ storageType;
3
+ accessTokenKey;
4
+ refreshTokenKey;
5
+ constructor(storageType, accessTokenKey, refreshTokenKey) {
6
+ this.storageType = storageType;
7
+ this.accessTokenKey = accessTokenKey;
8
+ this.refreshTokenKey = refreshTokenKey;
9
+ }
10
+ getAccessToken() {
11
+ switch (this.storageType) {
12
+ case "localStorage":
13
+ return localStorage.getItem(this.accessTokenKey) ?? undefined;
14
+ case "sessionStorage":
15
+ return sessionStorage.getItem(this.accessTokenKey) ?? undefined;
16
+ }
17
+ }
18
+ setAccessToken(token) {
19
+ switch (this.storageType) {
20
+ case "localStorage":
21
+ return localStorage.setItem(this.accessTokenKey, token);
22
+ case "sessionStorage":
23
+ return sessionStorage.setItem(this.accessTokenKey, token);
24
+ }
25
+ }
26
+ getRefreshToken() {
27
+ switch (this.storageType) {
28
+ case "localStorage":
29
+ return localStorage.getItem(this.refreshTokenKey) ?? undefined;
30
+ case "sessionStorage":
31
+ return (sessionStorage.getItem(this.refreshTokenKey) ?? undefined);
32
+ }
33
+ }
34
+ setRefreshToken(token) {
35
+ switch (this.storageType) {
36
+ case "localStorage":
37
+ return localStorage.setItem(this.refreshTokenKey, token);
38
+ case "sessionStorage":
39
+ return sessionStorage.setItem(this.refreshTokenKey, token);
40
+ }
41
+ }
42
+ }
43
+ export class InPlaceAuthorizationProvider {
44
+ accessToken;
45
+ refreshToken;
46
+ constructor(accessToken, refreshToken) {
47
+ this.accessToken = accessToken;
48
+ this.refreshToken = refreshToken;
49
+ }
50
+ getAccessToken() {
51
+ return this.accessToken;
52
+ }
53
+ setAccessToken(token) {
54
+ this.accessToken = token;
55
+ }
56
+ getRefreshToken() {
57
+ return this.refreshToken;
58
+ }
59
+ setRefreshToken(token) {
60
+ this.refreshToken = token;
61
+ }
62
+ }
@@ -0,0 +1,30 @@
1
+ export type AppError = {
2
+ type: "internalServerError";
3
+ } | {
4
+ type: "validationError";
5
+ errors: {
6
+ field: string;
7
+ message: string;
8
+ }[];
9
+ } | {
10
+ type: "anyError";
11
+ message: string;
12
+ } | {
13
+ type: "notFound";
14
+ message?: string;
15
+ } | {
16
+ type: "authorization";
17
+ message: string;
18
+ };
19
+ export declare class HttpError extends Error {
20
+ status: number;
21
+ constructor(status: number, message: string);
22
+ }
23
+ export declare function ErrorToMessage(err: HttpError | AppError | Error): string;
24
+ export declare function isAuthorizationError(obj: {
25
+ type?: string;
26
+ message?: string;
27
+ }): obj is {
28
+ type: "authorization";
29
+ message: string;
30
+ };
package/dist/error.js ADDED
@@ -0,0 +1,34 @@
1
+ export class HttpError extends Error {
2
+ status;
3
+ constructor(status, message) {
4
+ super(message);
5
+ this.name = "HttpError";
6
+ this.status = status;
7
+ }
8
+ }
9
+ export function ErrorToMessage(err) {
10
+ if (err instanceof HttpError) {
11
+ return `${err.status}: ${err.message}`;
12
+ }
13
+ if ("type" in err) {
14
+ const appError = err;
15
+ switch (appError.type) {
16
+ case "internalServerError":
17
+ return "Внутренняя ошибка сервера";
18
+ case "validationError":
19
+ return appError.errors
20
+ .map((e) => `${e.field}: ${e.message}`)
21
+ .join(", ");
22
+ case "anyError":
23
+ return appError.message;
24
+ case "notFound":
25
+ return appError.message ?? "Не найдено";
26
+ case "authorization":
27
+ return appError.message;
28
+ }
29
+ }
30
+ return err.message || "Неизвестная ошибка";
31
+ }
32
+ export function isAuthorizationError(obj) {
33
+ return obj.type === "authorization" && obj.message != undefined;
34
+ }
@@ -0,0 +1,21 @@
1
+ import type { AuthorizationProvider } from "./auth-provider";
2
+ import { Result } from "ts-result-meow";
3
+ import { HttpError, type AppError } from "./error";
4
+ export declare class HttpClient {
5
+ private url;
6
+ private headers;
7
+ private method;
8
+ private body;
9
+ private params;
10
+ private authorization?;
11
+ withUrl(url: string): this;
12
+ withParam(key: string, value: string): this;
13
+ withMethodGet(): this;
14
+ withMethodPost(): this;
15
+ withMethodPut(): this;
16
+ withMethodDelete(): this;
17
+ setHeader(key: string, value: string): this;
18
+ withAuthorization(authProvider: AuthorizationProvider): this;
19
+ withJsonBody(body: object): this;
20
+ send<T>(): Promise<Result<T, HttpError | AppError | Error>>;
21
+ }
@@ -0,0 +1,130 @@
1
+ import { Result } from "ts-result-meow";
2
+ import { HttpError } from "./error";
3
+ import { AccessToken } from "./token";
4
+ import { ACCOUNT_API } from "./account";
5
+ export class HttpClient {
6
+ url = "";
7
+ headers = {};
8
+ method = "GET";
9
+ body = null;
10
+ params = {};
11
+ authorization;
12
+ withUrl(url) {
13
+ this.url = url;
14
+ return this;
15
+ }
16
+ withParam(key, value) {
17
+ this.params[key] = value;
18
+ return this;
19
+ }
20
+ withMethodGet() {
21
+ this.method = "GET";
22
+ return this;
23
+ }
24
+ withMethodPost() {
25
+ this.method = "POST";
26
+ return this;
27
+ }
28
+ withMethodPut() {
29
+ this.method = "PUT";
30
+ return this;
31
+ }
32
+ withMethodDelete() {
33
+ this.method = "DELETE";
34
+ return this;
35
+ }
36
+ setHeader(key, value) {
37
+ this.headers[key] = value;
38
+ return this;
39
+ }
40
+ withAuthorization(authProvider) {
41
+ this.authorization = authProvider;
42
+ return this;
43
+ }
44
+ withJsonBody(body) {
45
+ this.body = JSON.stringify(body);
46
+ this.setHeader("Content-Type", "application/json");
47
+ return this;
48
+ }
49
+ async send() {
50
+ try {
51
+ if (Object.keys(this.params).length > 0) {
52
+ let params = "?";
53
+ for (const [key, value] of Object.entries(this.params)) {
54
+ params += `${key}=${value}&`;
55
+ }
56
+ this.url += params;
57
+ }
58
+ // if authorization required
59
+ if (this.authorization) {
60
+ const accessToken = this.authorization.getAccessToken();
61
+ if (!accessToken || new AccessToken(accessToken).isExpired()) {
62
+ const refreshToken = this.authorization.getRefreshToken();
63
+ if (!refreshToken) {
64
+ return Result.withError({
65
+ type: "authorization",
66
+ message: "Authorization error",
67
+ });
68
+ }
69
+ const updateAuth = await fetch(`${ACCOUNT_API}/v1/sessions/update-session`, {
70
+ method: "POST",
71
+ headers: {
72
+ "Content-Type": "application/json",
73
+ },
74
+ body: JSON.stringify({
75
+ refreshToken: refreshToken,
76
+ }),
77
+ });
78
+ if (!updateAuth.ok) {
79
+ return Result.withError({
80
+ type: "authorization",
81
+ message: "Update authorization error. Please login first",
82
+ });
83
+ }
84
+ const tokens = (await updateAuth.json());
85
+ this.authorization.setRefreshToken(tokens.refreshToken);
86
+ this.authorization.setAccessToken(tokens.accessToken);
87
+ this.setHeader("Authorization", tokens.accessToken);
88
+ }
89
+ else {
90
+ this.setHeader("Authorization", accessToken);
91
+ }
92
+ }
93
+ const response = await fetch(this.url, {
94
+ method: this.method,
95
+ headers: this.headers,
96
+ body: this.body,
97
+ });
98
+ if (response.ok) {
99
+ const contentType = response.headers.get("content-type");
100
+ if (contentType && contentType.includes("application/json")) {
101
+ const data = await response.json();
102
+ return Result.withOk(data);
103
+ }
104
+ const text = await response.text();
105
+ return Result.withOk(text);
106
+ }
107
+ else {
108
+ const contentType = response.headers.get("content-type");
109
+ if (contentType && contentType.includes("application/json")) {
110
+ const appErrorTypes = [
111
+ "anyError",
112
+ "validationError",
113
+ "internalServerError",
114
+ "notFound",
115
+ "authorization",
116
+ ];
117
+ const errorData = await response.json().catch(() => null);
118
+ if (errorData &&
119
+ appErrorTypes.includes(errorData?.type ?? "")) {
120
+ return Result.withError(errorData);
121
+ }
122
+ }
123
+ return Result.withError(new HttpError(response.status, response.statusText));
124
+ }
125
+ }
126
+ catch (err) {
127
+ return Result.withError(err);
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,13 @@
1
+ import { AccountClient } from "./account";
2
+ import type { AuthorizationProvider } from "./auth-provider";
3
+ import type { UndefinedString } from "./types";
4
+ export declare class MeowSdkClient<T extends AuthorizationProvider> {
5
+ readonly account: AccountClient;
6
+ private readonly authProvider;
7
+ protected constructor(authProvider: T);
8
+ isAuthorizationExist(): boolean;
9
+ updateAccessToken(accessToken: string): void;
10
+ updateRefreshToken(refreshToken: string): void;
11
+ getAccessToken(): UndefinedString;
12
+ static fromAuthorizationProvider<T extends AuthorizationProvider>(provider: AuthorizationProvider): MeowSdkClient<T>;
13
+ }
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ import { AccountClient } from "./account";
2
+ export class MeowSdkClient {
3
+ account;
4
+ authProvider;
5
+ constructor(authProvider) {
6
+ this.authProvider = authProvider;
7
+ this.account = new AccountClient(authProvider);
8
+ }
9
+ isAuthorizationExist() {
10
+ return (this.authProvider.getAccessToken() != undefined ||
11
+ this.authProvider.getRefreshToken() != undefined);
12
+ }
13
+ updateAccessToken(accessToken) {
14
+ this.authProvider.setAccessToken(accessToken);
15
+ }
16
+ updateRefreshToken(refreshToken) {
17
+ this.authProvider.setRefreshToken(refreshToken);
18
+ }
19
+ getAccessToken() {
20
+ return this.authProvider.getAccessToken();
21
+ }
22
+ static fromAuthorizationProvider(provider) {
23
+ return new MeowSdkClient(provider);
24
+ }
25
+ }
@@ -0,0 +1,8 @@
1
+ export declare class AccessToken {
2
+ private payload;
3
+ private token;
4
+ constructor(token: string);
5
+ isExpired(): boolean;
6
+ userLogin(): string;
7
+ raw(): string;
8
+ }
package/dist/token.js ADDED
@@ -0,0 +1,20 @@
1
+ import { jwtDecode } from "jwt-decode";
2
+ export class AccessToken {
3
+ payload;
4
+ token;
5
+ constructor(token) {
6
+ this.payload = jwtDecode(token);
7
+ this.token = token;
8
+ }
9
+ isExpired() {
10
+ const now = new Date();
11
+ const expiredAt = new Date(this.payload.lifetime.expired_at);
12
+ return now > expiredAt;
13
+ }
14
+ userLogin() {
15
+ return this.payload.user.login;
16
+ }
17
+ raw() {
18
+ return this.token;
19
+ }
20
+ }
@@ -0,0 +1,7 @@
1
+ import { Result } from "ts-result-meow";
2
+ export type UndefinedString = string | undefined;
3
+ export type UserRole = "root" | "admin" | "user" | "inactive";
4
+ type MeowError = string;
5
+ export declare class MeowResult<TRes> extends Result<TRes, MeowError> {
6
+ }
7
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ import { Result } from "ts-result-meow";
2
+ export class MeowResult extends Result {
3
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@meowinc/meow-sdk",
3
+ "version": "0.0.1",
4
+ "description": "Meow SDK",
5
+ "keywords": [
6
+ "sdk"
7
+ ],
8
+ "homepage": "https://github.com/ProjectMeowInc/meow-sdk-ts#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/ProjectMeowInc/meow-sdk-ts/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/ProjectMeowInc/meow-sdk-ts.git"
15
+ },
16
+ "license": "MIT",
17
+ "author": "BlackSoulHub",
18
+ "type": "module",
19
+ "main": "dist/index.js",
20
+ "types": "dist/index.d.ts",
21
+ "scripts": {
22
+ "build": "tsc"
23
+ },
24
+ "dependencies": {
25
+ "ts-result-meow": ">=0.0.13",
26
+ "jwt-decode": ">=4"
27
+ },
28
+ "devDependencies": {
29
+ "typescript": "^5.9.3"
30
+ }
31
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ // File Layout
4
+ "rootDir": "./src",
5
+ "outDir": "./dist",
6
+
7
+ "module": "ES2022",
8
+ "moduleResolution": "bundler",
9
+ "target": "ES2024",
10
+ "declaration": true,
11
+
12
+ "strict": true,
13
+ "noImplicitAny": true,
14
+ "esModuleInterop": true,
15
+ "strictNullChecks": true,
16
+ },
17
+ "exclude": ["node_modules"],
18
+ "include": ["src/**/*"],
19
+ }