1 |
- {"version":3,"file":"bundle.umd.js","sources":["../src/linkUtils.ts","../src/link.ts"],"sourcesContent":["import Observable from 'zen-observable-ts';\n\nimport { GraphQLRequest, Operation } from './types';\nimport { ApolloLink } from './link';\n\nimport { getOperationName } from 'apollo-utilities';\nimport { invariant, InvariantError } from 'ts-invariant';\nexport { getOperationName };\n\nexport function validateOperation(operation: GraphQLRequest): GraphQLRequest {\n const OPERATION_FIELDS = [\n 'query',\n 'operationName',\n 'variables',\n 'extensions',\n 'context',\n ];\n for (let key of Object.keys(operation)) {\n if (OPERATION_FIELDS.indexOf(key) < 0) {\n throw new InvariantError(`illegal argument: ${key}`);\n }\n }\n\n return operation;\n}\n\nexport class LinkError extends Error {\n public link: ApolloLink;\n constructor(message?: string, link?: ApolloLink) {\n super(message);\n this.link = link;\n }\n}\n\nexport function isTerminating(link: ApolloLink): boolean {\n return link.request.length <= 1;\n}\n\nexport function toPromise<R>(observable: Observable<R>): Promise<R> {\n let completed = false;\n return new Promise<R>((resolve, reject) => {\n observable.subscribe({\n next: data => {\n if (completed) {\n invariant.warn(\n `Promise Wrapper does not support multiple results from Observable`,\n );\n } else {\n completed = true;\n resolve(data);\n }\n },\n error: reject,\n });\n });\n}\n\n// backwards compat\nexport const makePromise = toPromise;\n\nexport function fromPromise<T>(promise: Promise<T>): Observable<T> {\n return new Observable<T>(observer => {\n promise\n .then((value: T) => {\n observer.next(value);\n observer.complete();\n })\n .catch(observer.error.bind(observer));\n });\n}\n\nexport function fromError<T>(errorValue: any): Observable<T> {\n return new Observable<T>(observer => {\n observer.error(errorValue);\n });\n}\n\nexport function transformOperation(operation: GraphQLRequest): GraphQLRequest {\n const transformedOperation: GraphQLRequest = {\n variables: operation.variables || {},\n extensions: operation.extensions || {},\n operationName: operation.operationName,\n query: operation.query,\n };\n\n // best guess at an operation name\n if (!transformedOperation.operationName) {\n transformedOperation.operationName =\n typeof transformedOperation.query !== 'string'\n ? getOperationName(transformedOperation.query)\n : '';\n }\n\n return transformedOperation as Operation;\n}\n\nexport function createOperation(\n starting: any,\n operation: GraphQLRequest,\n): Operation {\n let context = { ...starting };\n const setContext = next => {\n if (typeof next === 'function') {\n context = { ...context, ...next(context) };\n } else {\n context = { ...context, ...next };\n }\n };\n const getContext = () => ({ ...context });\n\n Object.defineProperty(operation, 'setContext', {\n enumerable: false,\n value: setContext,\n });\n\n Object.defineProperty(operation, 'getContext', {\n enumerable: false,\n value: getContext,\n });\n\n Object.defineProperty(operation, 'toKey', {\n enumerable: false,\n value: () => getKey(operation),\n });\n\n return operation as Operation;\n}\n\nexport function getKey(operation: GraphQLRequest) {\n // XXX We're assuming here that query and variables will be serialized in\n // the same order, which might not always be true.\n const { query, variables, operationName } = operation;\n return JSON.stringify([operationName, query, variables]);\n}\n","import Observable from 'zen-observable-ts';\nimport { invariant, InvariantError } from 'ts-invariant';\n\nimport {\n GraphQLRequest,\n NextLink,\n Operation,\n RequestHandler,\n FetchResult,\n} from './types';\n\nimport {\n validateOperation,\n isTerminating,\n LinkError,\n transformOperation,\n createOperation,\n} from './linkUtils';\n\nfunction passthrough(op, forward) {\n return forward ? forward(op) : Observable.of();\n}\n\nfunction toLink(handler: RequestHandler | ApolloLink) {\n return typeof handler === 'function' ? new ApolloLink(handler) : handler;\n}\n\nexport function empty(): ApolloLink {\n return new ApolloLink(() => Observable.of());\n}\n\nexport function from(links: ApolloLink[]): ApolloLink {\n if (links.length === 0) return empty();\n return links.map(toLink).reduce((x, y) => x.concat(y));\n}\n\nexport function split(\n test: (op: Operation) => boolean,\n left: ApolloLink | RequestHandler,\n right?: ApolloLink | RequestHandler,\n): ApolloLink {\n const leftLink = toLink(left);\n const rightLink = toLink(right || new ApolloLink(passthrough));\n\n if (isTerminating(leftLink) && isTerminating(rightLink)) {\n return new ApolloLink(operation => {\n return test(operation)\n ? leftLink.request(operation) || Observable.of()\n : rightLink.request(operation) || Observable.of();\n });\n } else {\n return new ApolloLink((operation, forward) => {\n return test(operation)\n ? leftLink.request(operation, forward) || Observable.of()\n : rightLink.request(operation, forward) || Observable.of();\n });\n }\n}\n\n// join two Links together\nexport const concat = (\n first: ApolloLink | RequestHandler,\n second: ApolloLink | RequestHandler,\n) => {\n const firstLink = toLink(first);\n if (isTerminating(firstLink)) {\n invariant.warn(\n new LinkError(\n `You are calling concat on a terminating link, which will have no effect`,\n firstLink,\n ),\n );\n return firstLink;\n }\n const nextLink = toLink(second);\n\n if (isTerminating(nextLink)) {\n return new ApolloLink(\n operation =>\n firstLink.request(\n operation,\n op => nextLink.request(op) || Observable.of(),\n ) || Observable.of(),\n );\n } else {\n return new ApolloLink((operation, forward) => {\n return (\n firstLink.request(operation, op => {\n return nextLink.request(op, forward) || Observable.of();\n }) || Observable.of()\n );\n });\n }\n};\n\nexport class ApolloLink {\n public static empty = empty;\n public static from = from;\n public static split = split;\n public static execute = execute;\n\n constructor(request?: RequestHandler) {\n if (request) this.request = request;\n }\n\n public split(\n test: (op: Operation) => boolean,\n left: ApolloLink | RequestHandler,\n right?: ApolloLink | RequestHandler,\n ): ApolloLink {\n return this.concat(split(test, left, right || new ApolloLink(passthrough)));\n }\n\n public concat(next: ApolloLink | RequestHandler): ApolloLink {\n return concat(this, next);\n }\n\n public request(\n operation: Operation,\n forward?: NextLink,\n ): Observable<FetchResult> | null {\n throw new InvariantError('request is not implemented');\n }\n}\n\nexport function execute(\n link: ApolloLink,\n operation: GraphQLRequest,\n): Observable<FetchResult> {\n return (\n link.request(\n createOperation(\n operation.context,\n transformOperation(validateOperation(operation)),\n ),\n ) || Observable.of()\n );\n}\n"],"names":["tslib_1.__extends","getOperationName"],"mappings":";;;;;;;;WASgB,iBAAiB,CAAC,SAAyB;MACzD,IAAM,gBAAgB,GAAG;UACvB,OAAO;UACP,eAAe;UACf,WAAW;UACX,YAAY;UACZ,SAAS;OACV,CAAC;MACF,KAAgB,UAAsB,EAAtB,KAAA,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAtB,cAAsB,EAAtB,IAAsB,EAAE;UAAnC,IA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