{"id":25633748,"url":"https://github.com/yiminghe/next-context","last_synced_at":"2025-04-14T19:22:10.620Z","repository":{"id":204874823,"uuid":"712846023","full_name":"yiminghe/next-context","owner":"yiminghe","description":"unified middleware and request context for nextjs","archived":false,"fork":false,"pushed_at":"2025-03-30T17:00:35.000Z","size":1574,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T17:34:23.626Z","etag":null,"topics":["expressjs","koa","middleware","nextjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/yiminghe.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":"2023-11-01T10:16:07.000Z","updated_at":"2025-03-30T17:00:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"934babda-fb8e-403a-9f84-4b335a722fac","html_url":"https://github.com/yiminghe/next-context","commit_stats":null,"previous_names":["yiminghe/next-compose-middleware","yiminghe/next-compose-middlewares"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiminghe%2Fnext-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiminghe%2Fnext-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiminghe%2Fnext-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiminghe%2Fnext-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiminghe","download_url":"https://codeload.github.com/yiminghe/next-context/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943415,"owners_count":21186958,"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":["expressjs","koa","middleware","nextjs"],"created_at":"2025-02-22T22:22:30.658Z","updated_at":"2025-04-14T19:22:10.595Z","avatar_url":"https://github.com/yiminghe.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# next-context\r\n- Using koa style middlewares inside nextjs\r\n- Unified request/response context(express api) across Page and Route/Action\r\n- SetCookie/clearCookie both inside Page and Route/Action\r\n- Easily access request/response context between next middleware, components inside Page and functions inside Route/Action\r\n- I18n support both for server and client component\r\n\r\n[![NPM version][npm-image]][npm-url]\r\n[![Test coverage][coveralls-image]][coveralls-url]\r\n[![npm download][download-image]][download-url]\r\n![Build Status](https://github.com/yiminghe/next-context/actions/workflows/ci.yaml/badge.svg)\r\n[![next-context](https://img.shields.io/endpoint?url=https://cloud.cypress.io/badge/simple/5v7p13/main\u0026style=flat\u0026logo=cypress)](https://cloud.cypress.io/projects/5v7p13/runs)\r\n\r\n[npm-image]: http://img.shields.io/npm/v/next-context.svg?style=flat-square\r\n[npm-url]: http://npmjs.org/package/next-context\r\n[coveralls-image]: https://img.shields.io/coveralls/yiminghe/next-context.svg?style=flat-square\r\n[coveralls-url]: https://coveralls.io/r/yiminghe/next-context?branch=main\r\n[node-image]: https://img.shields.io/badge/node.js-%3E=_18.0-green.svg?style=flat-square\r\n[node-url]: http://nodejs.org/download/\r\n[download-image]: https://img.shields.io/npm/dm/next-context.svg?style=flat-square\r\n[download-url]: https://npmjs.org/package/next-context\r\n\r\n\r\n## demo\r\n\r\n```\r\npnpm i\r\nnpm run dev\r\n```\r\n\r\n## docs\r\n\r\n[Docs](https://github.com/yiminghe/next-context/blob/main/docs/index.md)\r\n\r\n## Usage\r\n\r\n### nextjs middleware\r\n`src/middleware.ts`\r\n\r\n```js\r\nimport { createMiddleware } from 'next-context/middleware';\r\nexport const middleware = createMiddleware();\r\nexport const config = {\r\n  matcher: '/((?!_next|favicon.ico|sitemap.xml|robots.txt).*)',\r\n};\r\n```\r\n\r\n### extends type\r\n\r\n```ts\r\ndeclare module 'next-context' {\r\n  interface NextContext {\r\n    user: string;\r\n  }\r\n}\r\n```\r\n\r\n### page\r\n`src/app/page.tsx`\r\n\r\n```js\r\nimport React from 'react';\r\nimport { withPageMiddlewares, getNextContext } from 'next-context';\r\n\r\nexport default withPageMiddlewares([\r\n  async (context, next) =\u003e {\r\n    context.user = 'test';\r\n    await next();\r\n  }])(\r\n  async () =\u003e {\r\n    const { user } = getNextContext();\r\n    return (\r\n      \u003c\u003e\r\n        \u003cp\u003e{user}\u003c/p\u003e\r\n      \u003c/\u003e\r\n    );\r\n  },\r\n);\r\n```\r\n\r\n### action\r\n`src/action/getUser.ts`\r\n\r\n```js\r\nimport { withActionMiddlewares, getNextContext } from 'next-context';\r\n\r\nexport default withActionMiddlewares([\r\n  async (context, next) =\u003e {\r\n    context.user = 'test';\r\n    await next();\r\n  }])(\r\n  async () =\u003e {\r\n    const { user } = getNextContext();\r\n    return user;\r\n  },\r\n);\r\n```\r\n\r\n### route\r\n`src/app/get/route.ts`\r\n\r\n```js\r\nimport { withRouteMiddlewares,getNextContext } from 'next-context';\r\n\r\nexport const GET = withRouteMiddlewares([\r\n  async (context, next) =\u003e {\r\n    context.user = 'test';\r\n    await next();\r\n  }])(\r\n  async () =\u003e {\r\n    const { user, res } = getNextContext();\r\n    res.json({ user });\r\n  },\r\n);\r\n```\r\n\r\n### nginx\r\n\r\n```\r\nlocation /rewrite {\r\n    proxy_set_header X-Forwarded-URI $request_uri;\r\n    proxy_set_header X-Forwarded-For $remote_addr;\r\n    proxy_set_header X-Forwarded-Host $host:$server_port;\r\n    proxy_set_header X-Forwarded-Proto $scheme;\r\n    proxy_pass http://127.0.0.1:3000/dynamic;\r\n    proxy_http_version 1.1;\r\n    # Disable buffering for streaming support\r\n    proxy_buffering off;\r\n    proxy_set_header X-Accel-Buffering no;\r\n}\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiminghe%2Fnext-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiminghe%2Fnext-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiminghe%2Fnext-context/lists"}