export type HttpS3WebhookBucket = {name?: string | undefined}; export type HttpS3WebhookObject = {key?: string | undefined, size?: number | undefined, userMetadata?: {[key: string]: string} | undefined}; export type HttpS3WebhookDetail = {bucket?: HttpS3WebhookBucket | undefined, object?: HttpS3WebhookObject | undefined}; export type HttpS3WebhookRecord = {eventName?: string | undefined, eventSource?: string | undefined, s3?: HttpS3WebhookDetail | undefined}; export type HttpS3WebhookPayload = {Records?: Array | undefined}; export type CreateApiIWebhooksS3Args = {'X-Webhook-Secret': string, body: HttpS3WebhookPayload}; export type CreateApiIWebhooksS3Options = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * S3 upload-completed webhook * * POST /i/webhooks/s3 */ export const createApiIWebhooksS3 = async (args: CreateApiIWebhooksS3Args, options: CreateApiIWebhooksS3Options) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args['X-Webhook-Secret'] != null) headers.set("X-Webhook-Secret", String(args['X-Webhook-Secret'])) const url = `${baseUrl.replace(/\/+$/, '')}/i/webhooks/s3${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 };