import {z} from 'zod' export type InvoiceV1ApplyInvoiceRequest = {address?: string | undefined, bankAccount?: string | undefined, bankName?: string | undefined, mailingAddress?: string | undefined, phone?: string | undefined, receiverEmail?: string | undefined, receiverName?: string | undefined, receiverPhone?: string | undefined, remark?: string | undefined, taxNo?: string | undefined, title?: string | undefined, titleId?: string | undefined, titleType?: string | undefined, topupOrderIds?: Array | undefined, type?: string | undefined}; export type CreateApiV1ConsoleInvoicesArgs = {body: InvoiceV1ApplyInvoiceRequest}; export type CreateApiV1ConsoleInvoicesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const invoiceV1Invoice = z.object({address: z.string().optional(), amountMicros: z.string().optional(), appliedAt: z.string().optional(), bankAccount: z.string().optional(), bankName: z.string().optional(), cancelledAt: z.string().optional(), courierCompany: z.string().optional(), courierNo: z.string().optional(), createdAt: z.string().optional(), currency: z.string().optional(), fileFormat: z.string().optional(), hasRedFile: z.boolean().optional(), hasXml: z.boolean().optional(), id: z.string().optional(), invoiceNo: z.string().optional(), issuedAt: z.string().optional(), mailingAddress: z.string().optional(), phone: z.string().optional(), receiverEmail: z.string().optional(), receiverName: z.string().optional(), receiverPhone: z.string().optional(), redFileFormat: z.string().optional(), redInvoiceNo: z.string().optional(), reissueOf: z.string().optional(), rejectReason: z.string().optional(), remark: z.string().optional(), reversalReason: z.string().optional(), reversedAt: z.string().optional(), reviewedAt: z.string().optional(), sourceType: z.string().optional(), status: z.string().optional(), taxNo: z.string().optional(), title: z.string().optional(), titleType: z.string().optional(), type: z.string().optional()}); export const invoiceV1InvoiceOrderLine = z.object({amountMicros: z.string().optional(), currency: z.string().optional(), orderId: z.string().optional(), paidAt: z.string().optional(), tradeNo: z.string().optional()}); export const invoiceV1InvoiceReply = z.object({invoice: invoiceV1Invoice.optional(), orders: z.array(invoiceV1InvoiceOrderLine).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 }; /** * ApplyInvoice 申请开票(1-3-1):多充值订单合并开一张票,同币种约束 * * POST /v1/console/invoices */ export const createApiV1ConsoleInvoices = async (args: CreateApiV1ConsoleInvoicesArgs, options: CreateApiV1ConsoleInvoicesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/console/invoices${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(invoiceV1InvoiceReply, await res.json()) };