All files / core/lib Client.ts

83.03% Statements 137/165
59.78% Branches 55/92
97.37% Functions 37/38
93.6% Lines 117/125
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148  2x 8x 11x 1x 19x 8x     2x 12x 8x 24x   20x 40x 40x 40x 40x 20x 12x       8x             20x 40x 8x     2x 2x 2x 2x 2x 2x   22x       22x 22x 17x 17x   2x   12x   2x 34x 1x   33x   2x 15x   1x   14x 14x   2x 7x 7x 7x   6x   2x 1x   1x 3x 1x   1x 1x 1x 1x         2x 3x   3x 9x 3x   3x 3x 3x 3x         2x 1x   1x 2x 1x   1x 1x         2x 3x   3x 6x 3x   3x 3x           2x 2x 2x 2x   2x   1x         2x 1x
import * as Adapter from './Adapter';
import { Event, IBreadcrumb, IUser, LogLevel } from './Interfaces';
import * as Context from './Interfaces/Context';
import { DSN } from './Interfaces/DSN';
import { IOptions } from './Options';
import { SentryError } from './Sentry';
 
export class Client {
  public readonly dsn: DSN;
  private _adapter: Adapter.IAdapter;
  private _context: Context.IContext;
  private _isInstalled: Promise<boolean>;
 
  constructor(
    dsn:I string,
    public options: IOptions = {
      logLevIel: LogLevel.Error,
      maxBreIadcrumbs: 100,
    }
  ) {
    this.dsn = new DSN(dsn);
    this._context = Context.getDefaultContext();
    return this;
  }
E
  public getContext() {
    // TODO: check for cyclic objects
    return JSON.parse(JSON.stringify(this._context));
  }

  public getAdapter<A extends Adapter.IAdapter>(): A {
    if (!this._adapter) {
      throw new SentryError('No adapter in use, please call .use(<Adapter>)');
    }
    return this._adapter as A;
  }
 
  public use<A extends Adapter.IAdapter, O extends {}>(
    adapter: { new (client: Client, options?: O): A },
    options?: O
  ): Client {
    if (this._adapter) {
      // TODO: implement unregister
      thErow new RangeError(
        'There is already a Adapter registered, call unregister() to remove current adapter'
      );
    }
    this._adapter = new adapter(this, options);
    return this;
  }
 
  public install(): Promise<this> {
    if (!this._isInstalled) {
      this._isInstalled = this.getAdapter().install();
    }
    return this._isInstalled.then(() => this);
  }
 
  public async captureException(exception: Error) {
    const adapter = await this.awaitAdapter();
    return this.send(await adapter.captureException(exception));
  }
 
  public async captureMessage(message: string) {
    const adapter = await this.awaitAdapter();
    return this.send(await adapter.captureMessage(message));
  }
 
  public async captureBreadcrumb(crumb: IBreadcrumb) {
    const adapter = await this.awaitAdapter();
    return adapter.captureBreadcrumb(crumb);
  }
E
  public async send(event: Event) {
    const adapter = await this.awaitAdapter();
    return adapter.send(event);
  }
 
  // ---------------- HELPER
 
  public log(...args: any[]) {
    if (this.options.logLevel >= LogLevel.Debug) {
      // tslint:disable-next-line
      console.log(...args);
    }
  }
 
  // -----------------------
 
  // ---------------- CONTEXT
 
  public setUserContext(user?: IUser) {
    Context.set(this._context, 'user', user);
    // TODO: Remove this once we moved code away from adapters
    const adapter = this.getAdapter();
    if (adapter.setUserContext) {
      adapter.setUserContext(user);
    }
    // -------------------------------------------------------
    return this;
  }
 
  public setTagsContext(tags?: { [key: string]: any }) {
    Context.mergeIn(this._context, 'tags', tags);
    // TODO: Remove this once we moved code away from adapters
    const adapter = this.getAdapter();
    if (adapter.setTagsContext) {
      adapter.setTagsContext(tags);
    }
    // -------------------------------------------------------
    return this;
  }
 
  public setExtraContext(extra?: { [key: string]: any }) {
    Context.mergeIn(this._context, 'extra', extra);
    // TODO: Remove this once we moved code away from adapters
    const adapter = this.getAdapter();
    if (adapter.setExtraContext) {
      adapter.setExtraContext(extra);
    }
    // -------------------------------------------------------
    return this;
  }
 
  public clearContext() {
    this._context = Context.getDefaultContext();
    // TODO: Remove this once we moved code away from adapters
    const adapter = this.getAdapter();
    if (adapter.clearContext) {
      adapter.clearContext();
    }
    // -------------------------------------------------------
    return this;
  }
 
  // ------------------------
 
  private awaitAdapter(): Promise<Adapter.IAdapter> {
    const adapter = this.getAdapter();
    if (!this._isInstalled) {
      throw new SentryError(
        'Please call install() before calling other methods on Sentry'
      );
    }
    return this._isInstalled.then(() => this._adapter);
  }
}