vert-x3 / vertx-service-proxy

EventBus Proxy generation
Apache License 2.0
67 stars 58 forks source link

Allow for generating TypeScript service proxies for Angular #58

Closed InfoSec812 closed 5 years ago

InfoSec812 commented 7 years ago

Example TypeScript implementation:

declare type HandlerType = (err: any, result: any) => any;

class AmbassadorsService {

  private closed = false;

  // There's some ambiguity here around what are the right params....
  // See: https://github.com/vert-x3/vertx-lang-js/issues/67
  public static create(vertx: any, address: string): void {
    vertx.send(address,
      {'vertx': vertx},
      {'action': 'create'},
      (err, result) =>
        handler(err, result && new AmbassadorsService(vertx, result.headers.proxyaddr)));
  }

  public static createProxy(vertx: any, handler: HandlerType,
    eb: any, address: string, ebAddress: string): void {
    eb.send(address,
      {'vertx': vertx,
      'ebAddress': ebAddress},
      {'action': 'createProxy'});
  };

  constructor(private j_eb: any, private j_address: string) {
  }

  // TODO: do we need this?!?!?
  private convCharCollection(coll: any) {
    const ret = [];
      for (let i = 0; i < coll.length; i++) {
        ret.push(String.fromCharCode(coll[i]));
      }
      return ret;
  }

  public createOne(data: any, handler: HandlerType): AmbassadorsService {
    if (data === null || data === undefined) {
      throw new TypeError('function invoked with invalid arguments');
    }
    this.checkNotClosed();
    this.j_eb.send(this.j_address,
      {'data': data},
      {'action': 'createOne'},
      (err, result) => handler(err, result && result.body));
    return this;
  }

  public delete(id: string, handler: HandlerType): AmbassadorsService {
    if (id === null || id === undefined) {
      throw new TypeError('function invoked with invalid arguments');
    }
    this.checkNotClosed();
    this.j_eb.send(this.j_address,
      {'id' : id},
      {'action': 'delete'},
      (err, result) => handler(err, result && result.body));
    return this;
  }

  public getAll(handler: HandlerType): AmbassadorsService {
    this.checkNotClosed();
    this.j_eb.send(this.j_address,
      {},
      {'action': 'getAll'},
      (err, result) => handler(err, result && result.body));
    return this;
  }

  public getById(id: string, handler: HandlerType): AmbassadorsService {
    if (id === null || id === undefined) {
      throw new TypeError('function invoked with invalid arguments');
    }
    this.checkNotClosed();
    this.j_eb.send(this.j_address,
      {'id': id},
      {'action': 'getById'},
      (err, result) => handler(err, result && result.body));
    return this;
  }

  public commands(command: any, handler: HandlerType): AmbassadorsService {
    if (command === null || command === undefined) {
      throw new TypeError('function invoked with invalid arguments');
    }
    this.checkNotClosed();
    this.j_eb.send(this.j_address,
      {'command': command},
      {'action': 'commands'},
      (err, result) => handler(err, result && result.body));
    return this;
  }

  public close(): void {
    this.checkNotClosed();
    this.j_eb.send(this.j_address, {}, {'action': 'close'});
    closed = true;
  }

  private checkNotClosed() {
    if (this.closed) {
      throw new Error('Proxy is closed');
    }
  }
}
InfoSec812 commented 7 years ago

See https://github.com/vert-x3/vertx-lang-js/issues/67 as well. We're not sure that the JS is being generated properly, so this may be wrong.

pmlopes commented 7 years ago

see vert-x3/vertx-web#674