Skip to content

Commit 4e980b2

Browse files
authored
Merge pull request #7 from laurayco/use-async
Simplify code
2 parents b2a4528 + 1a38917 commit 4e980b2

File tree

9 files changed

+76
-196
lines changed

9 files changed

+76
-196
lines changed

lib/character.js

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ class Character extends Lib {
1212
page
1313
}
1414
*/
15-
search(name, params = {}) {
16-
return new Promise((resolve, reject) => {
17-
if(!name)
18-
reject(this.throwError('character.search()', 'a name'))
19-
20-
this.req(
21-
'/character/search',
22-
Object.assign(params, {name:name})
23-
).then((res) => {
24-
resolve(res)
25-
}).catch((err) => {
26-
reject(err)
27-
})
28-
})
15+
async search(name, params={}) {
16+
if(!name)
17+
throw this.throwError('character.search()', 'a name')
18+
return this.req('/character/search', Object.assign(params, {name}))
2919
}
3020

3121
/*
@@ -34,24 +24,12 @@ class Character extends Lib {
3424
data
3525
}
3626
*/
37-
get(id, params = {}) {
38-
return new Promise((resolve, reject) => {
39-
if(!id)
40-
reject(this.throwError('character.get()', 'an ID'))
41-
42-
params.extended = params.extended ? 1 : 0
27+
async get(id, params={}) {
28+
if(!id)
29+
throw this.throwError('character.get()','an ID')
4330

44-
this.req(
45-
'/character/' + id,
46-
params
47-
).then((res) => {
48-
resolve(res)
49-
}).catch((err) => {
50-
reject(err)
51-
})
52-
})
31+
return this.req(`/character/${id}`, params)
5332
}
54-
5533
}
5634

5735
module.exports = Character

lib/data.js

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@ class Content extends Lib {
66
super(parent)
77
}
88

9-
content() {
10-
return new Promise((resolve, reject) => {
11-
this.req('/content').then((res) => {
12-
resolve(res)
13-
}).catch((err) => {
14-
reject(err)
15-
})
16-
})
9+
async content() {
10+
return this.req('/content')
1711
}
1812

1913
/*
@@ -22,57 +16,33 @@ class Content extends Lib {
2216
ids
2317
}
2418
*/
25-
list(name, params = {}) {
26-
return new Promise((resolve, reject) => {
27-
if(typeof(name) === 'undefined')
28-
reject(this.throwError('content.list()', 'a name'))
19+
async list(name, params={}) {
20+
if(typeof name==='undefined')
21+
throw this.throwError('data.list()','a name')
2922

30-
params.ids = this.parent.utils.makeCSV(params.ids)
23+
params.ids = this.parent.utils.makeCSV(params.ids)
3124

32-
this.req(
33-
`/${name}`,
34-
params
35-
).then((res) => {
36-
resolve(res)
37-
}).catch((err) => {
38-
reject(err)
39-
})
40-
})
25+
return this.req(`/${name}`, params)
4126
}
4227

43-
get(name, id) {
44-
return new Promise((resolve, reject) => {
45-
if(typeof(name) === 'undefined')
46-
reject(this.throwError('content.get()', 'a name'))
47-
if(typeof(id) === 'undefined')
48-
reject(this.throwError('content.get()', 'an ID'))
28+
async get(name, id) {
29+
const missing_params = []
30+
if(typeof name==='undefined')
31+
missing_params.push('a name')
32+
if(typeof id==='undefined')
33+
missing_params.push('an ID')
34+
if(missing_params.length>0)
35+
throw this.throwError('data.get()', missing_params.join(','))
4936

50-
this.req(`/${name}/${id}`).then((res) => {
51-
resolve(res)
52-
}).catch((err) => {
53-
reject(err)
54-
})
55-
})
37+
return this.req(`/${name}/${id}`)
5638
}
5739

5840
servers() {
59-
return new Promise((resolve, reject) => {
60-
this.req('/servers').then((res) => {
61-
resolve(res)
62-
}).catch((err) => {
63-
reject(err)
64-
})
65-
})
41+
return this.req('/servers')
6642
}
6743

6844
datacenters() {
69-
return new Promise((resolve, reject) => {
70-
this.req('/servers/dc').then((res) => {
71-
resolve(res)
72-
}).catch((err) => {
73-
reject(err)
74-
})
75-
})
45+
return this.req('/servers/dc')
7646
}
7747

7848

lib/freecompany.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ class FreeCompany extends Lib {
1212
page int optional
1313
}
1414
*/
15-
search(name, params = {}) {
16-
return new Promise((resolve, reject) => {
17-
if(typeof(name) === 'undefined')
18-
reject(this.throwError('freecompany.search()', 'a name'))
19-
20-
this.req(
21-
'/freecompany/search',
22-
Object.assign(params, {name:name})
23-
).then((res) => {
24-
resolve(res)
25-
}).catch((err) => {
26-
reject(err)
27-
})
28-
})
15+
async search(name, params={}) {
16+
if(typeof(name) === 'undefined')
17+
throw this.throwError('freecompany.search()','a name')
18+
return this.req('/freecompany/search', Object.assign(params, {name}))
2919
}
3020

3121
/*
@@ -34,22 +24,13 @@ class FreeCompany extends Lib {
3424
data
3525
}
3626
*/
37-
get(id, params = {}) {
38-
return new Promise((resolve, reject) => {
39-
if(typeof(id) === 'undefined')
40-
reject(this.throwError('freecompany.get()', 'an ID'))
27+
async get(id, params={}) {
28+
if(typeof(id) === 'undefined')
29+
throw this.throwError('freecompany.get()', 'an ID')
4130

42-
params.data = this.makeCSV(params.data)
31+
params.data = this.makeCSV(params.data)
4332

44-
this.req(
45-
'/freecompany/' + id,
46-
params
47-
).then((res) => {
48-
resolve(res)
49-
}).catch((err) => {
50-
reject(err)
51-
})
52-
})
33+
return this.req(`/freecompany/${id}`, params)
5334
}
5435
}
5536

lib/linkshell.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,16 @@ class Linkshell extends Lib {
1212
page int optional
1313
}
1414
*/
15-
search(name, params = {}) {
16-
return new Promise((resolve, reject) => {
17-
if(typeof(name) === 'undefined')
18-
reject(this.throwError('linkshell.search()', 'a name'))
19-
20-
this.req(
21-
'/linkshell/search',
22-
Object.assign(params, {name:name})
23-
).then((res) => {
24-
resolve(res)
25-
}).catch((err) => {
26-
reject(err)
27-
})
28-
})
15+
async search(name, params={}) {
16+
if(typeof(name) === 'undefined')
17+
throw this.throwError('linkshell.search()','a name')
18+
return this.req('/linkshell/search', Object.assign(params, {name}))
2919
}
3020

31-
get(id) {
32-
return new Promise((resolve, reject) => {
33-
if(typeof(id) === 'undefined')
34-
reject(this.throwError('linkshell.get()', 'an ID'))
35-
36-
this.req('/linkshell/' + id).then((res) => {
37-
resolve(res)
38-
}).catch((err) => {
39-
reject(err)
40-
})
41-
})
21+
async get(id) {
22+
if(typeof(id) === 'undefined')
23+
throw this.throwError('linkshell.get()', 'an ID')
24+
return this.req(`/linkshell/${id}`)
4225
}
4326
}
4427

lib/pvpteam.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,18 @@ class PvPTeam extends Lib {
1212
page int optional
1313
}
1414
*/
15-
search(name, params = {}) {
16-
return new Promise((resolve, reject) => {
17-
if(typeof(name) === 'undefined')
18-
reject(this.throwError('pvpteam.search()', 'a name'))
15+
async search(name, params={}) {
16+
if(typeof name==='undefined')
17+
throw this.throwError('pvpteam.search()', 'a name')
1918

20-
this.req(
21-
'/pvpteam/search',
22-
Object.assign(params, {name:name})
23-
).then((res) => {
24-
resolve(res)
25-
}).catch((err) => {
26-
reject(err)
27-
})
28-
})
19+
return this.req('/pvpteam/search',Object.assign(params, {name}))
2920
}
3021

31-
get(id) {
32-
return new Promise((resolve, reject) => {
33-
if(typeof(id) === 'undefined')
34-
reject(this.throwError('pvpteam.get()', 'an ID'))
22+
async get(id) {
23+
if(typeof id==='undefined')
24+
throw this.throwError('pvpteam.get()', 'an ID')
3525

36-
this.req('/pvpteam/' + id).then((res) => {
37-
resolve(res)
38-
}).catch((err) => {
39-
reject(err)
40-
})
41-
})
26+
return this.req(`/pvpteam/${id}`)
4227
}
4328
}
4429

lib/search.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,27 @@ let { req, reqJSON, makeCSV, throwError } = require('../utils')
1010
limit
1111
}
1212
*/
13-
module.exports = function(input, params = {}) {
13+
module.exports = async function(input, params = {}) {
1414
req = req.bind(this)
1515
reqJSON = reqJSON.bind(this)
1616

17-
return new Promise((resolve, reject) => {
18-
if(typeof(input) === 'undefined')
19-
reject(throwError('search()', 'any input'))
17+
if(typeof(input) === 'undefined')
18+
throw throwError('search()', 'any input')
2019

21-
let path = params.lore ? '/lore' : '/search'
20+
let path = params.lore ? '/lore' : '/search'
2221

23-
switch(typeof(input)) {
24-
// GET method
25-
case 'string':
26-
params.indexes = makeCSV(params.indexes)
22+
switch(typeof(input)) {
23+
// GET method
24+
case 'string':
25+
params.indexes = makeCSV(params.indexes)
26+
return req(path, Object.assign(params, {'string': input}))
2727

28-
req(
29-
path,
30-
Object.assign(params, {string: input})
31-
).then((res) => {
32-
resolve(res)
33-
}).catch((err) => {
34-
reject(err)
35-
})
36-
break
28+
// ElasticSearch JSON method
29+
case 'object':
30+
input.indexes = makeCSV(params.indexes)
31+
return reqJSON(path, input)
3732

38-
// ElasticSearch JSON method
39-
case 'object':
40-
input.indexes = makeCSV(params.indexes)
41-
42-
reqJSON(
43-
path,
44-
input
45-
).then((res) => {
46-
resolve(res)
47-
}).catch((err) => {
48-
reject(err)
49-
})
50-
break
51-
52-
default:
53-
reject(Error(`Unexpected input type for search: '${typeof(input)}'`))
54-
break
55-
}
56-
})
33+
default:
34+
throw new Error(`Unexpected input type for search: '${typeof(input)}'`)
35+
}
5736
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xivapi/js",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"description": "A Node.JS wrapper for xivapi.com",
55
"main": "XIVAPI.js",
66
"directories": {

utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const request = require('request-promise-native')
22

33
module.exports = {
44
//standard request function
5-
req(path, params) {
6-
if(params && typeof params.snake_case !== 'undefined')
7-
params.snake_case = params.snake_case ? 1 : 0
5+
req(path, params={}) {
6+
let convs = ['snake_case', 'extended']
7+
for (const c of convs) {
8+
if(typeof params[c] !== 'undefined')
9+
params[c] = params[c] ? 1 : 0
10+
}
11+
812

913
params = Object.assign({}, this.globalParams, params)
1014

0 commit comments

Comments
 (0)