Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
/ http Public archive

HTTP module for the molecuel framework

License

Notifications You must be signed in to change notification settings

molecuel/http

Repository files navigation

@molecuel/http

NPM version Build Status Coverage percentage

HTTP module for the Molecuel framework

Based on the core modules and it's data functions http implments the automatic creation of HTTP endpoints with koa@2 async functions.

Endpoints can simply be defined in code with decorator functions and gets their url's through the project configuration. This guarantees a high flexibility and adds the option to reuse functions with different url's.

Functions need to be async and tagged as dataRead, dataCreate, dataDelete, dataUpdate or dataReplace. Parameters can be mapped by the mapDataParams function. All these decorators are provided by @molecuel/core to ensure that tagged functions can be reused with different services like FTP or websockets.

A running server is based on one or multiple classes using the decorators and a configuration mapping the urls to the classes.

The javascript part.

import {MlclCore, dataRead, dataCreate, dataUpdate, dataDelete, mapDataParams} from '@molecuel/core';
import {di, injectable} from "@molecuel/di";
import {MlclHttp, MlclHttpCoreRouter, MlclHttpMiddleware, MlclHttpRouter} from "@molecuel/http";

@injectable
class MyCreateTestRoutes {
  @mapDataParams([
      new MlclDataParam("id", "id", "integer", 25),
      new MlclDataParam("large", "size", "boolean"),
  ])
  @dataRead()
  public async dataReadeTest1(id, size) {
    return {
      id,
      size,
    };
  }
  @dataRead()
  public async dataReadeTest2() {
    return {};
  }
  @dataRead("application/rss+xml")
  public async dataReadeTestXml() {
    return "<xml></<xml>";
  }
  @dataCreate()
  public async dataCreateTest() {
    return true;
  }
  @mapDataParams([
    new MlclDataParam("id", "id", "integer", 25),
    new MlclDataParam("body", "postdata", "json"),
    new MlclDataParam("body.property", "child", "string"),
  ])
  @dataUpdate()
  public async dataUpdateTest(id, postdata, child) {
    return true;
  }
  @mapDataParams([
    new MlclDataParam("id", "id", "integer", 25),
  ])
  @dataReplace()
  public async dataReplaceTest(id) {
    return true;
  }
  @mapDataParams([
    new MlclDataParam("id", "id", "integer", 25),
  ])
  @dataDelete()
  public async dataDeleteTest(id) {
    return true;
  }
}
di.bootstrap(MlclCore, MlclHttpMiddleware, MlclHttp);

The configuration should be stored in ./config/development.json (or NODE_ENV) and look like this example:

{
  "http": {
    "security": {

    },
    "routes": [
      {
        "url": "/testread",
        "class": "MyCreateTestRoutes",
        "property": "dataReadeTest2"
      },
      {
        "url": "/testread/:id",
        "class": "MyCreateTestRoutes",
        "property": "dataReadeTest1"
      },
      {
        "url": "/testreadXml",
        "class": "MyCreateTestRoutes",
        "property": "dataReadeTestXml"
      },
      {
        "url": "/testcreate",
        "class": "MyCreateTestRoutes",
        "property": "dataCreateTest"
      },
      {
        "url": "/testupdate/:id",
        "class": "MyCreateTestRoutes",
        "property": "dataUpdateTest"
      },
      {
        "url": "/testreplace/:id",
        "class": "MyCreateTestRoutes",
        "property": "dataReplaceTest"
      },
       {
        "url": "/testdelete/:id",
        "class": "MyCreateTestRoutes",
        "property": "dataDeleteTest"
      }
    ]
  }
}

About

HTTP module for the molecuel framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •