Fumadocs

createOpenAPI()

OpenAPI 服务器实例。

OpenAPI 服务器

🌐 OpenAPI Server

Fumadocs OpenAPI 的主要配置。

🌐 The main config for Fumadocs OpenAPI.

它不应该在浏览器环境中被引用。

input

要读取的 OpenAPI 模式。

🌐 The OpenAPI schemas to read from.

  • 文件路径
  • 外部网址
  • 一个函数(见下文)
import { createOpenAPI } from 'fumadocs-openapi/server';

export const openapi = createOpenAPI({
  input: ['./unkey.json'],
});

创建代理

🌐 Creating Proxy

代理服务器对于执行 HTTP(fetch)请求非常有用,因为它不像浏览器那样受 CORS 限制。当目标 API 端点没有正确配置 CORS 时,我们可以在 OpenAPI Playground 上使用它来执行 HTTP 请求。

🌐 A proxy server is useful for executing HTTP (fetch) requests, as it doesn't have CORS constraints like on the browser. We can use it for executing HTTP requests on the OpenAPI playground, when the target API endpoints do not have CORS configured correctly.

警告

不要在不可靠的网站和 API 端点上使用此工具,代理服务器会转发所有接收到的头信息和正文,包括仅 HTTP 的 CookiesAuthorization 头信息。

安装

🌐 Setup

为代理服务器创建路由处理程序。

🌐 Create a route handler for proxy server.

/api/proxy/route.ts
import { openapi } from '@/lib/openapi';

export const { GET, HEAD, PUT, POST, PATCH, DELETE } = openapi.createProxy({
  // optional, we recommend to set a list of allowed origins for proxied requests
  allowedOrigins: ['https://example.com'],
});

如果尚未配置 openapi 实例,请按照 入门指南 进行操作。

并在 createOpenAPI 中设置代理 URL。

🌐 And set the proxy URL in createOpenAPI.

lib/openapi.ts
import { createOpenAPI } from 'fumadocs-openapi/server';

export const openapi = createOpenAPI({
  proxyUrl: '/api/proxy',
});

Last updated on

On this page