import {z} from 'zod' export type GetApiV1OrderIntakeConfigOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicIntakeBranding = z.object({display_name: z.string().nullable(), logo_url: z.string().nullable(), powered_by_nash: z.boolean()}); export const publicIntakeStore = z.object({id: z.string(), name: z.string(), address: z.string().nullable(), lat: z.number().nullable(), lng: z.number().nullable()}); export const publicIntakeVehicleType = z.object({value: z.string(), label: z.string()}); export const publicIntakeStrategy = z.object({id: z.string(), label: z.string()}); export const publicIntakeConfig = z.object({config_id: z.string(), mode: z.enum(["quote_and_book", "review"]), branding: publicIntakeBranding, stores: z.array(publicIntakeStore), vehicle_types: z.array(publicIntakeVehicleType), strategies: z.array(publicIntakeStrategy), country: z.string(), currency: z.string()}); 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 Order Intake Configuration * * GET /v1/order-intake/config */ export const getApiV1OrderIntakeConfig = async (options?: GetApiV1OrderIntakeConfigOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/order-intake/config${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(publicIntakeConfig, await res.json()) };