lib/index.js
function addRemoteMethods(app) {
  return Object.keys(app.models).forEach(modelName => {
    const Model = app.models[modelName]

    if (typeof Model._templates !== 'function') {
      return null
    }

    return Object.keys(Model._templates()).forEach(templateName => {
      const fnName = `_template_${templateName}`
      const fnNameRemote = `_template_${templateName}_remote`
      const path = `/${fnName}`

      // Don't add the method if it already exists on the model
      if (typeof Model[fnName] === 'function') {
        debug('Method already exists: %s.%s', Model.modelName, fnName)
        return null
      }

      // Don't add the remote method if it already exists on the model
      if (hasRemoteMethod(Model, fnNameRemote)) {
        debug('Remote method already exists: %s.%s', Model.modelName, fnName)
        return null
      }

      debug('Create remote method for %s.%s', Model.modelName, fnName)

      // The normal method does not need to be wrapped in a promise
      Model[fnName] = (options, params) => {
        // Get the random data
        const template = Model._templates(params)[templateName]

        // Overwrite the template with the passed in options
        _.forEach(options, (value, key) => {
          _.set(template, key, value)
        })

        return template
      }

      const fnSpecRemote = {
        description: `Generate template ${templateName}`,
        accepts: [
          { type: 'Object', arg: 'options', description: 'Overwrite values of template' },
          { type: 'Object', arg: 'params', description: 'Pass parameters into the template method' },
        ],
        returns: [ { type: 'Object', arg: 'result', root: true } ],
        http: { path, verb: 'get' },
      }

      // Support for loopback 2.x.
      if (app.loopback.version.startsWith(2)) {
        fnSpecRemote.isStatic = true
      }

      // Define the remote method on the model
      Model.remoteMethod(fnNameRemote, fnSpecRemote)

      // The remote method needs to be wrapped in a promise
      Model[fnNameRemote] = (options, params) => new Promise(resolve => resolve(Model[fnName](options, params)))

      // Send result as plain text if the content is a string
      Model.afterRemote(fnNameRemote, (ctx, result, next) => {
        if (typeof ctx.result !== 'string') {
          return next()
        }
        ctx.res.setHeader('Content-Type', 'text/plain')
        return ctx.res.end(ctx.result)
      })
      return true
    })
  })
}
function addRemoteMethods(app) {
  return Object.keys(app.models).forEach(modelName => {
    const Model = app.models[modelName]

    if (typeof Model._templates !== 'function') {
      return null
    }

    return Object.keys(Model._templates()).forEach(templateName => {
      const fnName = `_template_${templateName}`
      const fnNameRemote = `_template_${templateName}_remote`
      const path = `/${fnName}`

      // Don't add the method if it already exists on the model
      if (typeof Model[fnName] === 'function') {
        debug('Method already exists: %s.%s', Model.modelName, fnName)
        return null
      }

      // Don't add the remote method if it already exists on the model
      if (hasRemoteMethod(Model, fnNameRemote)) {
        debug('Remote method already exists: %s.%s', Model.modelName, fnName)
        return null
      }

      debug('Create remote method for %s.%s', Model.modelName, fnName)

      // The normal method does not need to be wrapped in a promise
      Model[fnName] = (options, params) => {
        // Get the random data
        const template = Model._templates(params)[templateName]

        // Overwrite the template with the passed in options
        _.forEach(options, (value, key) => {
          _.set(template, key, value)
        })

        return template
      }

      const fnSpecRemote = {
        description: `Generate template ${templateName}`,
        accepts: [
          { type: 'Object', arg: 'options', description: 'Overwrite values of template' },
          { type: 'Object', arg: 'params', description: 'Pass parameters into the template method' },
        ],
        returns: [ { type: 'Object', arg: 'result', root: true } ],
        http: { path, verb: 'get' },
      }

      // Support for loopback 2.x.
      if (app.loopback.version.startsWith(2)) {
        fnSpecRemote.isStatic = true
      }

      // Define the remote method on the model
      Model.remoteMethod(fnNameRemote, fnSpecRemote)

      // The remote method needs to be wrapped in a promise
      Model[fnNameRemote] = (options, params) => new Promise(resolve => resolve(Model[fnName](options, params)))

      // Send result as plain text if the content is a string
      Model.afterRemote(fnNameRemote, (ctx, result, next) => {
        if (typeof ctx.result !== 'string') {
          return next()
        }
        ctx.res.setHeader('Content-Type', 'text/plain')
        return ctx.res.end(ctx.result)
      })
      return true
    })
  })
}
function addRemoteMethods ( app ) { return Object . keys ( app . models ) . forEach ( modelName => { const Model = app . models [ modelName ] if ( typeof Model . _templates !== 'function' ) { return null } return Object . keys ( Model . _templates ( ) ) . forEach ( templateName => { const fnName = ` ${ templateName } ` const fnNameRemote = ` ${ templateName } ` const path = ` ${ fnName } ` // Don't add the method if it already exists on the model if ( typeof Model [ fnName ] === 'function' ) { debug ( 'Method already exists: %s.%s' , Model . modelName , fnName ) return null } // Don't add the remote method if it already exists on the model if ( hasRemoteMethod ( Model , fnNameRemote ) ) { debug ( 'Remote method already exists: %s.%s' , Model . modelName , fnName ) return null } debug ( 'Create remote method for %s.%s' , Model . modelName , fnName ) // The normal method does not need to be wrapped in a promise Model [ fnName ] = ( options , params ) => { // Get the random data const template = Model . _templates ( params ) [ templateName ] // Overwrite the template with the passed in options _ . forEach ( options , ( value , key ) => { _ . set ( template , key , value ) } ) return template } const fnSpecRemote = { description : ` ${ templateName } ` , accepts : [ { type : 'Object' , arg : 'options' , description : 'Overwrite values of template' } , { type : 'Object' , arg : 'params' , description : 'Pass parameters into the template method' } , ] , returns : [ { type : 'Object' , arg : 'result' , root : true } ] , http : { path , verb : 'get' } , } // Support for loopback 2.x. if ( app . loopback . version . startsWith ( 2 ) ) { fnSpecRemote . isStatic = true } // Define the remote method on the model Model . remoteMethod ( fnNameRemote , fnSpecRemote ) // The remote method needs to be wrapped in a promise Model [ fnNameRemote ] = ( options , params ) => new Promise ( resolve => resolve ( Model [ fnName ] ( options , params ) ) ) // Send result as plain text if the content is a string Model . afterRemote ( fnNameRemote , ( ctx , result , next ) => { if ( typeof ctx . result !== 'string' ) { return next ( ) } ctx . res . setHeader ( 'Content-Type' , 'text/plain' ) return ctx . res . end ( ctx . result ) } ) return true } ) } ) }
Add remote methods for each template.

@param {Object} app loopback application
@param {Object} config component configuration
Add remote methods for each template .