First commit
This commit is contained in:
19
src/core/environment.ts
Normal file
19
src/core/environment.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export interface SDKConfig {
|
||||
apiUrl: string;
|
||||
}
|
||||
|
||||
const DefaultEnvironment: SDKConfig = {
|
||||
apiUrl: "https://api.chatenium.hu"
|
||||
}
|
||||
|
||||
let currentConfig = {...DefaultEnvironment}
|
||||
|
||||
export const environment = {
|
||||
get: () => currentConfig,
|
||||
overwrite: (newConfig: Partial<SDKConfig>) => {
|
||||
currentConfig = {
|
||||
...currentConfig,
|
||||
...newConfig,
|
||||
};
|
||||
},
|
||||
}
|
||||
25
src/core/http.ts
Normal file
25
src/core/http.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import {environment} from "./environment";
|
||||
|
||||
let client: AxiosInstance;
|
||||
|
||||
export const getClient = () => {
|
||||
if (!client) {
|
||||
const env = environment.get();
|
||||
|
||||
client = axios.create({
|
||||
baseURL: env.apiUrl,
|
||||
timeout: 1000,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
|
||||
/*client.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});*/
|
||||
}
|
||||
return client;
|
||||
};
|
||||
9
src/domain/authService.schema.ts
Normal file
9
src/domain/authService.schema.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export interface GetAuthMethodsReq {
|
||||
unameMailPhone: string;
|
||||
}
|
||||
|
||||
export interface AuthMethods {
|
||||
password: boolean;
|
||||
email: boolean;
|
||||
sms: boolean;
|
||||
}
|
||||
3
src/domain/common.schema.ts
Normal file
3
src/domain/common.schema.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface GenericErrorBody {
|
||||
error: string;
|
||||
}
|
||||
13
src/index.test.ts
Normal file
13
src/index.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import {beforeAll, describe, expect, it} from "vitest";
|
||||
import {jezus} from "./index";
|
||||
import {KeyringAPI} from "./storage/keyring";
|
||||
import {KeyringMock} from "./mocks/storage/keyring";
|
||||
|
||||
describe("Testing", () => {
|
||||
it("should be defined", () => {
|
||||
let keyringMock = new KeyringMock();
|
||||
jezus(keyringMock)
|
||||
|
||||
expect(keyringMock.get("keyring")).toBe("apad");
|
||||
})
|
||||
})
|
||||
5
src/index.ts
Normal file
5
src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import {KeyringAPI} from "./storage/keyring";
|
||||
|
||||
export function jezus(keyring: KeyringAPI) {
|
||||
keyring.set("keyring", "apad");
|
||||
}
|
||||
12
src/mocks/handlers/auth.http.ts
Normal file
12
src/mocks/handlers/auth.http.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { http, HttpResponse } from 'msw'
|
||||
import {AuthMethods} from "../../domain/authService.schema";
|
||||
|
||||
export const authHandlers = [
|
||||
http.get('*/user/authOptions', () => {
|
||||
return HttpResponse.json(<AuthMethods>{
|
||||
email: true,
|
||||
password: true,
|
||||
sms: false,
|
||||
})
|
||||
}),
|
||||
]
|
||||
5
src/mocks/index.ts
Normal file
5
src/mocks/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import {authHandlers} from "./handlers/auth.http";
|
||||
|
||||
export const allHandlers = [
|
||||
...authHandlers
|
||||
]
|
||||
4
src/mocks/node.ts
Normal file
4
src/mocks/node.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import {setupServer} from "msw/node";
|
||||
import {allHandlers} from "./index";
|
||||
|
||||
export const mockServer = setupServer(...allHandlers)
|
||||
21
src/mocks/storage/keyring.ts
Normal file
21
src/mocks/storage/keyring.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {KeyringAPI} from "../../storage/keyring";
|
||||
|
||||
export class KeyringMock implements KeyringAPI {
|
||||
ring: { [key: string]: string } = {};
|
||||
|
||||
set(key: string, value: any) {
|
||||
this.ring[key] = value;
|
||||
}
|
||||
|
||||
get(key: string): string {
|
||||
return this.ring[key];
|
||||
}
|
||||
|
||||
delete(key: string) {
|
||||
delete this.ring[key];
|
||||
}
|
||||
|
||||
flush() {
|
||||
this.ring = {};
|
||||
}
|
||||
}
|
||||
12
src/services/authService.test.ts
Normal file
12
src/services/authService.test.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {describe, expect, it} from "vitest";
|
||||
import {AuthService} from "./authService";
|
||||
|
||||
describe("AuthService", () => {
|
||||
it("should return authMethods", async () => {
|
||||
const service = new AuthService();
|
||||
const methods = await service.getAuthMethods("")
|
||||
expect(methods.sms).toBeFalsy()
|
||||
expect(methods.email).toBeTruthy()
|
||||
expect(methods.password).toBeTruthy()
|
||||
})
|
||||
})
|
||||
18
src/services/authService.ts
Normal file
18
src/services/authService.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {getClient} from "../core/http";
|
||||
import {AuthMethods} from "../domain/authService.schema";
|
||||
import {isAxiosError} from "axios";
|
||||
import {GenericErrorBody} from "../domain/common.schema";
|
||||
|
||||
export class AuthService {
|
||||
async getAuthMethods(unameMailPhone: String): Promise<AuthMethods> {
|
||||
try {
|
||||
const resp = await getClient().get<AuthMethods>(`user/authOptions?unameMailPhone=${unameMailPhone}`);
|
||||
return resp.data
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/storage/keyring.ts
Normal file
6
src/storage/keyring.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface KeyringAPI {
|
||||
set(key: string, value: any): void;
|
||||
get(key: string): string;
|
||||
delete(key: string): void;
|
||||
flush(): void;
|
||||
}
|
||||
Reference in New Issue
Block a user