import axios, { AxiosInstance, AxiosResponse, Method, ResponseType } from 'axios'; import { A_AUTH_TYPES__IAuthenticator } from '../types/A_AUTH_Authenticator.types'; import { A_AUTH_ContextClass } from './A_AUTH_Context.class'; import { A_AUTH_TYPES__APIProviderRequestConfig } from '../types/A_AUTH_APIProvider.types'; import { A_SDK_ServerError } from '@adaas/a-sdk-types'; export class A_AUTH_APIProvider { loading: boolean = false; protected _axiosInstance!: AxiosInstance protected version: string = 'v1' protected context!: C; private _base: string constructor( context: C, baseURL?: string, ) { this.context = context; this._base = baseURL || this.baseURL; this.init(); } /** * Wrapper to work with dynamic ENV variables */ protected get baseURL(): string { return this.baseURL; } init() { this._axiosInstance = axios.create({ baseURL: this.baseURL }); } protected async request( method: Method, url: string, authenticator?: A_AUTH_TYPES__IAuthenticator, data?: any, params?: any, config?: A_AUTH_TYPES__APIProviderRequestConfig ): Promise { try { /** * Make sure the context is ready and all configurations are loaded */ await this.context.ready; this.loading = true; this.context.Logger.log(`Calling ${method.toUpperCase()} ${url}`, { data, params, }); this.context.Logger.log(`ENV CONFIGURATIONS AUTH ${this.context.getConfigurationProperty('ENABLE_AUTH')}`); this.context.Logger.log(`ENV CONFIGURATIONS AUTH ${typeof this.context.getConfigurationProperty('ENABLE_AUTH')}`); const includeAuth = this.context.getConfigurationProperty('ENABLE_AUTH') ? (!config || !config.adaas || config.adaas.auth !== false) : false; this.context.Logger.log(`Include Auth: ${includeAuth}`); let token: string | undefined; if (includeAuth) { const targetAuth = authenticator || this.context.getAuthenticator(); await targetAuth.authenticate(); token = await targetAuth.getToken(); this.context.Logger.log(`Authentication successful`); } else { this.context.Logger.log(`Authentication skipped`); } const result: AxiosResponse = await this._axiosInstance.request({ method, baseURL: this.baseURL, url: `/api/${this.version}${url}`, data, headers: (includeAuth && token) ? { ...config?.headers, Authorization: `Bearer ${token}` } : config?.headers, params: config?.params ? config.params : params, responseType: config?.responseType ? config.responseType : 'json', }); this.loading = false; this.context.Logger.log(`Response received -> result.data`, result.data); return this.context.responseFormatter(result, config?.meta); } catch (error) { this.loading = false; this.context.errorsHandler(error as any, config?.meta) const receivedError = new A_SDK_ServerError(error); this.context.Logger.error(receivedError); throw receivedError; } } protected async post( url: string, body?: any, config?: A_AUTH_TYPES__APIProviderRequestConfig, ): Promise { return this.request('post', url, config?.authenticator, body, {}, config); } protected async get( url: string, params?: any, config?: A_AUTH_TYPES__APIProviderRequestConfig, ): Promise { return this.request('get', url, config?.authenticator, {}, params, config); } protected async put( url: string, body?: any, config?: A_AUTH_TYPES__APIProviderRequestConfig, ): Promise { return this.request('put', url, config?.authenticator, body, {}, config); } protected async delete( url: string, config?: A_AUTH_TYPES__APIProviderRequestConfig, ): Promise { return this.request('delete', url, config?.authenticator, {}, {}, config); } protected async patch( url: string, body?: any, config?: A_AUTH_TYPES__APIProviderRequestConfig, ): Promise { return this.request('patch', url, config?.authenticator, body, {}, config); } }