{"id":17291316,"url":"https://github.com/cxres/express-negotiate-events","last_synced_at":"2025-04-14T11:51:43.857Z","repository":{"id":247783871,"uuid":"825847208","full_name":"CxRes/express-negotiate-events","owner":"CxRes","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-09T04:46:04.000Z","size":107,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T01:01:38.442Z","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-07-08T16:03:34.000Z","updated_at":"2024-10-09T04:25:16.000Z","dependencies_parsed_at":"2024-07-10T16:08:08.476Z","dependency_job_id":"4e54cbd6-bdd1-43fa-a1dc-88f9d2e90095","html_url":"https://github.com/CxRes/express-negotiate-events","commit_stats":null,"previous_names":["cxres/express-negotiate-events"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-negotiate-events","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-negotiate-events/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-negotiate-events/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CxRes%2Fexpress-negotiate-events/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CxRes","download_url":"https://codeload.github.com/CxRes/express-negotiate-events/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248877986,"owners_count":21176239,"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.409Z","updated_at":"2025-04-14T11:51:43.654Z","avatar_url":"https://github.com/CxRes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express Negotiate Events\n\nA Connect/Express style middleware to negotiate notification protocols requested by an HTTP user-agent using the [`Accept-Events`](https://cxres.github.io/prep/draft-gupta-httpbis-per-resource-events.html#accept-events-header) header field.\n\n## Installation\n\nInstall **Express Negotiate Events** and **Express Accept Events** using your favourite package manager.\n\n```sh\nnpm|pnpm|yarn|bun add express-negotiate-events express-accept-events\n```\n\nAlso install middleware for the notification 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 negotiateEvents from \"express-negotiate-events\";\n\n// Assuming the protocol you want to support is Per Resource Events\nimport prep from \"express-prep\";\n```\n\n### Invocation\n\nNow you are ready to invoke the **Express Negotiate Events** middleware in your server. In case one is using an Express server:\n\n```js\nconst app = express();\n\napp.use(acceptEvents, negotiateEvents, prep);\n```\n\n### Sending Notifications\n\nThe **Express Negotiate Events** middleware populates response object with a `sendEvents()` function.\n\nYou must specify the notification protocols you wish to support on a given resource as properties of the `config` object. The value of each protocol property is the configuration for that protocol specified as a string. Default configuration is used when the value for a protocol is specified as falsy (but not when the protocol is not specified, in which case the protocol is ignored).\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 negotiateEventFields(defaultEvents) {\n  // custom negotiation logic supported by PREP.\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      negotiateEventFields,\n    },\n  },\n});\n```\n\n## Copyright and License\n\nCopyright © 2024, [Rahul Gupta](https://cxres.pages.dev/profile#i) and Express Negotiate Events contributors.\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-negotiate-events","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcxres%2Fexpress-negotiate-events","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcxres%2Fexpress-negotiate-events/lists"}