@advanced-rest-client/body-editor

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

HTTP body editor for a HTTP request made as a web component

原始使用文档
文档目录10 节
  1. Usage
  2. Installation
  3. body-editor
  4. Example
  5. External dependencies
  6. Monaco dependencies
  7. CodeMirror dependencies
  8. Development
  9. Running the demo locally
  10. Running the tests

Deprecated

This component is deprecated. Use @advanced-rest-client/app instead.


A module containing the UI regions and the logic to render HTTP body editor.

The package contains:

  • body-editor - An element to render fully featured body editor with specialized editors to edit different kind of data
  • body-raw-editor - Monaco editor based body editor.
  • body-multipart-editor - An element that specializes in multipart form data generation
  • body-formdata-editor - A form based input for the www-url-form-encoded type

Published on NPM

tests

Usage

Installation

npm install --save @advanced-rest-client/body-editor

body-editor

The editor produces the value that can be string, File, or FormData. It also produces the model that should be set back on the editor (instead of value) when restoring the previous state. The editor can produce the view model from each supported data types but setting both the value and the model would duplicate the work and the last set value wins.

The view model keeps configuration for each editor (raw/FormData/URL encoded). The value of the editor is the value of currently rendered editor. When the user switches the editors a new value is produced.

Also, see notes for the raw editor to see the list of requirements.

Example

import { LitElement, html } from 'lit-element';
import '@advanced-rest-client/body-editor/body-editor.js';

class SampleElement extends LitElement {
  render() {
    const {body, model} = this;
    const setValue = Array.isArray(model) && model.length ? undefined : body;
    return html`
    <body-editor
      .value="${setValue}"
      .model="${model}"
      @change="${this.valueAndModelHandler}"
      autoEncode
      contentType="application/json"
      selected="raw"
      editorType="Monaco"
    ></body-editor>
    `;
  }
}
customElements.define('sample-element', SampleElement);

The autoEncode property allows to hide encode/decode buttons in the www-url-form-encoded editor and automatically handle value processing. The contentType property is passed down to the raw editor to detect the language to use. The editorType allows to switch between Monaco and CodeMirror editors to enable compatibility with the precious elements.

External dependencies

Both Monaco and CodeMirror editors have external dependencies that has to be included from the outside of the element. You can check the demo folder to see how it is handled in a demo environment.

Monaco dependencies

Include Monaco in the application before running the component. For now the component can't include it directly as Monaco performs an import on CSS files.

Monaco uses web workers to work. These workers has to be in the final build of the application. Set a global MonacoEnvironment property to instruct the Monaco editor how to get the workers. Also, take a look into Monaco's documentation to learn more about build process and plugins for WebPack.

window.MonacoEnvironment = {
  getWorker: (moduleId, label) => {
    let url;
    const prefix = '../node_modules/monaco-editor/esm/vs/';
    const langPrefix = `${prefix}language/`;
    switch (label) {
      case 'json': url = `${langPrefix}json/json.worker.js`; break;
      case 'css': url = `${langPrefix}css/css.worker.js`; break;
      case 'html': url = `${langPrefix}html/html.worker.js`; break;
      case 'javascript':
      case 'typescript': url = `${langPrefix}typescript/ts.worker.js`; break;
      default: url = `${prefix}editor/editor.worker.js`; break;
    }
    return new Worker(url, {
      type: 'module', // <- this uses workers from the `esm` build so the web worker must handle modules properly.
    });
  }
}

CodeMirror dependencies

CodeMirror does not work with ES modules. Libraries expect the global CodeMirror object to be set on the global object. This is the list of files to be loaded into the page before the body editor is initialized:

  • codemirror/lib/codemirror.js
  • jsonlint/lib/jsonlint.js
  • codemirror/addon/mode/loadmode.js
  • codemirror/mode/meta.js
  • codemirror/mode/javascript/javascript.js
  • codemirror/mode/xml/xml.js
  • codemirror/mode/htmlmixed/htmlmixed.js
  • codemirror/addon/lint/lint.js
  • codemirror/addon/lint/json-lint.js
  • codemirror/addon/lint/lint.css - this is css!

Also, the linter required this module to be loaded as an ES module: @advanced-rest-client/code-mirror-linter/code-mirror-linter.js.

Finally set CodeMirror.modeURL property to point to the code mirror modes location.

CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js';

Development

git clone https://github.com/advanced-rest-client/body-editor
cd body-editor
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@advanced-rest-client/body-editor 共发布 20 个版本,首次发布于 2020/11/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.7latest2021/10/215 年前
0.2.52021/08/135 年前
0.2.42021/07/055 年前
0.2.32021/07/045 年前
0.2.22021/07/045 年前
0.2.12021/07/045 年前

关于 @advanced-rest-client/body-editor 的常见问题

@advanced-rest-client/body-editor 是什么?

HTTP body editor for a HTTP request made as a web component

如何安装 @advanced-rest-client/body-editor?

在项目目录运行 npm install @advanced-rest-client/body-editor,或使用 pnpm add / yarn add / bun add。

@advanced-rest-client/body-editor 支持 TypeScript 吗?

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

@advanced-rest-client/body-editor 支持 ESM 还是 CommonJS?

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

@advanced-rest-client/body-editor 可以商用吗?

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

@advanced-rest-client/body-editor 的下载量有多少?

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

@advanced-rest-client/body-editor 还在维护吗?

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