{"id":15703837,"url":"https://github.com/danybeltran/serverless-request-validator","last_synced_at":"2025-05-12T16:27:42.197Z","repository":{"id":110079365,"uuid":"338654908","full_name":"danybeltran/serverless-request-validator","owner":"danybeltran","description":"Validate requests made to a Vercel serverless application in an Express-like way","archived":false,"fork":false,"pushed_at":"2021-02-16T07:43:21.000Z","size":21,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T22:17:16.503Z","etag":null,"topics":["api","api-validator","backend","serveless"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/serverless-request-validator","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/danybeltran.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-13T19:41:52.000Z","updated_at":"2023-05-04T11:54:04.000Z","dependencies_parsed_at":"2023-06-11T00:15:17.801Z","dependency_job_id":null,"html_url":"https://github.com/danybeltran/serverless-request-validator","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"033b7e08d799e8ddd3eb572a42e3cc7a7afb263f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fserverless-request-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fserverless-request-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fserverless-request-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danybeltran%2Fserverless-request-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danybeltran","download_url":"https://codeload.github.com/danybeltran/serverless-request-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253775653,"owners_count":21962387,"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","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","api-validator","backend","serveless"],"created_at":"2024-10-03T20:06:54.140Z","updated_at":"2025-05-12T16:27:42.176Z","avatar_url":"https://github.com/danybeltran.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serverless request validator\n\nThis is made to be used in Vercel serverless applications, it can be used with Javascript and Typescript.\n\nTo install it:\n\n```sh\nnpm install serverless-request-validator\n```\nor\n```\nyarn add serverless-request-validator\n```\n\n## Using the library\n\nIf you are using Javascript:\n\n```js\nconst Validate = require(\"serverless-request-validator\");\n```\n\nWith Typescript:\n\n```ts\nimport Validate from \"serverless-request-validator\";\n```\n\n\u003cbr/\u003e\n\n#### Handling one-method-only requests\n\nIf an endpoint in your app should only accept one HTTP method, use the property named as the only method allowed.\nFor example:\n\n\n**Javascript**\n\n```js\n// /api/index.js\nconst Validate = require(\"serverless-request-validator\")\n\n// Only HTTP `GET` is allowed\nmodule.exports = Validator.get((req, res)=\u003e{\n    res.send(\"get request\")\n})\n\n```\n\n**Typescript**\n\n```ts\n// /api/index.ts\nimport Validate from \"serverless-request-validator\";\n\n// Only HTTP `GET` is allowed\nexport default Validate.get((req, res)=\u003e{\n    res.send(\"get request\")\n})\n\n```\n\n\u003e The default export means that the application endpoint will only allow a request using the `GET` method.\n\u003e If a request is made using a different method, it will respond with a `405` (method not allowed) status code, and saying the method can't be used in that url (like in Express)\n\n\n#### Handling multi-method requests\n\nThis makes it possible for an endpoint to handle requests of different methods. Very similar to the previous example:\n\n**Javascript**\n\n```js\n// /api/index.js\nconst Validate = require(\"serverless-request-validator\")\n\n// GET, POST and PUT are allowed.\nmodule.exports = Validate({\n  get(req, res) {\n    res.send(\"a get request\");\n  },\n  post(req, res) {\n    res.send(`A ${req.method} request`);\n  },\n  put(req, res) {\n    res.send(\"a put request\");\n  },\n})\n\n```\n\n**Typescript**\n\n```ts\n// /api/index.ts\nimport Validate from \"serverless-request-validator\";\n\n// GET, POST and PUT are allowed\nexport default Validate({\n  get(req, res) {\n    res.send(\"Hello\");\n  },\n  post(req, res) {\n    res.send(`A ${req.method} request`);\n  },\n  put(req, res) {\n    res.send(\"a PUT request\");\n  },\n})\n\n```\n\n\u003e In this case, this function will handle `GET`,`POST` and `PUT` methods sent to it.\n\u003e As in the previous example, other methods different that the allowed ones will send a `405` status code and the message saying the method can't be used in that url.\n\n#### Sending files\n\nIt is possible to use the `sendFile()` method available\n\n```ts\n// /api/index.ts\nimport Validate from \"serverless-request-validator\";\nimport path from \"path\"\n\nexport default Validate.get((req,res)=\u003e{\n  res.sendFile(path.resolve(__dirname,\"../src/cat.png\"))\n})\n```\n\u003e If a file doesn't exist, a 404 status code will be sent to the user\n\n\nThat's basically it, hopefuly it makes it easier to have different reponses for different methods=)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanybeltran%2Fserverless-request-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanybeltran%2Fserverless-request-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanybeltran%2Fserverless-request-validator/lists"}