decl-api
Declarative API definition for REST and real time APIs.
API-Definition
An API is defined by a JSON object:
declapi = require'../index.js't = declapitypes api = api.todo = actions: listTasks: rest: type: "GET" url: "/api/tasks" description: "Lists all tasks" params: result: tasks: type: tarray getTask: description: "Get a task by id" rest: type: "GET" url: "/api/tasks/:taskId" params: taskId: type: tstring result: task: type: tobject addTask: description: "Adds a task" rest: type: "POST" url: "/api/tasks" params: taskId: type: tstring task: type: tobject properties: description: type: tstring done: type: tboolean optional: yes result: task: type: tobject properties: description: type: tstring done: type: tboolean
Implementation
Your Controller must implement the declared functions:
TodoApp = tasks: : -> @tasks : for t in @tasks if tid is taskId return task return null : unless taskdone then task.done = no task.id = taskId @taskspush task return task
Binding
Express
app = # Your express app declapi = envrequire 'decl-api'todoApp = declapicreateExpressRestApiappapitodotodoApp
REST-API
listTasks
Returns the task list as JSON object
GET /api/tasks
RESPONSE {success: true, tasks: [...]}
addTask
Creates a task with the id: someId and returns it as JSON object
POST /api/tasks/someId
task[description]="some description"
task[done]=false
RESPONSE
{success: true, task: {id: "someId", description: "some description", done: false}}
getTask
Returns the task with id == someId
GET /api/tasks/someId
RESPONSE
{success: true, task: {id: "someId", description: "some description", done: false}}