// @ts-nocheck import config from '@/config/global'; import { IAPIHost, IRequestOptions } from './request.interfaces'; import Cookie from 'js-cookie'; const getToken = () => { return ( Cookie.get('x-token', { path: '/', domain: '.top3-talent.com' }) || window.localStorage.getItem('Authorization') || null ); }; export async function postAPI( host: IAPIHost, path: string, body?: {}, query?: {}, headers?: {}, ): Promise { return requestAPI(host, path, { method: 'POST', body, query, headers }); } export async function getAPI( host: IAPIHost, path: string, query?: {}, body?: {}, ): Promise { return requestAPI(host, path, { method: 'GET', query, body }); } export async function putAPI( host: IAPIHost, path: string, body?: {}, ): Promise { return requestAPI(host, path, { method: 'PUT', body }); } export async function deleteAPI( host: IAPIHost, path: string, body?: {}, ): Promise { return requestAPI(host, path, { method: 'DELETE', body }); } export async function patchAPI( host: IAPIHost, path: string, body?: {}, query?: {}, headers?: {}, ): Promise { return requestAPI(host, path, { method: 'PATCH', body, query, headers }); } export async function requestAPI( host: IAPIHost, path: string, options: IRequestOptions | undefined, ): Promise { let url: string = host.dev + path; if (config.mode == 'development') { url = host.dev + path; } else if (config.mode == 'test') { url = host.test + path; } else if (config.mode == 'production') { url = host.prod + path; } return request(url, { ...options, api: true }); } export async function request( url: string, options: IRequestOptions | undefined, ): Promise { let auth: string | null = getToken(); let op: any = { method: (options && options.method) || 'GET' }; if (options && options.api) { op.headers = (options && options.headers) || {}; if ( (!options || !options.no_token) && !op.headers['Authorization'] && auth ) { op.headers['Authorization'] = auth; } // if (!op.headers['x-talent-saas'] && config.system) { // op.headers['x-talent-saas'] = config.system; // } } if (options && options.body) { if (op.headers['Content-Type'] == 'application/x-www-form-urlencoded') { var formBody = []; for (var property in options.body) { var encodedKey = encodeURIComponent(property); var encodedValue = encodeURIComponent(options.body[property]); if (options.body?.[property]) formBody.push(encodedKey + '=' + encodedValue); } op.body = formBody.join('&'); } else { op.body = typeof options.body == 'string' ? options.body : JSON.stringify(options.body); } } else if (options && options.form) { if (options.form instanceof FormData) { op.body = options.form; } else { let formData = new FormData(); for (let key in options.form) { if (options.form?.[key]) formData.append(key, options.form[key]); } op.body = formData; } } if (options && options.query) { url += (url.indexOf('?') < 0 ? '?' : '&') + Object.keys(options.query) .filter((key) => options?.query?.[key]) .map((d) => `${d}=${options.query![d]}`) .join('&'); } return new Promise((resolve, reject) => { fetch(url, op) ?.then(async (res) => { if (res.ok) { return res.status == 204 ? new Promise((r) => r({})) : res.json(); } else { const responseJSON = await res.json(); return new Promise((resolve) => resolve({ error: res.status, msg: responseJSON?.msg || '', errorMsg: responseJSON?.error || '', }), ); } }) .then((data: any) => { resolve(data); }) .catch((err) => { console.log(err); }); }); }