import {z} from 'zod' export type GetApiProjectsArgs = {page?: number | undefined, per_page?: number | undefined, query?: string | undefined, modified_since?: string | undefined, status?: ('active' | 'deleted') | undefined}; export type GetApiProjectsOptions = {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 getApiProjectsResponse = z.array(project); 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 }; /** * List Projects * * GET /projects */ export const getApiProjects = async (args: GetApiProjectsArgs, options?: GetApiProjectsOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.query != null) query.append("query", String(args.query)) if (args.modified_since != null) query.append("modified_since", String(args.modified_since)) if (args.status != null) query.append("status", String(args.status)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects${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(getApiProjectsResponse, await res.json()) };