import {z} from 'zod' export type CreateApiV1ValidateDslArgs = {body: {hostApp?: string | undefined, debug?: boolean | undefined, dslDocument: {[key: string]: unknown}}}; export type CreateApiV1ValidateDslOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const requestSummary = z.object({method: z.string(), uri: z.string(), path: z.string(), host: z.string()}); export const validationErrorEntry = z.object({path: z.string(), message: z.string(), keyword: z.string(), source: z.string(), suggestion: z.string().nullable(), candidates: z.array(z.string()).optional()}); export const validateDslResult = z.object({kind: z.literal("dsl"), hostApp: z.string(), ok: z.boolean(), validatorEngine: z.string(), diagnostics: z.object({schemaErrorCount: z.number().int().gte(0), guardErrorCount: z.number().int().gte(0), warningCount: z.number().int().gte(0)}), summary: z.string(), suggestions: z.array(z.string()), debug: z.object({}).nullable(), errorCount: z.number().int().gte(0), errors: z.array(validationErrorEntry), warningCount: z.number().int().gte(0), warnings: z.array(validationErrorEntry)}); export const validateDslResponse = z.object({service: z.string(), status: z.literal("ok"), timestampUtc: z.string(), message: z.string(), request: requestSummary, validation: validateDslResult}).and(z.record(z.string(), z.unknown())); 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 }; /** * Validate an Automation Agent for Premiere Pro DSL document * * POST /v1/validate/dsl */ export const createApiV1ValidateDsl = async (args: CreateApiV1ValidateDslArgs, options?: CreateApiV1ValidateDslOptions) => { const {baseUrl = "https://api.mamoworld.com", 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(/\/+$/, '')}/v1/validate/dsl${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(validateDslResponse, await res.json()) };