Files
directus/sdk/src/utils/request.ts
2024-03-31 14:28:11 +02:00

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 });
});
});
};