https://github.com/gastonpereyra/vercel-serverless-api
A handler for Serverless Function in Vercel to develop API
https://github.com/gastonpereyra/vercel-serverless-api
api node npm package serverless vercel
Last synced: 7 days ago
JSON representation
A handler for Serverless Function in Vercel to develop API
- Host: GitHub
- URL: https://github.com/gastonpereyra/vercel-serverless-api
- Owner: gastonpereyra
- License: mit
- Created: 2021-09-11T20:42:35.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-18T15:22:53.000Z (about 4 years ago)
- Last Synced: 2025-07-26T05:39:02.073Z (3 months ago)
- Topics: api, node, npm, package, serverless, vercel
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/vercel-serverless-api
- Size: 61.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Vercel-Serverless-Api
## Code Quality Status

[](https://coveralls.io/r/gastonpereyra/vercel-serverless-api?branch=master)

## Description
A handler for Serverless Function in Vercel to develop API
## Installation
```
npm i vercel-serverless-api
```
## API
Its a Class to help to create an API.
### Getters
#### Ids
* `pathIds`: _object_, Query Parameters or Path-Parameters for ids
* Example: `https://example.verce.app/api/message?pathIds.id=10`
* `pathIds.id`: '10'
* Example: `https://example.verce.app/api/message/?pathIds.emailId=11`
* `pathIds.emailId`: '11'
#### Body
* `data`: _object_, Body
#### Queries
* `filters`: _object_, Query parameters to filter
* Example `https://example.verce.app/api/message?name=John&filters.age=10`
* `filters.name`: 'John'
* `filters.age`: '10'
* `this.sort`: _object_, Query parameters to sort
* Example: `https://example.verce.app/api/message?sortBy=name&sortDirection=desc`
* `sort.by`: 'name'
* `sort.direction`: 'desc'
* `query`: _object_, Query parameters
* Example: `https://example.verce.app/api/message?other.foo=name`
* `query.foo`: 'name'
#### Request
Other request data
* `request`
* `request.url`: Request URL
* `request.method`: Request REST Method
* `request.headers`: Request Headers
* `request.cookies`: Request cookies
### Methods
* `setCode(code)`: To setup a custom response status-code
* `code`: _number_
* `setHeader(header, value)`: To setup a custom response header.
* `header`: _string_
* `value`: _string_ or _number_ or _boolean_
* `setBody(body)`: To setup a custom response body. If you do not set a custom Content-type, this will be `application/json`
* `body`: _object_ (for JSON) or _any_ (for other content-type)
* `validate`: For validation. If you throw an error, will setup status-code 400 by default
* `async`
* `process`: The API itself. If you throw an error, will setup status-code 500 by default
* `async`
### Structure Validation
Can use [`superstruct@0.7.0`](https://github.com/ianstormtaylor/superstruct/tree/v0.7.0) to validate body, ids, filters, sort, only must rewrite the following method
* `idsStruct`
* `static`
* `bodyStruct`
* `static`
* `filtersStruct`
* `static`
* `sortStruct`
* `static`
* `queryStruct`
* `static`
### Usage
```js
const { struct } = require('superstruct'); // only works up to 0.7.0 version
const { API } = require('vercel-serverless-api');
module.exports = class MyApi extends API {
static get idsStruct() {
return struct({
id: 'string'
});
}
static get bodyStruct() {
return struct({
name: 'string',
age: 'string?'
});
}
static get filtersStruct() {
return struct({
name: 'string|null?',
age: 'string|null?'
});
}
static get sortStruct() {
return struct({
by: 'string?',
direction: 'string?'
});
}
validate() {
if(this.data.age < 10)
throw new Error('Too Young'); // statusCode will be 400
}
process() {
if(!this.data.name)
throw new Error('Empty String is not valid'); // statusCode will be 500
if(this.data.image) {
this.setHeader('Content-Type', 'text/plain') // Set a Custom Content-Type
.setHeader('X-Custom', 100) // Set a Custom Header
.setBody('
Secret
') // Because setting a Custom Type will Response a Plain Text
}
this.setCode(201).setBody({
name: this.data.name
lastname: 'Stark',
age: this.data.age + 1
});
}
}
```
## Handler
The API Class and Handler can be combined to help to devolope Serverless Function in Vercel
```js
// in ./api/message/post.js
const { hanlder } = require('vercel-serverless-api');
const MyApi = require('./my-api');
module.exports = (..args) => handler(MyApi, ...args);
```