import {z} from 'zod' export type QuickInfoRequestDTO = {weight?: number | undefined, boxCount?: number | undefined, vehicleType?: ('BIKE' | 'CAR') | undefined, pickupRequestDateTime?: string | undefined, fromAddress?: string | undefined, toAddress?: string | undefined, toAddress2?: string | undefined, comment?: string | undefined, boxType?: ('XSMALL' | 'SMALL' | 'LARGE') | undefined}; export type CreateApiV1TotalCostQuickArgs = {body: QuickInfoRequestDTO}; export type CreateApiV1TotalCostQuickOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const legacyQuickInfoDTO = z.object({quickInfoId: z.string().optional(), createdDateTime: z.string().optional(), expireDateTime: z.string().optional(), vehicleType: z.enum(["BIKE", "CAR", "ANY"]).optional(), originalCost: z.number().int().optional(), cost: z.number().int().optional(), distance: z.number().int().optional(), costDetail: z.object({}).optional(), duration: z.number().int().optional(), discountRatio: z.number().int().optional()}); export const jFSQuickInfoDTO = z.object({success: z.boolean().optional(), message: z.string().optional(), data: legacyQuickInfoDTO.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/totalCost/quick */ export const createApiV1TotalCostQuick = async (args: CreateApiV1TotalCostQuickArgs, options: CreateApiV1TotalCostQuickOptions) => { 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 (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/totalCost/quick${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(jFSQuickInfoDTO, await res.json()) };