@wireapp/api-client
Advanced tools
Comparing version
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var WebSocket = require("ws"); | ||
var EventEmitter = require("events"); | ||
var AuthAPI_1 = require("../auth/AuthAPI"); | ||
var HttpClient_1 = require("../http/HttpClient"); | ||
var UserAPI_1 = require("../user/UserAPI"); | ||
var WireAPIClient = (function (_super) { | ||
__extends(WireAPIClient, _super); | ||
var WireAPIClient = (function () { | ||
function WireAPIClient(urls) { | ||
var _this = _super.call(this) || this; | ||
_this.urls = urls; | ||
_this.CONNNECTION_URL = { | ||
this.urls = urls; | ||
this.CONNNECTION_URL = { | ||
REST: undefined, | ||
WebSocket: undefined | ||
}; | ||
_this.auth = { | ||
this.auth = { | ||
api: undefined | ||
}; | ||
_this.http = { | ||
this.http = { | ||
client: undefined | ||
}; | ||
_this.user = { | ||
this.user = { | ||
api: undefined | ||
}; | ||
_this.CONNNECTION_URL.REST = urls.rest; | ||
_this.CONNNECTION_URL.WebSocket = urls.ws; | ||
_this.http.client = new HttpClient_1.default(_this.CONNNECTION_URL.REST); | ||
_this.auth.api = new AuthAPI_1.default(_this.http.client); | ||
_this.user.api = new UserAPI_1.default(_this.http.client); | ||
return _this; | ||
this.CONNNECTION_URL.REST = urls.rest; | ||
this.CONNNECTION_URL.WebSocket = urls.ws; | ||
this.http.client = new HttpClient_1.default(this.CONNNECTION_URL.REST); | ||
this.auth.api = new AuthAPI_1.default(this.http.client); | ||
this.user.api = new UserAPI_1.default(this.http.client); | ||
} | ||
WireAPIClient.prototype.connectToWebSocket = function (accessToken, clientId) { | ||
var url = this.CONNNECTION_URL.WebSocket + "/await?access_token=" + accessToken + "&client=" + clientId; | ||
var socket = new WebSocket(url); | ||
socket.binaryType = 'arraybuffer'; | ||
socket.on('message', function message(data) { | ||
this.emit(WireAPIClient.TOPIC.WEB_SOCKET_MESSAGE, data.byteLength); | ||
}); | ||
return new Promise(function (resolve) { | ||
socket.on('open', function open() { | ||
resolve(socket); | ||
}); | ||
}); | ||
}; | ||
WireAPIClient.prototype.login = function (data, shouldConnectToSocket) { | ||
WireAPIClient.prototype.login = function (data) { | ||
var _this = this; | ||
@@ -61,5 +33,2 @@ return this.auth.api.postLogin(data) | ||
_this.http.client.accessToken = accessToken; | ||
if (shouldConnectToSocket) { | ||
_this.connectToWebSocket(accessToken.access_token, ''); | ||
} | ||
return accessToken; | ||
@@ -69,6 +38,3 @@ }); | ||
return WireAPIClient; | ||
}(EventEmitter)); | ||
WireAPIClient.TOPIC = { | ||
WEB_SOCKET_MESSAGE: 'WireAPIClient.TOPIC.WEB_SOCKET_MESSAGE' | ||
}; | ||
}()); | ||
exports.default = WireAPIClient; |
@@ -19,3 +19,2 @@ "use strict"; | ||
Object.assign(config.headers, { | ||
baseURL: this.baseURL, | ||
'Content-Type': ContentType_1.default.APPLICATION_JSON | ||
@@ -22,0 +21,0 @@ }); |
@@ -21,7 +21,3 @@ const argv = require('optimist') | ||
const client = new WireAPIClient(urls); | ||
client.on(WireAPIClient.TOPIC.WEB_SOCKET_MESSAGE, function(notification) { | ||
console.log('Received notification via WebSocket', notification); | ||
}); | ||
client.login(login, true) | ||
client.login(login) | ||
.then((accessTokenData) => { | ||
@@ -28,0 +24,0 @@ console.log('Login successful', accessTokenData); |
{ | ||
"author": "Wire", | ||
"dependencies": { | ||
"axios": "0.16.1", | ||
"ws": "2.3.1" | ||
"axios": "0.16.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "7.0.18", | ||
"@types/ws": "0.0.41", | ||
"concurrently": "3.4.0", | ||
@@ -41,3 +39,3 @@ "istanbul": "0.4.5", | ||
}, | ||
"version": "0.0.6" | ||
"version": "0.0.7" | ||
} |
@@ -1,4 +0,1 @@ | ||
import * as WebSocket from 'ws'; | ||
import EventEmitter = require("events"); | ||
import AuthAPI from '../auth/AuthAPI'; | ||
@@ -8,3 +5,3 @@ import HttpClient from '../http/HttpClient'; | ||
export default class WireAPIClient extends EventEmitter { | ||
export default class WireAPIClient { | ||
private CONNNECTION_URL: { REST: string, WebSocket: string } = { | ||
@@ -27,9 +24,3 @@ REST: undefined, | ||
public static TOPIC = { | ||
WEB_SOCKET_MESSAGE: 'WireAPIClient.TOPIC.WEB_SOCKET_MESSAGE' | ||
}; | ||
constructor(public urls: { rest: string, ws?: string }) { | ||
super(); | ||
this.CONNNECTION_URL.REST = urls.rest; | ||
@@ -44,26 +35,6 @@ this.CONNNECTION_URL.WebSocket = urls.ws; | ||
public connectToWebSocket(accessToken: string, clientId: string): Promise<WebSocket> { | ||
const url = `${this.CONNNECTION_URL.WebSocket}/await?access_token=${accessToken}&client=${clientId}`; | ||
const socket = new WebSocket(url); | ||
socket.binaryType = 'arraybuffer'; | ||
socket.on('message', function message(data: ArrayBuffer) { | ||
this.emit(WireAPIClient.TOPIC.WEB_SOCKET_MESSAGE, data.byteLength); | ||
}); | ||
return new Promise((resolve) => { | ||
socket.on('open', function open() { | ||
resolve(socket); | ||
}); | ||
}); | ||
} | ||
public login(data: LoginData, shouldConnectToSocket: boolean): Promise<AccessTokenData> { | ||
public login(data: LoginData): Promise<AccessTokenData> { | ||
return this.auth.api.postLogin(data) | ||
.then((accessToken: AccessTokenData) => { | ||
this.http.client.accessToken = accessToken; | ||
if (shouldConnectToSocket) { | ||
this.connectToWebSocket(accessToken.access_token, ''); | ||
} | ||
return accessToken; | ||
@@ -70,0 +41,0 @@ }); |
@@ -24,3 +24,2 @@ import axios, {AxiosPromise, AxiosRequestConfig} from 'axios'; | ||
Object.assign(config.headers, { | ||
baseURL: this.baseURL, | ||
'Content-Type': ContentType.APPLICATION_JSON | ||
@@ -27,0 +26,0 @@ }); |
1
-50%11
-8.33%612838
-0.44%3805
-1.58%- Removed
- Removed
- Removed
- Removed