All files / src client.ts

95.12% Statements 39/41
87.5% Branches 21/24
100% Functions 17/17
95% Lines 38/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1431x 1x     1x                                       1x 16x 48x   1x     1x         16x 160x   1x     15x   15x     15x   15x                         1x               16x 16x 16x 16x 16x   16x       10x 10x               10x 10x 8x 8x   8x     10x   10x         1x       1x       3x       1x       1x       3x               2x                    
import Debug from 'debug';
import { convert, Timeout } from '@feathersjs/errors';
import { Params } from '@feathersjs/feathers';
 
const debug = Debug('@feathersjs/transport-commons/client');
 
const namespacedEmitterMethods = [
  'addListener',
  'emit',
  'listenerCount',
  'listeners',
  'on',
  'once',
  'prependListener',
  'prependOnceListener',
  'removeAllListeners',
  'removeListener'
];
const otherEmitterMethods = [
  'eventNames',
  'getMaxListeners',
  'setMaxListeners'
];
 
const addEmitterMethods = (service: any) => {
  otherEmitterMethods.forEach(method => {
    service[method] = function(...args: any[]) {
      if (typeof this.connection[method] !== 'function') {
        throw new Error(`Can not call '${method}' on the client service connection`);
      }
 
      return this.connection[method](...args);
    };
  });
 
  // Methods that should add the namespace (service path)
  namespacedEmitterMethods.forEach(method => {
    service[method] = function(name: string, ...args: any[]) {
      if (typeof this.connection[method] !== 'function') {
        throw new Error(`Can not call '${method}' on the client service connection`);
      }
 
      const eventName = `${this.path} ${name}`;
 
      debug(`Calling emitter method ${method} with ` +
        `namespaced event '${eventName}'`);
 
      const result = this.connection[method](eventName, ...args);
 
      return result === this.connection ? this : result;
    };
  });
};
 
interface ServiceOptions {
  name: string;
  connection: any;
  method: string;
  events?: string[];
  timeout?: number;
}
 
export class Service {
  events: string[];
  path: string;
  connection: any;
  method: string;
  timeout: number;
 
  constructor(options: ServiceOptions) {
    this.events = options.events;
    this.path = options.name;
    this.connection = options.connection;
    this.method = options.method;
    this.timeout = options.timeout || 5000;
 
    addEmitterMethods(this);
  }
 
  send(method: string, ...args: any[]) {
    return new Promise((resolve, reject) => {
      const timeoutId = setTimeout(() => reject(
        new Timeout(`Timeout of ${this.timeout}ms exceeded calling ${method} on ${this.path}`, {
          timeout: this.timeout,
          method,
          path: this.path
        })
      ), this.timeout);
 
      args.unshift(method, this.path);
      args.push(function(error: any, data: any) {
        error = convert(error);
        clearTimeout(timeoutId);
 
        return error ? reject(error) : resolve(data);
      });
 
      debug(`Sending socket.${this.method}`, args);
 
      this.connection[this.method](...args);
    });
  }
 
  find(params: Params = {}) {
    return this.send('find', params.query || {});
  }
 
  get(id: number|string, params: Params = {}) {
    return this.send('get', id, params.query || {});
  }
 
  create(data: any, params: Params = {}) {
    return this.send('create', data, params.query || {});
  }
 
  update(id: number|string, data: any, params: Params = {}) {
    return this.send('update', id, data, params.query || {});
  }
 
  patch(id: number|string, data: any, params: Params = {}) {
    return this.send('patch', id, data, params.query || {});
  }
 
  remove(id: number|string, params: Params = {}) {
    return this.send('remove', id, params.query || {});
  }
 
  // `off` is actually not part of the Node event emitter spec
  // but we are adding it since everybody is expecting it because
  // of the emitter-component Socket.io is using
  off(name: string, ...args: any[]) {
    if (typeof this.connection.off === 'function') {
      return this.connection.off(`${this.path} ${name}`, ...args);
    } else if (args.length === 0) {
      // @ts-ignore
      return this.removeAllListeners(name);
    }
 
    // @ts-ignore
    return this.removeListener(name, ...args);
  }
}