import {z} from 'zod' export type UUID = string; export type GetApiChargebackRequestsIdArgs = {id: UUID}; export type GetApiChargebackRequestsIdOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(25).max(25); export const chargebackStatus = z.enum(["pending", "accepted", "cancelled", "expired", "defended", "lost"]); export const chargebackTimelineEntry = z.object({type: z.string(), occurred_at: z.string()}); export const amount = z.number().int().gte(0); export const currency = z.enum(["JPY", "USD", "EUR", "TWD", "KRW", "PLN", "GBP", "HKD", "SGD", "NZD", "AUD", "IDR", "MYR", "PHP", "THB", "CNY", "BRL", "CHF", "CAD", "VND"]); export const chargebackPaymentMethod = z.object({type: z.string(), brand: z.string().nullable(), last_four_digits: z.string().nullable()}); export const chargebackPayment = z.object({id: uUID, amount: amount, currency: currency, created_at: z.string(), captured_at: z.string().nullable(), payment_method: chargebackPaymentMethod, masked_card_number: z.string().nullable()}); export const chargebackCustomer = z.object({name: z.string().nullable(), email: z.string().nullable()}); export const chargebackDefenseShippingInfo = z.object({company_name: z.string().nullable(), shipping_date: z.string().nullable(), tracking_number: z.string().nullable(), shipping_address: z.string().nullable()}); export const chargebackDefenseRecipientInfo = z.object({name: z.string().nullable(), phone: z.string().nullable(), email: z.string().nullable()}); export const chargebackDefenseDocument = z.object({file_name: z.string(), content_type: z.string(), file_size: z.number().int(), uploaded_at: z.string(), url: z.string()}); export const chargebackDefense = z.object({product_name: z.string().nullable(), reason: z.string(), shipping_info: chargebackDefenseShippingInfo, recipient_info: chargebackDefenseRecipientInfo, documents: z.array(chargebackDefenseDocument)}); export const chargebackRequestDetail = z.object({id: uUID, status: chargebackStatus, reason_code: z.string(), reason: z.string(), created_at: z.string(), due_date: z.string(), last_updated_at: z.string(), timeline: z.array(chargebackTimelineEntry), payment: chargebackPayment, customer: chargebackCustomer, defense: chargebackDefense}); 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 }; /** * Chargeback: Show * * GET /chargeback_requests/{id} */ export const getApiChargebackRequestsId = async (args: GetApiChargebackRequestsIdArgs, options?: GetApiChargebackRequestsIdOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/chargeback_requests/${encodeURIComponent(String(args.id))}${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(chargebackRequestDetail, await res.json()) };