1.38.6 • Published 6 years ago

@weflex/gian v1.38.6

Weekly downloads
4
License
UNLICENSED
Repository
github
Last release
6 years ago

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:

propertyvalue
envstring in "dev", "staging" and "production"
optionsoptions
options.storagedefault: localStorage
options.onAuthFaila function fired when authorization failed

To get the gateway info:

propertydescription
client.gateway.apiAPI gateway URI
client.gateway.appAPP gateway URI
client.gateway.wsWebSocket 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

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.rest.org

Organization Member

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.rest.orgMember

Member

propertyvalue
baseContext.RESTFul
namespaceGianClient.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

propertyvalue
baseContext.RESTFul
namespaceGianClient.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

propertyvalue
baseContext.RESTFul
namespaceGianClient.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

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.class

Class Template

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.classTemplate

Class Package

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.classPackage

Order

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.order

Payment

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.payment

Coin

propertyvalue
baseContext.RESTFul
namespaceGianClient.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

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.externalResource

Trainer

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.trainer

Membership

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.membership

PTSchedule

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.ptschedule

PTSession

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.ptsession

CheckIn

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.checkin

Operation

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.operation

Installation

propertyvalue
baseContext.RESTFul
namespaceGianClient.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

propertyvalue
baseContext.RESTFul
namespaceGianClient.prototype.notification

CHANGELOG

See CHANGELOG.md

1.38.6

6 years ago

1.38.5

6 years ago

1.38.4

6 years ago

1.38.3

6 years ago

1.38.2

6 years ago

1.38.1

6 years ago

1.38.0

6 years ago

1.37.7

6 years ago

1.37.6

6 years ago

1.37.5

6 years ago

1.37.4

6 years ago

1.37.3

6 years ago

1.37.2

6 years ago

1.37.1

6 years ago

1.37.0

6 years ago

1.36.0

6 years ago

1.35.6

6 years ago

1.35.5

6 years ago

1.35.4

6 years ago

1.35.3

7 years ago

1.35.2

7 years ago

1.35.1

7 years ago

1.35.0

7 years ago

1.34.1

7 years ago

1.34.0

7 years ago

1.33.0

7 years ago

1.32.1

7 years ago

1.32.0

7 years ago

1.31.1

7 years ago

1.31.0

7 years ago

1.30.1

7 years ago

1.30.0

7 years ago

1.29.2

7 years ago

1.29.1

7 years ago

1.29.0

7 years ago

1.28.2

8 years ago

1.28.1

8 years ago

1.16.11

8 years ago

1.16.10

8 years ago

1.16.9

8 years ago

1.16.8

8 years ago

1.16.7

8 years ago

1.16.6

8 years ago

1.16.5

8 years ago

1.16.4

8 years ago

1.16.3

8 years ago

1.16.2

8 years ago

1.16.1

8 years ago

1.16.0

8 years ago

1.15.1

8 years ago

1.15.0

8 years ago

1.14.2

8 years ago

1.14.1

8 years ago

1.14.0

8 years ago

1.13.1

8 years ago

1.13.0

8 years ago

1.12.0

8 years ago

1.11.10

8 years ago

1.11.9

8 years ago

1.11.8

8 years ago

1.11.7

8 years ago

1.11.6

8 years ago

1.11.5

8 years ago

1.11.4

8 years ago

1.11.3

8 years ago

1.11.2

8 years ago

1.11.1

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.4

8 years ago

1.9.3

8 years ago

1.9.2

8 years ago

1.9.1

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.32

8 years ago

1.4.31

8 years ago

1.4.30

8 years ago

1.4.29

8 years ago

1.4.28

8 years ago

1.4.27

8 years ago

1.4.26

8 years ago

1.4.25

8 years ago

1.4.24

8 years ago

1.4.23

8 years ago

1.4.22

8 years ago

1.4.21

8 years ago

1.4.20

8 years ago

1.4.19

8 years ago

1.4.18

8 years ago

1.4.17

8 years ago

1.4.16

8 years ago

1.4.15

8 years ago

1.4.14

8 years ago

1.4.13

8 years ago

1.4.12

8 years ago

1.4.11

8 years ago

1.4.10

8 years ago

1.4.9

8 years ago

1.4.8

8 years ago

1.4.7

8 years ago

1.4.6

8 years ago

1.4.5

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.3

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago