{"id":23084084,"url":"https://github.com/cmradiology/nitro-conditional-middleware","last_synced_at":"2025-10-08T19:05:47.740Z","repository":{"id":260620427,"uuid":"881857017","full_name":"cmradiology/nitro-conditional-middleware","owner":"cmradiology","description":"Tools for creating conditional middlewares for nitro","archived":false,"fork":false,"pushed_at":"2024-11-01T15:44:41.000Z","size":813,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-09-28T08:37:39.247Z","etag":null,"topics":["conditional","conditional-middleware","h3","middleware","middlewares","nitro","nitro-conditional-middleware","nitrojs","npm","npm-package","skip","skip-middleware","typescript"],"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/cmradiology.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T11:44:51.000Z","updated_at":"2025-02-16T17:11:03.000Z","dependencies_parsed_at":"2024-11-01T12:28:29.613Z","dependency_job_id":"867315dd-67ad-4e42-b9cb-6cb72d8820b0","html_url":"https://github.com/cmradiology/nitro-conditional-middleware","commit_stats":null,"previous_names":["cmradiology/nitro-conditional-middleware"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cmradiology/nitro-conditional-middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmradiology%2Fnitro-conditional-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmradiology%2Fnitro-conditional-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmradiology%2Fnitro-conditional-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmradiology%2Fnitro-conditional-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cmradiology","download_url":"https://codeload.github.com/cmradiology/nitro-conditional-middleware/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmradiology%2Fnitro-conditional-middleware/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000682,"owners_count":26082804,"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-08T02:00:06.501Z","response_time":56,"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":["conditional","conditional-middleware","h3","middleware","middlewares","nitro","nitro-conditional-middleware","nitrojs","npm","npm-package","skip","skip-middleware","typescript"],"created_at":"2024-12-16T15:48:40.178Z","updated_at":"2025-10-08T19:05:47.735Z","avatar_url":"https://github.com/cmradiology.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nitro-conditional-middleware\n\n## Development\n\n### install\n\n```bash\nnpm install\n```\n\n### test\n\n```bash\nnpm run test\n```\n\n### lint\n\n```bash\nnpm run lint:fix\n```\n\n### build\n\n```bash\nnpm run build\n```\n\n## Documentation\n\nA brief explanation on how middlewares in nitrojs work.\n\nNitroJS provides as base for their middlewares H3 event handlers created by `defineEventHandler`.\n\nMiddlewares in the nitrojs framework are executed in every request.\n\n## Real life scenarios\n\nIn this diagram we display 3 different scenarios that our server needs to handle:\n\n- Open API requests\n- Authenticated API requests\n- HealthCheck Requests\n\n### Problem\n\nKnowing that all events will pass through all middlewares, we need to plan the **flow** based on **conditions** that will **run** or **skip** these middlewares\n\n![Middleware diagrams](./docs/images/middlewares.png)\n\n## Skipping middlewares\n\nWith the constrain that the event flow will pass through all middleware event handlers, we needed a way to skip a middleware for some events that will go through the middleware pipeline based on a particular condition.\n\nThis library creates a way to pass a condition as parameter to create `defineMiddlewareHandler` functions with a run condition. This function has the same signature as as H3's `defineEventHandler`. Making it a 1:1 substitute.\n\n```ts\ntype condition = (event: H3Event) =\u003e boolean\n\n// This function would return a handler as H3's `defineEventHandler`\nconst defineMiddlewareHandler = createMiddlewareHandler(condition)\n```\n\n### Creating and using multiple `middlewareHandler`s\n\n```ts\n// defineApiMiddleware.ts\nimport { createMiddlewareHandler } from 'nitro-conditional-middleware'\n\nconst apiV1MiddlewareHandler = createMiddlewareHandler((event) =\u003e {\n  return event.path.includes('/api/v1/')\n})\n\nconst apiV2ConditionFn = (event: H3Event) =\u003e boolean {\n  return event.path.includes('/api/v2/')\n}\n\nconst apiV2MiddlewareHandler = createMiddlewareHandler(apiV2ConditionFn)\n```\n\n```ts\n// usage 1.my-first-middleware.ts\nimport { apiV1MiddlewareHandler } from './defineApiMiddleware'\n\nconst myFirstMiddleware = (event) =\u003e {\n  console.log('myFirstMiddleware: This will only run in api/v1 endpoints')\n}\n\nexport default apiV1MiddlewareHandler(myFirstMiddleware)\n```\n\n```ts\n// usage 2.my-second-middleware.ts\nimport { apiV2MiddlewareHandler } from './defineApiMiddleware'\n\nexport default apiV2MiddlewareHandler((event) =\u003e {\n  console.log('mySecondMiddleware: This will only run in api/v2 endpoints')\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmradiology%2Fnitro-conditional-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmradiology%2Fnitro-conditional-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmradiology%2Fnitro-conditional-middleware/lists"}