SwaggerEditor
Table of Contents
Anonymized analytics
Swagger Editor uses Scarf to collect anonymized installation analytics. These analytics help support the maintainers of this library and ONLY run during installation. To opt out, you can set the scarfSettings.enabled field to false in your project's package.json:
// package.json
{
// ...
"scarfSettings": {
"enabled": false
}
// ...
}
Alternatively, you can set the environment variable SCARF_ANALYTICS to false as part of the environment that installs your npm packages, e.g., SCARF_ANALYTICS=false npm install.
Getting started
Prerequisites
These prerequisites are required both for installing SwaggerEditor as a npm package and local development setup.
Installation
Assuming prerequisites are already installed, SwaggerEditor npm package is installable and works with Node.js >= 12.22.0.
You can install SwaggerEditor via npm CLI by running the following command:
$ npm install swagger-editor@alpha
NOTE: when using bundler to build your project which is using swagger-editor@5 npm package,
you might run into following Node.js error: Reached heap limit Allocation failed - JavaScript heap out of memory.
It's caused by significant amount of code that needs to be bundled. This error can be resolved
by extending the Node.js max heap limit: export NODE_OPTIONS="--max_old_space_size=4096".
Usage
Use the package in your application:
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import SwaggerEditor from 'swagger-editor';
import 'swagger-editor/swagger-editor.css';
const url = "https://raw.githubusercontent.com/asyncapi/spec/v2.2.0/examples/streetlights-kafka.yml";
const MyApp = () => (
<div>
<h1>SwaggerEditor Integration</h1>
<SwaggerEditor url={url} />
</div>
);
self.MonacoEnvironment = {
/**
* We're building into the dist/ folder. When application starts on
* URL=https://example.com then SwaggerEditor will look for
* `apidom.worker.js` on https://example.com/dist/apidom.worker.js,
* `editor.worker` on https://example.com/dist/editor.worker.js and
* `asyncapi-parser.worker.js` on https://example.com/dist/asyncapi-parser.worker.js.
*/
baseUrl: `${document.baseURI || location.href}dist/`,
}
ReactDOM.render(<MyApp />, document.getElementById('swagger-editor'));
webpack.config.js (webpack@5)
Install dependencies needed for webpack@5 to properly build SwaggerEditor.
$ npm i stream-browserify --save-dev
$ npm i https-browserify --save-dev
$ npm i stream-http --save-dev
$ npm i util --save-dev
$ npm i buffer --save-dev
$ npm i process --save-dev
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'production',
entry: {
app: './index.js',
'apidom.worker': 'swagger-editor/apidom.worker',
'editor.worker': 'swagger-editor/editor.worker',
'asyncapi-parser.worker': 'swagger-editor/asyncapi-parser.worker',
},
output: {
globalObject: 'self',
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
fallback: {
path: false,
fs: false,
http: require.resolve('stream-http'), // required for asyncapi parser
https: require.resolve('https-browserify'), // required for asyncapi parser
stream: require.resolve('stream-browserify'),
util: require.resolve('util'),
url: require.resolve('url'),
buffer: require.resolve('buffer'),
zlib: false,
},
alias: {
// This alias make sure we don't pull two different versions of monaco-editor
'monaco-editor': '/node_modules/monaco-editor',
// This alias makes sure we're avoiding a runtime error related to this package
'@stoplight/ordered-object-literal$': '/node_modules/@stoplight/ordered-object-literal/src/index.mjs',
'react/jsx-runtime.js': 'react/jsx-runtime',
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: ['process'],
}),
],
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
/**
* The default way in which webpack loads wasm files won’t work in a worker,
* so we will have to disable webpack’s default handling of wasm files and
* then fetch the wasm file by using the file path that we get using file-loader.
*
* Resource: https://pspdfkit.com/blog/2020/webassembly-in-a-web-worker/
*
* Alternatively, WASM file can be bundled directly into JavaScript bundle as data URLs.
* This configuration reduces the complexity of WASM file loading
* but increases the overal bundle size:
*
* {
* test: /\.wasm$/,
* type: 'asset/inline',
* }
*/
{
test: /\.wasm$/,
loader: 'file-loader',
type: 'javascript/auto', // this disables webpacks default handling of wasm
},
]
}
};
Alternative webpack.config.js (webpack@5)
We've already built Web Workers artifacts for you, and they're located inside our npm distribution
package in dist/umd/ directory. To avoid the complexity of building the Web Worker artifacts, you can
use those artifacts directly. This setup will work both for production and development (webpack-dev-server)
and will significantly shorten your build process.
Install copy-webpack-plugin and other needed dependencies.
$ npm i copy-webpack-plugin --save-dev
$ npm i stream-browserify --save-dev
$ npm i https-browserify --save-dev
$ npm i stream-http --save-dev
$ npm i util --save-dev
$ npm i buffer --save-dev
$ npm i process --save-dev