Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new API to get and set location of user to new collection #82

Open
wants to merge 9 commits into
base: multiple-clients
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,27 @@ export interface ILivechatUploadAPI {
rid: string
file: File
}

/** Structure for sending location data to the server */
export interface ILivechatSessionAPI {
location: Location
token: string
deviceInfo: Device
}

/** Browser and device info */
export interface Device {
os: string
osVersion: number
browserName: string
browserVersion: number
}

/** Location data sent by the API */
export interface Location {
city: string
countryCode: string
countryName: string
latitude: number
longitude: number
}
21 changes: 16 additions & 5 deletions src/lib/api/Livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
INewLivechatCustomFieldsAPI,
ILivechatRoom,
INewLivechatRoomCredentialAPI,
ILivechatUploadAPI
ILivechatUploadAPI,
ILivechatSessionAPI
} from '../../interfaces'

import ApiBase from './api'
Expand All @@ -37,18 +38,28 @@ export default class ApiLivechat extends ApiBase {
}
return visitor
}
async deleteVisitor () { return (await this.del(`livechat/visitor/${this.credentials.token}`)).visitor}
async updateVisitorStatus(status: string) { return (await this.post(`livechat/visitor.status`, { token: this.credentials.token, status })).status }
async nextAgent (department: string = '' ) { return (await this.get(`livechat/agent.next/${this.credentials.token}`, { department })).agent }
updateVisitorSessionOnRegister (guest: INewLivechatGuestAPI) {
return this.put('livechat/session.register', guest, false)
}
async deleteVisitor () {
return (await this.del(`livechat/visitor/${this.credentials.token}`)).visitor
}
async updateVisitorStatus (status: string) { return (await this.post(`livechat/visitor.status`, { token: this.credentials.token, status })).status }
async nextAgent (department: string = '') { return (await this.get(`livechat/agent.next/${this.credentials.token}`, { department })).agent }
async agent ({ rid }: any) { return (await this.get(`livechat/agent.info/${rid}/${this.credentials.token}`)).agent }
async message (id: string, params: ILivechatRoom) { return (await this.get(`livechat/message/${id}`, { token: this.credentials.token, ...params } )).message }
async message (id: string, params: ILivechatRoom) { return (await this.get(`livechat/message/${id}`, { token: this.credentials.token, ...params })).message }
sendMessage (message: INewLivechatMessageAPI) { return (this.post('livechat/message', { ...message, token: this.credentials.token }, false)) }
editMessage (id: string, message: INewLivechatMessageAPI) { return (this.put(`livechat/message/${id}`, message, false)) }
deleteMessage (id: string, { rid }: ILivechatRoom) { return (this.del(`livechat/message/${id}`, { rid, token: this.credentials.token }, false)) }
async loadMessages (rid: string, params?: ILivechatRoomMessagesAPI) { return (await this.get(`livechat/messages.history/${rid}`, { ...params, token: this.credentials.token }, false)).messages }
async sendOfflineMessage (message: INewLivechatOfflineMessageAPI) { return (await this.post('livechat/offline.message', { ...message }, false)).message }
sendVisitorNavigation (page: INewLivechatNavigationAPI) { return (this.post('livechat/page.visited', { ...page }, false)) }
requestTranscript (email: string, { rid }: ILivechatRoom) { return (this.post('livechat/transcript', { token: this.credentials.token, rid, email }, false)) }
sendSessionData (sessionData: ILivechatSessionAPI) {
return (this.post('livechat/session.register', { ...sessionData }, false))
}
async updateSessionStatus (status: string, token: string) { return (await this.post(`livechat/session.updateSessionStatus`, { token: token, status })).status }
updateVisitCount (token: string) { return this.post(`livechat/session.incVisitCount/${token}`) }
videoCall ({ rid }: ILivechatRoom) { return this.get(`livechat/video.call/${this.credentials.token}`, { rid }, false) }
sendCustomField (field: INewLivechatCustomFieldAPI) { return this.post('livechat/custom.field', field, false) }
sendCustomFields (fields: INewLivechatCustomFieldsAPI) { return this.post('livechat/custom.fields', fields, false) }
Expand Down
10 changes: 5 additions & 5 deletions src/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ class Client implements IClient {
return fetch(`${this.host}/api/v1/${encodeURI(url)}?${this.getParams(data)}`, {
method: 'GET',
headers: this.getHeaders(options)
}).then(this.handle) as Promise<any>
}).then(this.handle)
}
post (url: string, data: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'POST',
body: this.getBody(data),
headers: this.getHeaders(options)
}).then(this.handle) as Promise<any>
}).then(this.handle)
}
put (url: string, data: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'PUT',
body: this.getBody(data),
headers: this.getHeaders(options)
}).then(this.handle) as Promise<any>
}).then(this.handle)
}

delete (url: string, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'DELETE',
headers: this.getHeaders(options)
}).then(this.handle) as Promise<any>
}).then(this.handle)
}
private async handle (r: any) {
const { status } = r
Expand Down Expand Up @@ -220,7 +220,7 @@ export default class Api extends EventEmitter {
if (!result) throw new Error(`API ${ method } ${ endpoint } result undefined`)
if (!this.success(result, ignore)) throw result
this.logger && this.logger.debug(`[API] ${method} ${endpoint} result ${result.status}`)
const hasDataInsideResult = result && !result.data;
const hasDataInsideResult = result && !result.data
return (method === 'DELETE') && hasDataInsideResult ? result : result.data
} catch (err) {
this.logger && this.logger.error(`[API] POST error(${ endpoint }): ${ JSON.stringify(err) }`)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IDriver {

onTyping (cb: ICallback): Promise<any>

notifyVisitorTyping(rid: string, username: string, typing: boolean, token: string): Promise<any>
notifyVisitorTyping (rid: string, username: string, typing: boolean, token: string): Promise<any>

methodCall (method: string, ...args: any[]): Promise<any>
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class MQTTDriver extends EventEmitter implements ISocket, IDriver {
}

notifyVisitorTyping(rid: string, username: string, typing: boolean, token: string): Promise<any> {
return Promise.resolve() as any;
return Promise.resolve() as any
}

onStreamData (name: string, cb: ICallback): Promise<any> {
Expand Down