MCPcopy Index your code
hub / github.com/WebThingsIO/webthing-node / post

Method post

lib/server.ts:497–532  ·  view source on GitHub ↗

* Handle a POST request. * * @param {Object} req The request object * @param {Object} res The response object

(req: express.Request, res: express.Response)

Source from the content-addressed store, hash-verified

495 * @param {Object} res The response object
496 */
497 post(req: express.Request, res: express.Response): void {
498 const thing = this.getThing(req);
499 if (thing === null) {
500 res.status(404).end();
501 return;
502 }
503
504 const actionName = req.params.actionName;
505
506 const keys = Object.keys(req.body);
507 if (keys.length !== 1) {
508 res.status(400).end();
509 return;
510 }
511
512 if (keys[0] !== actionName) {
513 res.status(400).end();
514 return;
515 }
516
517 let input = null;
518 if (req.body[actionName].hasOwnProperty('input')) {
519 input = req.body[actionName].input;
520 }
521
522 const action = thing.performAction(actionName, input);
523 if (action) {
524 const response = action.asActionDescription();
525 action.start();
526
527 res.status(201);
528 res.json(response);
529 } else {
530 res.status(400).end();
531 }
532 }
533}
534
535/**

Callers 1

constructorMethod · 0.95

Calls 4

asActionDescriptionMethod · 0.80
getThingMethod · 0.45
performActionMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected