import {z} from 'zod' export type GetApiNotificationChannelsPublicIdDeliveriesArgs = {publicId: string, page?: number | undefined, per_page?: number | undefined}; export type GetApiNotificationChannelsPublicIdDeliveriesOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationDelivery = z.object({id: z.number().int(), channelId: z.number().int().optional(), eventType: z.string(), payload: z.object({}).optional(), statusCode: z.number().int().nullable().optional(), responseBody: z.string().nullable().optional(), attempts: z.number().int().optional(), nextRetryAt: z.string().nullable().optional(), deliveredAt: z.string().nullable().optional(), createdAt: z.string()}); export const paginationMeta = z.object({page: z.number().int(), perPage: z.number().int(), total: z.number().int(), totalPages: z.number().int()}); export const getApiNotificationChannelsPublicIdDeliveriesResponse = z.object({deliveries: z.array(notificationDelivery), meta: paginationMeta}); 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 }; /** * Delivery history * * GET /notification-channels/{publicId}/deliveries * * Also accepts auth: bearerOAuth */ export const getApiNotificationChannelsPublicIdDeliveries = async (args: GetApiNotificationChannelsPublicIdDeliveriesArgs, options?: GetApiNotificationChannelsPublicIdDeliveriesOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/notification-channels/${encodeURIComponent(String(args.publicId))}/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(getApiNotificationChannelsPublicIdDeliveriesResponse, await res.json()) };