import {z} from 'zod' export type CreateApiAuthoringV1CopySyncArgs = {referencesToClear?: string | undefined, referencesToClone?: string | undefined, referenceLevels?: number | undefined, clearElementsOnLastLevel?: boolean | undefined, 'x-ibm-dx-publish-priority'?: 'now' | undefined, body: Array}; export type CreateApiAuthoringV1CopySyncOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiAuthoringV1CopySyncResponse = z.object({classification: z.literal("copy").optional(), status: z.enum(["success", "failed"]).optional(), params: z.object({}).optional(), rootCopiedItemCount: z.number().int().optional(), rootCopiedItemList: z.object({}).optional(), nonrootCopiedItemCount: z.number().int().optional(), nonrootCopiedItemList: z.object({}).optional(), failedToCopyCount: z.number().int().optional(), failedToCopyList: z.object({}).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 }; /** * Copies the specified documents. * * POST /authoring/v1/copy/sync */ export const createApiAuthoringV1CopySync = async (args: CreateApiAuthoringV1CopySyncArgs, options: CreateApiAuthoringV1CopySyncOptions) => { 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.referencesToClear != null) query.append("referencesToClear", String(args.referencesToClear)) if (args.referencesToClone != null) query.append("referencesToClone", String(args.referencesToClone)) if (args.referenceLevels != null) query.append("referenceLevels", String(args.referenceLevels)) if (args.clearElementsOnLastLevel != null) query.append("clearElementsOnLastLevel", String(args.clearElementsOnLastLevel)) if (args['x-ibm-dx-publish-priority'] != null) headers.set("x-ibm-dx-publish-priority", String(args['x-ibm-dx-publish-priority'])) const url = `${baseUrl.replace(/\/+$/, '')}/authoring/v1/copy/sync${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(createApiAuthoringV1CopySyncResponse, await res.json()) };