<commit_before>import { Subscription } from 'rxjs/Subscription';
import { RequestOptions, Response, URLSearchParams, Headers } from '@angular/http';
import { isUndefined, stringify, TargetMetadata } from '@tdm/core';

import {
  Adapter,
  findProp,
  ExecuteContext,
  AdapterResponse,
  ExecuteResponse
} from '@tdm/data';

import { HttpActionMetadata, UrlParamMetadata } from '../metadata';

import { HttpActionOptions, TrailingSlashesStrategy } from './interfaces';
import { httpDefaultConfig } from '../http-default-config';
import { Params, getParamNames, formatPattern } from '../utils/match-pattern';

import { getHttp } from '../providers';

const EMPTY_META: TargetMetadata = <any>{};

export class HttpAdapter implements Adapter<HttpActionMetadata, HttpActionOptions> {
  readonly supports = { cancel: true };

  private executing = new Map<number, Subscription>();

  private idCount = 1;

  execute(ctx: ExecuteContext<HttpActionMetadata>, options: HttpActionOptions): AdapterResponse {
    const id = this.idCount++;
    try {
      const http = getHttp();

      if (!options) options = {} as any;
      let {action} = ctx;
      let resource = ctx.targetMeta || EMPTY_META;

      const url = findProp('endpoint', resource, action);
      if (!url) {
        // TODO: move to @tdm error with more info.
        throw new Error('Invalid endpoint, no endpoint found.')
      }

      const withCredentials = findProp('withCredentials', httpDefaultConfig, resource, action, options);
      const strip = findProp('trailingSlashes', httpDefaultConfig, resource, action, options);

      const urlParams = this.getParams(ctx, resource, options);

      const {path, query} = this.splitParams(url, urlParams);

      const requestOptions = new RequestOptions({
        url: processUrl(this.parseUrl(url, path), strip),
        method: action.methodInfo.source as any,
        search: this.paramsToSearchParams(query),
        headers: this.getHeaders(ctx, resource, options),
        body: ctx.body,
        withCredentials
      });

      const response = new Promise<ExecuteResponse>((resolve, reject) => {
        let value: ExecuteResponse;

        const subscription = http.request(requestOptions.url, requestOptions)
          .map(response => ({data: response.json(), response, request: requestOptions}))
          .subscribe(
            v => value = v,
            err => {
              this.executing.delete(id);
              reject(err);
            },
            () => {
              this.executing.delete(id);
              resolve(value);
            }
          );

        this.executing.set(id, subscription);
      });
      return { id, response };
    } catch (err) {
      return { id, response: Promise.reject(err) };
    }
  }

  cancel(id: number): void {
    const sub = this.executing.get(id);
    if (sub && !sub.closed) {
      sub.unsubscribe();
    }
  }
<commit_msg>fix(ngx-http): work with AOT compilation<commit_after>import { Subscription } from 'rxjs/Subscription';
import { map } from 'rxjs/operator/map';

import { RequestOptions, URLSearchParams, Headers } from '@angular/http';
import { isUndefined, stringify, TargetMetadata } from '@tdm/core';

import {
  Adapter,
  findProp,
  ExecuteContext,
  AdapterResponse,
  ExecuteResponse
} from '@tdm/data';

import { HttpActionMetadata, UrlParamMetadata } from '../metadata';

import { HttpActionOptions, TrailingSlashesStrategy } from './interfaces';
import { httpDefaultConfig } from '../http-default-config';
import { Params, getParamNames, formatPattern } from '../utils/match-pattern';

import { getHttp } from '../providers';

const EMPTY_META: TargetMetadata = <any>{};

export class HttpAdapter implements Adapter<HttpActionMetadata, HttpActionOptions> {
  readonly supports = { cancel: true };

  private executing = new Map<number, Subscription>();

  private idCount = 1;

  execute(ctx: ExecuteContext<HttpActionMetadata>, options: HttpActionOptions): AdapterResponse {
    const id = this.idCount++;
    try {
      const http = getHttp();

      if (!options) options = {} as any;
      let {action} = ctx;
      let resource = ctx.targetMeta || EMPTY_META;

      const url = findProp('endpoint', resource, action);
      if (!url) {
        // TODO: move to @tdm error with more info.
        throw new Error('Invalid endpoint, no endpoint found.')
      }

      const withCredentials = findProp('withCredentials', httpDefaultConfig, resource, action, options);
      const strip = findProp('trailingSlashes', httpDefaultConfig, resource, action, options);

      const urlParams = this.getParams(ctx, resource, options);

      const {path, query} = this.splitParams(url, urlParams);

      const requestOptions = new RequestOptions({
        url: processUrl(this.parseUrl(url, path), strip),
        method: action.methodInfo.source as any,
        search: this.paramsToSearchParams(query),
        headers: this.getHeaders(ctx, resource, options),
        body: ctx.body,
        withCredentials
      });

      const response = new Promise<ExecuteResponse>((resolve, reject) => {
        let value: ExecuteResponse;

        const subscription = map.call(
          http.request(requestOptions.url, requestOptions),
          response => ({data: response.json(), response, request: requestOptions}))
          .subscribe(
            v => value = v,
            err => {
              this.executing.delete(id);
              reject(err);
            },
            () => {
              this.executing.delete(id);
              resolve(value);
            }
        );

        this.executing.set(id, subscription);
      });
      return { id, response };
    } catch (err) {
      return { id, response: Promise.reject(err) };
    }
  }

  cancel(id: number): void {
    const sub = this.executing.get(id);
    if (sub && !sub.closed) {
      sub.unsubscribe();
    }
  }

{'fraction_non_alphanumeric': 0.1239193083573487, 'fraction_numerical': 0.0003390405153415833, 'mean_word_length': 2.879026955950033, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3858891', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1607, 'n_words': 428}