import {z} from 'zod' export type Confidentiality = 'confidential' | 'public'; export type TokenEndpointAuthMethod = 'client_secret_basic' | 'client_secret_post' | 'none'; export type GrantType = Array<'authorization_code' | 'client_credentials'>; export type TokenConfiguration = {expires_after: number, token_signing_algorithm?: 'RS256' | undefined, subject_field?: ('id' | 'email' | 'username') | undefined}; export type PkceConfig = 'disabled' | 'plain' | 's256'; export type Application = {id?: string | undefined, realm_id?: string | undefined, tenant_id?: string | undefined, resource_server_id?: string | undefined, authenticator_config_id?: string | undefined, display_name?: string | undefined, is_managed?: boolean | undefined, protocol_config?: ({type: 'oauth2', allowed_scopes?: Array | undefined, client_id?: string | undefined, client_secret?: string | undefined, confidentiality?: Confidentiality | undefined, token_endpoint_auth_method?: TokenEndpointAuthMethod | undefined, grant_type?: GrantType | undefined, redirect_uris?: Array | undefined, token_configuration?: TokenConfiguration | undefined, pkce?: PkceConfig | undefined, token_format?: ('self_contained' | 'referential') | undefined} | {type: 'oidc', allowed_scopes?: Array | undefined, client_id?: string | undefined, client_secret?: string | undefined, confidentiality?: Confidentiality | undefined, token_endpoint_auth_method?: TokenEndpointAuthMethod | undefined, grant_type?: GrantType | undefined, redirect_uris?: Array | undefined, token_configuration?: TokenConfiguration | undefined, pkce?: PkceConfig | undefined, token_format?: unknown} | unknown) | undefined}; export type CreateApiV1TenantsTenantIdRealmsRealmIdApplicationsArgs = {tenant_id: string, realm_id: string, body: {application: Application}}; export type CreateApiV1TenantsTenantIdRealmsRealmIdApplicationsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const confidentiality = z.enum(["confidential", "public"]); export const tokenEndpointAuthMethod = z.enum(["client_secret_basic", "client_secret_post", "none"]); export const grantType = z.array(z.enum(["authorization_code", "client_credentials"])); export const tokenConfiguration = z.object({expires_after: z.number().int().gte(0), token_signing_algorithm: z.literal("RS256").optional(), subject_field: z.enum(["id", "email", "username"]).optional()}); export const pkceConfig = z.enum(["disabled", "plain", "s256"]); export const application = z.object({id: z.string().optional(), realm_id: z.string().optional(), tenant_id: z.string().optional(), resource_server_id: z.string().optional(), authenticator_config_id: z.string().optional(), display_name: z.string().optional(), is_managed: z.boolean().optional(), protocol_config: z.union([z.object({type: z.literal("oauth2"), allowed_scopes: z.array(z.string()).optional(), client_id: z.string().optional(), client_secret: z.string().optional(), confidentiality: confidentiality.optional(), token_endpoint_auth_method: tokenEndpointAuthMethod.optional(), grant_type: grantType.optional(), redirect_uris: z.array(z.string()).optional(), token_configuration: tokenConfiguration.optional(), pkce: pkceConfig.optional(), token_format: z.enum(["self_contained", "referential"]).optional()}), z.object({type: z.literal("oidc"), allowed_scopes: z.array(z.string()).optional(), client_id: z.string().optional(), client_secret: z.string().optional(), confidentiality: confidentiality.optional(), token_endpoint_auth_method: tokenEndpointAuthMethod.optional(), grant_type: grantType.optional(), redirect_uris: z.array(z.string()).optional(), token_configuration: tokenConfiguration.optional(), pkce: pkceConfig.optional(), token_format: z.unknown().optional()}), z.unknown()]).optional()}); 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 }; /** * Create a New Application * * POST /v1/tenants/{tenant_id}/realms/{realm_id}/applications */ export const createApiV1TenantsTenantIdRealmsRealmIdApplications = async (args: CreateApiV1TenantsTenantIdRealmsRealmIdApplicationsArgs, options?: CreateApiV1TenantsTenantIdRealmsRealmIdApplicationsOptions) => { const {baseUrl = "https://api-us.beyondidentity.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tenants/${encodeURIComponent(String(args.tenant_id))}/realms/${encodeURIComponent(String(args.realm_id))}/applications${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(application, await res.json()) };