import {z} from 'zod' export type ContainerUUID = {identifier?: string | undefined, uuid?: string | undefined, historyUUIDs?: Array | undefined}; export type TemplateLayoutColumnView = {containers?: Array | undefined, width?: number | undefined, leftOffset?: number | undefined, styleClass?: string | undefined, metadata?: {[key: string]: Record} | undefined}; export type TemplateLayoutRowView = {styleClass?: string | undefined, columns?: Array | undefined, metadata?: {[key: string]: Record} | undefined}; export type BodyView = {rows?: Array | undefined}; export type SidebarView = {containers?: Array | undefined, location?: string | undefined, width?: string | undefined}; export type TemplateLayoutView = {width?: string | undefined, title?: string | undefined, header?: boolean | undefined, footer?: boolean | undefined, body?: BodyView | undefined, sidebar?: SidebarView | undefined}; export type TemplateForm = {siteId?: string | undefined, identifier?: string | undefined, inode?: string | undefined, body?: string | undefined, selectedimage?: string | undefined, image?: string | undefined, drawed?: boolean | undefined, showOnMenu?: boolean | undefined, drawedBody?: string | undefined, countAddContainer?: number | undefined, countContainers?: number | undefined, headCode?: string | undefined, theme?: string | undefined, themeName?: string | undefined, footer?: string | undefined, friendlyName?: string | undefined, header?: string | undefined, name?: string | undefined, title: string, sortOrder?: number | undefined, headerCheck?: boolean | undefined, footerCheck?: boolean | undefined, layout?: TemplateLayoutView | undefined}; export type UpdateApiApiV1TemplatesArgs = {body: TemplateForm}; export type UpdateApiApiV1TemplatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const containerUUID = z.object({identifier: z.string().optional(), uuid: z.string().optional(), historyUUIDs: z.array(z.string()).optional()}); export const templateLayoutColumnView = z.object({containers: z.array(containerUUID).optional(), width: z.number().int().optional(), leftOffset: z.number().int().optional(), styleClass: z.string().optional(), metadata: z.record(z.string(), z.object({})).optional()}); export const templateLayoutRowView = z.object({styleClass: z.string().optional(), columns: z.array(templateLayoutColumnView).optional(), metadata: z.record(z.string(), z.object({})).optional()}); export const bodyView = z.object({rows: z.array(templateLayoutRowView).optional()}); export const sidebarView = z.object({containers: z.array(containerUUID).optional(), location: z.string().optional(), width: z.string().optional()}); export const templateLayoutView = z.object({width: z.string().optional(), title: z.string().optional(), header: z.boolean().optional(), footer: z.boolean().optional(), body: bodyView.optional(), sidebar: sidebarView.optional()}); export const container = z.object({getiDate: z.string().optional(), type: z.string().optional(), owner: z.string().optional(), inode: z.string().optional(), identifier: z.string().optional(), source: z.enum(["UNKNOWN", "DB", "FILE"]).optional(), title: z.string().optional(), friendlyName: z.string().optional(), modDate: z.string().optional(), modUser: z.string().optional(), sortOrder: z.number().int().optional(), showOnMenu: z.boolean().optional(), code: z.string().optional(), maxContentlets: z.number().int().optional(), useDiv: z.boolean().optional(), preLoop: z.string().optional(), postLoop: z.string().optional(), notes: z.string().optional(), hostId: z.string().optional(), hostName: z.string().optional(), variant: z.string().optional(), archived: z.boolean().optional(), versionType: z.string().optional(), permissionId: z.string().optional(), versionId: z.string().optional(), working: z.boolean().optional(), deleted: z.boolean().optional(), live: z.boolean().optional(), name: z.string().optional(), locked: z.boolean().optional(), permissionType: z.string().optional(), idate: z.string().optional(), categoryId: z.string().optional(), new: z.boolean().optional(), parents: z.array(z.object({})).optional()}); export const containerView = z.object({container: container.optional(), path: z.string().optional()}); export const themeView = z.object({path: z.string().optional(), defaultFileType: z.string().optional(), filesMasks: z.string().optional(), getiDate: z.string().optional(), hostId: z.string().optional(), identifier: z.string().optional(), inode: z.string().optional(), modDate: z.string().optional(), name: z.string().optional(), showOnMenu: z.boolean().optional(), sortOrder: z.number().int().optional(), title: z.string().optional(), type: z.string().optional()}); export const templateView = z.object({identifier: z.string().optional(), inode: z.string().optional(), categoryId: z.string().optional(), body: z.string().optional(), selectedimage: z.string().optional(), image: z.string().optional(), drawed: z.boolean().optional(), deleted: z.boolean().optional(), live: z.boolean().optional(), locked: z.boolean().optional(), lockedBy: z.string().optional(), working: z.boolean().optional(), hasLiveVersion: z.boolean().optional(), showOnMenu: z.boolean().optional(), drawedBody: z.string().optional(), countAddContainer: z.number().int().optional(), countContainers: z.number().int().optional(), headCode: z.string().optional(), theme: z.string().optional(), themeName: z.string().optional(), footer: z.string().optional(), friendlyName: z.string().optional(), header: z.string().optional(), modDate: z.string().optional(), modUser: z.string().optional(), owner: z.string().optional(), name: z.string().optional(), title: z.string().optional(), sortOrder: z.number().int().optional(), canRead: z.boolean().optional(), canWrite: z.boolean().optional(), canPublish: z.boolean().optional(), layout: templateLayoutView.optional(), containers: z.record(z.string(), containerView).optional(), themeInfo: themeView.optional(), hostName: z.string().optional(), hostId: z.string().optional(), fullTitle: z.string().optional(), htmlTitle: z.string().optional(), new: z.boolean().optional()}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityTemplateView = z.object({errors: z.array(errorEntity).optional(), entity: templateView.optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 }; /** * Update an existing template * * PUT /api/v1/templates */ export const updateApiApiV1Templates = async (args: UpdateApiApiV1TemplatesArgs, options: UpdateApiApiV1TemplatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/templates${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(responseEntityTemplateView, await res.json()) };