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

[NEW] Added function to delete visitor data #45

Merged
merged 2 commits into from
Jan 7, 2019
Merged
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
5 changes: 3 additions & 2 deletions src/lib/api/Livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default class ApiLivechat extends ApiBase {
token: visitor.token
}
return visitor
}
async agent ({ rid }: any) { return (await this.get(`livechat/agent.info/${rid}/${this.credentials.token}`)).agent }
}
async deleteVisitor () { return (await this.del(`livechat/visitor/${this.credentials.token}`)).visitor}
async nextAgent ({ department }: any) { 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 }
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)) }
Expand Down
5 changes: 3 additions & 2 deletions src/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Client implements IClient {

delete (url: string, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'DEL',
method: 'DELETE',
headers: this.getHeaders(options)
}).then(this.handle) as Promise<any>
}
Expand Down Expand Up @@ -220,7 +220,8 @@ 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}`)
return (method === 'DELETE') ? result : result.data
const hasDataInsideResult = result && !result.data;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcosSpessatto, just a suggestion, I would implement something like the statement below:

const { data } = result;
return (method === 'DELETE') && (data || result);

It's just a suggestion, your code is working fine as well.

return (method === 'DELETE') && hasDataInsideResult ? result : result.data
} catch (err) {
this.logger && this.logger.error(`[API] POST error(${ endpoint }): ${ JSON.stringify(err) }`)
throw err
Expand Down