import {z} from 'zod' export type UserAuthFunction = {auth_fn?: 'auth_user' | undefined, user_id: string}; export type EmailDomainFunction = {auth_fn?: 'auth_email_suffix' | undefined, connection?: (string | null) | undefined, email_domain: string}; export type OrganizationRoleFunction = {auth_fn?: 'auth_organization_role' | undefined, connection?: (string | null) | undefined, roles_claim: string, role: string, organization_claim: string, organization: string}; export type OrganizationMembershipFunction = {auth_fn?: 'auth_organization' | undefined, connection?: (string | null) | undefined, organization_claim: string, organization: string}; export type AllowAllAuthFunction = {auth_fn?: 'auth_allow_all' | undefined}; export type AddAuthFunctionRequest = {tenant_id: string, auth_fn: UserAuthFunction | EmailDomainFunction | OrganizationRoleFunction | OrganizationMembershipFunction | AllowAllAuthFunction}; export type CreateApiAdminAddAuthFunctionArgs = {body: AddAuthFunctionRequest}; export type CreateApiAdminAddAuthFunctionOptions = {baseUrl: string, AdminKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAdminAddAuthFunctionResponse = z.unknown(); 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 }; /** * Add Auth Function Endpoint * * POST /admin/add-auth-function */ export const createApiAdminAddAuthFunction = async (args: CreateApiAdminAddAuthFunctionArgs, options: CreateApiAdminAddAuthFunctionOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AdminKey !== undefined) headers.set("X-Tenzir-AdminKey", AdminKey) const url = `${baseUrl.replace(/\/+$/, '')}/admin/add-auth-function${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAdminAddAuthFunctionResponse, await res.json()) };