First commit

This commit is contained in:
2026-03-31 21:51:07 +02:00
commit 59d9434af9
20 changed files with 2312 additions and 0 deletions

19
src/core/environment.ts Normal file
View 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
View 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;
};

View File

@@ -0,0 +1,9 @@
export interface GetAuthMethodsReq {
unameMailPhone: string;
}
export interface AuthMethods {
password: boolean;
email: boolean;
sms: boolean;
}

View File

@@ -0,0 +1,3 @@
export interface GenericErrorBody {
error: string;
}

13
src/index.test.ts Normal file
View 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
View File

@@ -0,0 +1,5 @@
import {KeyringAPI} from "./storage/keyring";
export function jezus(keyring: KeyringAPI) {
keyring.set("keyring", "apad");
}

View 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
View File

@@ -0,0 +1,5 @@
import {authHandlers} from "./handlers/auth.http";
export const allHandlers = [
...authHandlers
]

4
src/mocks/node.ts Normal file
View File

@@ -0,0 +1,4 @@
import {setupServer} from "msw/node";
import {allHandlers} from "./index";
export const mockServer = setupServer(...allHandlers)

View 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 = {};
}
}

View 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()
})
})

View 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
View File

@@ -0,0 +1,6 @@
export interface KeyringAPI {
set(key: string, value: any): void;
get(key: string): string;
delete(key: string): void;
flush(): void;
}