Code Monkey home page Code Monkey logo

loopback-nodejs-client's Introduction

loopback-nodejs-client

NPM Version Coverage Status Build Status Downloads Dependency Status License

NodeJS client library with loopback SDK syntax

Install

$ npm install --save loopback-nodejs-client

Usage

Without authentification

var LoopbackClient = require('loopback-nodejs-client')

var loopbackClient = new LoopbackClient('http://localhost:3000/api');

var Customers = loopbackClient.getModel('customers')

Customers.create({name: 'ido4pro', city: 'plougonvelin'})
  .then(function (customer) {
    console.log(customer)

    Customers.find({filter: {where: {name: 'ido4pro'}}})
      .then(function (customers) {
        console.log(customers)

      })
      .catch(function (err) {
        console.log(err)
      })

    Customers.count({where: {name: 'ido4pro'}})
      .then(function (customers) {
        console.log(customers)

      })
      .catch(function (err) {
        console.log(err)
      })

  })
  .catch(function (err) {
    console.log(err)
  })

With authentification

var loopbackClientWithAuth = new LoopbackClient('http://localhost:3000/api','xxx EMAIL USER xxx', 'xxx PASSWORD xxx')

loopbackClientWithAuth.createToken()
  .then(function()
  {
      
       var Customers = loopbackClientWithAuth.getModel('customers')
       
       Customers.count({where: {name: 'ido4pro'}})
            .then(function (customers) {
              console.log(customers)
      
            })
            .catch(function (err) {
              console.log(err)
            })
      
  })

With custom login path

var loopbackClientWithAuth = new LoopbackClient('http://localhost:3000/api','xxx EMAIL USER xxx', 'xxx PASSWORD xxx')


// Default Value is : '/users/login?include=user'
loopbackClientWithAuth.setCustomLoginPath('/user/login?include=user')

loopbackClientWithAuth.createToken()
  .then(function()
  {
      
       var Customers = loopbackClientWithAuth.getModel('customers')
       
       Customers.count({where: {name: 'ido4pro'}})
            .then(function (customers) {
              console.log(customers)
      
            })
            .catch(function (err) {
              console.log(err)
            })
      
  })

Methods

Try to be compliant with https://loopback.io/doc/en/lb3/Working-with-data.html

create(data) --> data = { id: 'xxx', filter: {...} } (filter is optional)

count(query) --> query = { where : {name: 'xxx'} }, where = { where: {and: [{name: 'xxx'},{city: 'xxx'}]

*NOT AVAILABLE IN LOOPBACK EXTERNALY * updateAll(query,data)

updateById(id,data)

updateAttributesById(id,data)

find(filter)

findById(data)

findOne(query)

deleteById(id)

replaceOrCreate(data)

upsertWithWhere(query,data) --> query = { where : {name: 'xxx'} }

replaceOrCreate(data)

remote(customRoute,data) -> your customRoute name (example: greet) see https://loopback.io/doc/en/lb3/Remote-methods.html

remote(customRoute,query,data) -> your customRoute name (example: greet) see https://loopback.io/doc/en/lb3/Remote-methods.html

remoteGet(customRoute,query)

remotePut(customRoute,data)

remotePatch(customRoute,data)

Options

All methods accepts an option object to define timeout and/or additionnals headers

find({},{ timeout:2000, headers:{'TAG_ID':'146464646464}})

Unit Tests

Check the test/index.js

DEBUG

This lib use debug (https://github.com/visionmedia/debug) to log request

Activate log trace with environment variable "DEBUG=LoopBackClient"

example : DEBUG=LoopBackClient npm run test

License

MIT © Stéphane GUILLY

loopback-nodejs-client's People

Contributors

fizzl avatar ido4pro avatar petitchevalroux avatar sguilly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

loopback-nodejs-client's Issues

remote get/put

hi
please add get request for custom remote methods.
something like this

remoteGet(name,query) {
    const url = `${this.baseUrl}/${this.model}/${name}`;
    return this.get(url,query);
  }

Needed to add .data to createToken promise handler

I'm using LoopBack v3.19.0 with a custom extension to the User model. Using the LNC, authentication against the custom endpoint works fine, but I needed to change

this.token = result.id;

resolve(result.id);

to

this.token = result.data.id

resolve(result.data.id)

I'm not sure if this is a one-off because of the customer User model.

UnhandledPromiseRejectionWarning: Error: Not Found

node: v8.11.4
npm: 6.9.0

Trace:
(node:49595) UnhandledPromiseRejectionWarning: Error: Not Found
at handleErrors (.../node_modules/loopback-nodejs-client/src/LoopbackClient.js:13:11)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:49595) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:49595) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any idea on how to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.