import {z} from 'zod' export type FieldValue = {filename: string, binarydata: string, mimeType: string} | {fields: {[key: string]: Array}} | {fields: {[key: string]: Array}, fieldGroupType: string}; export type DocumentField = {name: string, value: Array}; export type System = {createdBy?: string | undefined, createdAt?: string | undefined, updatedBy?: string | undefined, updatedAt?: string | undefined}; export type Document = {contentType: string, fields: Array, name: string, displayName: string, locale?: string | undefined, path: string, system?: System | undefined}; export type UpdateApiProjectProjectIdDocumentDocumentPathArgs = {'X-Resource-Version'?: string | undefined, project_id: string, document_path: string, body: Document}; export type UpdateApiProjectProjectIdDocumentDocumentPathOptions = {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 }; /** * Create or update a document in a specific project * * PUT /project/{project_id}/document/{document_path} */ export const updateApiProjectProjectIdDocumentDocumentPath = async (args: UpdateApiProjectProjectIdDocumentDocumentPathArgs, options?: UpdateApiProjectProjectIdDocumentDocumentPathOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Resource-Version'] != null) headers.set("X-Resource-Version", String(args['X-Resource-Version'])) 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: "PUT", headers, body: JSON.stringify(args.body)}) 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()) };