{"id":18458493,"url":"https://github.com/appgeist/restful-next-api","last_synced_at":"2025-10-25T14:10:03.507Z","repository":{"id":45549796,"uuid":"196744984","full_name":"appgeist/restful-next-api","owner":"appgeist","description":"Build restful API methods for Next.js \u003e 9 and validate the incoming requests with yup","archived":false,"fork":false,"pushed_at":"2023-01-06T02:05:08.000Z","size":1890,"stargazers_count":7,"open_issues_count":13,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-21T01:23:10.824Z","etag":null,"topics":["api","api-helper","api-server","nextjs","nodejs","rest","rest-api","rest-api-framework","rest-api-helper","validation","validation-engine","validation-library","yup"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/appgeist.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}},"created_at":"2019-07-13T16:48:51.000Z","updated_at":"2023-05-12T17:01:37.000Z","dependencies_parsed_at":"2023-02-05T02:15:54.689Z","dependency_job_id":null,"html_url":"https://github.com/appgeist/restful-next-api","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/appgeist/restful-next-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appgeist%2Frestful-next-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appgeist%2Frestful-next-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appgeist%2Frestful-next-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appgeist%2Frestful-next-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appgeist","download_url":"https://codeload.github.com/appgeist/restful-next-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appgeist%2Frestful-next-api/sbom","scorecard":{"id":203542,"data":{"date":"2025-08-11","repo":{"name":"github.com/appgeist/restful-next-api","commit":"e48d2215fd9dbd0e0c00dd829b973b67e415c473"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.9,"checks":[{"name":"Code-Review","score":1,"reason":"Found 2/20 approved changesets -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: ISC License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 12 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"72 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-6chw-6frg-f759","Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw","Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5","Warn: Project is vulnerable to: GHSA-257v-vj4p-3w2h","Warn: Project is vulnerable to: GHSA-7gc6-qh9x-w6h8","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w","Warn: Project is vulnerable to: GHSA-434g-2637-qmqr","Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m","Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw","Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p","Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747","Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh","Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h","Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3","Warn: Project is vulnerable to: MAL-2023-462","Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37","Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr","Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp","Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq","Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488","Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-x56p-c8cg-q435","Warn: Project is vulnerable to: GHSA-vxf5-wxwp-m7g9","Warn: Project is vulnerable to: GHSA-25mp-g6fv-mqxx","Warn: Project is vulnerable to: GHSA-c59h-r6p8-q9wc","Warn: Project is vulnerable to: GHSA-qpjv-v59x-3qc4","Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r","Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g","Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr","Warn: Project is vulnerable to: GHSA-cwx2-736x-mf6w","Warn: Project is vulnerable to: GHSA-v39p-96qg-c8rf","Warn: Project is vulnerable to: GHSA-8v63-cqqc-6r2c","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-h7cp-r72f-jxh6","Warn: Project is vulnerable to: GHSA-v62p-rq8g-8h59","Warn: Project is vulnerable to: GHSA-566m-qj78-rww5","Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3","Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j","Warn: Project is vulnerable to: GHSA-6fw4-hr69-g3rv","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94","Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7","Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq","Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9","Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw","Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-16T23:14:26.175Z","repository_id":45549796,"created_at":"2025-08-16T23:14:26.175Z","updated_at":"2025-08-16T23:14:26.175Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280965092,"owners_count":26421559,"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-10-25T02:00:06.499Z","response_time":81,"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","api-helper","api-server","nextjs","nodejs","rest","rest-api","rest-api-framework","rest-api-helper","validation","validation-engine","validation-library","yup"],"created_at":"2024-11-06T08:18:51.349Z","updated_at":"2025-10-25T14:10:03.471Z","avatar_url":"https://github.com/appgeist.png","language":"JavaScript","readme":"# @appgeist/restful-next-api\n\n[![NPM version][npm-image]][npm-url]\n[![License][license-image]][license-url]\n\n![AppGeist Restful Next.js API](https://user-images.githubusercontent.com/581999/61737683-6782cf80-ad91-11e9-8c7d-703b9f6c7b9b.png)\n\nBuild restful [API methods for Next.js \u003e 9](https://nextjs.org/docs#api-routes) and validate the incoming requests with [yup](https://www.npmjs.com/package/yup).\n\n## Why\n\n[Next.js brought API routes support in v9](https://nextjs.org/docs#api-routes), but you have to provide your own implementation of handling different rest methods (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`). This helper enables you to clearly structure your method handling and validation.\n\n## Installation\n\n- Install with `npm i @appgeist/restful-next-api` or `yarn add @appgeist/restful-next-api`;\n- Run `npx install-peerdeps -do @appgeist/restful-next-api` to make sure you have the necessary `peerDependencies` (`yup` and of course `next`) in your project.\n\n## Usage example\n\nIn `/pages/api/products.js`:\n\n```js\nimport { object, number, string } from 'yup';\nimport methods from '@appgeist/restful-next-api';\nimport { Product, User } from '~/models';\nimport { log } from '~/utils';\n\nexport default methods({\n  get: ({ query: { page } }) =\u003e Product.browse({ page }),\n\n  post: {\n    bodySchema: object({\n      name: string()\n        .min(5)\n        .max(20)\n        .required(),\n      description: string()\n        .min(5)\n        .max(1000),\n      price: number()\n        .positive()\n        .max(9999)\n        .required(),\n      inventoryItems: number()\n        .integer()\n        .positive()\n        .max(999)\n        .required()\n    }).noUnknown(),\n\n    onRequest: async ({ body, req }) =\u003e {\n      const product = await Product.create(body);\n      await log(`Product ${product.id} created at ${new Date()} by user ${req.userId}`);\n      return product;\n    }\n  }\n});\n```\n\nIn `/pages/api/products/[id].js`:\n\n```js\nimport { object, number, string } from 'yup';\nimport { FORBIDDEN } from 'http-status-codes';\nimport methods, { ApiError } from '@appgeist/restful-next-api';\nimport { Product } from '~/models';\nimport { log } from '~/utilities';\n\nexport default methods({\n  patch: {\n    querySchema: {\n      id: number()\n        .integer()\n        .positive()\n        .required()\n    },\n\n    bodySchema: object({\n      name: string()\n        .min(5)\n        .max(20)\n        .required(),\n      description: string()\n        .min(5)\n        .max(1000),\n      price: number()\n        .positive()\n        .max(9999)\n        .required(),\n      inventoryItems: number()\n        .integer()\n        .positive()\n        .max(999)\n        .required()\n    }).noUnknown(),\n\n    onRequest: async ({ body, req }) =\u003e {\n      const product = await Product.create(body);\n      await log(`Product ${product.id} updated at ${new Date()} by user ${req.userId}`);\n      return product;\n    }\n  },\n\n  delete: {\n    querySchema: {\n      id: number()\n        .integer()\n        .positive()\n        .required()\n    },\n\n    onRequest: async ({ query: { id }, req }) =\u003e {\n      const { userId } = req;\n      const acl = await User.getACL(userId);\n      if (!acl.includes('deleteProduct')) throw new ApiError(FORBIDDEN);\n      await Product.destroy(id);\n      await log(`Product ${id} deleted at ${new Date()} by user ${userId}`);\n    }\n  }\n});\n```\n\nEach method can be:\n\n- a request handler function (see details below)\n- an object shaped like so: `{ querySchema, bodySchema, handler, errorHandler }`.\n\nA `querySchema`/`bodySchema` definition can be:\n\n- a simple JS object for brevity (the object will be converted automatically to a yup schema)\n- a yup schema (for complex scenarios when you need to add a `.noUnknown()` modifier)\n\n## Request flow\n\n1. For each request, the `beforeRequest` handler is invoked if present:\n\n   ```js\n   import methods from '@appgeist/restful-next-api';\n\n   export default methods({\n     get: {\n       beforeRequest: () =\u003e {\n         console.log('Before GET');\n       },\n       onRequest: () =\u003e {\n         console.log('On GET request');\n       }\n     },\n\n     delete: () =\u003e {\n       console.log('On DELETE request');\n     },\n\n     beforeRequest: () =\u003e {\n       // ...\n       console.log('Before REQUEST');\n       // ...\n     }\n   });\n   ```\n\n2. If `beforeRequest` completes without throwing an error, the data for each request is validated (and transformed) according to the specified `querySchema` and `bodySchema` definitions. See `yup` [readme](https://github.com/jquense/yup) for more information on data validation and transformation.\n\n   - **If validation fails, the request handler invocation is skipped** and a `400` (`BAD_REQUEST`) response is sent to the client with a `JSON` body type structured like so:\n\n   ```json\n   {\n     \"message\": \"There were 2 validation errors\",\n     \"errors\": [\"body.price must be an integer\", \"body.inventoryItems is required\"]\n   }\n   ```\n\n   - If validation succeeds, the `onRequest` handler will be invoked.\n\n3. The `onRequest` handler:\n\n   ```js\n   function onRequest({ query, body, req }) =\u003e { /* do work and return data */ };\n   ```\n\n   ...or\n\n   ```js\n   async function onRequest({ query, body, req }) =\u003e { /* do work and return Promise which resolves to data */ };\n   ```\n\n   This method can return an object or a Promise resolving to an object that will be serialized to `JSON` and sent back to the client with a `200` (`OK`) status code. If `onRequest` returns `undefined` or `null`, an empty response will be sent with a `201` (`CREATED`) header for `POST` requests and `204` (`NO_CONTENT`) for non-`POST` request.\n\n4. Default error handling\n\n   If `beforeRequest` or `onRequest` throws an `ApiError` (also exported by `@appgeist/restful-next-api`), a specific http status code is returned to the client. For instance, the following code will result in a `403` (`FORBIDDEN`) being sent to the client:\n\n   ```js\n   import methods, { ApiError } from '@appgeist/restful-next-api';\n   import { FORBIDDEN } from 'http-status-codes';\n\n   export default methods({\n     get: {\n       // ...\n       onRequest: () =\u003e {\n         // ...\n         throw new ApiError(FORBIDDEN);\n         // ...\n       }\n       // ...\n     }\n   });\n   ```\n\n   Other error types are treated as `500` / `INTERNAL_SERVER_ERROR` and are also logged to the console.\n\n## Custom error handling\n\nYou can override the default error handling mechanism by providing a custom error handling function like so:\n\n```js\nexport default methods({\n  patch: {\n    // querySchema: ..., bodySchema: ...,\n    onRequest: ({ body, req }) =\u003e {\n      /* handle patch request */\n    },\n\n    // Error handler for patch requests\n    onError: ({ res, err }) =\u003e {\n      res.status(500).send('Error while trying to patch');\n    }\n  },\n\n  delete: {\n    // querySchema: ...,\n    onRequest: ({ query: { id }, req }) =\u003e {\n      /* handle delete request */\n    }\n  },\n\n  // Generic error handler - this will also handle errors for delete requests\n  onError: ({ res, err }) =\u003e {\n    res.status(500).send('Error');\n  }\n});\n```\n\nA specific method error handler takes precedence over the generic error handler.\n\n## IDE support\n\nJsDocs are provided for IDE support for now; an `index.d.ts` will be provided at some point in the future.\n\n## License\n\nThe [ISC License](LICENSE).\n\n[npm-image]: https://img.shields.io/npm/v/@appgeist/restful-next-api.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/@appgeist/restful-next-api\n[license-image]: https://img.shields.io/npm/l/@appgeist/restful-next-api.svg?style=flat-square\n[license-url]: LICENSE\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappgeist%2Frestful-next-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappgeist%2Frestful-next-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappgeist%2Frestful-next-api/lists"}