export type CreateApiArgs = {Authorization?: string | undefined, 'Content-Type'?: string | undefined, Accept?: string | undefined, body: Record}; export type CreateApiOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; /** * Webhook - Protocolo Revogado * * POST / */ export const createApi = async (args: CreateApiArgs, options?: CreateApiOptions) => { const {baseUrl = "https://cloud-hom.bry.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/${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 };