import {z} from 'zod' export type GetApiApiV1NotificationsArgs = {limit?: number | undefined, offset?: number | undefined, unread_only?: boolean | undefined}; export type GetApiApiV1NotificationsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationType = z.enum(["welcome", "quote_ready", "quote_rejected", "payment_confirmed", "shipment_update", "delivery_confirmed", "project_status_changed", "project_submitted_by_admin", "po_approved", "po_revoked", "po_uploaded_by_admin", "quote_approved_by_admin", "waitlist_confirmation", "waitlist_invite_sent", "new_comment", "new_project_submitted", "payment_received", "order_confirmed", "po_submitted", "new_user_registered", "new_waitlist_signup", "new_project_comment", "order_request_submitted", "order_request_edited", "agent_escalation", "rfq_drafts_ready", "supplier_reply_parsed", "supplier_quote_received", "agent_escalation_rfq", "rfq_clarification_drafted", "rfq_response_sla", "quote_revision_overdue", "quote_reactivation_requested"]); export const notificationResponse = z.object({id: z.string(), type: notificationType, title: z.string(), body: z.string(), is_read: z.boolean(), read_at: z.string().nullable(), resource_type: z.string().nullable(), resource_id: z.string().nullable(), created_at: z.string()}); export const notificationListResponse = z.object({items: z.array(notificationResponse), total: z.number().int()}); 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 Notifications * * GET /api/v1/notifications/ */ export const getApiApiV1Notifications = async (args: GetApiApiV1NotificationsArgs, options: GetApiApiV1NotificationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.unread_only != null) query.append("unread_only", String(args.unread_only)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/notifications/${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(notificationListResponse, await res.json()) };