import {z} from 'zod' export type DynamicTranslation = {type?: string | undefined, externalId?: string | undefined, field?: string | undefined, locale?: string | undefined, value?: string | undefined}; export type StaticTranslation = {key?: string | undefined, locale?: string | undefined, value?: string | undefined}; export type TranslationRequest = {dynamicTranslations: Array, staticTranslations: Array}; export type UpdateContentRequest = {title?: string | undefined, subTitle?: string | undefined, bodyText?: string | undefined, headerImage?: string | undefined, thumbnailImage?: string | undefined, labels?: Array | undefined, state?: ('visible' | 'hidden' | 'scheduled') | undefined, contentType?: ('article' | 'news') | undefined, publishDate?: string | undefined, pages?: Array | undefined, slug?: string | undefined, visibility?: ('public' | 'private') | undefined, tag?: string | undefined, translations?: Array | undefined}; export type UpdateApiV1PortalContentContentIdArgs = {contentId: string, 'x-publisher-token': string, body: UpdateContentRequest}; export type UpdateApiV1PortalContentContentIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dynamicTranslation = z.object({type: z.string().optional(), externalId: z.string().optional(), field: z.string().optional(), locale: z.string().optional(), value: z.string().optional()}); export const staticTranslation = z.object({key: z.string().optional(), locale: z.string().optional(), value: z.string().optional()}); export const translationResponse = z.object({dynamicTranslations: z.array(dynamicTranslation).optional(), staticTranslations: z.array(staticTranslation).optional()}); export const page = z.object({id: z.string().optional(), pageName: z.string().optional()}); export const contentItem = z.object({id: z.string().optional(), title: z.string().optional(), subTitle: z.string().optional(), bodyText: z.string().optional(), headerImage: z.string().optional(), thumbnailImage: z.string().optional(), labels: z.array(z.string()).optional(), state: z.enum(["visible", "hidden", "scheduled"]).optional(), contentType: z.enum(["article", "news"]).optional(), publishDate: z.string().optional(), slug: z.string().optional(), visibility: z.enum(["public", "private"]).optional(), tag: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), translations: z.array(translationResponse).optional(), pages: z.array(page).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 Content * * PUT /v1/portal-content/{contentId} */ export const updateApiV1PortalContentContentId = async (args: UpdateApiV1PortalContentContentIdArgs, options: UpdateApiV1PortalContentContentIdOptions) => { 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") if (args['x-publisher-token'] != null) headers.set("x-publisher-token", String(args['x-publisher-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/portal-content/${encodeURIComponent(String(args.contentId))}${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(contentItem, await res.json()) };