import {z} from 'zod' export type GetApi202409BillingDocumentsIdTransactionRecordsArgs = {page?: number | undefined, per_page?: number | undefined, id: string, from_date?: string | undefined, to_date?: string | undefined}; export type GetApi202409BillingDocumentsIdTransactionRecordsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionRecord = z.object({id: z.string().optional(), adjustment_type: z.enum(["Refund after Cancellation", "Remote Area Surcharge (ODA Reconciliation)", "Declared Value Revaluation (Tax Reconciliation)", "Adjustment based on Weight (Weight Reconciliation)", "Payment of Tax & Duty on behalf of Consignee", "Return Fees", "Declared Value Revaluation", "Address Correction Fee", "Refund (100%)", "Residential Area Surcharge", "Charge After Refund", "Warehouse Handling Fee", "Refund for Fixed Shipment", "Charge for Fixed Shipment", "Refund (50%)", "Oversized Surcharge", "Refund - Damaged", "Refund - Lost", "Pay On Scan", "Tax & Duty Adjustment by Courier Invoice", "Account Consolidation", "Others", "Bank Account Charge Failed", "Pay by Bank Cashback"]).nullable().optional(), amount: z.number().optional(), description: z.string().nullable().optional(), payment_recipient: z.string().optional(), settled_at: z.string().nullable().optional(), easyship_shipment_id: z.string().nullable().optional()}); export const pagination = z.object({next: z.number().int().nullable().optional(), count: z.number().int().nullable().optional(), page: z.number().int().optional()}); export const metaWithPagination = z.object({pagination: pagination.optional(), request_id: z.string().optional()}); export const transactionRecordList = z.union([z.object({transaction_records: z.array(transactionRecord).optional(), meta: metaWithPagination.optional()}), z.string()]); 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 all Billing Document's Transaction Records * * GET /2024-09/billing_documents/{id}/transaction_records */ export const getApi202409BillingDocumentsIdTransactionRecords = async (args: GetApi202409BillingDocumentsIdTransactionRecordsArgs, options?: GetApi202409BillingDocumentsIdTransactionRecordsOptions) => { const {baseUrl = "https://public-api.easyship.com", fetch: fetchFn = fetch, Bearer} = 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.per_page != null) query.append("per_page", String(args.per_page)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/2024-09/billing_documents/${encodeURIComponent(String(args.id))}/transaction_records${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(transactionRecordList, await res.json()) };