是否可能使用本地浏览器对象来模拟远程URL行为?

huangapple go评论74阅读模式
英文:

Is it possible to emulate remote URL behavior using local browser object?

问题

我正在使用Redoc独立版与我们的API服务器。在加载redoc.standalone.js后,redoc尝试从API服务器获取schema.yaml。然而,我们的服务器需要一个带有令牌的Authorization头,而Redoc默认不包括这个头部。我正在寻找一种解决方案,要么将模式文件下载并保存到本地,要么将Authorization头部添加为页面发送的所有请求的默认头部。是否有另一种方法可以在不向私有API服务器发出请求的情况下由Redoc本地读取schema.yaml?以下是我正在使用Redoc的当前HTML代码:

<!DOCTYPE html>
<html>
   <head>
      <title>示例API</title>
      <meta charset="utf-8"/>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
      <style>
         body {
         margin: 0;
         padding: 0;
         }
      </style>
   </head>
   <body>
      <redoc spec-url='https://api.example.com/schema.yaml'></redoc>
      <script src="redoc.standalone.js"></script>
   </body>
</html>
英文:

I am using Redoc standalone with our API server. After redoc.standalone.js is loaded, redoc tries to fetch schema.yaml from the API server. However, our server needs an Authorization header with token, which Redoc does not include by default. I am looking for a solution to either download and save the schema file locally or add the Authorization header as a default header for all requests sent from the page. Is there another way to read schema.yaml locally by Redoc without making a request to the private API server? Below is the current HTML code I am using with Redoc:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
   &lt;head&gt;
      &lt;title&gt;Example API&lt;/title&gt;
      &lt;meta charset=&quot;utf-8&quot;/&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
      &lt;link href=&quot;https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700&quot; rel=&quot;stylesheet&quot;&gt;
      &lt;style&gt;
         body {
         margin: 0;
         padding: 0;
         }
      &lt;/style&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;redoc spec-url=&#39;https://api.example.com/schema.yaml&#39;&gt;&lt;/redoc&gt;
      &lt;script src=&quot;redoc.standalone.js&quot;&gt;&lt;/script&gt;
   &lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

我认为可以用一种不同的方法来完成这个任务。不要使用React渲染Redoc,而是使用redocly build-docs命令(查看文档:https://redocly.com/docs/cli/commands/build-docs/)在一个可以访问模式架构或在生成文档之前可以下载它的位置来构建HTML文档。然后发布HTML输出。

(免责声明,我在Redocly工作,他们制作了Redoc)

英文:

I think this can be done with a different approach. Instead of using the react rendering with Redoc, build the HTML documentation with the redocly build-docs command (see docs: https://redocly.com/docs/cli/commands/build-docs/) from a location where you do have access to the schema, or can download it before generating the docs. Then publish the HTML output.

(disclaimer, I work at Redocly, who make Redoc)

huangapple
  • 本文由 发表于 2023年6月16日 14:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定