export type OpaqueEnvelope = {body: string, schema: 'taskhawk.opaque-envelope.v1'}; export type CreateApiV1EventsArgs = {body: OpaqueEnvelope}; export type CreateApiV1EventsOptions = {baseUrl: string, enrolledBearer?: string | undefined, fetch?: typeof fetch | undefined}; /** * Submit an opaque event * * POST /v1/events */ export const createApiV1Events = async (args: CreateApiV1EventsArgs, options: CreateApiV1EventsOptions) => { const {baseUrl, fetch: fetchFn = fetch, enrolledBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (enrolledBearer !== undefined) headers.set('authorization', `Bearer ${enrolledBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events${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 undefined };