0
votes

I'm using Fireloop with Loopback 3 and wanting to know how best to create typesafe hooks and remote methods using type checked PersistedModel and Validatable methods. I'd like to change the type of the constructor from ...

constructor(public model: any) { }

to ...

constructor(public model: SomeType) { }

I'd like to make PersistedModel calls like

this.model.count().then((n) => ...); 

OR Validatable calls like:

model.validatesLengthOf('code', { 
    min: 6, max: 12, message: { min: 'too short',  max: 'too long'} 
});

The Fireloop examples like the one below only use any as type of this.model. The firestarter model samples and Fireloop documentation were also of no use here.

I know that there is a type called ModelConstructor declared in the fireloop source tree under core/index.d.ts. This interface looks correct because it implements all the PersistedModel and Validatable methods but where is it published in npmjs? Is it already part of the Fireloop server SDK or do I need to npm install it? No idea.

import { Model } from '@mean-expert/model';

/**
 * @module Account
 * @description
 * Write a useful Account Model description.
 * Register hooks and remote methods within the
 * Model Decorator
 **/
@Model({
  hooks: {
    beforeSave: { name: 'before save', type: 'operation' }
  },
  remotes: {
    myRemote: {
      returns: { arg: 'result', type: 'array' },
      http: { path: '/my-remote', verb: 'get' }
    }
  }
})

class Account {
  // LoopBack model instance is injected in constructor
  constructor(public model: any) { }

  // Example Operation Hook
  beforeSave(ctx: any, next: Function): void {
    console.log('Account: Before Save', ctx.instance);    
    next();
  }
  // Example Remote Method
  myRemote(next: Function): void {
    this.model.find(next);
  }
}

module.exports = Account;

Finially, I've also attempted to use the Loopback 3 Typescript definitions but hit more problems as the PersistedModel methods here are all declared as static so fail type checks and return Promise<T> | void. The later means you’re forced to type cast the result back to just Promise<T> so it seems like the type def authors have never actually used them. Is this a bug or am I missing something? Can't find any working examples to prove otherwise.

This is the server side API pain. Client side REST API for Fireloop is also undocumented (lots of example for Real-time API) but none for the REST api it's also supposed to include (just mentioned once in one issue). Would be nice to find it can all be type checked by Typescript.

1

1 Answers

0
votes

I found that ModelConstructor validation methods were missing arguments like { message: 'my error message' } and methods like exists() returned Promise<any> instead of Promise<boolean>.

The type definitions in Loopback 3 Type definitions were more complete but were unusable unless fixed as described above.

In the end I used ..

# Used modified type defs from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/loopback/index.d.ts

import { Validatable } from '../types/validatable';
 # Fixed static methods and return types.
import { PersistedModel } from '../types/loopback';

constructor(public MyModel: Validatable | PersistedModel) {
  let Model = MyModel as Validatable;
  Model.validatesLengthOf('code', {
    min: 6, 
    max: 80, 
    message: { min: 'too short',  max: 'too long' } });
  Model.validatesFormatOf('email', 
    { with: this.reEmail, 
      message: 'invalid email address', 
      allowNull: false });
  Model.validatesInclusionOf('role', { 
    in: RoleNames, 
    message: 'is not valid' 
  });
}

And in later methods ..

let Model = this.MyModel as PersistedModel;

// knows that isFound is boolean
Model.exists(code).then(isFound => { ... });