import {z} from 'zod' export type GetApiAppOrderArgs = {page?: number | undefined, limit?: number | undefined, status?: ('APPROVED' | 'REJECTED' | 'CANCELED' | 'REFUNDED' | 'CHARGEDBACK' | Array<'APPROVED' | 'REJECTED' | 'CANCELED' | 'REFUNDED' | 'CHARGEDBACK'>) | undefined, email?: string | undefined, client_id?: string | undefined, user_id?: string | undefined, q?: string | undefined, pay_id?: string | undefined, package?: (number | Array) | undefined, discount?: (number | Array) | undefined, variables?: (string | Array) | undefined, gateways?: (string | Array) | undefined, affiliates?: (number | Array) | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiAppOrderOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAppOrderResponse = z.object({meta: z.object({total: z.number().optional(), per_page: z.number().optional(), current_page: z.number().optional(), last_page: z.number().optional(), first_page: z.number().optional(), first_page_url: z.string().optional(), last_page_url: z.string().optional(), next_page_url: z.string().nullable().optional(), previous_page_url: z.string().nullable().optional()}).optional(), data: z.array(z.object({store_id: z.number().optional(), internal_id: z.string().optional(), gateway: z.string().optional(), price: z.number().optional(), client_identifier: z.string().optional(), client_name: z.string().optional(), client_email: z.string().optional(), currency: z.enum(["BRL", "USD", "EUR"]).optional(), paid_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), status: z.enum(["APPROVED", "REJECTED", "CANCELED", "REFUNDED", "CHARGEDBACK"]).optional(), promo_code: z.string().nullable().optional(), promo_type: z.enum(["PERCENTAGE", "PRICE"]).nullable().optional(), promo_value: z.number().nullable().optional()})).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 }; /** * List Order * * GET /app/order */ export const getApiAppOrder = async (args: GetApiAppOrderArgs, options?: GetApiAppOrderOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch, bearerAuth} = 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.limit != null) query.append("limit", String(args.limit)) if (args.status != null) query.append("status", String(args.status)) if (args.email != null) query.append("email", String(args.email)) if (args.client_id != null) query.append("client_id", String(args.client_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.q != null) query.append("q", String(args.q)) if (args.pay_id != null) query.append("pay_id", String(args.pay_id)) if (args.package != null) query.append("package", String(args.package)) if (args.discount != null) query.append("discount", String(args.discount)) if (args.variables != null) query.append("variables", String(args.variables)) if (args.gateways != null) query.append("gateways", String(args.gateways)) if (args.affiliates != null) query.append("affiliates", String(args.affiliates)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/app/order${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(getApiAppOrderResponse, await res.json()) };