import { DyFM_Error, DyFM_getConstructionStackLocation, DyFM_HttpCallType, DyFM_HttpResponseType, DyFM_Log, } from '@futdevpro/fsm-dynamo'; import { DyNTS_global_settings } from '../../_collections/global-settings.const'; export interface DyNTS_AxiosSettings { headers?: { [header: string]: string | string[] }; httpAgent?: any; httpsAgent?: any; timeout?: number; maxRedirects?: number; withCredentials?: boolean; responseType?: DyFM_HttpResponseType; } /** * API call params, * such as * type (HttpCallType), * endpoint, * and other options as httpOptions */ export class DyNTS_ApiCall_Params { /** * name the call to be able to identify it when debugging */ name: string; /** * type of the call as; HttpCallType .get, .post, ...ect */ type: DyFM_HttpCallType = DyFM_HttpCallType.get; /** * the endpoint of the API, without the baseUrl: * '/user/get/:userId', * where the parts starting with ':', will be used as pathParam */ baseUrl: string; /** * baseUrl of the API; * 'https://futdevpro.hu/api' */ endpoint: string; /** * if this value is true, the call will return the full HTTP call response in raw */ getFullResponse: boolean = false; /** * this setting will be passed to the axios http call */ httpOptions: any | DyNTS_AxiosSettings = {}; pathParamKeys: string[] = []; readonly stack: string; constructor( /** * set of options necessary for setting up an API call */ set: Partial ) { this.stack = DyFM_getConstructionStackLocation(); if (!set.name) { // ?TODO: nullable warning DyFM_Log.warn( 'WARNING: DyNTS_ApiCall_Params: name is not provided, using stack location!', '\n stack:', this.stack ); this.name = this.stack; } if (!set.baseUrl) { // ?TODO: nullable warning DyFM_Log.error( 'ERROR: DyNTS_ApiCall_Params: baseUrl is not provided, using global baseUrl!', '\n stack:', this.stack ); this.baseUrl = DyNTS_global_settings.baseUrl; } if (!set.endpoint) { // ?TODO: nullable warning DyFM_Log.warn( 'WARNING: DyNTS_ApiCall_Params: endpoint is not provided', '\n stack:', this.stack ); this.endpoint = ''; } else if (!set.endpoint.includes) { throw new Error( 'DyNTS_ApiCall_Params Error: something went wrong with the endpoint, it is provided but not valid' ); } else if (set.endpoint.includes('{')) { DyFM_Log.warn( `WARNING: using {param} bracets in endpoint definitions will be deprecated... (${set.endpoint})` + '\n use :param instead ({pathparam} --> :pathparam)' ); set.endpoint = set.endpoint.replace(/{/g, ':').replace(/}/g, ''); } else { this.pathParamKeys = set.endpoint.split('/') .filter(part => part.startsWith(':')) .map(part => part.slice(1)); } Object.assign(this, set); } } /** * basic axios HttpsOptions, * * learn more on axios package.... * TOD: link package documentations */ export class DyNTS_HttpOptions { responseType?: DyFM_HttpResponseType; headers?: { [header: string]: string | string[]; }; observe?: 'body'; reportProgress?: boolean; withCredentials?: boolean; constructor( set: { responseType?: DyFM_HttpResponseType, headers?: { [header: string]: string | string[]; }, observe?: 'body', reportProgress?: boolean, withCredentials?: boolean } ) { this.responseType = set.responseType; this.headers = set.headers; this.observe = set.observe; this.reportProgress = set.reportProgress; this.withCredentials = set.withCredentials; } }