import {z} from 'zod' export type GetApiProjectsIdArgs = {id: string}; export type GetApiProjectsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({street_address_1: z.string().nullable().optional(), street_address_2: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), country: z.string().nullable().optional()}); export const coordinate = z.object({lat: z.number(), lon: z.number()}); export const imageURI = z.object({type: z.string(), uri: z.string(), url: z.string().optional()}); export const projectIntegration = z.object({type: z.string(), relation_id: z.string()}); export const projectContactResponse = z.object({id: z.string().optional(), project_id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phone_number: z.string().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); export const project = z.object({id: z.string(), company_id: z.string().optional(), creator_id: z.string().optional(), creator_type: z.string().optional(), creator_name: z.string().optional(), status: z.enum(["active", "deleted"]).optional(), archived: z.boolean().optional(), name: z.string().nullable().optional(), address: address.optional(), coordinates: coordinate.optional(), featured_image: z.array(imageURI).optional(), project_url: z.string().optional(), embedded_project_url: z.string().optional(), integrations: z.array(projectIntegration).optional(), slug: z.string().optional(), public: z.boolean().optional(), geofence: z.array(coordinate).optional(), primary_contact: projectContactResponse.optional(), notepad: z.string().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); 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 }; /** * Retrieve Project * * GET /projects/{id} */ export const getApiProjectsId = async (args: GetApiProjectsIdArgs, options?: GetApiProjectsIdOptions) => { const {baseUrl = "https://api.companycam.com/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.id))}${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(project, await res.json()) };