85 lines
2.6 KiB
TypeScript
85 lines
2.6 KiB
TypeScript
import { WalletType } from 'rnwalletman';
|
|
|
|
const DOMAIN = '192.168.1.168:16000';
|
|
const BASE_URL = `http://${DOMAIN}`;
|
|
const WS_URL = `ws://${DOMAIN}/ws`;
|
|
|
|
class Api {
|
|
public static readonly BASE_URL = BASE_URL;
|
|
public static readonly WS_URL = WS_URL;
|
|
private static _instance: Api | null = null;
|
|
private userId: number = 0;
|
|
|
|
private constructor() {}
|
|
|
|
public setUserId(userId: number) {
|
|
this.userId = userId;
|
|
}
|
|
|
|
public getUserId(): number {
|
|
return this.userId;
|
|
}
|
|
|
|
public static get instance() {
|
|
if (Api._instance === null) {
|
|
Api._instance = new Api();
|
|
}
|
|
return Api._instance;
|
|
}
|
|
|
|
private headers(): Record<string, string> {
|
|
const h: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
if (this.userId > 0) {
|
|
h['X-User-ID'] = String(this.userId);
|
|
}
|
|
return h;
|
|
}
|
|
|
|
public async login(username: string, password: string): Promise<number> {
|
|
const res = await fetch(`${Api.BASE_URL}/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
const data = await res.json();
|
|
if (!data.success) throw new Error(data.message);
|
|
this.userId = data.data.userId;
|
|
return this.userId;
|
|
}
|
|
|
|
public async register(walletType: WalletType, params: any) {
|
|
const res = await fetch(`${Api.BASE_URL}/register`, {
|
|
method: 'POST',
|
|
headers: this.headers(),
|
|
body: JSON.stringify({ walletType, params }),
|
|
});
|
|
const data = await res.json();
|
|
if (!data.success) throw new Error(data.message);
|
|
return data;
|
|
}
|
|
|
|
public async requestOTP(walletType: WalletType, mobile: string, params: any = {}) {
|
|
const res = await fetch(`${Api.BASE_URL}/request-otp`, {
|
|
method: 'POST',
|
|
headers: this.headers(),
|
|
body: JSON.stringify({ walletType, mobile, ...params }),
|
|
});
|
|
const data = await res.json();
|
|
if (!data.success) throw new Error(data.message);
|
|
return data;
|
|
}
|
|
|
|
public async verifyOTP(walletType: WalletType, mobile: string, otp: string, params: any = {}) {
|
|
const res = await fetch(`${Api.BASE_URL}/verify-otp`, {
|
|
method: 'POST',
|
|
headers: this.headers(),
|
|
body: JSON.stringify({ walletType, mobile, otp, params }),
|
|
});
|
|
const data = await res.json();
|
|
if (!data.success) throw new Error(data.message);
|
|
return data;
|
|
}
|
|
}
|
|
|
|
export default Api;
|