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>
46 lines (38 loc) • 1.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchFile = void 0;
exports.fetchFile = `import axios, { AxiosRequestConfig } from 'axios';
import FormData from 'form-data';
const urlServer = '';
interface FETCH {
method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';
path: string;
body?: Record<any, any>;
query?: Record<string, string>;
headers?: Record<string, string>;
files?: Record<string, File>;
}
const getQueryUrl = (query: FETCH["query"]): string => {
if (!query || !Object.values(query).some((val) => val)) return "";
return Object.entries(query)
.filter(([_, value]) => value)
.map(([key, value]) => \`\${key}=\${value}\`)
.join("&");
};
export const Fetch = async ({ path, method, body, query, headers, files }: FETCH) => {
let completeUrl = urlServer.replace(/\\/$/, '') + path;
if (query && Object.keys(query).length > 0) completeUrl += '?' + getQueryUrl(query)
const config: AxiosRequestConfig = { url: completeUrl, method };
if (headers) config.headers = headers;
if (body) config.data = body;
else if (files) {
const formData = new FormData();
Object.entries(files).forEach(([name, file]) => formData.append(name, file));
config.data = formData;
config.headers = { ...config.headers, 'Content-Type': 'multipart/form-data' };
}
return axios(config)
.then((res) => res.data)
.catch((err) => err.response.data);
};`;
//# sourceMappingURL=fetchFile.js.map