https://github.com/pimatic/decl-api
Declarative definition for REST and real-time APIs
https://github.com/pimatic/decl-api
api rest
Last synced: 3 months ago
JSON representation
Declarative definition for REST and real-time APIs
- Host: GitHub
- URL: https://github.com/pimatic/decl-api
- Owner: pimatic
- License: mit
- Created: 2014-05-29T20:49:10.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2019-03-24T17:32:58.000Z (over 7 years ago)
- Last Synced: 2025-02-20T09:19:27.749Z (over 1 year ago)
- Topics: api, rest
- Language: JavaScript
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
decl-api
========
Declarative API definition for REST and real time APIs.
## API-Definition
An API is defined by a JSON object:
```coffee
declapi = require('../index.js')
t = declapi.types
api = {}
api.todo = {
actions:
listTasks:
rest:
type: "GET"
url: "/api/tasks"
description: "Lists all tasks"
params: {}
result:
tasks:
type: t.array
getTask:
description: "Get a task by id"
rest:
type: "GET"
url: "/api/tasks/:taskId"
params:
taskId:
type: t.string
result:
task:
type: t.object
addTask:
description: "Adds a task"
rest:
type: "POST"
url: "/api/tasks"
params:
taskId:
type: t.string
task:
type: t.object
properties:
description:
type: t.string
done:
type: t.boolean
optional: yes
result:
task:
type: t.object
properties:
description:
type: t.string
done:
type: t.boolean
}
```
## Implementation
Your Controller must implement the declared functions:
```coffee
TodoApp = {
tasks: []
listTasks: -> @tasks
getTask: (taskId) ->
for t in @tasks
if t.id is taskId
return task
return null
addTask: (taskId, task) ->
unless task.done then task.done = no
task.id = taskId
@tasks.push task
return task
}
```
## Binding
### Express
```coffee
app = # Your express app
declapi = env.require 'decl-api'
todoApp = new TodoApp()
declapi.createExpressRestApi(app, api.todo, todoApp)
```
## 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}}