import {z} from 'zod' export type CreateApiCrxPackmgrServiceJsonPathArgs = {path: string, cmd: string, groupName?: string | undefined, packageName?: string | undefined, packageVersion?: string | undefined, _charset_?: string | undefined, force?: boolean | undefined, recursive?: boolean | undefined, body: BodyInit}; export type CreateApiCrxPackmgrServiceJsonPathOptions = {baseUrl: string, aemAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiCrxPackmgrServiceJsonPathResponse = z.string(); 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 }; /** * POST /crx/packmgr/service/.json/{path} */ export const createApiCrxPackmgrServiceJsonPath = async (args: CreateApiCrxPackmgrServiceJsonPathArgs, options: CreateApiCrxPackmgrServiceJsonPathOptions) => { const {baseUrl, fetch: fetchFn = fetch, aemAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cmd != null) query.append("cmd", String(args.cmd)) if (args.groupName != null) query.append("groupName", String(args.groupName)) if (args.packageName != null) query.append("packageName", String(args.packageName)) if (args.packageVersion != null) query.append("packageVersion", String(args.packageVersion)) if (args._charset_ != null) query.append("_charset_", String(args._charset_)) if (args.force != null) query.append("force", String(args.force)) if (args.recursive != null) query.append("recursive", String(args.recursive)) if (aemAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${aemAuth.username}:${aemAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/crx/packmgr/service/.json/${encodeURIComponent(String(args.path))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCrxPackmgrServiceJsonPathResponse, await res.json()) };