DEV Community

Cover image for Total.js API routing: The game changer
Louis Bertson
Louis Bertson

Posted on

1

Total.js API routing: The game changer

API routing is Total.js internal mechanism used to expose API endpoints in a very simple way. The mechanism is designed to focus on handling a maximum number of internal operations with a minimum number of external endpoints. Therefore API routing only works with Total.js Schema and their operations.

How does it work?

You can create one endpoint (e.g /api/) and use it to target an infinite number of operations within your application. All you have to do is call that endpoint with the POST method and have in the body of the request, a JSON payload to specify what operation of what schema to target.

Example :

Download empty project

$ git clone https://github.com/totaljs/emptyproject-restservice api
$ cd api
$ npm install total4
$ node index.js
Enter fullscreen mode Exit fullscreen mode

Declare your API routes

// in /controllers/api.js


// Users
ROUTE('API  /api/   -users_query            *Users    --> query');
ROUTE('API  /api/   -users_read/{id}        *Users    --> read');
ROUTE('API  /api/   +users_insert           *Users    --> check insert (response)');
ROUTE('API  /api/   +users_update/{id}      *Users    --> update');
ROUTE('API  /api/   -users_remove/{id}      *Users    --> remove');
ROUTE('API  /api/   -users_refresh          *Users    --> refresh');

// ...
Create Schema and operations


// in /schemas/users.js

NEWSCHEMA('Users', function(schema) {

    // Fields definition

    schema.setQuery(function($) { 
        $.callback([]);
    });

    schema.setRead(function($) { 
        $.callback({}); 
    });

    schema.setInsert(function($, model) { 
        $.done(model.id) 
    });

    schema.setUpdate(function($, model) { 
        $.done(model.id) 
    });

    schema.setRemove(function($) { 
        $.success();
    });

    // Async/Await workflow

    schema.addWorkflow('refresh', async function ($) {
        var data = await FUNC.fetchData();

        if (!data) {
            $.invalid(404);
            return;
        }

        $.callback(data);

    });
    // Custom workflows
});
Enter fullscreen mode Exit fullscreen mode

Call endpoint from external

POST /api/
Content-Type: application/json

{
    "schema": "users_query?page=1",
    "data": {}
}
Enter fullscreen mode Exit fullscreen mode

Output

[]
Enter fullscreen mode Exit fullscreen mode

Required The request body is in JSON format and expects two parameters:

schema ---> (schema_name/{dynamic_arg_1}/{dynamic_arg_2}?query=arguments) to specify schema (supports params and query).
data ---> data payload (optional).

Good to know

The routing can contain two times +/- characters.
First : ROUTE('+API ...') or ROUTE('-API ...') for authorization purpose. (Read more here).
Second: +schema_name or -schema_name to specify if data reception is expected or not.

Websocket API

Yes. The API Routing supports the WebSocket protocol and we recommend it too. Its declaration is the same as the standard API Routing, except that the route must contain identifiers in the form @YOUR_NAME instead of the relative URL address. Let us try it:

Declare your Websocket API routes


// Users
ROUTE('API  @api   -users_remove/{id}   *Users --> remove');

// ...

// IMPORTANT: socket route
ROUTE('SOCKET / @api');
Websocket message:

{
    "TYPE": "api",
    "callbackid": "CUSTOM_CALLBACK_ID", // The value will be returned back
    "data": {
        "schema": "schema_name/{dynamic_arg_1}/{dynamic_arg_2}?query=arguments",
        "data": {}
    }
}
Enter fullscreen mode Exit fullscreen mode

Documentation

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay