export type Event = {time: number, uuid: string} | {[key: string]: unknown}; export type CreateApiDatabaseTableArgs = {database: string, table: string, body: Event}; export type CreateApiDatabaseTableOptions = {baseUrl?: string | undefined, td1?: string | undefined, wp13n?: string | undefined, fetch?: typeof fetch | undefined}; /** * Get offers for server request * * POST /{database}/{table} */ export const createApiDatabaseTable = async (args: CreateApiDatabaseTableArgs, options?: CreateApiDatabaseTableOptions) => { const {baseUrl = "https://us01.p13n.in.treasuredata.com", fetch: fetchFn = fetch, td1, wp13n} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.treasuredata.v1+json") if (td1 !== undefined) headers.set('authorization', `Bearer ${td1}`) if (wp13n !== undefined) headers.set("WP13n-Token", wp13n) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.database))}/${encodeURIComponent(String(args.table))}${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 };