import {z} from 'zod' export type StatementV2Request = {apikey: string, client_secret: string, time_start: string, time_end: string}; export type CreateApiOpenUserStatementV2Args = {body: StatementV2Request}; export type CreateApiOpenUserStatementV2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const statementV2PathUsage = z.object({fee_times: z.number().optional(), used_money: z.number().optional(), usage_times: z.number().optional(), request_path: z.string().optional(), serial_number: z.string().optional(), request_path_name: z.string().optional()}); export const statementV2ComboUsage = z.object({url: z.string().optional(), url_name: z.string().optional(), used_num: z.number().int().optional(), usage_num: z.number().int().optional()}); export const statementV2StatementResponseV2 = z.object({usage: z.array(statementV2PathUsage).optional(), combo_usage: z.array(statementV2ComboUsage).optional(), usage_money: z.number().optional(), combo_use_num: z.number().int().optional(), combo_usage_num: z.number().int().optional()}); export const statementV2Response = z.object({data: statementV2StatementResponseV2.optional(), status: z.boolean(), error_msg: z.string().optional(), error_code: z.number().int()}); 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 }; /** * Statement of account V2 * * POST /open-user/statement/v2 */ export const createApiOpenUserStatementV2 = async (args: CreateApiOpenUserStatementV2Args, options?: CreateApiOpenUserStatementV2Options) => { const {baseUrl = "https://connect.patsnap.com", 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(/\/+$/, '')}/open-user/statement/v2${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(statementV2Response, await res.json()) };