'use client'; import { OpenAPICopyButton } from './OpenAPICopyButton'; import { OpenAPIDisclosureGroup } from './OpenAPIDisclosureGroup'; import { useSelectState } from './OpenAPISelect'; import type { OpenAPIClientContext } from './context'; import { t } from './translate'; import type { OpenAPISecurityScope } from './types'; import type { OperationSecurityInfo } from './utils'; /** * Present securities authorization that can be used for this operation. */ export function OpenAPIRequiredScopes(props: { securities: OperationSecurityInfo[]; context: OpenAPIClientContext; stateKey: string; }) { const { securities, stateKey, context } = props; const { key: selectedKey } = useSelectState(stateKey, securities[0]?.key); const selectedSecurity = securities.find((security) => security.key === selectedKey); if (!selectedSecurity) { return null; } const scopeAlternatives = selectedSecurity.scopeAlternatives.length > 0 ? selectedSecurity.scopeAlternatives : [ selectedSecurity.schemes.flatMap((scheme) => { return scheme.scopes ?? []; }), ]; const resolvedAlternatives = scopeAlternatives .map((scopes) => dedupeScopes(scopes)) .filter((scopes) => scopes.length > 0); if (!resolvedAlternatives.length) { return null; } return ( {context.icons.lock} {t(context.translation, 'required_scopes')} ), tabs: [ { key: 'scopes', label: '', body: ( ), }, ], }, ]} /> ); } function OpenAPIScopeAlternatives(props: { alternatives: OpenAPISecurityScope[][]; context: OpenAPIClientContext; }) { const { alternatives, context } = props; if (alternatives.length === 1) { return ; } return (
{t(context.translation, 'required_scopes_description')}
{alternatives.map((scopes, index) => (
{index < alternatives.length - 1 ? ( {t(context.translation, 'or')} ) : null}
))}
); } export function OpenAPISchemaScopes(props: { scopes: OpenAPISecurityScope[] | undefined; context: OpenAPIClientContext; isOAuth2?: boolean; hideDescription?: boolean; }) { const { scopes, context, hideDescription } = props; return (
{!hideDescription ? (
{t(context.translation, 'required_scopes_description')}
) : null}
    {scopes?.map((scope) => ( ))}
); } /** * Display a scope item. Either a key-value pair or a single string. */ function OpenAPIScopeItem(props: { scope: OpenAPISecurityScope; context: OpenAPIClientContext; }) { const { scope, context } = props; return (
  • {scope[1] ? : {scope[1]} : null}
  • ); } /** * Displays the scope name within a copyable button. */ function OpenAPIScopeItemKey(props: { name: string; context: OpenAPIClientContext; }) { const { name, context } = props; return ( {name} ); } function dedupeScopes(scopes: OpenAPISecurityScope[]) { const seen = new Set(); const deduped: OpenAPISecurityScope[] = []; for (const scope of scopes) { if (seen.has(scope[0])) { continue; } seen.add(scope[0]); deduped.push(scope); } return deduped; }