{"name":"apollo-link-context","version":"0.1.0","description":"An easy way to set and cache context changes for Apollo Link","author":{"name":"James Baxley","email":"james@meteor.com"},"license":"MIT","main":"./lib/bundle.umd.js","module":"./lib/index.js","jsnext:main":"./lib/index.js","typings":"./lib/index.d.ts","repository":{"type":"git","url":"git+https://github.com/apollographql/apollo-link.git"},"bugs":{"url":"https://github.com/apollographql/apollo-link/issues"},"homepage":"https://github.com/apollographql/apollo-link#readme","scripts":{"build:browser":"browserify ./lib/bundle.umd.js -o=./lib/bundle.js --i apollo-link && npm run minify:browser","build":"tsc -p .","bundle":"rollup -c","clean":"rimraf lib/* && rimraf coverage/*","filesize":"npm run build && npm run build:browser","lint":"tslint --type-check -p tsconfig.json -c ../../tslint.json src/*.ts","minify:browser":"uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js","postbuild":"npm run bundle","prebuild":"npm run clean","prepublishOnly":"npm run clean && npm run build","test":"jest","watch":"tsc -w -p ."},"peerDependencies":{"apollo-link":"^0.8.0"},"devDependencies":{"@types/graphql":"0.11.5","@types/jest":"21.1.4","apollo-link":"^0.8.0","browserify":"14.5.0","graphql":"0.11.7","graphql-tag":"2.5.0","jest":"21.2.1","rimraf":"2.6.1","rollup":"0.45.2","ts-jest":"21.1.3","tslint":"5.7.0","typescript":"2.5.1","uglify-js":"3.1.4"},"jest":{"transform":{".(ts|tsx)":"<rootDir>/node_modules/ts-jest/preprocessor.js"},"testRegex":"(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$","moduleFileExtensions":["ts","tsx","js","json"]},"_id":"apollo-link-context@0.1.0","_npmVersion":"5.5.1","_nodeVersion":"8.1.2","_npmUser":{"name":"jbaxleyiii","email":"james@meteor.com"},"dist":{"integrity":"sha512-vDET7I9jxj4FWJlzNNnjQX+tC+fkHjyKwott5ZaGae9PKNljtb32Mr4yvgSqCpiohSHjnd5EOJOdS9ySgY8OjQ==","shasum":"9d986ac721372b33d4312702d5a7a6e4d3abd45e","tarball":"http://registry-npm.ywwl.com/apollo-link-context/download/apollo-link-context-0.1.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGS4CA+kGCbNB2sGwC9WGa8Nx+sbfvSiC+KskqAj5tr5AiEA7lQRDvsJG+gXp8n1giKhP+SRz7ZzVuMumaFdKeowpZo="}],"size":5024,"noattachment":false,"key":"/apollo-link-context/-/apollo-link-context-0.1.0.tgz"},"maintainers":[{"name":"jbaxleyiii","email":"james@meteor.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/apollo-link-context-0.1.0.tgz_1508544820527_0.08832001220434904"},"directories":{},"readme":"---\ntitle: apollo-link-context\ndescription: Easily set a context on your operation, which is used by other links further down the chain.\n---\n\nThe `setContext` function takes a function that returns either an object or a promise that returns an object to set the new context of a request.\n\nIt receives two arguments: the GraphQL request being executed, and the previous context. This link makes it easy to perform async look up of things like authentication tokens and more!\n\n```js\nimport { setContext } from \"apollo-link-context\";\n\nconst setAuthorizationLink = setContext((request, previousContext) => ({\n  headers: {authorization: \"1234\"}\n}));\n\nconst asyncAuthLink = setContext(\n  request =>\n    new Promise((success, fail) => {\n      // do some async lookup here\n      setTimeout(() => {\n        success({ token: \"async found token\" });\n      }, 10);\n    })\n);\n```\n\n## Caching lookups\n\nTypically async actions can be expensive and may not need to be called for every request, especially when a lot of request are happening at once. You can setup your own caching and invalidation outside of the link to make it faster but still flexible!\n\nTake for example a user auth token being found, cached, then removed on a 401 response:\n\n```js\nimport { setContext } from \"apollo-link-context\";\nimport { onError } from \"apollo-link-error\";\n\n// cached storage for the user token\nlet token;\nconst withToken = setContext(() => {\n  // if you have a cached value, return it immediately\n  if (token) return { token };\n\n  return AsyncTokenLookup().then(userToken => {\n    token = userToken;\n    return { token };\n  });\n});\n\nconst resetToken = onError(({ networkError }) => {\n  if (networkError && networkError.name ==='ServerError' && networkError.statusCode === 401) {\n    // remove cached token on 401 from the server\n    token = null;\n  }\n});\n\nconst authFlowLink = withToken.concat(resetToken);\n```\n","publish_time":1508544821394,"_cnpm_publish_time":1508544821394,"dist-tags":{"beta":"2.0.0-beta.0","latest":"1.0.20"}}