Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on

     

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$cdapi$npminstalltotal4$node index.js
Enter fullscreen modeExit fullscreen mode

Declare your API routes

// in /controllers/api.js// UsersROUTE('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');// ...CreateSchemaandoperations// in /schemas/users.jsNEWSCHEMA('Users',function(schema){// Fields definitionschema.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 workflowschema.addWorkflow('refresh',asyncfunction($){vardata=awaitFUNC.fetchData();if(!data){$.invalid(404);return;}$.callback(data);});// Custom workflows});
Enter fullscreen modeExit fullscreen mode

Call endpoint from external

POST/api/Content-Type:application/json{"schema":"users_query?page=1","data":{}}
Enter fullscreen modeExit fullscreen mode

Output

[]
Enter fullscreen modeExit 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 ...') orROUTE('-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

// UsersROUTE('API  @api   -users_remove/{id}   *Users --> remove');// ...// IMPORTANT: socket routeROUTE('SOCKET / @api');Websocketmessage:{"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 modeExit fullscreen mode

Documentation

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer at Total.js
  • Location
    Ouagadougou, Burkina Faso
  • Work
    Developer
  • Joined

More fromLouis Bertson

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp