@clementoh/better-openapi-viewer-core

中文文档
0.1.8发布于 2026/05/12MITTypeScriptES Module零依赖近 30 天下载 5636

Framework-agnostic OpenAPI normalization and viewer primitives.

原始使用文档
文档目录5 节
  1. Current primitives
  2. Viewer config
  3. Try It Out
  4. Validation and auth metadata
  5. Server variables

@clementoh/better-openapi-viewer-core

Framework-agnostic OpenAPI normalization and viewer primitives.

Current primitives

  • Document ingestion helpers for detecting OpenAPI 3.0, OpenAPI 3.1, and Swagger 2.0 documents.
  • Internal JSON Pointer $ref resolution for pre-normalizing documents, plus external $ref resolver scaffolding that can call a consumer-provided loader.
  • Operation extraction with tag, parameter, response, schema, auth, content type, server, and vendor-extension metadata.
  • Search, filter, and tag grouping helpers for navigation and discovery.
  • Schema tree, schema type, and schema/media/response example helpers for later UI rendering.
  • Response normalization helpers with status range/category classification and Swagger 2.0 produces fallbacks.
  • Request body media type discovery for OpenAPI 3.x requestBody.content and Swagger 2.0 body/form parameters.
  • Parameter and request body validation helpers for required values, schema types, enums, constants, object properties, arrays, and composition guards.
  • Security scheme extraction across OpenAPI 3.x components.securitySchemes and Swagger 2.0 securityDefinitions, including OAuth flow metadata and OpenID Connect discovery URLs.
  • Markdown-safe text escaping for descriptions that need to be rendered through Markdown-aware surfaces.
  • Try It Out request primitives for server selection and variable substitution, server variable defaults/options, common parameter serialization styles (form, simple, spaceDelimited, pipeDelimited, deepObject, matrix, and label), request URL/header/cookie/body construction, basic/bearer/api-key/OAuth/OpenID credential application, preauthorization credential merging, request/response interceptor hooks, and curl/fetch/httpie/python snippet generation.
  • Viewer configuration primitives for route path, JSON path, title, layout, default expansion, deep linking, filter, request duration display, persisted auth, syntax highlighting, supported submit methods, operation/tag sorters, model expansion depth, and plugin metadata.

Viewer config

ViewerConfig captures the framework-agnostic knobs adapters and UIs can share without depending on a specific renderer. Use mergeViewerConfig to layer package defaults, adapter defaults, and user overrides.

import { mergeViewerConfig } from '@clementoh/better-openapi-viewer-core';

const config = mergeViewerConfig({
  title: 'Example API',
  defaultExpansion: 'list',
  deepLinking: true,
  filter: true,
  displayRequestDuration: true,
  persistAuthorization: false,
  supportedSubmitMethods: ['get', 'post', 'patch', 'delete'],
  operationsSorter: 'alpha',
  tagsSorter: 'alpha',
  defaultModelExpandDepth: 2,
});

sortOperations, sortOperationTags, and isSubmitMethodSupported provide reusable behavior for consumers that want to honor the same configuration in different runtimes.

Try It Out

buildTryItOutRequest builds a framework-neutral request model. Consumers can provide request interceptors before execution and apply response interceptors after their own HTTP client returns.

import { applyTryItOutResponseInterceptor, buildTryItOutRequest, generateRequestSnippet } from '@clementoh/better-openapi-viewer-core';

const request = buildTryItOutRequest({
  operation,
  parameters: { id: 42 },
  interceptors: {
    request: (next) => ({ ...next, headers: { ...next.headers, 'X-Client': 'docs' } }),
    response: (next) => next,
  },
});

const snippet = generateRequestSnippet(request, 'fetch');
const response = applyTryItOutResponseInterceptor({ status: 200, headers: {}, body: {} }, { operation }, request);

Use resolveReference(document, ref, { loader }) when a renderer or adapter wants to resolve external references. Internal #/... pointers resolve from the provided document first; non-internal refs are fetched only when a loader is supplied.

Validation and auth metadata

Core exposes typed helpers that renderer packages can call before sending a Try It Out request.

import {
  getOpenIdConnectUrls,
  getSecuritySchemes,
  getSecuritySchemeMetadata,
  mergePreauthorizedCredentials,
  validateParameterValues,
  validateRequestBody,
} from '@clementoh/better-openapi-viewer-core';

const parameterResult = validateParameterValues({
  parameters: operation.parameters,
  values: { id: 42 },
});

const bodyResult = validateRequestBody({
  requestBody: operation.requestBody,
  body: { name: 'Ada' },
  contentType: 'application/json',
});

const securityMetadata = getSecuritySchemeMetadata(getSecuritySchemes(document));
const openIdUrls = getOpenIdConnectUrls(getSecuritySchemes(document));
const oauthFlows = securityMetadata.flatMap((scheme) => scheme.oauthFlows);

const auth = mergePreauthorizedCredentials({
  auth: {
    bearerAuth: { value: 'token' },
    oauth: { value: 'access-token', scopes: ['read:users'] },
  },
});

Server variables

Server variable helpers expose defaults and allowed options without binding consumers to a UI.

import { getServerVariableDefaults, getServerVariableOptions, normalizeServerVariables, selectServerUrl } from '@clementoh/better-openapi-viewer-core';

const variables = normalizeServerVariables(server, { environment: 'staging' });
const url = selectServerUrl([server], { variables });
const defaults = getServerVariableDefaults(server);
const options = getServerVariableOptions(server);

版本与发布记录

@clementoh/better-openapi-viewer-core 共发布 8 个版本,首次发布于 2026/05/07;过去 12 个月发布了 8 个版本。

版本发布日期距今
0.1.8latest2026/05/125 个月前
0.1.72026/05/125 个月前
0.1.62026/05/076 个月前
0.1.52026/05/076 个月前
0.1.42026/05/076 个月前
0.1.32026/05/076 个月前

关于 @clementoh/better-openapi-viewer-core 的常见问题

@clementoh/better-openapi-viewer-core 是什么?

Framework-agnostic OpenAPI normalization and viewer primitives.

如何安装 @clementoh/better-openapi-viewer-core?

在项目目录运行 npm install @clementoh/better-openapi-viewer-core,或使用 pnpm add / yarn add / bun add。

@clementoh/better-openapi-viewer-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@clementoh/better-openapi-viewer-core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@clementoh/better-openapi-viewer-core 可以商用吗?

@clementoh/better-openapi-viewer-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@clementoh/better-openapi-viewer-core 的下载量有多少?

根据 npm 官方下载统计,@clementoh/better-openapi-viewer-core 在 2026/10/10 – 2026/11/08 这 30 天内被下载 5,636 次,日均约 188 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@clementoh/better-openapi-viewer-core 还在维护吗?

最新版本 0.1.8 发布于 2026/05/12(5 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/05/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。