import {z} from 'zod' export type CreateApiAuthoringV1ReviewArgs = {body: {name: string, targetDate?: string | undefined, approvers?: (Array<{id?: string | undefined}>) | undefined, items?: (Array<{id?: string | undefined, classification?: ('asset' | 'content') | undefined}>) | undefined}}; export type CreateApiAuthoringV1ReviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiAuthoringV1ReviewResponse = z.object({id: z.string(), rev: z.string(), classification: z.string().optional(), name: z.string(), targetDate: z.string().optional(), status: z.enum(["active", "complete"]).optional(), approvalStatus: z.enum(["approved", "not-approved"]).optional(), completed: z.string().optional(), approvers: z.array(z.object({id: z.string().optional()})).optional(), approvals: z.array(z.object({user: z.object({id: z.string().optional()}).optional(), date: z.string().optional(), items: z.array(z.object({id: z.string().optional(), classification: z.enum(["asset", "content"]).optional()})).optional()})).optional(), rejections: z.array(z.object({user: z.object({id: z.string().optional()}).optional(), date: z.string().optional(), items: z.array(z.object({id: z.string().optional(), classification: z.enum(["asset", "content"]).optional()})).optional()})).optional(), items: z.array(z.object({id: z.string().optional(), classification: z.enum(["asset", "content"]).optional()})).optional(), lastModified: z.string().optional(), lastModifierId: z.string().optional(), created: z.string().optional(), creatorId: z.string().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 }; /** * Start a review. * * POST /authoring/v1/review */ export const createApiAuthoringV1Review = async (args: CreateApiAuthoringV1ReviewArgs, options: CreateApiAuthoringV1ReviewOptions) => { 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(/\/+$/, '')}/authoring/v1/review${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiAuthoringV1ReviewResponse, await res.json()) };