import { fetch } from 'cross-fetch'; import { HttpSuccessResponseBody } from '../../index.js'; import { formatRequestHeaders } from './formatRequestHeaders.js'; import { makeSpecifyHttpApiRoute } from './makeSpecifyHttpApiRoute.js'; import { wrapFetchCall } from './wrapFetchCall.js'; /** * Creates a fetch client to consume data from the Specify HTTP Public API. * @internal * @param personalAccessToken */ export function createFetchClient(personalAccessToken: string | undefined) { const headers = formatRequestHeaders(personalAccessToken); return { get: async ({ path }: { path: string }) => wrapFetchCall>( fetch(makeSpecifyHttpApiRoute(path), { method: 'GET', headers: headers, }), ), post: async ({ path, body }: { path: string; body: any }) => wrapFetchCall>( fetch(makeSpecifyHttpApiRoute(path), { method: 'POST', body: JSON.stringify(body), headers: headers, }), ), }; }