{"id":16550168,"url":"https://github.com/b2a3e8/apollo-server-plugin-http-headers","last_synced_at":"2025-03-21T10:32:00.341Z","repository":{"id":42943281,"uuid":"234159146","full_name":"b2a3e8/apollo-server-plugin-http-headers","owner":"b2a3e8","description":"Allows you to set HTTP Headers and Cookies easily in your resolvers.","archived":false,"fork":false,"pushed_at":"2023-12-13T22:22:44.000Z","size":70,"stargazers_count":12,"open_issues_count":4,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T03:16:34.036Z","etag":null,"topics":["apollo","apollo-server","cookie","header","lambda","plugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/apollo-server-plugin-http-headers","language":"JavaScript","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/b2a3e8.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":"2020-01-15T19:43:48.000Z","updated_at":"2022-10-23T18:31:26.000Z","dependencies_parsed_at":"2024-06-18T21:36:28.737Z","dependency_job_id":"1e940599-8047-4063-864d-60bf39cbf04b","html_url":"https://github.com/b2a3e8/apollo-server-plugin-http-headers","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"e2015cf67fa9823570d25a3454fa6f6de99a366e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2a3e8%2Fapollo-server-plugin-http-headers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2a3e8%2Fapollo-server-plugin-http-headers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2a3e8%2Fapollo-server-plugin-http-headers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2a3e8%2Fapollo-server-plugin-http-headers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/b2a3e8","download_url":"https://codeload.github.com/b2a3e8/apollo-server-plugin-http-headers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244777957,"owners_count":20508820,"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":["apollo","apollo-server","cookie","header","lambda","plugin"],"created_at":"2024-10-11T19:33:08.672Z","updated_at":"2025-03-21T10:32:00.056Z","avatar_url":"https://github.com/b2a3e8.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# apollo-server-plugin-http-headers\n\nAllows you to set HTTP Headers and Cookies easily in your resolvers. This is especially useful in apollo-server-lambda, because you don't have any other options there to set headers or cookies.\n\nThe way it works is simple: you put an array for cookies and an array for headers in your context; you can then access them in your resolvers (and therefore add, alter or delete headers and cookies). Before your request is sent to the client this plugin loops through the arrays and adds every item to the HTTP response. The logic is very easy, actually the documentation is way longer than the source code.\n\n## Installation\n\n### Install the package\n\n`npm install apollo-server-plugin-http-headers`\n\n### Register plugin\n\nImport and register the plugin, then add `setHeaders` and `setCookies` to context:\n\n```javascript\nconst httpHeadersPlugin = require(\"apollo-server-plugin-http-headers\");\n\nconst server = new ApolloServer({\n    typeDefs,\n    resolvers,\n    plugins: [httpHeadersPlugin],\n    context: {\n        setCookies: new Array(),\n        setHeaders: new Array()\n    }\n});\n```\n\nPlease note: The context argument varies depending on the specific integration (e.g. Express, Koa,  Lambda, etc.) being used. See the [official apollo-server documentation](https://www.apollographql.com/docs/apollo-server/api/apollo-server/) for more details.\nExample for the Lambda integration (apollo-server-lambda):\n\n```javascript\nconst httpHeadersPlugin = require(\"apollo-server-plugin-http-headers\");\n\nconst server = new ApolloServer({\n    typeDefs,\n    resolvers,\n    plugins: [httpHeadersPlugin],\n    context: ({ event, context }) =\u003e {\n        return {\n            event,\n            context,\n            setCookies: new Array(),\n            setHeaders: new Array()\n        };\n    }\n});\n```\n\n## Usage\n\n### Headers\n\nSet a header in a resolver:\n\n```javascript\ncontext.setHeaders.push({ key: \"headername\", value: \"headercontent\" });\n```\n\nComplete example:\n\n```javascript\nconst resolvers = {\n    Query: {\n        hello: async (parent, args, context, info) =\u003e {\n            context.setHeaders.push({ key: \"X-TEST-ONE\", value: \"abc\" });\n            context.setHeaders.push({ key: \"X-TEST-TWO\", value: \"def\" });\n            return \"Hello world!\";\n        }\n    }\n};\n```\n\n### Cookies\n\nSet a cookie in a resolver:\n\n```javascript\ncontext.setCookies.push({\n    name: \"cookieName\",\n    value: \"cookieContent\",\n    options: {\n        domain: \"example.com\",\n        expires: new Date(\"2021-01-01T00:00:00\"),\n        httpOnly: true,\n        maxAge: 3600,\n        path: \"/\",\n        sameSite: true,\n        secure: true\n    }\n});\n```\n\nComplete example:\n\n```javascript\nconst resolvers = {\n    Query: {\n        hello: async (parent, args, context, info) =\u003e {\n\n            context.setCookies.push({\n                name: \"cookieName\",\n                value: \"cookieContent\",\n                options: {\n                    domain: \"example.com\",\n                    expires: new Date(\"2021-01-01T00:00:00\"),\n                    httpOnly: true,\n                    maxAge: 3600,\n                    path: \"/\",\n                    sameSite: true,\n                    secure: true\n                }\n            });\n\n            return \"Hello world!\";\n        }\n    }\n};\n```\n\n#### Cookie Options\n\nThis package uses [jshttp/cookie](https://github.com/jshttp/cookie) for serializing cookies and you can use all the options they provide. Find an overview below or the complete documentation [here](https://github.com/jshttp/cookie#cookieserializename-value-options).\n\noption | description\n--- | ---\ndomain | Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3). By default, no domain is set.\nencode | Specifies a function that will be used to encode a cookie's value. Default: `encodeURIComponent`\nexpires | Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1). If expires and maxAge are set, maxAge mostly wins on the client side. By default, no expiration is set.\nhttpOnly | Specifies the boolean value for the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6). Defaults to `false`.\nmaxAge | Specifies the number (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2). By default, no maximum age is set.\npath | Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4). By default, no path is set and the user agent computes a path according to [these algorithms](https://tools.ietf.org/html/rfc6265#section-5.1.4).\nsameSite | Specifies the boolean or string to be the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7). Valid values: `true`, `false`, `'lax'`, `'none'` and `'strict'`.\nsecure | Specifies the boolean value for the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).By default, the `Secure` attribute is not set.\n\n## Limitations\n\n### Only one cookie can be set per request\n\nThere is at the moment no possility to set multiple cookies because apollo server does not support that. Find details and workaround inspiration [here](https://github.com/apollographql/apollo-server/issues/3040).\nIf you add multiple items to setCookie I'll throw an exception at your face (-;\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb2a3e8%2Fapollo-server-plugin-http-headers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fb2a3e8%2Fapollo-server-plugin-http-headers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb2a3e8%2Fapollo-server-plugin-http-headers/lists"}