npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@advanced-rest-client/body-editor

v0.2.7

Published

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

Downloads

1,765

Readme

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