import {z} from 'zod' export type CreateApiPaymentwallDeliveryArgs = {body: {imp_uid: string, merchant_uid: string, type: 'physical' | 'digital', status: 'order_placed' | 'order_shipped' | 'delivering' | 'delivered', carrier_tracking_id?: string | undefined, carrier_type?: string | undefined, estimated_delivery_datetime: number, estimated_update_datetime: number, reason?: string | undefined, refundable: 'yes' | 'no', details?: string | undefined, shipping_address_email: string, shipping_address_country?: string | undefined, shipping_address_city?: string | undefined, shipping_address_zip?: string | undefined, shipping_address_state?: string | undefined, shipping_address_street?: string | undefined, shipping_address_phone?: string | undefined, shipping_address_firstname?: string | undefined, shipping_address_lastname?: string | undefined}}; export type CreateApiPaymentwallDeliveryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentwallDeliveryDetailAnnotation = z.object({error_code: z.number().int().optional(), error: z.string().optional(), notices: z.array(z.string()).optional()}); export const paymentwallDeliveryAnnotation = z.object({code: z.number().int().optional(), message: z.string().optional(), response: paymentwallDeliveryDetailAnnotation.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 }; /** * 페이먼트월 배송등록 API * * POST /paymentwall/delivery */ export const createApiPaymentwallDelivery = async (args: CreateApiPaymentwallDeliveryArgs, options?: CreateApiPaymentwallDeliveryOptions) => { const {baseUrl = "https://api.iamport.kr", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/paymentwall/delivery${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paymentwallDeliveryAnnotation, await res.json()) };