import {z} from 'zod' export type PatchApiAppOrderOrderIdArgs = {order_id: string, body: {status?: ('APPROVED' | 'REFUNDED') | undefined, client_identifier?: string | undefined, client_discord?: string | undefined, variables?: (Array<{name?: string | undefined, value?: string | undefined}>) | undefined, package_options?: (Array<{id?: number | undefined, value?: string | undefined, variable_option_id?: number | undefined}>) | undefined}}; export type PatchApiAppOrderOrderIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiAppOrderOrderIdResponse = z.object({store_id: z.number().optional(), client_identifier: z.string().optional(), client_email: z.string().optional(), client_name: z.string().optional(), client_document: z.string().nullable().optional(), client_phone: z.string().nullable().optional(), client_discord: z.string().nullable().optional(), price: z.number().optional(), net_received: z.number().optional(), internal_id: z.string().optional(), external_id: z.string().optional(), promo_code: z.string().nullable().optional(), promo_type: z.enum(["PERCENTAGE", "PRICE"]).nullable().optional(), promo_value: z.number().nullable().optional(), gateway: z.string().optional(), status: z.enum(["APPROVED", "REJECTED", "CANCELED", "REFUNDED", "CHARGEDBACK", "PENDING"]).optional(), currency: z.enum(["BRL", "USD", "EUR"]).optional(), manually_created: z.boolean().optional(), manually_refunded: z.boolean().optional(), manually_approved: z.boolean().optional(), paid_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), variables: z.array(z.unknown()).optional(), price_display: z.string().optional(), status_display: z.string().optional(), gateway_display: 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 }; /** * Update Order * * PATCH /app/order/{order_id} */ export const patchApiAppOrderOrderId = async (args: PatchApiAppOrderOrderIdArgs, options?: PatchApiAppOrderOrderIdOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/app/order/${encodeURIComponent(String(args.order_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patchApiAppOrderOrderIdResponse, await res.json()) };