import {z} from 'zod' export type GetApiV1EngagementsEngagementUuidFilesFileUuidArgs = {engagement_uuid: string, file_uuid: string}; export type GetApiV1EngagementsEngagementUuidFilesFileUuidOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const halLink = z.object({href: z.string(), title: z.string().optional(), type: z.string()}); export const engagementFileReadLinks = z.object({fieldguide: halLink}); export const engagementFileRead = z.object({name: z.string(), size: z.number(), mime_type: z.string(), url: z.string().nullable(), url_expiration: z.string().nullable(), uuid: z.string(), created_at: z.string(), updated_at: z.string().nullable(), engagement_uuid: z.string(), folder_uuid: z.string().nullable(), _links: engagementFileReadLinks}); export const getApiV1EngagementsEngagementUuidFilesFileUuidResponse = z.object({data: engagementFileRead}); 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 }; /** * Fetch a File by Engagement and File UUIDs * * GET /v1/engagements/{engagement_uuid}/files/{file_uuid} */ export const getApiV1EngagementsEngagementUuidFilesFileUuid = async (args: GetApiV1EngagementsEngagementUuidFilesFileUuidArgs, options?: GetApiV1EngagementsEngagementUuidFilesFileUuidOptions) => { const {baseUrl = "https://api.fieldguide.io", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/engagements/${encodeURIComponent(String(args.engagement_uuid))}/files/${encodeURIComponent(String(args.file_uuid))}${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(getApiV1EngagementsEngagementUuidFilesFileUuidResponse, await res.json()) };