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

12
.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
/tmp
/out-tsc
/node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/.pnp
.pnp.js
.vscode/*
.idea/*

10
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

2092
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "chatenium-sdk",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"test": "vitest run",
"test:watch": "vitest"
},
"devDependencies": {
"typescript": "^5.5.3",
"vitest": "^4.1.2"
},
"private": true,
"dependencies": {
"axios": "^1.14.0",
"msw": "^2.12.14"
}
}

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;
}

12
tsconfig.json Normal file
View File

@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["src"]
}

8
vitest.config.ts Normal file
View File

@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
setupFiles: ['./vitest.setup.ts'],
},
});

6
vitest.setup.ts Normal file
View File

@@ -0,0 +1,6 @@
import { beforeAll, afterEach, afterAll, vi } from 'vitest';
import {mockServer} from "./src/mocks/node";
beforeAll(() => mockServer.listen({ onUnhandledRequest: 'error' }));
afterEach(() => mockServer.resetHandlers());
afterAll(() => mockServer.close());