@weflex/gian

1.38.6 • Public • Published

gian.js

The fat client-side library for next-generation WeFlex apps, inspired by "胖虎" in DORAEMON

Usage

var gian = require('@weflex/gian');
var client = new gian.GianClient('dev');

Installation

$ npm install @weflex/gian --save

Test

$ npm test

API Documentation

Constructor

To initialize a GianClient:

import { GianClient } from '@weflex/gian';
const client = new GianClient('dev'); // for dev

The arguments of constructor:

property value
env string in "dev", "staging" and "production"
options options
options.storage default: localStorage
options.onAuthFail a function fired when authorization failed

To get the gateway info:

property description
client.gateway.api API gateway URI
client.gateway.app APP gateway URI
client.gateway.ws WebSocket gateway URI

Or you can intialize a client with your customized gateway config:

const client = new GianClient({
  api: 'http://192.168.1.102',
  app: 'http://192.168.1.101',
  ws: 'ws://192.168.1.102'
});

Note: this is just for developing and debugging, don't use it in production.

.getClient(env, options)

This library provides the cache way to get instance as well:

const client = gian.getClient('dev', options);

Context

Every new {GianClient} instance has a {Context} object, which does:

  • parse gateway out from given env from gateway.json.
  • manage the life cycle of session/localStorage.

Note: this context shouldn't be directly accessed in user-land.

.request(endpoint, method, data)

Make an origin request with given parameters:

  • endpoint {String} the url like "/api/users".
  • method {String} method string like "get", "post".
  • data {Object}

.requestMiddleware(url, data)

Make an origin request to get response with the following possible parameters:

  • url {String} the path of middleware.
  • data {Object} the object post to api server

.requestView(name, where, sortBy, limit, skip)

Make an origin request to get views with the following possible parameters:

  • name {String} the name of view
  • where {Object} the where object on this view
  • sortBy {Object} the sortBy object on this view
  • limit {Number} the limit on this view
  • skip {Number} the skip on this view

.restify(model, properties)

Returns a {RESTFul} object and also extend by the 2nd argument properties.

  • model {String} the model plural name to join in request url, like "users", "venues".
  • properties {Object} the object will be attached to the returned object.

RESTFul

Every {RESTFul} object is only returned and extended by the function Context.prototype.restify. filter document Description

.list(filter)

List resources by the object filter and return a {Promise} object with results.

  • filter {Object} The filter object, optional.

.count(filter)

Count resources by the object filter and return a {Promise} object with results. example: Query the order total number with what venue.

  await client.order.count({where:{venueId: 'String'}});
  //=> {count: number}
  • filter {Object} The filter object, optional.

.create(resouce)

Create a resource with resouce and return a {Promise} with creating status.

  • {Resource} resouce - the resouce data that you want to create.

.get(id)

Get the {Resource} object by id and return a {Promise} with res.body.

.update(id, patches)

Update the specified resouce by id and patches, it returns a {Promise} with updated resource/resouce.

  • id {String} The id to find the resouce to be updated.
  • patches {Object} Same to the type {Resource}, but required rules.

.upsert(resouce)

Upsert a resouce.

  • resouce {Resource} The resouce to be upsert-ed.

.remove(id)

Remove a resouce instance by specified id.

  • id {String} The resouce id.

Organization

property value
base Context.RESTFul
namespace GianClient.prototype.rest.org

Organization Member

property value
base Context.RESTFul
namespace GianClient.prototype.rest.orgMember

Member

property value
base Context.RESTFul
namespace GianClient.prototype.rest.member

