@api-components/api-server-selector

中文文档
0.7.2发布于 2021/10/10Apache-2.0ESM + CJS近 30 天下载 3156

A component that renders OAS' servers as a dropdown option to select API server.

原始使用文档
文档目录10 节
  1. Version compatibility
  2. Usage
  3. Installation
  4. In an html file
  5. In a LitElement
  6. Subscribing to change events
  7. Custom servers
  8. Development
  9. Running the demo locally
  10. Running the tests

DEPRECATED

This component is being deprecated. The code base has been moved to amf-components module. This module will be archived when PR 1 is merged.


Custom element that renders a list of servers encoded in an API specification powered by the AMF model.

Published on NPM

Tests and publishing

Version compatibility

This version only works with AMF model version 2 (AMF parser >= 4.0.0).

Usage

Installation

npm install --save @api-components/api-server-selector

In an html file

<html>
  <head>
    <script type="module">
      import '@api-components/api-server-selector/api-server-selector.js';
    </script>
  </head>
  <body>
    <api-server-selector amf="..."></api-server-selector>
  </body>
</html>

In a LitElement

import { LitElement, html } from 'lit-element';
import '@api-components/api-server-selector/api-server-selector.js';

class SampleElement extends LitElement {
  render() {
    return html`
    <api-server-selector .amf="${this.model}"></api-server-selector>
    `;
  }
}
customElements.define('sample-element', SampleElement);

Subscribing to change events

apiserverchanged event

The component dispatches the apiserverchanged custom event when the server selection changes.

element.addEventListener('apiserverchanged', function(e) {
    const { value, type } = e.detail;
    // value is the selected base URI
    // type tells whether it's a server defined value of a custom property
    // This is also the same as const { value, type } = e.target;
});

When Custom URL is selected, a change in the input field dispatches the api-server-changed event with the current value of the input.

Additionally the element supports onapiserverchange setter for event callback function:

element.onapiserverchange = (e) => {
  // ...
};

serverscountchanged event

The event is dispatched when a number of rendered servers changed.

element.addEventListener('serverscountchanged', function(e) {
    const { value } = e.detail;
    // value is the number of rendered servers
});
element.onserverscountchange = (e) => {
  // ...
};

Custom servers

Sometimes the hosting application may want to define additional list of servers to render in the selector, other than the ones defined in the API sepcification. This can relate to additional services working with the components like a proxy or mocking service.

The element accepts list items as children that are insterted after the API defined servers. We recommend using anypoint-item as it is already used to build the selecotr. However, it can be any HTML element. For a list item to be rendered it has to have slot="custom-base-uri" attribute. For the item to be selectable in the list it has to have the value="..." attribute. If the value is missing then the item is rendered but the selector won't react on the list item click.

<api-server-selector>
  <div slot="custom-base-uri">Other options</div>
  <anypoint-item slot="custom-base-uri" data-value="http://mocking.com">Mocking service</anypoint-item>
  <anypoint-item slot="custom-base-uri"><input type="checkbox"/> Configuration option</anypoint-item>
</api-server-selector>

When you decide to use a list item without a value, a click on an item won't close the selector. When the user perform the action related to the item then use opened property on the selector to close the drop down.

<api-server-selector>
  <div slot="custom-base-uri">Other options</div>
  <anypoint-item slot="custom-base-uri"><input type="checkbox" id="option"/> Configuration option</anypoint-item>
</api-server-selector>

<script>
option.onclick = (e) => {
  e.target.parentNode.opened = false;
};
</script>

Development

git clone https://github.com/advanced-rest-client/api-server-selector
cd api-server-selector
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@api-components/api-server-selector 共发布 29 个版本,首次发布于 2020/03/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.7.2latest2021/10/105 年前
0.7.12021/09/265 年前
0.7.02021/09/255 年前
0.6.42021/08/275 年前
0.6.32021/01/145 年前
0.6.22021/01/085 年前

关于 @api-components/api-server-selector 的常见问题

@api-components/api-server-selector 是什么?

A component that renders OAS' servers as a dropdown option to select API server.

如何安装 @api-components/api-server-selector?

在项目目录运行 npm install @api-components/api-server-selector,或使用 pnpm add / yarn add / bun add。

@api-components/api-server-selector 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/api-components__api-server-selector 是否存在。

@api-components/api-server-selector 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@api-components/api-server-selector 可以商用吗?

@api-components/api-server-selector 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@api-components/api-server-selector 的下载量有多少?

根据 npm 官方下载统计,@api-components/api-server-selector 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,156 次,日均约 105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@api-components/api-server-selector 还在维护吗?

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