export type CreateApiPathArgs = {path: string, 'jcr:primaryType': string, ':name': string}; export type CreateApiPathOptions = {baseUrl: string, aemAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * POST /{path}/ */ export const createApiPath = async (args: CreateApiPathArgs, options: CreateApiPathOptions) => { const {baseUrl, fetch: fetchFn = fetch, aemAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args['jcr:primaryType'] != null) query.append("jcr:primaryType", String(args['jcr:primaryType'])) if (args[':name'] != null) query.append(":name", String(args[':name'])) if (aemAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${aemAuth.username}:${aemAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.path))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };