DEV Community

⚫️ nothingness negates itself
⚫️ nothingness negates itself

Posted on

1 2

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay