import {z} from 'zod' export type InvoiceV1CreateTitleRequest = {bankAccount?: string | undefined, bankName?: string | undefined, name?: string | undefined, regAddress?: string | undefined, regPhone?: string | undefined, setDefault?: boolean | undefined, taxNo?: string | undefined, titleType?: string | undefined}; export type CreateApiV1ConsoleInvoiceTitlesArgs = {body: InvoiceV1CreateTitleRequest}; export type CreateApiV1ConsoleInvoiceTitlesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const invoiceV1InvoiceTitle = z.object({bankAccount: z.string().optional(), bankName: z.string().optional(), createdAt: z.string().optional(), id: z.string().optional(), isDefault: z.boolean().optional(), name: z.string().optional(), regAddress: z.string().optional(), regPhone: z.string().optional(), taxNo: z.string().optional(), titleType: z.string().optional(), updatedAt: z.string().optional()}); export const invoiceV1TitleReply = z.object({title: invoiceV1InvoiceTitle.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 }; /** * CreateTitle 新建抬头(1-2) * * POST /v1/console/invoice/titles */ export const createApiV1ConsoleInvoiceTitles = async (args: CreateApiV1ConsoleInvoiceTitlesArgs, options: CreateApiV1ConsoleInvoiceTitlesOptions) => { 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/invoice/titles${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(invoiceV1TitleReply, await res.json()) };