import { HttpStatus } from '@nestjs/common'; import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; import { RESTError } from '../models/wrappers/rest-wrapper.class'; export interface IAPIConfig { name?: string; baseUrl: string; timeout?: number; accessToken?: string; } export class RestClient { private _name: string; private _instance: AxiosInstance; constructor(config: IAPIConfig) { this._name = config.name || config.baseUrl; const axiosConfig: AxiosRequestConfig = { baseURL: config.baseUrl, timeout: config.timeout || 30000, }; if (config.accessToken) { axiosConfig.headers = { 'x-access-token': config.accessToken }; } this._instance = axios.create(axiosConfig); } public async get(route: string, sessionToken?: string): Promise { try { const config = this._buildRequestConfig(sessionToken); if (config) { const response = await this._instance.get(route, config); return response.data.result.data; } const response = await this._instance.get(route); return response.data.result.data; } catch (e: any) { this._handleError(e); } } public async post(route: string, requestBody: any, sessionToken?: string): Promise { try { const config = this._buildRequestConfig(sessionToken); if (config) { const response = await this._instance.post(route, requestBody, config); return response.data.result.data; } const response = await this._instance.post(route, requestBody); return response.data.result.data; } catch (e: any) { this._handleError(e); } } public async put(route: string, requestBody: any, sessionToken?: string): Promise { try { const response = await this._instance.put(route, requestBody, this._buildRequestConfig(sessionToken)); return response.data.result.data; } catch (e: any) { this._handleError(e); } } public async delete(route: string, sessionToken?: string): Promise { try { const response = await this._instance.delete(route, this._buildRequestConfig(sessionToken)); return response.data.result.data; } catch (e: any) { this._handleError(e); } } public async patch(route: string, requestBody: any, sessionToken?: string): Promise { try { const response = await this._instance.patch(route, requestBody, this._buildRequestConfig(sessionToken)); return response.data.result.data; } catch (e: any) { this._handleError(e); } } private _buildRequestConfig(sessionToken?: string): AxiosRequestConfig | undefined { if (!sessionToken) return undefined; return { headers: { 'authorization': sessionToken }, }; } private _handleError(e: any): never { if (e.response) { if (e.response.data?.errors?.length && e.response.data.errors[0].keyword) { throw new RESTError(e.response.data.errors[0].keyword, null, HttpStatus.BAD_REQUEST); } else if (e.response.data?.errors && e.response.data.errors.keyword) { // because of damn storage api made by DITO throw new RESTError(e.response.data.errors.keyword, null, HttpStatus.BAD_REQUEST); } throw new RESTError('internal_error', e, HttpStatus.INTERNAL_SERVER_ERROR); } else if (e.request) { throw new RESTError(`service_unavailable_${this._name}`, e, HttpStatus.SERVICE_UNAVAILABLE); } else { throw new RESTError(`bad_config_${this._name}`, e, HttpStatus.SERVICE_UNAVAILABLE); } } }