import { ToolsHelperForTS } from '../utils/ToolsHelperForTS'; export interface HttpParamsGet { url: string query?: Record | Object headers?: Record } export interface HttpParamsPost { url: string data?: string | Object | ArrayBuffer query?: Record | Object headers?: Record } export interface HttpParamsForm { url: string data?: Record | Object | undefined query?: Record | Object headers?: Record } export class HttpHelperX { /** * * @param ct Content-Type * @param headers * @returns */ static getHeaders(ct: string, headers?: Record) { return { "Content-Type": ct, // "Accept": "application/json", ...headers } } static getUrl(url: string, query?: Record | Object) { let u = url if (query) { let q = query if (typeof query === 'object') { q = ToolsHelperForTS.classToRecord(query) } u = `${u}${u.indexOf('?') < 0 ? '?' : u.endsWith('$') ? '' : '&'}` Object.entries(q).forEach((row) => { if (row[1]) { u = `${u}${row[0]}=${row[1] as string}&` } }); u = u.slice(0, u.length - 1) } return u } static getContent(data?: Record | Object) { if (!data) { return undefined } let u = '' let q = data if (typeof data === 'object') { q = ToolsHelperForTS.classToRecord(data) } Object.entries(q).forEach((row) => { if (row[1]) { u = `${u}${row[0]}=${row[1] as string}&` } }); u = u.slice(0, u.length - 1) return u } }