Skip to content

Commit

Permalink
refactor: remove unneeded checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bhovhannes committed May 31, 2018
1 parent f6be34e commit 7772427
Showing 1 changed file with 7 additions and 40 deletions.
47 changes: 7 additions & 40 deletions src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ export class Api {
* @return {Promise} A promise which will resolved with API key if everything went ok and rejected otherwise
*/
getApiKey(username: string, password: string): Promise<string> {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
if (typeof this._httpOptions.headers.apiKey !== 'undefined') {
resolve(this._httpOptions.headers.apiKey)
}
Expand Down Expand Up @@ -188,10 +185,7 @@ export class Api {
* @return {Promise} A promise which will resolved if everything went ok and rejected otherwise
*/
clearApiKey() {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const req = this.execute('USER', null, 'clearApiKey') as Promise<any>
req.then((result) => {
if (result) {
Expand Down Expand Up @@ -307,9 +301,6 @@ export class Api {
* @return {Promise} A promise which will resolved with logged in user data if everything went ok and rejected otherwise
*/
login(username: string, password: string) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
const req = this.request('login', {username: username, password: password}, null, Api.Methods.POST)
return (req as Promise<any>).then((data) => {
this.setSessionID(data.sessionID)
Expand All @@ -323,10 +314,7 @@ export class Api {
* @return {Promise} A promise which will resolved if everything went ok and rejected otherwise
*/
logout(): Promise<undefined> {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const req = this.request('logout', null, null, Api.Methods.GET);
(req as Promise<any>).then((result) => {
if (result && result.success) {
Expand Down Expand Up @@ -512,9 +500,6 @@ export class Api {
search(objCode: string, query?: object, fields?: TFields, useHttpPost = false) {
let searchQuery, method
if (useHttpPost) {
if (this._uriGenerationMode) {
throw new Error('Using useHttpPost=true is not supported in batch mode for this method')
}
searchQuery = objectAssign({}, query, {method: Api.Methods.GET})
method = Api.Methods.POST
}
Expand Down Expand Up @@ -543,9 +528,6 @@ export class Api {
batch(uriCollector: (batchApi: IBatchApi) => string[], isAtomic?: false): Promise<any[]>
batch(uriCollector: (batchApi: IBatchApi) => string[], isAtomic?: true): Promise<undefined>
batch(uriCollector: (batchApi: IBatchApi) => string[], isAtomic?: boolean): Promise<any[] | undefined> {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
const batchApi = batchApiFactory(this)
const uris = uriCollector(batchApi)
if (uris.length === 0) {
Expand Down Expand Up @@ -574,9 +556,6 @@ export class Api {
* @return {string} returns the given api key value
*/
setApiKey(apiKey) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
return this._httpOptions.headers.apiKey = apiKey
}

Expand All @@ -586,10 +565,7 @@ export class Api {
* @param {String|undefined} sessionID sessionID to set
*/
setSessionID(sessionID) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
if (sessionID) {
if (sessionID) {
this._httpOptions.headers.sessionID = sessionID
}
else {
Expand All @@ -603,10 +579,7 @@ export class Api {
* @param {String|undefined} xsrfToken X-XSRF-TOKEN to set
*/
setXSRFToken(xsrfToken?: string) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
if (xsrfToken) {
if (xsrfToken) {
this._httpOptions.headers['X-XSRF-TOKEN'] = xsrfToken
}
else {
Expand All @@ -626,19 +599,13 @@ export class Api {
* @param {String} filename Override the filename
*/
uploadFromStream(stream: Readable, filename: string) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
const data = new NodeFormData()
const data = new NodeFormData()
data.append('uploadedFile', stream, filename)
return this.request('upload', data, null, Api.Methods.POST)
}

uploadFileContent(fileContent, filename: string) {
if (this._uriGenerationMode) {
throw new Error('This method is not supported in batch mode')
}
const data = new GlobalScope.FormData()
const data = new GlobalScope.FormData()
data.append('uploadedFile', fileContent, filename)
return this.request('upload', data, null, Api.Methods.POST)
}
Expand Down

0 comments on commit 7772427

Please sign in to comment.