.listCheckInsById(id, [filter]

List CheckIn history of member with id.

const startOfDay = require('date-fns/start_of_day');
const endOfDay = require('date-fns/end_of_day');

const today = new Date();

let checkIns;
try {
    checkIns = await client.member.listCheckInsById(memberId, {
      where: {
        timestamp: {
          lt: startOfDay(today),
          gt: endOfDay(today)
        }
      }
    }
} catch (error) {
  // handle errors
}

User

property value
base Context.RESTFul
namespace GianClient.prototype.rest.user

.login(username, password)

Login with username and password.

  • username {String} The username string
  • password {String} The password string

Example:

try {
  await client.rest.user.login(username, password);
} catch (err) {
  alert(err);
}

.logout()

Logout

.pending(onrenderCode, onloggedIn)

Create a web socket connection to passport-server, and get notified on onloggedIn when remote login gets done.

  • onrenderCode {Function} The trigger when get random code from server.
  • onloggedIn {Function} The callback when remote login gets done.

This returns a {Promise} with the generated id by passport-server and composite the authorization url.

class LoginPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      url: null
    };
  }
  componentDidMount() {
    client.user.pending(
      this.onrenderCode.bind(this),
      this.onlogged.bind(this)
    );
  }
  onrenderCode(code) {
    this.setState({
      url: 'http://api.theweflex.com/auth/wechat?state=' + btoa('qrcode:' + code)
    });
  }
  onlogged() {
    alert('login successfully');
  }
  render() {
    return <QRCode value={this.state.url} size={128} />;
  }
}

.getCurrent()

Get the profile of logged user.

.getVenueById(id)

Get venue by id, if the id is omit, returns selected venue.

.setVenueById(id)

Set selected venue id, if the id is omit, will set from venues' first element.

.smsRequest(phone)

Make a request to send a message to given phone, this will require the following service:

  • resque-workers:send

Before testing this functionality, make sure your redis and resque-workers are up correctly.

.smsLogin(phone, smscode)

Login with phone and what you got code from SMS:

try {
  const payload = await user.smsLogin(phone, this.smscodeInput.value);
  // payload.userId
  // payload.passcode
} catch (err) {
  // handle errors from gateway or networks
}

You are able to save userId and passcode secretly for later sessions.

.smsRegisterNewOrgAndVenue(phone, smscode, options)

Register an organization and venue with a smscode, calling this function correctly will auto login as well.

Venue

property value
base Context.RESTFul
namespace GianClient.prototype.venue

.listCheckInsById(id, [filter]

List CheckIn history of venue with id.

const startOfDay = require('date-fns/start_of_day');
const endOfDay = require('date-fns/end_of_day');

const today = new Date();

let checkIns;
try {
    checkIns = await client.venue.listCheckInsById(venueId, {
      where: {
        timestamp: {
          lt: startOfDay(today),
          gt: endOfDay(today)
        }
      }
    }
} catch (error) {
  // handle errors
}

Class

property value
base Context.RESTFul
namespace GianClient.prototype.class

Class Template

property value
base Context.RESTFul
namespace GianClient.prototype.classTemplate

Class Package

property value
base Context.RESTFul
namespace GianClient.prototype.classPackage

Order

property value
base Context.RESTFul
namespace GianClient.prototype.order

Payment

property value
base Context.RESTFul
namespace GianClient.prototype.payment

Coin

property value
base Context.RESTFul
namespace GianClient.prototype.coin

.batch(data, count)

Batch count coins with data.

  • data {Coin} the coin to be created
  • count {Number} the number to create
client.rest.coins.batch({
  owner: 'WeFlex',
  //...
}, 100);

External Resource

property value
base Context.RESTFul
namespace GianClient.prototype.externalResource

Trainer

property value
base Context.RESTFul
namespace GianClient.prototype.trainer

Membership

property value
base Context.RESTFul
namespace GianClient.prototype.membership

PTSchedule

property value
base Context.RESTFul
namespace GianClient.prototype.ptschedule

PTSession

property value
base Context.RESTFul
namespace GianClient.prototype.ptsession

CheckIn

property value
base Context.RESTFul
namespace GianClient.prototype.checkin

Operation

property value
base Context.RESTFul
namespace GianClient.prototype.operation

Installation

property value
base Context.RESTFul
namespace GianClient.prototype.installation

Register an installation namely device:

client.create({
  appId: 'your appid',
  userId: 'your userId',
  deviceToken: 'your device token'
})

To show the complete model, visit pancake:weflex/installation

Notification

property value
base Context.RESTFul
namespace GianClient.prototype.notification

CHANGELOG

See CHANGELOG.md

Dependents (0)

Package Sidebar

Install

npm i @weflex/gian

Weekly Downloads

4

Version

1.38.6

License

UNLICENSED

Unpacked Size

447 kB

Total Files

45

Last publish

Collaborators

  • pbalan
  • suqin