All files HttpLink.ts

97.87% Statements 46/47
80% Branches 12/15
91.67% Functions 11/12
97.5% Lines 39/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1013x 3x 3x 3x       3x     3x         12x   12x   12x   12x 12x 12x 12x           12x   12x           12x         12x 1x     12x 1x       12x 3x     12x 12x   12x             12x   4x           4x       12x 4x 4x             3x 12x   3x     3x 12x   3x 12x   3x  
import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse, HttpHeaders} from '@angular/common/http';
import {ApolloLink, Observable, RequestHandler, Operation} from 'apollo-link';
import {print} from 'graphql/language/printer';
import {ExecutionResult} from 'graphql';
 
import {Options, Body} from './types';
import {normalizeUrl, mergeHeaders} from './utils';
 
// XXX find a better name for it
export class HttpLinkHandler extends ApolloLink {
  public requester: RequestHandler;
  private options: Options;
 
  constructor(httpClient: HttpClient, options: Options) {
    super();
 
    this.options = options;
 
    this.requester = new ApolloLink(
      (operation: Operation) =>
        new Observable((observer: any) => {
          const {
            headers,
            withCredentials,
          }: {
            headers?: HttpHeaders;
            withCredentials?: boolean;
          } = operation.getContext();
 
          const {operationName, variables, query, extensions} = operation;
 
          const body: Body = {
            operationName,
            variables,
            query: print(query),
          };
 
          const postOptions = {
            withCredentials: this.options.withCredentials,
            headers: this.options.headers,
          };
 
          if (this.options.includeExtensions) {
            body.extensions = extensions;
          }
 
          if (typeof withCredentials !== 'undefined') {
            postOptions.withCredentials = withCredentials;
          }
 
          // merge headers
          if (headers) {
            postOptions.headers = mergeHeaders(postOptions.headers, headers);
          }
 
          const endpointURI = normalizeUrl(this.options.uri);
          const defaultURI = 'graphql';
 
          const obs = httpClient.post<Object>(endpointURI || defaultURI, body, {
            observe: 'response',
            responseType: 'json',
            reportProgress: false,
            ...postOptions,
          });
 
          const sub = obs.subscribe({
            next: (result: HttpResponse<any>) => {
              observer.next(result.body);
            },
            error: (err: Error) => {
              observer.error(err);
            },
            complete: () => {
              observer.complete();
            },
          });
 
          return () => {
            Eif (!sub.closed) {
              sub.unsubscribe();
            }
          };
        }),
    ).request;
  }
 
  public request(op: Operation): Observable<ExecutionResult> | null {
    return this.requester(op);
  }
}
 
@Injectable()
export class HttpLink {
  constructor(private httpClient: HttpClient) {}
 
  public create(options: Options): HttpLinkHandler {
    return new HttpLinkHandler(this.httpClient, options);
  }
}