export type CreateApiApiV3AuthTokenArgs = {scope?: ('submissions:read' | 'submissions:write') | undefined}; export type CreateApiApiV3AuthTokenOptions = {baseUrl: string, submissionBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Requires fresh HTTP Basic credentials. Omit scope for existing identity-only token behavior. submissions:write additionally requires enabled admission and an enrolled account; submissions:read permits authenticated owners to inspect retained drafts after unenrollment. Submission capabilities currently support context0 only. * * POST /api/v3/auth/token */ export const createApiApiV3AuthToken = async (args: CreateApiApiV3AuthTokenArgs, options: CreateApiApiV3AuthTokenOptions) => { const {baseUrl, fetch: fetchFn = fetch, submissionBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args.scope != null) query.append("scope", String(args.scope)) if (submissionBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${submissionBasic.username}:${submissionBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/auth/token${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 };