import {z} from 'zod' export type PrintDataRequestDTO = {invoiceNo?: Array | undefined, transporterInvoiceNo?: Array | undefined, partnerOrderNo?: Array | undefined}; export type CreateApiV1OrderPrintArgs = {countCheck?: boolean | undefined, body: PrintDataRequestDTO}; export type CreateApiV1OrderPrintOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicePrintDataDTO = z.object({invoiceNo: z.string().optional(), partnerOrderNo: z.string().optional(), tmsSortCode: z.string().optional(), areaName: z.string().optional(), tmsAreaName: z.string().optional(), toZipCode: z.string().optional(), fromName: z.string().optional(), fromPhoneNumber: z.string().optional(), fromAddr1: z.string().optional(), fromAddr2: z.string().optional(), toName: z.string().optional(), toPhoneNumber: z.string().optional(), toAddr1: z.string().optional(), toAddr2: z.string().optional(), itemName: z.string().optional(), itemQuantity: z.number().int().optional(), customerMessage: z.string().optional(), transporterInvoiceNo: z.string().optional()}); export const jFSStringPrintData = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.array(invoicePrintDataDTO).optional(), responseDateTime: z.string().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 }; /** * 송장출력을 위한 데이터 조회 * * POST /v1/order/print */ export const createApiV1OrderPrint = async (args: CreateApiV1OrderPrintArgs, options: CreateApiV1OrderPrintOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.countCheck != null) query.append("countCheck", String(args.countCheck)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/order/print${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(jFSStringPrintData, await res.json()) };