Create/edit OpenAPI version of Web Connected Lamp (Example 2) from Mozilla WoT
GitHub Page : phyunsj.github.io/web-thing-swagger-page Only GET
method is permitted.
$ git clone https://github.com/swagger-api/swagger-editor.git
$ cd swagger-editor
$ npm install
$ npm run build
$ npm start -> open the browser : https://localhost:3001
nodes/core/io/21-httpin.js
: consolidate all http nodes into a single node usingall()
routing method fromexpress
(RED.httpNode
is an instance ofexpress
) instead of creating mutiple http nodes.
function HTTPIn(n) {
...
this.callback = function(req,res) {
var msgid = RED.util.generateId();
res._msgid = msgid;
if (node.method.match(/^(post|delete|put|options|patch)$/)) {
node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.body});
} else if (node.method == "get") {
node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.query});
+ } else if (node.method == "all") {
+ if ( req.method.match(/^(POST|DELETE|PUT|OPTIONS|PATCH)$/)) {
+ node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.body});
+ } else if (req.method == "GET") {
+ node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.query});
+ }
} else {
node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)});
}
};
...
if (this.method == "get") {
...
+ } else if (this.method == "all") {
+ RED.httpNode.all(this.url,cookieParser(),...,this.callback,this.errorHandler);
+ }
...
}
nodes/core/io/21-httpin.html
: add ALL option.
<div class="form-row">
<label for="node-input-method"><i class="fa fa-tasks"></i>
<span data-i18n="httpin.label.method"></span></label>
<select type="text" id="node-input-method" style="width:70%;">
<option value="get">GET</option>
<option value="post">POST</option>
<option value="put">PUT</option>
<option value="delete">DELETE</option>
<option value="patch">PATCH</option>
+ <option value="all">ALL</option>
</select>
</div>
set URL as {basePath}/*
with ALL option. basePath is/things/lamp/v1
.
RED.httpNode.all( "/things/lamp/v1/*" /* <- this.url */ , ..., handler);
/*
/things/lamp/v1 <- will be ignored
The following URL will be accepted.
/things/lamp/v1/
/things/lamp/v1/actions
/things/lamp/v1/properties
*/
nodes/core/core/80-function.js
: allow to usenedb
module directly infunction
node. access$HOME\actions.db
and$HOME\events.db
to generate GET/POST responses. This is not an ideal approach but it is good enough to test REST APIs.
function FunctionNode(n) {
...
var sandbox = {
console:console,
util:util,
Buffer:Buffer,
Date: Date,
RED: {
util: RED.util
},
+ require : require,
...
}
Example : function node "55d769c4.468388"
const homeDir = require('os').homedir();
var Datastore = require('nedb');
var db = new Datastore(homeDir+'/actions.db');
db.loadDatabase();
// convert "pending" to "complete" for msg.payload ( <- transacition-id)
db.update({ href: msg.payload }, { $set: { status: 'complete',timeCompleted : dateStr+' '+timeStr } }, { }, function (err, numReplaced) {
// Do something if any
});
- create a repository
- generate
wot.yaml
- index.html
const editor = SwaggerEditorBundle({
+ url: 'wot.yaml',
dom_id: '#swagger-editor',
layout: 'StandaloneLayout',
presets: [
SwaggerEditorStandalonePreset
]
})
- upload
index.html
&wot.yaml
&dist/*
- GitHub Repository Settings
- visit
https://<your-name>.github.io/<repository-name>
- Mozilla WebThing API Specification
- Documenting APIs: A guide for technical writers and engineers
- Swagger
- swagger-editor
- AsyncAPI - Messsage Drive API
- Run Chrome browser without CORS : disable CORS if you wish to run swagger-editor & Node-RED server on the same machine to prevent fetch error.
- nedb : JSON database