import {z} from 'zod' export type GetApiV1CostDeliveryTypeArgs = {deliveryType: 'D_SHIPPING' | 'D_TODAY_D' | 'D_TODAY_P' | 'D_TODAY_PD' | 'D_QUICK', transporter?: ('HANJIN' | 'LOTTE' | 'CJGLS' | 'EPOST') | undefined}; export type GetApiV1CostDeliveryTypeOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const boxTypeDTO = z.object({boxType: z.string().optional(), boxTypeName: z.string().optional(), weight: z.number().int().optional(), volume: z.number().int().optional(), cost: z.number().int().optional(), accountGroupCost: z.boolean().optional(), accountGroupName: z.string().optional()}); export const jFSAvailableDeliveryTypeDTOBoxTypeDTOList = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.array(boxTypeDTO).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 }; /** * 요금정보 조회 * * GET /v1/cost/{deliveryType} */ export const getApiV1CostDeliveryType = async (args: GetApiV1CostDeliveryTypeArgs, options: GetApiV1CostDeliveryTypeOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.transporter != null) query.append("transporter", String(args.transporter)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cost/${encodeURIComponent(String(args.deliveryType))}${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(jFSAvailableDeliveryTypeDTOBoxTypeDTOList, await res.json()) };