mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
29 lines
828 B
TypeScript
29 lines
828 B
TypeScript
import type { FetchInterface } from '../index.js';
|
|
import { extractData } from './extract-data.js';
|
|
|
|
/**
|
|
* Request helper providing default settings
|
|
*
|
|
* @param url The request URL
|
|
* @param options The request options
|
|
*
|
|
* @returns The API result if successful
|
|
*/
|
|
export const request = async <Output = any>(
|
|
url: string,
|
|
options: RequestInit,
|
|
fetcher: FetchInterface = globalThis.fetch,
|
|
): Promise<Output> => {
|
|
options.headers =
|
|
typeof options.headers === 'object' && !Array.isArray(options.headers)
|
|
? (options.headers as Record<string, string>)
|
|
: {};
|
|
|
|
return fetcher(url, options).then((response) => {
|
|
return extractData(response).catch((reason) => {
|
|
const errors = typeof reason === 'object' && 'errors' in reason ? reason.errors : reason;
|
|
return Promise.reject({ errors, response });
|
|
});
|
|
});
|
|
};
|