import {z} from 'zod' export type CompanyRelated = {company?: ({data?: ({type?: 'company' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type PersonRelated = {person?: ({data?: ({type?: 'person' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type PropertyRelated = {property?: ({data?: ({type?: 'property' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type TenancyRelated = {tenancy?: ({data?: ({type?: 'tenancy' | 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 LettingsApplicationRelated = {lettings_application?: ({data?: ({type?: 'lettingsApplication' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type EnquiryRelated = {enquiry?: ({data?: ({type?: 'enquiry' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type OwnerRelated = {owner?: ({data?: ({type?: 'owner' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type SaleRelated = {sale?: ({data?: ({type?: 'sale' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type SalesOfferRelated = {sales_offer?: ({data?: ({type?: 'salesOffer' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type LettingsOfferRelated = {lettings_offer?: ({data?: ({type?: 'lettingsOffer' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type ViewingRelated = {viewing?: ({data?: ({type?: 'viewing' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type ValuationRelated = {valuation?: ({data?: ({type?: 'valuation' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type InspectionRelated = {inspection?: ({data?: ({type?: 'inspection' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type MaintenanceJobRelated = {maintenance?: ({data?: ({type?: 'maintenance' | undefined, id?: string | undefined}) | undefined}) | undefined}; export type CreateApiNotesArgs = {body: {data: {type: 'note', attributes: {body: string, pinned?: boolean | undefined, author?: (string | null) | undefined}, relationships: CompanyRelated | PersonRelated | PropertyRelated | TenancyRelated | SalesApplicantRelated | LettingsApplicantRelated | LettingsApplicationRelated | EnquiryRelated | OwnerRelated | SaleRelated | SalesOfferRelated | LettingsOfferRelated | ViewingRelated | ValuationRelated | InspectionRelated | MaintenanceJobRelated}}}; export type CreateApiNotesOptions = {baseUrl?: string | undefined, yourApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const note = z.object({id: z.string().optional(), type: z.literal("note").optional(), attributes: z.object({body: z.string().nullable().optional(), author: z.string().nullable().optional(), pinned_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), relationships: z.object({}).optional()}); export const createApiNotesResponse = z.object({data: note}); 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 Note * * POST /notes */ export const createApiNotes = async (args: CreateApiNotesArgs, options?: CreateApiNotesOptions) => { 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(/\/+$/, '')}/notes${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(createApiNotesResponse, await res.json()) };