import {z} from 'zod' export type GetApiApiV1TriggerDeliveryEndpointsEndpointIdDeliveriesArgs = {endpoint_id: string, page_size?: number | undefined, page_token?: string | undefined, status?: ('TRIGGER_DELIVERY_STATUS_DELIVERED' | 'TRIGGER_DELIVERY_STATUS_PENDING' | 'TRIGGER_DELIVERY_STATUS_FAILED' | 'TRIGGER_DELIVERY_STATUS_RETRYING') | undefined, channel?: string | undefined, after?: string | undefined, before?: string | undefined}; export type GetApiApiV1TriggerDeliveryEndpointsEndpointIdDeliveriesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const triggersTriggerDeliveryStatus = z.enum(["TRIGGER_DELIVERY_STATUS_DELIVERED", "TRIGGER_DELIVERY_STATUS_PENDING", "TRIGGER_DELIVERY_STATUS_FAILED", "TRIGGER_DELIVERY_STATUS_RETRYING"]); export const triggersTriggerMode = z.enum(["WEBHOOK", "POLL"]); export const triggersTriggerDelivery = z.object({connection_id: z.string().optional(), connection_key_id: z.string().optional(), connection_provider: z.string().optional(), endpoint_id: z.string().optional(), event_id: z.string().optional(), message_timestamp: z.string().optional(), msg_id: z.string().optional(), next_attempt_at: z.string().optional(), status: triggersTriggerDeliveryStatus.optional(), subscription_id: z.string().optional(), trigger_mode: triggersTriggerMode.optional(), trigger_name: z.string().optional()}); export const triggersListTriggerDeliveriesByEndpointResponse = z.object({deliveries: z.array(triggersTriggerDelivery).optional(), next_page_token: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List trigger deliveries to an endpoint * * GET /api/v1/trigger-delivery/endpoints/{endpoint_id}/deliveries */ export const getApiApiV1TriggerDeliveryEndpointsEndpointIdDeliveries = async (args: GetApiApiV1TriggerDeliveryEndpointsEndpointIdDeliveriesArgs, options?: GetApiApiV1TriggerDeliveryEndpointsEndpointIdDeliveriesOptions) => { const {baseUrl = "https://$SCALEKIT_ENVIRONMENT_URL", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.status != null) query.append("status", String(args.status)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/trigger-delivery/endpoints/${encodeURIComponent(String(args.endpoint_id))}/deliveries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(triggersListTriggerDeliveriesByEndpointResponse, await res.json()) };