export type CreateApiPathNameArgs = {path: string, name: string, ':operation'?: string | undefined, deleteAuthorizable?: string | undefined, body: BodyInit}; export type CreateApiPathNameOptions = {baseUrl: string, aemAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * POST /{path}/{name} */ export const createApiPathName = async (args: CreateApiPathNameArgs, options: CreateApiPathNameOptions) => { const {baseUrl, fetch: fetchFn = fetch, aemAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args[':operation'] != null) query.append(":operation", String(args[':operation'])) if (args.deleteAuthorizable != null) query.append("deleteAuthorizable", String(args.deleteAuthorizable)) if (aemAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${aemAuth.username}:${aemAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.path))}/${encodeURIComponent(String(args.name))}${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 undefined };