Hypertrack client library

This library helps in fetching and updating HyperTrack entities.

The library is written in typescript but can be used with vanilla javascript

Installation

npm install ht-client
Prerequesit

Install HyperTrack library internally used by ht-client.

npm i ht-data
npm i ht-utility
npm i ht-models -D // if used in typescript

Install other helper libraries

npm i rxjs
npm i underscore
npm i date-fns

Usage

Library exposes htClient as a global variable when used as es5. Otherwise can be used with es6+ by directly importing the relevent class/functions of the library.

javascript

var client = htClient.initClient({token: TOKEN});

typescript

import {initClient} from "ht-client"

var TOKEN = "sk_xxxxxxxxxxxxxxxxxxxxxxxxx"
var client = initClient({token: TOKEN});

Setup

initClient(options: {token?: string, request?: HtRequest}) => HtClient Before any feature of the library is used client needs to be initialised. Use factory function initClient with token as its argument to initialize the client.

javascript

var TOKEN = "sk_xxxxxxxxxxxxxxxxxxxxxxxxx"
var client = htClient.initClient({token: TOKEN});

typescript

import { initClient} from 'ht-client';

var TOKEN = "sk_xxxxxxxxxxxxxxxxxxxxxxxxx"
var client = initClient({token: TOKEN});

Users client

usersClientFactory(config: Partial<IUsersClientConfig> = {}): HtUsersClient

HtUsersClient class contains users related client functions. This class can be initialized using usersClientFactory factory function. This takes in optional config object of following interface.

import {IDateRange} from "ht-models";

export interface IUsersClientConfig {
  dateRange$: Observable<IDateRange>,
}
  • dateRange$ : Date range observable for the client can be passed using this params.
import { usersClientFactory} from 'ht-client';

var usersClient = usersClientFactory();

usersClientFactory can be run multiple times to create multiple instances of user client.

HtUsersClient exposes following users features

  • placeline
  • list
  • listAll (users across pages)

Users placeline client

HtUsersClient.placeline This class exposes placeline related apis.

Fetch placeline

HtUsersClient.placeline.api$: (id: string, query: object): Observable<IUserData>

Fetches placeline data of user. This will fetch placeline data only once.

var usersClient = usersClientFactory();
usersClient.placeline.api$(<id>, {}).subscribe((data: IUserData) => {
  console.log(data);
})

Set user id

HtUsersClient.placeline.setId(userId: string | null)

To get placeline data of a user on regular interval, pass a userId to start fetching placeline data of the user. Fetched data is available via placeline.data$ observable. Set to null to clear existing placeline.

var usersClient = usersClientFactory();
var id = "423dsasd234234";
usersClient.placeline.setId(id);
usersClient.placeline.data$.subscribe(data => {
  //emits placeline data on regular interval
})

Set placeline query

HtUsersClient.placeline.setQuery(query: object)

Pass additional query object for the placeline api. example {day: "2017-10-29"}

Consume placeline data

HtUsersClient.placeline.data$: Observable<IUserData>

To consume placeline data fetched by the client library on regular interval, subscribe to data$ property of placeline client.

Get loading state of placeline

HtUsersClient.placeline.loading$: Observable<boolean | string>

This observable will emit false (for false loading state) or 'string' as user Id (for true loading state)

Get current placeline id being fetched

HtUsersClient.placeline.id$: Observable<null | string>

Observable which emits current user id which is being fetched.

Users list client

HtUsersClient.list

This class exposes users list related apis.

Fetch list of users

HtUsersClient.list.api$(query: object): Observable<Page<IUsersAnalytics>>

Fetches users list once.

var usersClient = usersClientFactory();
usersClient.list.api$({page_size: 10}).subscribe((data: Page<IUsersAnalytics>) => {
  console.log(data);
})

Fetch and update list of users

HtUsersClient.list.setActive(active: boolean = true)

By called setActive(), the client will start fetching the users list data. The fetched data can be consumed by data$ property of list client.

Set default query

HtUsersClient.list.defaultQuery: object

Set this to an object which will be default query params for the api.

Set query

HtUsersClient.list.setQuery(query: object)

Set query which will be used to fetch the users list. example {day: "2017-10-29"}. This will override default query.

Configure update strategy

HtUsersClient.list.updateStrategy: 'once' | 'live' | poll': 'once'

  • once : Will fetch users list once.
  • live Will fetch users list with provided query on regular interval
  • poll Will fetch users list once and updated the current users in the list on regular interval

Consume fetched users list

HtUsersClient.list.data$: Observable<Page<IUsersAnalytics>>

Emits users list as paginated data on regular interval.

var usersClient = usersClientFactory();
usersClient.list.setActive();
usersClient.list.data$.subscribe((data: Page<IUsersAnalytics>) => {
  data //emitted users list
})

HtUsersClient.list.dataArray$: Observable<IUsersAnalytics[]>

Emits users list as array of users.

Get loading state of users list

HtUsersClient.list.loading$: Observable<boolean | string>

This observable will emit false (for false loading state) or true as user Id (for true loading state)

Get api query

HtUsersClient.list.apiQuery$: Observable<object>

Observable emits final query object of the api. Merges query, default query and date range query.

All Users client

HtUsersClient.listAll

This class exposes apis to fetch users across pages. Useful for showing all active users on map.

Fetch all users

HtUsersClient.listAll.api$(query: object): Observable<IUsersAnalytics[]>

Fetches all users once.

var usersClient = usersClientFactory();
usersClient.listAll.api$({page_size: 10}).subscribe((data: IUsersAnalytics[]) => {
  console.log(data);
})

Fetch and update all users

HtUsersClient.list.setActive(active: boolean = true)

By called setActive(), the client will start fetching the users list data. The fetched data can be consumed by data$ property of list client. Calling this function multiple times will start fetching all users data again.

Set default query

HtUsersClient.listAll.defaultQuery: object

Set this to an object which will be default query params for the api. By default the api fetches all users in batch of 100. Set defaultQuery = {page_size: 200} to change the batch size.

Set query

HtUsersClient.listAll.setQuery(query: object)

Set query which will be used to fetch the users list. example {day: "2017-10-29"}. This will override default query.

Configure update strategy

HtUsersClient.listAll.updateStrategy: 'once' | 'live' | poll': 'once'

  • once : Will fetch users list once.
  • live Will fetch users list with provided query on regular interval
  • poll Will fetch users list once and updated the current users in the list on regular interval

By default this is set to 'once' for all users list, as this is a heavy api calls which fetches all the users.

Consume all fetched users

HtUsersClient.listAll.data$: Observable<Page<IUsersAnalytics>>

Emits users list as paginated data on regular interval.

var usersClient = usersClientFactory();
usersClient.listAll.setActive();
usersClient.listAll.data$.subscribe((data: All<IUsersAnalytics>) => {
  data //emitted users list
})

HtUsersClient.listAll.dataArray$: Observable<IUsersAnalytics[]>

Emits users list as array of users.

Get loading state

HtUsersClient.listAll.loading$: Observable<boolean | string>

This observable will emit false (for false loading state) or true as user Id (for true loading state)

Get api query

HtUsersClient.listAll.apiQuery$: Observable<object>

Observable emits final query object of the api. Merges query, default query and date range query.

results matching ""

    No results matching ""