import {z} from 'zod' export type GetApiApiV1PageRenderSourcesUriArgs = {uri: string, host_id?: string | undefined, language_id?: string | undefined, persona_id?: string | undefined, variantName?: string | undefined, mode?: string | undefined}; export type GetApiApiV1PageRenderSourcesUriOptions = {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 pageSourceRefView = z.object({identifier: z.string().optional(), uri: z.string().optional(), languageId: z.number().int().optional()}); export const fileRefView = z.object({path: z.string().optional(), identifier: z.string().optional(), extension: z.string().optional()}); export const themeSourceView = z.object({id: z.string().optional(), name: z.string().optional(), folderPath: z.string().optional(), files: z.array(fileRefView).optional()}); export const contentTypeEntryView = z.object({contentTypeVar: z.string().optional(), path: z.string().optional(), identifier: z.string().optional()}); export const containerSourceView = z.object({source: z.enum(["DB", "FILE"]).optional(), contentTypes: z.array(contentTypeEntryView).optional()}); export const widgetSourceView = z.object({contentTypeVar: z.string().optional(), title: z.string().optional(), contentletId: z.string().optional(), contentletInode: z.string().optional(), source: z.enum(["FILE", "CODE"]).optional(), path: z.string().optional(), identifier: z.string().optional()}); export const urlContentMapView = z.object({contentTypeVar: z.string().optional(), title: z.string().optional(), contentletId: z.string().optional(), contentletInode: z.string().optional()}); export const pageRenderSourcesView = z.object({page: pageSourceRefView.optional(), theme: themeSourceView.optional(), containers: z.record(z.string(), containerSourceView).optional(), widgets: z.array(widgetSourceView).optional(), urlContentMap: urlContentMapView.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 responseEntityPageRenderSourcesView = z.object({errors: z.array(errorEntity).optional(), entity: pageRenderSourcesView.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 }; /** * Get render sources for a page * * GET /api/v1/page/_render-sources/{uri} */ export const getApiApiV1PageRenderSourcesUri = async (args: GetApiApiV1PageRenderSourcesUriArgs, options: GetApiApiV1PageRenderSourcesUriOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.host_id != null) query.append("host_id", String(args.host_id)) if (args.language_id != null) query.append("language_id", String(args.language_id)) if (args.persona_id != null) query.append("persona_id", String(args.persona_id)) if (args.variantName != null) query.append("variantName", String(args.variantName)) if (args.mode != null) query.append("mode", String(args.mode)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/page/_render-sources/${encodeURIComponent(String(args.uri))}${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(responseEntityPageRenderSourcesView, await res.json()) };