{"id":22220262,"url":"https://github.com/marcus-rise/next-api-interceptor","last_synced_at":"2025-03-25T07:41:04.537Z","repository":{"id":57841804,"uuid":"528420744","full_name":"Marcus-Rise/next-api-interceptor","owner":"Marcus-Rise","description":"Nextjs api interceptor","archived":false,"fork":false,"pushed_at":"2022-08-24T13:18:10.000Z","size":65,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T17:29:48.155Z","etag":null,"topics":["api","interceptor","nextjs","serverless"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marcus-Rise.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-24T12:46:50.000Z","updated_at":"2022-08-24T12:55:09.000Z","dependencies_parsed_at":"2022-09-03T11:02:04.710Z","dependency_job_id":null,"html_url":"https://github.com/Marcus-Rise/next-api-interceptor","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Rise%2Fnext-api-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Rise%2Fnext-api-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Rise%2Fnext-api-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Rise%2Fnext-api-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marcus-Rise","download_url":"https://codeload.github.com/Marcus-Rise/next-api-interceptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245422921,"owners_count":20612725,"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","interceptor","nextjs","serverless"],"created_at":"2024-12-02T23:07:41.226Z","updated_at":"2025-03-25T07:41:04.494Z","avatar_url":"https://github.com/Marcus-Rise.png","language":"TypeScript","readme":"# Next api interceptor\n\nInterceptor for serverless nextjs api\n\n## Install\n\nusing `npm`\n\n```bash\nnpm install @marcus-rise/next-api-interceptor\n```\n\nor using `yarn`\n\n```bash\nyarn add @marcus-rise/next-api-interceptor\n```\n\n## Usage\n\nYou can create several interceptors and apply them in train\n\nInterceptor is a wrapper function, that runs before a wrapped function\n\n```tsx\n// src/interceptor/method-handlers.interceptor.ts\nimport type {Interceptor} from \"@marcus-rise/next-api-interceptor\";\nimport type {NextApiHandler} from \"next\";\n\nenum RequestMethod {\n  GET = \"GET\",\n  POST = \"POST\",\n}\n\ntype MethodHandler\u003cHandler extends NextApiHandler = NextApiHandler\u003e = {\n  method: RequestMethod | string;\n  handler: Handler;\n};\n\nconst withMethodHandlers =\n  (...allowedHandlers: Array\u003cMethodHandler\u003e): Interceptor =\u003e\n    (handler: NextApiHandler = () =\u003e {\n    }) =\u003e\n      async (req, res) =\u003e {\n        const allowedHandler = allowedHandlers.find((h) =\u003e h.method === req.method)?.handler;\n\n        if (!allowedHandler) {\n          res.status(405).end();\n        } else {\n          await allowedHandler(req, res);\n        }\n\n        return handler(req, res);\n      };\n\nexport {withMethodHandlers};\n```\n\n```ts\n// pages/api/test.ts\nimport type {NextApiHandler} from \"next\";\nimport {withMethodHandlers} from \"../../src/interceptor/method-handlers.interceptor.ts\";\n\nconst TestHandler: NextApiHandler = (req, response) =\u003e {\n  return response.status(res.status).json({res: \"hellow\"});\n};\n\nexport default withMethodHandlers({\n  method: \"GET\",\n  handler: TestHandler,\n})();\n\n// or\n\nexport default applyInterceptors(\n  TestHandler,\n  anotherInterceptor,\n  withMethodHandlers({\n    method: \"GET\",\n    handler: TestHandler,\n  }),\n  yetAnotherOneInterceptor,\n);\n```\n\nThe execution order of example described above is: anotherInterceptor, withMethodHandlers,\nyetAnotherOneInterceptor, TestHandler\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcus-rise%2Fnext-api-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcus-rise%2Fnext-api-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcus-rise%2Fnext-api-interceptor/lists"}