import {z} from 'zod' export type GetApiV1AvailableDeliveryTypeArgs = {fromZipCode: string, toZipCode: string}; export type GetApiV1AvailableDeliveryTypeOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const availableDeliveryTypeSimpleDTO = z.object({deliveryType: z.enum(["D_SHIPPING", "D_CONVENIENCE", "D_PICKUP", "D_QUICK", "D_CARGO", "D_TODAY_P", "D_TODAY_D", "D_TODAY_PD"]).optional(), transporter: z.enum(["HOMEPICK", "HOMEPICK3", "TODAY", "EPOST", "HANJIN", "CJGLS", "LOTTE", "LOGEN", "CUHOME", "SEVENELEVEN", "RETURNALL", "DVER", "GOGOX", "INSUNG"]).optional(), available: z.boolean().optional(), addedCost: z.number().int().optional()}); export const jFSAvailableDeliveryTypeSimpleDTOList = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.array(availableDeliveryTypeSimpleDTO).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/available/deliveryType */ export const getApiV1AvailableDeliveryType = async (args: GetApiV1AvailableDeliveryTypeArgs, options: GetApiV1AvailableDeliveryTypeOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromZipCode != null) query.append("fromZipCode", String(args.fromZipCode)) if (args.toZipCode != null) query.append("toZipCode", String(args.toZipCode)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/available/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(jFSAvailableDeliveryTypeSimpleDTOList, await res.json()) };