import {z} from 'zod' export type GetApiV1AdminInvoicesExportArgs = {status?: string | undefined, userId?: string | undefined, appliedFrom?: string | undefined, appliedTo?: string | undefined, amountMin?: string | undefined, amountMax?: string | undefined, invoiceNo?: string | undefined}; export type GetApiV1AdminInvoicesExportOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const adminInvoiceV1ExportInvoicesReply = z.object({data: z.string().optional(), filename: 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 }; /** * ExportInvoices 导出 CSV(base64 in JSON envelope,同 List 筛选;照 monitor 先例,1-4-1) * * GET /v1/admin/invoices/export */ export const getApiV1AdminInvoicesExport = async (args: GetApiV1AdminInvoicesExportArgs, options: GetApiV1AdminInvoicesExportOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.appliedFrom != null) query.append("appliedFrom", String(args.appliedFrom)) if (args.appliedTo != null) query.append("appliedTo", String(args.appliedTo)) if (args.amountMin != null) query.append("amountMin", String(args.amountMin)) if (args.amountMax != null) query.append("amountMax", String(args.amountMax)) if (args.invoiceNo != null) query.append("invoiceNo", String(args.invoiceNo)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/invoices/export${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(adminInvoiceV1ExportInvoicesReply, await res.json()) };