import {z} from 'zod' export type PropertyRelated = {property?: ({data?: ({type?: 'property' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type SalesApplicantRelated = {sales_applicant?: ({data?: ({type?: 'salesApplicant' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type LettingsApplicantRelated = {lettings_applicant?: ({data?: ({type?: 'lettingsApplicant' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type CreateApiEnquiriesArgs = {body: {data: {type: 'enquiry', attributes: {first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, email_address: string, telephone_number?: (string | null) | undefined, message: string, custom_source?: (string | null) | undefined, postcode?: (string | null) | undefined, property_uuid?: (string | null) | undefined, branch_uuid?: (string | null) | undefined, request_viewing?: boolean | undefined, request_valuation?: boolean | undefined, property_to_sell?: boolean | undefined, property_to_let?: boolean | undefined}, relationships: PropertyRelated | SalesApplicantRelated | LettingsApplicantRelated}}}; export type CreateApiEnquiriesOptions = {baseUrl?: string | undefined, yourApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({}); export const link = z.union([z.string(), z.object({href: z.string(), meta: meta.optional()})]); export const enquiry = z.object({id: z.string().optional(), type: z.literal("enquiry").optional(), attributes: z.object({first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), email_address: z.string().nullable().optional(), telephone_number: z.string().nullable().optional(), message: z.string().optional(), property_uuid: z.string().nullable().optional(), custom_source: z.string().max(25).nullable().optional(), portal: z.string().nullable().optional(), branch_uuid: z.string().nullable().optional(), request_viewing: z.boolean().optional(), request_valuation: z.boolean().optional(), property_to_sell: z.boolean().optional(), property_to_let: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), relationships: z.object({branch: z.object({data: z.object({type: z.literal("branch"), id: z.string()}).nullable().optional(), links: z.object({self: link.optional(), related: link.optional()}).optional()}).optional(), property: z.object({data: z.object({type: z.literal("property"), id: z.string()}).nullable().optional(), links: z.object({self: link.optional(), related: link.optional()}).optional()}).optional(), person: z.object({data: z.object({type: z.literal("person"), id: z.string()}).nullable().optional(), links: z.object({self: link.optional(), related: link.optional()}).optional()}).optional(), notes: z.object({data: z.array(z.object({type: z.literal("note").optional(), id: z.string().optional()})).optional(), links: z.object({self: link.optional(), related: link.optional()}).optional()}).optional()}).optional()}); export const createApiEnquiriesResponse = z.object({data: enquiry}); 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 }; /** * Create an Enquiry * * POST /enquiries */ export const createApiEnquiries = async (args: CreateApiEnquiriesArgs, options?: CreateApiEnquiriesOptions) => { const {baseUrl = "https://street.co.uk/open-api/v1", fetch: fetchFn = fetch, yourApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (yourApiToken !== undefined) headers.set('authorization', `Bearer ${yourApiToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/enquiries${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(createApiEnquiriesResponse, await res.json()) };