import {z} from 'zod' export type ProjectError = {target?: string | undefined, message?: string | undefined}; export type ProjectErrors = {channels?: Array | undefined, contentTypes?: Array | undefined, documents?: Array | undefined, pages?: Array | undefined, resourceBundles?: Array | undefined} | null; export type ProjectState = {status?: ('IN_PROGRESS' | 'ADDING_CHANNEL' | 'REBASING' | 'REBASE_ERROR' | 'IN_REVIEW' | 'APPROVED' | 'MERGING' | 'MERGED' | 'MERGE_ERROR' | 'DELETED' | 'DELETING' | 'RUNNING' | 'START_RUNNING' | 'STOP_RUNNING') | undefined, message?: string | undefined, errors?: ProjectErrors | undefined, availableActions?: Array | undefined}; export type ProjectChannel = {id?: string | undefined, displayName?: string | undefined}; export type ProjectContentType = {name?: string | undefined, displayName?: string | undefined}; export type ProjectDocument = {path?: string | undefined, displayName?: string | undefined}; export type ProjectPage = {path?: string | undefined, displayName?: string | undefined}; export type ProjectResourceBundle = {path?: string | undefined, displayName?: string | undefined}; export type ProjectItems = {channels?: Array | undefined, contentTypes?: Array | undefined, documents?: Array | undefined, pages?: Array | undefined, resourceBundles?: Array | undefined} | null; export type ProjectSystem = {createdBy?: string | undefined, createdAt?: string | undefined, updatedBy?: string | undefined, updatedAt?: string | undefined, mergedBy?: (string | null) | undefined, mergedAt?: (string | null) | undefined}; export type Project = {id?: string | undefined, name: string, includeContentTypes?: boolean | undefined, description?: string | undefined, state?: ProjectState | undefined, items?: ProjectItems | undefined, system?: ProjectSystem | undefined}; export type CreateApiArgs = {body: Project}; export type CreateApiOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const projectError = z.object({target: z.string().optional(), message: z.string().optional()}); export const projectErrors = z.object({channels: z.array(projectError).optional(), contentTypes: z.array(projectError).optional(), documents: z.array(projectError).optional(), pages: z.array(projectError).optional(), resourceBundles: z.array(projectError).optional()}).nullable(); export const projectState = z.object({status: z.enum(["IN_PROGRESS", "ADDING_CHANNEL", "REBASING", "REBASE_ERROR", "IN_REVIEW", "APPROVED", "MERGING", "MERGED", "MERGE_ERROR", "DELETED", "DELETING", "RUNNING", "START_RUNNING", "STOP_RUNNING"]).optional(), message: z.string().optional(), errors: projectErrors.optional(), availableActions: z.array(z.string()).optional()}); export const projectChannel = z.object({id: z.string().optional(), displayName: z.string().optional()}); export const projectContentType = z.object({name: z.string().optional(), displayName: z.string().optional()}); export const projectDocument = z.object({path: z.string().optional(), displayName: z.string().optional()}); export const projectPage = z.object({path: z.string().optional(), displayName: z.string().optional()}); export const projectResourceBundle = z.object({path: z.string().optional(), displayName: z.string().optional()}); export const projectItems = z.object({channels: z.array(projectChannel).optional(), contentTypes: z.array(projectContentType).optional(), documents: z.array(projectDocument).optional(), pages: z.array(projectPage).optional(), resourceBundles: z.array(projectResourceBundle).optional()}).nullable(); export const projectSystem = z.object({createdBy: z.string().optional(), createdAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional(), mergedBy: z.string().nullable().optional(), mergedAt: z.string().nullable().optional()}); export const project = z.object({id: z.string().optional(), name: z.string(), includeContentTypes: z.boolean().optional(), description: z.string().optional(), state: projectState.optional(), items: projectItems.optional(), system: projectSystem.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 }; /** * Create a developer project * * POST / */ export const createApi = async (args: CreateApiArgs, options?: CreateApiOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", 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(/\/+$/, '')}/${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(project, await res.json()) };