import {z} from 'zod' export type FcfAuthentication = {name?: string | undefined, attributes: {[key: string]: unknown}, roles: Array}; export type TemporalUnit = {duration?: Duration | undefined, durationEstimated?: boolean | undefined, dateBased?: boolean | undefined, timeBased?: boolean | undefined}; export type Duration = {units?: Array | undefined, positive?: boolean | undefined, zero?: boolean | undefined, negative?: boolean | undefined, seconds?: number | undefined, nano?: number | undefined}; export type DatabaseSession = {authentication?: (FcfAuthentication | null) | undefined, id?: string | undefined, creationTime?: string | undefined, expires?: string | undefined, expired?: boolean | undefined, lastAccessedTime?: string | undefined, maxInactiveInterval?: Duration | undefined, new?: boolean | undefined, modified?: boolean | undefined, modifiedTime?: string | undefined}; export type GetApiAuthInfoArgs = {requestSession?: (DatabaseSession | null) | undefined, originPath?: (string | null) | undefined, utmSource?: (string | null) | undefined, utmMedium?: (string | null) | undefined, utmCampaign?: (string | null) | undefined}; export type GetApiAuthInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fcfAuthentication = z.object({name: z.string().optional(), attributes: z.record(z.string(), z.unknown()), roles: z.array(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 }; /** * GET /auth/info */ export const getApiAuthInfo = async (args: GetApiAuthInfoArgs, options: GetApiAuthInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.requestSession != null) query.append("requestSession", JSON.stringify(args.requestSession)) if (args.originPath != null) query.append("originPath", String(args.originPath)) if (args.utmSource != null) query.append("utmSource", String(args.utmSource)) if (args.utmMedium != null) query.append("utmMedium", String(args.utmMedium)) if (args.utmCampaign != null) query.append("utmCampaign", String(args.utmCampaign)) const url = `${baseUrl.replace(/\/+$/, '')}/auth/info${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(fcfAuthentication, await res.json()) };