51 lines
918 B
TypeScript
51 lines
918 B
TypeScript
import type { AxiosInstance } from "axios";
|
|
import { logWarning } from "./logger.js";
|
|
|
|
export type ServiceResult<T = unknown> = {
|
|
result: T;
|
|
successful: boolean;
|
|
};
|
|
|
|
export class API_Error {
|
|
constructor(public error: string) {}
|
|
}
|
|
|
|
export abstract class BaseClient {
|
|
private axiosInstance: AxiosInstance;
|
|
|
|
constructor(axiosInstance: AxiosInstance) {
|
|
this.axiosInstance = axiosInstance;
|
|
}
|
|
|
|
getAxios(): AxiosInstance {
|
|
return this.axiosInstance;
|
|
}
|
|
}
|
|
|
|
export abstract class BaseService<T> {
|
|
private client: T;
|
|
|
|
constructor(client: T) {
|
|
this.client = client;
|
|
}
|
|
|
|
getClient(): T {
|
|
return this.client;
|
|
}
|
|
|
|
getSuccessfulResult<R = unknown>(result: R): ServiceResult<R> {
|
|
return {
|
|
result,
|
|
successful: true,
|
|
};
|
|
}
|
|
|
|
getErrorResult(errorMessage: string, error?: unknown): ServiceResult<string> {
|
|
logWarning(errorMessage, error);
|
|
return {
|
|
result: errorMessage,
|
|
successful: false,
|
|
};
|
|
}
|
|
}
|