{"id":17291321,"url":"https://github.com/cxres/express-events-negotiate-temp","last_synced_at":"2025-03-26T18:46:22.321Z","repository":{"id":231540215,"uuid":"781013116","full_name":"CxRes/express-events-negotiate-temp","owner":"CxRes","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-03T02:30:36.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-01T00:27:17.210Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CxRes.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":"2024-04-02T15:32:26.000Z","updated_at":"2024-06-03T02:30:39.000Z","dependencies_parsed_at":"2024-04-04T14:44:48.846Z","dependency_job_id":"f1cf6b00-fd61-4a0f-b51c-af274d57ca54","html_url":"https://github.com/CxRes/express-events-negotiate-temp","commit_stats":null,"previous_names":["cxres/express-events-negotiate-temp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-events-negotiate-temp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-events-negotiate-temp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-events-negotiate-temp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-events-negotiate-temp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CxRes","download_url":"https://codeload.github.com/CxRes/express-events-negotiate-temp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245717938,"owners_count":20661151,"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":[],"created_at":"2024-10-15T10:40:43.710Z","updated_at":"2025-03-26T18:46:22.296Z","avatar_url":"https://github.com/CxRes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express-Events-Negotiate\n\nA Connect/Express style middleware to provide to negotiate the appropriate notifications protocol and send notifications using it.\n\n## Installation\n\nInstall **Express-Events-Negotiate** and **Express-Accept-Events** using your favourite package manager.\n\n```sh\nnpm|pnpm|yarn add express-events-negotiate express-accept-events\n```\n\nAlso install your the middleware for the notifications protocols you might want to support.\n\n## Usage\n\n### Setup\n\nAdd the following imports to your server:\n\n```js\nimport acceptEvents from \"express-accept-events\";\nimport prep from \"express-prep\";\nimport events from \"express-events-negotiate\";\n```\n\n### Invocation\n\nNow you are ready to invoke the middleware in your server. In case one is using an Express server:\n\n```js\nconst app = express();\n\napp.use(acceptEvents, events, prep);\n```\n\n### Sending Notifications\n\nThe middleware populates response object with a `sendEvents` function.\n\nYou must specify supported events for each protocol as a string using the `config` property of the argument. Default configuration is used when the value is falsy (but not when the protocol is not specified).\n\n```js\napp.get(\"/foo\", (req, res) =\u003e {\n  // Get the response body first\n  const body = getContent(req.url);\n  // Get the content-* headers\n  const headers = getMediaType(responseBody);\n\n  const failStatus = res.sendEvents({\n    body,\n    headers,\n    config: {\n      prep: '',\n    },\n  });\n\n  if (!failStatus) return;\n\n  // If notifications are not sent, send regular response\n  res.setHeaders(new Headers(headers));\n  res.write(responseBody);\n  res.end();\n});\n```\n\n### Advanced Configuration\n\nYou can customize notifications for each protocol by specifying an object specific to that protocol on the `modifiers` property.\n\n```js\nfunction negotiateEvents(defaultEvents) {\n  // custom negotiation logic\n  // must return in the structured-headers format\n}\n\nconst failStatus = res.sendEvents({\n  body,\n  headers,\n  config: {\n    prep: `accept=(\"message/rfc822\";delta=\"text/plain\")`,\n  },\n  modifiers: {\n    prep: {\n      negotiateEvents,\n    },\n  },\n});\n```\n\n## Copyright and License\n\nCopyright © 2024, [Rahul Gupta](https://cxres.pages.dev/profile#i)\n\nThe source code in this repository is released under the [Mozilla Public License v2.0](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcxres%2Fexpress-events-negotiate-temp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcxres%2Fexpress-events-negotiate-temp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcxres%2Fexpress-events-negotiate-temp/lists"}