UNPKG

bridgets

Version:

<p align="center"> <a href="https://bridgets.co"> <img src="http://bridgets.co/assets/logo-short.svg" height="48" /> <h1 align="center">BridgeTS</h1> </a> </p>

2 lines 1.43 kB
export declare const fetchFile = "import axios, { AxiosRequestConfig } from 'axios';\nimport FormData from 'form-data';\n\nconst urlServer = '';\n\ninterface FETCH {\n method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';\n path: string;\n body?: Record<any, any>;\n query?: Record<string, string>;\n headers?: Record<string, string>;\n files?: Record<string, File>;\n}\n\nconst getQueryUrl = (query: FETCH[\"query\"]): string => {\n if (!query || !Object.values(query).some((val) => val)) return \"\";\n\n return Object.entries(query)\n .filter(([_, value]) => value)\n .map(([key, value]) => `${key}=${value}`)\n .join(\"&\");\n};\n\nexport const Fetch = async ({ path, method, body, query, headers, files }: FETCH) => {\n let completeUrl = urlServer.replace(/\\/$/, '') + path;\n if (query && Object.keys(query).length > 0) completeUrl += '?' + getQueryUrl(query)\n\n const config: AxiosRequestConfig = { url: completeUrl, method };\n\n if (headers) config.headers = headers;\n if (body) config.data = body;\n else if (files) {\n const formData = new FormData();\n Object.entries(files).forEach(([name, file]) => formData.append(name, file));\n config.data = formData;\n config.headers = { ...config.headers, 'Content-Type': 'multipart/form-data' };\n }\n\n return axios(config)\n .then((res) => res.data)\n .catch((err) => err.response.data);\n};"; //# sourceMappingURL=fetchFile.d.ts.map