Todo list combining CLI and HATEOAS

dualyticalchemy

⚫️ nothingness negates itself

Posted on October 9, 2020

Todo list combining CLI and HATEOAS

basically:

app.get('/notes', (req, res) => {
  const rawNotesList = fs.readFileSync('../cli/notes.json', {encoding: 'utf8', flag: 'r'});
  const parsedNotesList = JSON.parse(rawNotesList);
  const links = [
    {
      rel: ["self"],
      href: [baseUrl, '/notes'].join('')
    }
  ];

  const actions = [
    {
      class: "add-note",
      href: [baseUrl, '/notes'].join(''),
      method: 'POST',
      fields: [
        {
          name: "command",
          type: "string"
        },
        {
          name: "content",
          type: "string"
        }
      ]
    }
  ];

  const responseWithControlData = {
    class: "note",
    ...parsedNotesList,
    links: links,
    actions: actions

  };

  res.json(responseWithControlData);
});
Enter fullscreen mode Exit fullscreen mode

and:

app.post('/notes', (req, res) => {

  const body = req.body;

  if (!body.command.includes('add')) {
    return res.status(400).json({
      status: 'failed to add content', 
      content: body.content
    });
  }

  const cliCapture = new CliCapture({
    notesPath: './notes.json',
    command: body.command,
    content: body.content
  });

  res.json({
    status: 'added from server'
  });

});
Enter fullscreen mode Exit fullscreen mode

We could analogize this CoAP/CoRE and Amundsen's DARRT approach: anything really can substitute for "CliCapture" and wherever fs is available, we can expand its capabilities with a resource directory mapping to HTTP (https://github.com/JelmerT/coap-rd/blob/master/requesthandlers.js#L82) with transitions:

See https://github.com/nerdfiles/notes-cli/blob/master/cli/server/index.js#L19-L56 and https://github.com/nerdfiles/notes-cli/blob/master/cli/server/index.js#L69-L73

💖 💪 🙅 🚩
dualyticalchemy
⚫️ nothingness negates itself

Posted on October 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related