export type CreateApiOauth2TokenArgs = {body: {grant_type?: string | undefined, client_id?: string | undefined, client_secret?: string | undefined}}; export type CreateApiOauth2TokenOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; /** * Authenticate * * POST /oauth2/token */ export const createApiOauth2Token = async (args: CreateApiOauth2TokenArgs, options?: CreateApiOauth2TokenOptions) => { const {baseUrl = "https://api.datavant.io/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/oauth2/token${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return (await res.json()) as unknown };