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 CreateApiDocumentsArgs = {body: {data: {type: 'document', attributes: {file_url: string, title: string, description?: (string | null) | undefined, is_public?: (boolean | null) | undefined, key?: ('api_tenancy_guide' | 'api_landlord_offer_letter' | 'api_tenancy_agreement' | 'api_tenant_guarantor_reference_form' | 'api_tenant_guarantor_reference_report' | 'api_tenant_guarantor_id_document' | null) | undefined}, relationships: PropertyRelated | SalesApplicantRelated | LettingsApplicantRelated}}}; export type CreateApiDocumentsOptions = {baseUrl?: string | undefined, yourApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const media = z.object({id: z.string().optional(), type: z.literal("media").optional(), attributes: z.union([z.object({name: z.string().optional(), order: z.number().int().optional(), is_featured: z.boolean().nullable().optional(), include_in_listing: z.boolean().nullable().optional(), feature_index: z.number().int().nullable().optional(), title: z.string().nullable().optional(), collection: z.string().optional(), media_type: z.literal("image").optional(), url: z.string().optional()}), z.object({name: z.string().optional(), order: z.number().int().optional(), is_featured: z.boolean().nullable().optional(), include_in_listing: z.boolean().nullable().optional(), feature_index: z.number().int().nullable().optional(), title: z.string().nullable().optional(), collection: z.string().optional(), media_type: z.literal("pdf"), url: z.string().optional()})]).optional(), relationships: z.object({}).optional()}); export const createApiDocumentsResponse = z.object({data: media}); 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 a Document * * POST /documents */ export const createApiDocuments = async (args: CreateApiDocumentsArgs, options?: CreateApiDocumentsOptions) => { 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(/\/+$/, '')}/documents${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(createApiDocumentsResponse, await res.json()) };