UNPKG

@tmlmobilidade/utils

Version:

A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.

60 lines (59 loc) 1.84 kB
/* * */ import { HttpResponse } from './response.js'; /** * Sends a multipart form data request to a URL. * @param url - The URL to send the request to * @param formData - The FormData object containing the multipart form data * @returns Promise resolving to HttpResponse containing data, error and status * @example * ```ts * const formData = new FormData(); * formData.append('file', fileBlob); * formData.append('name', 'profile.jpg'); * const response = await multipartFetch('/api/upload', formData); * ``` */ export async function multipartFetch(url, formData) { try { const response = await fetch(url, { body: formData, credentials: 'include', method: 'POST', }); const data = await response.json(); if (!response.ok || data.error) { return new HttpResponse({ data: null, error: data.error, statusCode: response.status, }); } return new HttpResponse({ data: data.data, error: null, statusCode: response.status, }); } catch (error) { return new HttpResponse({ data: null, error: error instanceof Error ? error.message : 'Network error', statusCode: 500, }); } } /** * Uploads a file to a URL using multipart form data. * @param url - The URL to send the request to * @param file - The file to upload * @returns Promise resolving to HttpResponse containing data, error and status * @example * ```ts * const response = await uploadFile('/api/upload', file); * ``` */ export async function uploadFile(url, file) { const formData = new FormData(); formData.append('file', file, file.name); return await multipartFetch(url, formData); }