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.

54 lines (53 loc) 2 kB
/* * */ import { HttpException } from '@tmlmobilidade/consts'; /** * Fetches data from a URL using the SWR fetcher function. * @param urlOrOptions The URL to fetch from or the options object. * @returns Promise resolving to the fetched data * @example * ```ts * // Fetch data from a URL * const data = await swrFetcher('/api/users/123'); * * // Fetch data from a URL with options * const data = await swrFetcher({ url: '/api/users/123', credentials: 'omit' }); * ``` */ export async function swrFetcher(urlOrOptions) { // // // Extract the URL either from the string directly // or from the options object const urlValue = typeof urlOrOptions === 'string' ? urlOrOptions : urlOrOptions.url; // // Extract the credentials option from the options object // or use the default value. By default, it uses 'include' credentials. const credentialsValue = typeof urlOrOptions === 'string' ? 'include' : urlOrOptions.credentials ?? 'include'; // // Perform the fetch operation const res = await fetch(urlValue, { credentials: credentialsValue }); const data = await res.json(); if (!res.ok) { throw new HttpException(res.status, data.error ?? 'An error occurred'); } // // Return the data directly if the useProperApiResponse option is false // or directly accessing the data property of the HttpResponse object. if (typeof urlOrOptions === 'object' && urlOrOptions.useProperApiResponse === false) { return data; } return data.data; } ; /** * Fetches data from a URL using the SWR fetcher function. * @param urlOrOptions The URL to fetch from or the options object. * @returns Promise resolving to the fetched data * @deprecated Use `swrFetcher` with an options object instead. * ```ts * const data = await swrFetcher({ url: '/api/users/123', credentials: 'omit' }); * ``` */ export async function unauthenticatedSwrFetcher(url) { return swrFetcher({ credentials: 'omit', url: url }); }