import {z} from 'zod' export type QuoteRequestCreate = {notes?: (string | null) | undefined, target_delivery_date?: (string | null) | undefined}; export type CreateApiApiV1ProjectsProjectIdQuotesRequestArgs = {project_id: string, body: QuoteRequestCreate}; export type CreateApiApiV1ProjectsProjectIdQuotesRequestOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const additionalLineItemOutput = z.object({label: z.string().min(1).max(100), quantity: z.number().int().gte(1).nullable().optional(), amount: z.string()}); export const quoteResponse = z.object({id: z.string(), status: z.string(), intent: z.string().optional(), total_amount: z.string().nullable().optional(), lead_time_days: z.number().int().nullable().optional(), lead_time_display: z.string().nullable().optional(), additional_line_items: z.array(additionalLineItemOutput).nullable().optional(), additional_line_items_post_tax: z.array(additionalLineItemOutput).nullable().optional(), valid_until: z.string().nullable().optional(), notes: z.string().nullable().optional(), is_human_reviewed: z.boolean(), require_deposit: z.boolean().optional(), deposit_percentage: z.string().nullable().optional(), customer_name_override: z.string().nullable().optional(), customer_email_override: z.string().nullable().optional(), customer_billing_address_override: z.string().nullable().optional(), customer_shipping_address_override: z.string().nullable().optional(), customer_shipping_name_override: z.string().nullable().optional(), selected_delivery_option_id: z.string().nullable().optional(), snapshot_version: z.number().int().optional(), project_id: z.string(), created_at: z.string(), updated_at: 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 }; /** * Request Quote * * POST /api/v1/projects/{project_id}/quotes/request */ export const createApiApiV1ProjectsProjectIdQuotesRequest = async (args: CreateApiApiV1ProjectsProjectIdQuotesRequestArgs, options: CreateApiApiV1ProjectsProjectIdQuotesRequestOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.project_id))}/quotes/request${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(quoteResponse, await res.json()) };