import {z} from 'zod' export type GetApiProjectProjectIdDocumentDocumentPathArgs = {project_id: string, document_path: string}; export type GetApiProjectProjectIdDocumentDocumentPathOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const fieldValue: z.ZodType = z.union([z.object({filename: z.string(), binarydata: z.string(), mimeType: z.string()}), z.object({fields: z.record(z.string(), z.array(z.lazy(() => fieldValue)))}), z.object({fields: z.record(z.string(), z.array(z.lazy(() => fieldValue))), fieldGroupType: z.string()})]); export const documentField = z.object({name: z.string(), value: z.array(fieldValue)}); export const system = z.object({createdBy: z.string().optional(), createdAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional()}); export const document = z.object({contentType: z.string(), fields: z.array(documentField), name: z.string(), displayName: z.string(), locale: z.string().optional(), path: z.string(), system: system.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 }; /** * Get a document in a specific project * * GET /project/{project_id}/document/{document_path} */ export const getApiProjectProjectIdDocumentDocumentPath = async (args: GetApiProjectProjectIdDocumentDocumentPathArgs, options?: GetApiProjectProjectIdDocumentDocumentPathOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/project/${encodeURIComponent(String(args.project_id))}/document/${encodeURIComponent(String(args.document_path))}${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(document, await res.json()) };