FaaS agnostic restful microframework
npm install @cloudifyjs/restful
!npm
!npm





The @cloudifyjs/restful is a microframework that aims to make the development of RESTful Web Services on top of FaaS free from cloud providers implementations. Develop first and choose the cloud provider later!
Install with npm:
```
npm install @cloudifyjs/restful --save
Install with yarn:
``
yarn add @cloudifyjs/restful
* Provides cloud agnostic wrappers for RESTful Web Services.
* Built-in request handlers (aws and google).
* Minimal configuration.
* Supports HATEOAS principle.
* Supports request validation with @hapijs/joi.
* Supports RESTful principle.
* Extensible: Plug you own hypster cloud provider as you need.
* async/await oriented / No Callback Hell.
> From restfulapi.net: A document resource is a singular concept that is akin to an object instance or database record. In REST, you can view it as a single resource inside resource collection. A document’s state representation typically includes both fields with values and links to other related resources.
`javascript
const api = require('@cloudifyjs/restful').api
// GET /cars/{id}
module.exports.get = api.document({
target: async (request) => ({
id: 1,
name: 'Model S',
vendor: 'Tesla'
})
})
``
Response:json
HTTP 200
Content-Type: application/json
{
"id": 1,
"name": "Model S",
"vendor": "Tesla"
}
`
Wow! The above example demostrates how to response a document by wrappping a business function that returns a simple JSON.
> From restfulapi.net: A collection resource is a server-managed directory of resources. Clients may propose new resources to be added to a collection. However, it is up to the collection to choose to create a new resource or not. A collection resource chooses what it wants to contain and also decides the URIs of each contained resource.
`javascript
const api = require('@cloudifyjs/restful').api
// GET /cars
module.exports.list = api.collection({
target: async (request) => ([
{
id: 1,
name: 'Model S',
vendor: 'Tesla'
},
{
id: 2,
name: 'Model 3',
vendor: 'Tesla'
}
])
})
``
Response:json
HTTP 200
Content-Type: application/json
[
{
"id": 1,
"name": "Model S",
"vendor": "Tesla"
},
{
"id": 2,
"name": "Model 3",
"vendor": "Tesla"
}
]
`
The @cloudifyjs/restful provides the methods api.document and api.collection to exposes your RESTful endpoints. These methods handle requests received from the cloud provider (like aws, google, etc.) and deliver to the target function a normalized request object.
javascript
const Joi = require('@hapi/joi')
const api = require('@cloudifyjs/restful').apimodule.exports.get = api.document({
validators: {
pathParameters: Joi.object({
id: Joi.string().required()
})
},
target: async (request) => {
return {
text: 'My task',
checked: true
}
}
})
`$3
-
consumes (Optional): list of allowed values in Content-Type header, current only supports JSON formats (e.g. application/x-javascript, text/x-json). Default: application/json
- vendor (Optional): When specified, will not try automatically detect the cloud environment, currently the build-in vendors are aws and google.
- decorator (Optional): used to provide custom response modification before serialize the target function response.
- links