import {z} from 'zod' export type TicketStatus = 0 | 1 | 2 | 3 | 4; export type GetApiStoresStoreIdTicketsArgs = {storeId: string, subject?: string | undefined, status?: TicketStatus | undefined, customerEmail?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, Filters?: string | undefined, Sorts?: string | undefined, Page?: number | undefined, PageSize?: number | undefined}; export type GetApiStoresStoreIdTicketsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ticketStatus = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const displayTicket = z.object({id: z.string(), dateCreated: z.string(), subject: z.string(), emailAddress: z.string(), status: ticketStatus, storeId: z.string(), customerId: z.string(), messageCount: z.number().int(), lastMessage: z.string()}); export const displayTicketPaginatedResponse = z.object({success: z.boolean(), pages: z.number().int(), data: z.array(displayTicket)}); 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 }; /** * Get tickets * * GET /stores/{storeId}/tickets */ export const getApiStoresStoreIdTickets = async (args: GetApiStoresStoreIdTicketsArgs, options?: GetApiStoresStoreIdTicketsOptions) => { const {baseUrl = "https://api.komerza.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.subject != null) query.append("subject", String(args.subject)) if (args.status != null) query.append("status", String(args.status)) if (args.customerEmail != null) query.append("customerEmail", String(args.customerEmail)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.Filters != null) query.append("Filters", String(args.Filters)) if (args.Sorts != null) query.append("Sorts", String(args.Sorts)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeId))}/tickets${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(displayTicketPaginatedResponse, await res.json()) };