import {z} from 'zod' export type GetApiPhotosArgs = {page?: number | undefined, per_page?: number | undefined, after?: string | undefined, before?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, project_ids?: Array | undefined, user_ids?: Array | undefined, group_ids?: Array | undefined, tag_ids?: Array | undefined}; export type GetApiPhotosOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 photo = 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(), project_id: z.string().optional(), processing_status: z.enum(["pending", "processing", "processed", "processing_error", "duplicate"]).optional(), status: z.enum(["active", "deleted"]).optional(), coordinates: coordinate.optional(), uris: z.array(imageURI).optional(), hash: z.string().optional(), description: z.string().optional(), internal: z.boolean().optional(), photo_url: z.string().optional(), captured_at: z.number().int().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); export const getApiPhotosResponse = z.array(photo); 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 Photos * * GET /photos */ export const getApiPhotos = async (args: GetApiPhotosArgs, options?: GetApiPhotosOptions) => { 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.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) for (const item of args.project_ids ?? []) query.append("project_ids", String(item)) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) for (const item of args.group_ids ?? []) query.append("group_ids", String(item)) for (const item of args.tag_ids ?? []) query.append("tag_ids", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/photos${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(getApiPhotosResponse, await res.json()) };