All files / core/lib Client.ts

100% Statements 94/94
91.67% Branches 11/12
100% Functions 19/19
100% Lines 69/69
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  2x 2x 2x   2x   2x               78x         26x 20x 20x     2x 45x 1x   44x     2x       18x   1x       17x 17x     15x 15x 14x   14x     2x 2x 1x     2x 3x 3x     2x 1x 1x     2x 3x 3x         2x 2x   1x           2x 1x 1x 1x         2x   12x     2x 1x 1x 1x 1x     2x 6x 6x 6x 6x     2x 2x 2x 2x 2x     2x 3x 3x 3x 3x         22x 22x 22x 1x       21x   2x  
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: string,
    public Eoptions: IOptions = {
      logLevel: LogLevel.Error,
      maxBreadcrumbs: 100,
    }
  ) {
    this.dsn = new DSN(dsn);
    this._context = Context.getDefaultContext();
    return this;
  }
 
  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
      throw 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);
  }
 
  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);
    }
  }
 
  // -----------------------
 
  public async setRelease(release: string) {
    const adapter = await this.awaitAdapter();
    await adapter.setRelease(release);
    return this;
  }
 
  // ---------------- CONTEXT
 
  public getContext() {
    // TODO: check for cyclic objects
    return JSON.parse(JSON.stringify(this._context));
  }
 
  public async setUserContext(user?: IUser) {
    Context.set(this._context, 'user', user);
    const adapter = await this.awaitAdapter();
    await adapter.setUserContext(user);
    return this;
  }
 
  public async setTagsContext(tags?: { [key: string]: any }) {
    Context.mergeIn(this._context, 'tags', tags);
    const adapter = await this.awaitAdapter();
    await adapter.setTagsContext(tags);
    return this;
  }
 
  public async setExtraContext(extra?: { [key: string]: any }) {
    Context.mergeIn(this._context, 'extra', extra);
    const adapter = await this.awaitAdapter();
    await adapter.setExtraContext(extra);
    return this;
  }
 
  public async clearContext() {
    this._context = Context.getDefaultContext();
    const adapter = await this.awaitAdapter();
    await 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);
  }
}