@wireapp/api-client
Advanced tools
Comparing version 0.0.4 to 0.0.5
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const axios_1 = require("axios"); | ||
const ContentType_1 = require("../http/ContentType"); | ||
const StatusCode_1 = require("../http/StatusCode"); | ||
class AuthAPI { | ||
@@ -20,34 +17,31 @@ constructor(client) { | ||
} | ||
login(login) { | ||
const url = `${AuthAPI.URL.LOGIN}?persist=${login.persist}`; | ||
const instance = axios_1.default.create({ | ||
baseURL: this.client.baseURL, | ||
postCookiesRemove(login, labels) { | ||
const config = { | ||
data: { | ||
email: login.email, | ||
labels: labels, | ||
password: login.password.toString() | ||
}, | ||
method: 'post', | ||
url: `${AuthAPI.URL.COOKIES}/remove` | ||
}; | ||
return this.client.sendRequest(config); | ||
} | ||
postLogin(login) { | ||
const config = { | ||
data: { | ||
email: login.email, | ||
password: login.password.toString() | ||
}, | ||
headers: { | ||
'Content-Type': ContentType_1.default.APPLICATION_JSON, | ||
withCredentials: true | ||
} | ||
}); | ||
return instance.post(url, { | ||
email: login.email, | ||
password: login.password + '', | ||
}).then(function (response) { | ||
}, | ||
method: 'post', | ||
url: `${AuthAPI.URL.LOGIN}?persist=${login.persist}` | ||
}; | ||
return this.client.sendJSONRequest(config).then((response) => { | ||
return response.data; | ||
}).catch((error) => { | ||
if (error.response.status === StatusCode_1.default.TOO_MANY_REQUESTS && login.email) { | ||
return this.removeCookies(login).then(() => this.login(login)); | ||
} | ||
else { | ||
throw error; | ||
} | ||
}); | ||
} | ||
removeCookies(login, labels) { | ||
const url = this.client.createUrl(`${AuthAPI.URL.COOKIES}/remove`); | ||
return axios_1.default.post(url, { | ||
email: login.email, | ||
labels: labels, | ||
password: login.password, | ||
}); | ||
} | ||
} | ||
exports.default = AuthAPI; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const HttpClient_1 = require("../http/HttpClient"); | ||
const UserAPI_1 = require("../user/UserAPI"); | ||
class WireAPIClient { | ||
@@ -19,2 +20,5 @@ constructor(urls) { | ||
}; | ||
this.user = { | ||
api: undefined | ||
}; | ||
this.CONNNECTION_URL.REST = urls.rest; | ||
@@ -24,7 +28,12 @@ this.CONNNECTION_URL.WebSocket = urls.ws; | ||
this.auth.api = new AuthAPI_1.default(this.http.client); | ||
this.user.api = new UserAPI_1.default(this.http.client); | ||
} | ||
login(data) { | ||
return this.auth.api.login(data); | ||
return this.auth.api.postLogin(data) | ||
.then((accessToken) => { | ||
this.http.client.accessToken = accessToken; | ||
return accessToken; | ||
}); | ||
} | ||
} | ||
exports.default = WireAPIClient; |
@@ -0,0 +0,0 @@ "use strict"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const axios_1 = require("axios"); | ||
const ContentType_1 = require("./ContentType"); | ||
class HttpClient { | ||
@@ -10,3 +12,18 @@ constructor(baseURL) { | ||
} | ||
sendRequest(config) { | ||
config.baseURL = this.baseURL; | ||
return axios_1.default.request(config); | ||
} | ||
sendJSONRequest(config) { | ||
config.headers = config.headers || {}; | ||
Object.assign(config.headers, { | ||
baseURL: this.baseURL, | ||
'Content-Type': ContentType_1.default.APPLICATION_JSON | ||
}); | ||
if (!config.headers.withCredentials) { | ||
config.headers.Authorization = `${this.accessToken.token_type} ${this.accessToken.access_token}`; | ||
} | ||
return this.sendRequest(config); | ||
} | ||
} | ||
exports.default = HttpClient; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class StatusCode { | ||
static get TOO_MANY_REQUESTS() { | ||
return 429; | ||
} | ||
} | ||
StatusCode.ACCEPTED = 202; | ||
StatusCode.BAD_GATEWAY = 502; | ||
StatusCode.BAD_REQUEST = 400; | ||
StatusCode.CONFLICT = 409; | ||
StatusCode.CONNECTIVITY_PROBLEM = 0; | ||
StatusCode.CREATED = 201; | ||
StatusCode.FORBIDDEN = 403; | ||
StatusCode.INTERNAL_SERVER_ERROR = 500; | ||
StatusCode.NO_CONTENT = 204; | ||
StatusCode.NOT_FOUND = 404; | ||
StatusCode.OK = 200; | ||
StatusCode.PRECONDITION_FAILED = 412; | ||
StatusCode.REQUEST_TIMEOUT = 408; | ||
StatusCode.REQUEST_TOO_LARGE = 413; | ||
StatusCode.TOO_MANY_REQUESTS = 429; | ||
StatusCode.UNAUTHORIZED = 401; | ||
exports.default = StatusCode; |
"use strict"; | ||
const WireAPIClient_1 = require("./core/WireAPIClient"); | ||
module.exports = WireAPIClient_1.default; |
@@ -0,0 +0,0 @@ import WireAPIClient from "./commonjs"; |
@@ -22,8 +22,12 @@ const argv = require('optimist') | ||
client.login(login) | ||
.then((result) => { | ||
console.log('Login successful', result); | ||
.then((accessTokenData) => { | ||
console.log('Login successful', accessTokenData); | ||
return client.user.api.getSelf(); | ||
}) | ||
.then((userData) => { | ||
console.log('Got self user', userData.name); | ||
}) | ||
.catch((error) => { | ||
console.error(error.message); | ||
console.error(error.message, error); | ||
}); | ||
@@ -12,4 +12,6 @@ { | ||
"optimist": "0.6.1", | ||
"rimraf": "2.6.1", | ||
"sinon": "1.17.6 ", | ||
"sinon-har-server": "0.3.0", | ||
"typescript": "2.3.2", | ||
"webpack": "2.5.1", | ||
@@ -27,4 +29,5 @@ "webpack-dev-server": "2.4.5" | ||
"scripts": { | ||
"clean": "rimraf dist/commonjs", | ||
"coverage": "istanbul cover --report html ./node_modules/jasmine/bin/jasmine.js", | ||
"dist": "tsc", | ||
"dist": "yarn clean && tsc", | ||
"lint": "echo \"No linting specified\" && exit 0", | ||
@@ -34,7 +37,7 @@ "preversion": "yarn lint && yarn dist && yarn test", | ||
"postversion": "git push && git push --tags", | ||
"start": "concurrently \"tsc -w\" \"yarn watch\"", | ||
"start": "yarn clean && concurrently \"tsc -w\" \"yarn watch\"", | ||
"test": "jasmine", | ||
"watch": "webpack-dev-server --config webpack.config.js --open" | ||
}, | ||
"version": "0.0.4" | ||
"version": "0.0.5" | ||
} |
@@ -0,0 +0,0 @@ ----- |
@@ -1,5 +0,4 @@ | ||
import axios, {AxiosError, AxiosInstance, AxiosPromise, AxiosResponse} from 'axios'; | ||
import ContentType from '../http/ContentType'; | ||
import {AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios'; | ||
import HttpClient from '../http/HttpClient'; | ||
import StatusCode from '../http/StatusCode'; | ||
@@ -21,37 +20,33 @@ export default class AuthAPI { | ||
public login(login: LoginData): any { | ||
const url: string = `${AuthAPI.URL.LOGIN}?persist=${login.persist}`; | ||
postCookiesRemove(login: LoginData, labels?: string[]): AxiosPromise { | ||
const config: AxiosRequestConfig = { | ||
data: { | ||
email: login.email, | ||
labels: labels, | ||
password: login.password.toString() | ||
}, | ||
method: 'post', | ||
url: `${AuthAPI.URL.COOKIES}/remove` | ||
}; | ||
const instance: AxiosInstance = axios.create({ | ||
baseURL: this.client.baseURL, | ||
return this.client.sendRequest(config); | ||
} | ||
public postLogin(login: LoginData): Promise<AccessTokenData> { | ||
const config: AxiosRequestConfig = { | ||
data: { | ||
email: login.email, | ||
password: login.password.toString() | ||
}, | ||
headers: { | ||
'Content-Type': ContentType.APPLICATION_JSON, | ||
withCredentials: true | ||
} | ||
}); | ||
}, | ||
method: 'post', | ||
url: `${AuthAPI.URL.LOGIN}?persist=${login.persist}` | ||
}; | ||
return instance.post(url, { | ||
email: login.email, | ||
password: login.password + '', // Safety net if someone enters only numbers | ||
}).then(function (response: AxiosResponse) { | ||
return this.client.sendJSONRequest(config).then((response: AxiosResponse) => { | ||
return response.data; | ||
}).catch((error: AxiosError) => { | ||
if (error.response.status === StatusCode.TOO_MANY_REQUESTS && login.email) { | ||
// Backend blocked our user account from login, so we have to reset our cookies | ||
return this.removeCookies(login).then(() => this.login(login)); | ||
} else { | ||
throw error; | ||
} | ||
}); | ||
} | ||
removeCookies(login: LoginData, labels?: string[]): AxiosPromise { | ||
const url = this.client.createUrl(`${AuthAPI.URL.COOKIES}/remove`); | ||
return axios.post(url, { | ||
email: login.email, | ||
labels: labels, | ||
password: login.password, | ||
}); | ||
} | ||
} |
interface LoginData { | ||
email: string; | ||
phone: string; | ||
password: string; | ||
password: number | string; | ||
code: string; | ||
persist: boolean; | ||
} |
@@ -1,3 +0,4 @@ | ||
import AuthAPI from "../auth/AuthAPI"; | ||
import HttpClient from "../http/HttpClient"; | ||
import AuthAPI from '../auth/AuthAPI'; | ||
import HttpClient from '../http/HttpClient'; | ||
import UserAPI from '../user/UserAPI'; | ||
@@ -18,2 +19,6 @@ export default class WireAPIClient { | ||
public user: { api: UserAPI } = { | ||
api: undefined | ||
}; | ||
constructor(public urls: { rest: string, ws?: string }) { | ||
@@ -24,8 +29,14 @@ this.CONNNECTION_URL.REST = urls.rest; | ||
this.http.client = new HttpClient(this.CONNNECTION_URL.REST); | ||
this.auth.api = new AuthAPI(this.http.client); | ||
this.user.api = new UserAPI(this.http.client); | ||
} | ||
public login(data: LoginData): any { | ||
return this.auth.api.login(data); | ||
public login(data: LoginData): Promise<AccessTokenData> { | ||
return this.auth.api.postLogin(data) | ||
.then((accessToken: AccessTokenData) => { | ||
this.http.client.accessToken = accessToken; | ||
return accessToken; | ||
}); | ||
} | ||
} |
@@ -0,2 +1,8 @@ | ||
import axios, {AxiosPromise, AxiosRequestConfig} from 'axios'; | ||
import ContentType from './ContentType'; | ||
export default class HttpClient { | ||
public accessToken: AccessTokenData; | ||
constructor(public baseURL: string) { | ||
@@ -8,2 +14,22 @@ } | ||
} | ||
public sendRequest(config: AxiosRequestConfig): AxiosPromise { | ||
config.baseURL = this.baseURL; | ||
return axios.request(config); | ||
} | ||
public sendJSONRequest(config: AxiosRequestConfig): AxiosPromise { | ||
config.headers = config.headers || {}; | ||
Object.assign(config.headers, { | ||
baseURL: this.baseURL, | ||
'Content-Type': ContentType.APPLICATION_JSON | ||
}); | ||
if (!config.headers.withCredentials) { | ||
config.headers.Authorization = `${this.accessToken.token_type} ${this.accessToken.access_token}`; | ||
} | ||
return this.sendRequest(config); | ||
} | ||
} |
export default class StatusCode { | ||
public static get TOO_MANY_REQUESTS(): number { | ||
return 429; | ||
} | ||
public static readonly ACCEPTED: number = 202; | ||
public static readonly BAD_GATEWAY: number = 502; | ||
public static readonly BAD_REQUEST: number = 400; | ||
public static readonly CONFLICT: number = 409; | ||
public static readonly CONNECTIVITY_PROBLEM: number = 0; | ||
public static readonly CREATED: number = 201; | ||
public static readonly FORBIDDEN: number = 403; | ||
public static readonly INTERNAL_SERVER_ERROR: number = 500; | ||
public static readonly NO_CONTENT: number = 204; | ||
public static readonly NOT_FOUND: number = 404; | ||
public static readonly OK: number = 200; | ||
public static readonly PRECONDITION_FAILED: number = 412; | ||
public static readonly REQUEST_TIMEOUT: number = 408; | ||
public static readonly REQUEST_TOO_LARGE: number = 413; | ||
public static readonly TOO_MANY_REQUESTS: number = 429; | ||
public static readonly UNAUTHORIZED: number = 401; | ||
} |
@@ -0,0 +0,0 @@ module.exports = { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
611901
52
3784
11
3