@apollo/client
Version:
A fully-featured caching GraphQL client.
1 lines • 8.2 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../../../../src/link/context/index.ts"],"sourcesContent":["import { Observable } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\n\nexport declare namespace SetContextLink {\n namespace SetContextLinkDocumentationTypes {\n /**\n * A function that returns an updated context object for an Apollo Link\n * operation.\n *\n * The context setter function is called for each operation and allows you to\n * modify the operation's context before it's passed to the next link in the\n * chain. The returned context object is shallowly merged with the previous\n * context object.\n *\n * @param prevContext - The previous context of the operation (e.g. the value\n * of `operation.getContext()`)\n * @param operation - The GraphQL operation being executed, without the\n * `getContext` and `setContext` methods\n * @returns A partial context object or a promise that resolves to a partial context object\n */\n export function ContextSetter(\n prevContext: Readonly<ApolloLink.OperationContext>,\n operation: SetContextLink.SetContextOperation\n ):\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n }\n\n /** {@inheritDoc @apollo/client/link/context!SetContextLink.SetContextLinkDocumentationTypes.ContextSetter:function(1)} */\n export type ContextSetter = (\n prevContext: Readonly<ApolloLink.OperationContext>,\n operation: SetContextLink.SetContextOperation\n ) =>\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n\n /**\n * @deprecated\n * Use `ContextSetter` instead. This type is used by the deprecated\n * `setContext` function.\n */\n export type LegacyContextSetter = (\n operation: SetContextLink.SetContextOperation,\n prevContext: Readonly<ApolloLink.OperationContext>\n ) =>\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n\n /**\n * An `ApolloLink.Operation` object without the `getContext` and `setContext`\n * methods. This prevents context setters from directly manipulating the\n * context during the setter function execution.\n */\n export type SetContextOperation = Omit<\n ApolloLink.Operation,\n \"getContext\" | \"setContext\"\n >;\n}\n\n/**\n * @deprecated\n * Use `SetContextLink` from `@apollo/client/link/context` instead. Note you\n * will need to flip the arguments when using `SetContextLink` as `prevContext`\n * is the first argument.\n *\n * ```ts\n * new SetContextLink((prevContext, operation) => {\n * // ...\n * });\n * ```\n */\nexport function setContext(setter: SetContextLink.LegacyContextSetter) {\n return new SetContextLink((prevContext, operation) =>\n setter(operation, prevContext)\n );\n}\n/**\n * `SetContextLink` is a non-terminating link that allows you to modify the\n * context of GraphQL operations before they're passed to the next link in the\n * chain. This is commonly used for authentication, adding headers, and other\n * request-time configuration.\n *\n * @example\n *\n * ```ts\n * import { SetContextLink } from \"@apollo/client/link/context\";\n *\n * const link = new SetContextLink((prevContext, operation) => {\n * return {\n * credentials: \"include\",\n * // ...\n * };\n * });\n * ```\n */\nexport class SetContextLink extends ApolloLink {\n constructor(setter: SetContextLink.ContextSetter) {\n super((operation, forward) => {\n const { ...request } = operation as SetContextLink.SetContextOperation;\n\n Object.defineProperty(request, \"client\", {\n enumerable: false,\n value: operation.client,\n });\n\n return new Observable((observer) => {\n let closed = false;\n Promise.resolve(request)\n .then((req) => setter(operation.getContext(), req))\n .then(operation.setContext)\n .then(() => {\n if (!closed) {\n forward(operation).subscribe(observer);\n }\n })\n .catch(observer.error.bind(observer));\n\n return () => {\n closed = true;\n };\n });\n });\n }\n}\n"],"names":[],"mappings":";;;AAwEA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AAxEA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AAEA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAA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