import {z} from 'zod' export type GetApiApiV1ProductsRejectedArgs = {style_key?: (string | null) | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1ProductsRejectedOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rejectionReasonSchema = z.object({key: z.string().max(255), type: z.string().max(255), name: z.string().max(255), description: z.string()}); export const rejectedProductSchema = z.object({style_key: z.string().nullable().optional(), rejection_reasons: z.array(rejectionReasonSchema), rejection_message: z.string().nullable().optional(), rejected_product_ids_hint: z.object({}).nullable().optional()}); export const pagination = z.object({per_page: z.number().int().gt(0), page: z.number().int().gt(0).nullable().optional(), pages: z.number().int().gte(0).nullable().optional(), total: z.number().int().gte(0).nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional(), next_cursor: z.string().nullable().optional()}); export const pagedRejectedProductSchema = z.object({items: z.array(rejectedProductSchema), pagination: pagination}); 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 Rejected Products * * GET /api/v1/products/rejected */ export const getApiApiV1ProductsRejected = async (args: GetApiApiV1ProductsRejectedArgs, options: GetApiApiV1ProductsRejectedOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.style_key != null) query.append("style_key", String(args.style_key)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/products/rejected${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(pagedRejectedProductSchema, await res.json()) };