{"id":15184756,"url":"https://github.com/gastonpereyra/vercel-serverless-api","last_synced_at":"2026-02-22T21:43:59.499Z","repository":{"id":46992311,"uuid":"405479006","full_name":"gastonpereyra/vercel-serverless-api","owner":"gastonpereyra","description":"A handler for Serverless Function in Vercel to develop API","archived":false,"fork":false,"pushed_at":"2021-09-18T15:22:53.000Z","size":63,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-26T05:39:02.073Z","etag":null,"topics":["api","node","npm","package","serverless","vercel"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/vercel-serverless-api","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gastonpereyra.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-11T20:42:35.000Z","updated_at":"2021-10-10T18:09:55.000Z","dependencies_parsed_at":"2022-09-26T21:40:38.477Z","dependency_job_id":null,"html_url":"https://github.com/gastonpereyra/vercel-serverless-api","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"gastonpereyra/npm-template","purl":"pkg:github/gastonpereyra/vercel-serverless-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gastonpereyra","download_url":"https://codeload.github.com/gastonpereyra/vercel-serverless-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271442002,"owners_count":24760351,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","node","npm","package","serverless","vercel"],"created_at":"2024-09-27T17:23:12.260Z","updated_at":"2025-10-30T04:03:53.187Z","avatar_url":"https://github.com/gastonpereyra.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Vercel-Serverless-Api\n\n## Code Quality Status\n![Build Status](https://github.com/gastonpereyra/vercel-serverless-api/workflows/Build%20Status/badge.svg)\n[![Coverage Status](https://img.shields.io/coveralls/github/gastonpereyra/vercel-serverless-api/master.svg)](https://coveralls.io/r/gastonpereyra/vercel-serverless-api?branch=master)\n\n![npm-vercel-serverless-api](https://user-images.githubusercontent.com/39351850/132961710-27605cee-2e42-410a-bc08-cbf091094873.png)\n\n## Description\nA handler for Serverless Function in Vercel to develop API\n\n## Installation\n\n```\nnpm i vercel-serverless-api\n```\n\n## API\n\nIts a Class to help to create an API.\n\n### Getters\n\n#### Ids\n* `pathIds`: _object_, Query Parameters or Path-Parameters for ids\n    * Example: `https://example.verce.app/api/message?pathIds.id=10`\n        * `pathIds.id`: '10'\n    * Example: `https://example.verce.app/api/message/?pathIds.emailId=11`\n        * `pathIds.emailId`: '11'\n\n#### Body\n* `data`: _object_, Body\n\n#### Queries\n* `filters`: _object_, Query parameters to filter\n    * Example `https://example.verce.app/api/message?name=John\u0026filters.age=10`\n        * `filters.name`: 'John'\n        * `filters.age`: '10'\n* `this.sort`: _object_, Query parameters to sort\n    * Example: `https://example.verce.app/api/message?sortBy=name\u0026sortDirection=desc`\n        * `sort.by`: 'name'\n        * `sort.direction`: 'desc'\n\n* `query`: _object_, Query parameters\n    * Example: `https://example.verce.app/api/message?other.foo=name`\n        * `query.foo`: 'name'\n\n#### Request\n\nOther request data\n\n* `request`\n    * `request.url`: Request URL\n    * `request.method`: Request REST Method\n    * `request.headers`: Request Headers\n    * `request.cookies`: Request cookies\n\n### Methods\n\n* `setCode(code)`: To setup a custom response status-code\n    * `code`: _number_\n\n* `setHeader(header, value)`: To setup a custom response header.\n    * `header`: _string_\n    * `value`: _string_ or _number_ or _boolean_\n\n* `setBody(body)`: To setup a custom response body. If you do not set a custom Content-type, this will be `application/json`\n    * `body`: _object_ (for JSON) or _any_ (for other content-type)\n\n* `validate`: For validation. If you throw an error, will setup status-code 400 by default\n    * `async`\n\n* `process`: The API itself. If you throw an error, will setup status-code 500 by default\n    * `async`\n\n### Structure Validation\n\nCan 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\n\n* `idsStruct`\n    * `static`\n* `bodyStruct`\n    * `static`\n* `filtersStruct`\n    * `static`\n* `sortStruct`\n    * `static`\n* `queryStruct`\n    * `static`\n\n### Usage\n\n```js\nconst { struct } = require('superstruct'); // only works up to 0.7.0 version\nconst { API } = require('vercel-serverless-api');\n\nmodule.exports = class MyApi extends API {\n\n    static get idsStruct() {\n        return struct({\n            id: 'string'\n        });\n    }\n\n    static get bodyStruct() {\n        return struct({\n            name: 'string',\n            age: 'string?'\n        });\n    }\n\n    static get filtersStruct() {\n        return struct({\n            name: 'string|null?',\n            age: 'string|null?'\n        });\n    }\n\n    static get sortStruct() {\n        return struct({\n            by: 'string?',\n            direction: 'string?'\n        });\n    }\n\n    validate() {\n\n        if(this.data.age \u003c 10)\n            throw new Error('Too Young'); // statusCode will be 400\n    }\n\n    process() {\n\n        if(!this.data.name)\n            throw new Error('Empty String is not valid'); // statusCode will be 500\n\n        if(this.data.image) {\n            this.setHeader('Content-Type', 'text/plain') // Set a Custom Content-Type\n                .setHeader('X-Custom', 100) // Set a Custom Header\n                .setBody('\u003ch1\u003eSecret\u003c/h1\u003e') // Because setting a Custom Type will Response a Plain Text\n        }\n\n        this.setCode(201).setBody({\n            name: this.data.name\n            lastname: 'Stark',\n            age: this.data.age + 1\n        });\n    }\n}\n\n```\n\n## Handler\n\nThe API Class and Handler can be combined to help to devolope Serverless Function in Vercel\n\n```js\n// in ./api/message/post.js\nconst { hanlder } = require('vercel-serverless-api');\n\nconst MyApi = require('./my-api');\n\nmodule.exports = (..args) =\u003e handler(MyApi, ...args);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api/lists"}