Skip to content

Commit 9650902

Browse files
committed
Properly escape all args, some rework
Makes sure to encode all uri components
1 parent f001a2a commit 9650902

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

index.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,30 @@
33
var querystring = require('querystring');
44
var request = require('request');
55

6+
function encodeURIComponentArray(arr) {
7+
return arr.map(function(component) {
8+
return encodeURIComponent(component);
9+
});
10+
}
11+
612
var hn = {
7-
// make a request to the specified endpoint
8-
call: function (endpoint, cb) {
9-
var query = 'https://hn.algolia.com/api/v1/' + endpoint;
13+
// Make a request with the spicified uri component array
14+
// and query argument object. If the queryObj is omitted,
15+
// it will be assumed to be empty and the callback may be
16+
// there instead
17+
call: function (components, queryObj, cb) {
18+
if(!Array.isArray(components)) components = [components];
19+
if(typeof queryObj === 'function') {
20+
cb = queryObj;
21+
queryObj = {};
22+
}
23+
24+
var endpoint_parts = encodeURIComponentArray(components);
25+
var query = 'https://hn.algolia.com/api/v1/' + endpoint_parts.join('/');
26+
27+
var query_args = querystring.stringify(queryObj);
28+
if(query_args.length > 0) query += '?' + query_args;
29+
1030
request(query, function (error, response, body) {
1131
if (!error && response.statusCode != 200)
1232
error = response.statusCode;
@@ -23,93 +43,93 @@ var hn = {
2343

2444
// get popular/recent comments
2545
getComments: function (cb) {
26-
hn.call('search?tags=comment', cb);
46+
hn.call('search', {tags: 'comment'}, cb);
2747
},
2848
getLastComments: function (cb) {
29-
hn.call('search_by_date?tags=comment', cb);
49+
hn.call('search_by_date', {tags: 'comment'}, cb);
3050
},
3151

3252
// get popular/recent polls
3353
getPolls: function (cb) {
34-
hn.call('search?tags=poll', cb);
54+
hn.call('search', {tags: 'poll'}, cb);
3555
},
3656
getLastPolls: function (cb) {
37-
hn.call('search_by_date?tags=poll', cb);
57+
hn.call('search_by_date', {tags: 'poll'}, cb);
3858
},
3959

4060
// get popular/recent posts
4161
getPosts: function (cb) {
42-
hn.call('search?tags=(story,poll)', cb);
62+
hn.call('search', {tags: '(story,poll)'}, cb);
4363
},
4464
getLastPosts: function (cb) {
45-
hn.call('search_by_date?tags=(story,poll)', cb);
65+
hn.call('search_by_date', {tags: '(story,poll)'}, cb);
4666
},
4767

4868
// get popular/recent stories
4969
getStories: function (cb) {
50-
hn.call('search?tags=story', cb);
70+
hn.call('search', {tags: 'story'}, cb);
5171
},
5272
getLastStories: function (cb) {
53-
hn.call('search_by_date?tags=story', cb);
73+
hn.call('search_by_date', {tags: 'story'}, cb);
5474
},
5575

5676
// get unique post/comment
5777
getItem: function (id, cb) {
58-
hn.call('items/' + id, cb);
78+
hn.call(['items', id], cb);
5979
},
6080

6181
// get unique user
6282
getUser: function (username, cb) {
63-
hn.call('users/' + username, cb);
83+
hn.call(['users', username], cb);
6484
},
6585

6686
// get popular/recent user comments
6787
getUserComments: function (username, cb) {
68-
hn.call('search?tags=comment,author_' + username, cb);
88+
hn.call('search', {tags: 'comment,author_' + username}, cb);
6989
},
7090
getLastUserComments: function (username, cb) {
71-
hn.call('search_by_date?tags=comment,author_' + username, cb);
91+
hn.call('search_by_date', {tags: 'comment,author_' + username}, cb);
7292
},
7393

7494
// get popular/recent user polls
7595
getUserPolls: function (username, cb) {
76-
hn.call('search?tags=poll,author_' + username, cb);
96+
hn.call('search', {tags: 'poll,author_' + username}, cb);
7797
},
7898
getLastUserPolls: function (username, cb) {
79-
hn.call('search_by_date?tags=poll,author_' + username, cb);
99+
hn.call('search_by_date', {tags: 'poll,author_' + username}, cb);
80100
},
81101

82102
// get popular/recent user posts
83103
getUserPosts: function (username, cb) {
84-
hn.call('search?tags=(story,poll),author_' + username, cb);
104+
hn.call('search', {tags: '(story,poll),author_' + username}, cb);
85105
},
86106
getLastUserPosts: function (username, cb) {
87-
hn.call('search_by_date?tags=(story,poll),author_' + username, cb);
107+
hn.call('search_by_date', {tags: '(story,poll),author_' + username}, cb);
88108
},
89109

90110
// get popular/recent user stories
91111
getUserStories: function (username, cb) {
92-
hn.call('search?tags=story,author_' + username, cb);
112+
hn.call('search', {tags: 'story,author_' + username}, cb);
93113
},
94114
getLastUserStories: function (username, cb) {
95-
hn.call('search_by_date?tags=story,author_' + username, cb);
115+
hn.call('search_by_date', {tags: 'story,author_' + username}, cb);
96116
},
97117

98118

99119
// search popular/recent comments
100120
searchComments: function (query, cb) {
101-
hn.call('search?query=' + query + '&tags=comment', cb);
121+
hn.call('search', {query: query, tags: 'comment'}, cb);
102122
},
103123
searchLastComments: function (query, cb) {
104-
hn.call('search_by_date?query=' + query + '&tags=comment', cb);
124+
hn.call('search_by_date', {query: query, tags: 'comment'}, cb);
105125
},
106126

107127
// search popular/recent polls
108128
searchPolls: function (query, cb) {
109-
hn.call('search?query=' + query + '&tags=poll', cb);
129+
hn.call('search', {query: query, tags: 'poll'}, cb);
110130
},
111131
searchLastPolls: function (query, cb) {
112-
hn.call('search_by_date?query=' + query + '&tags=poll', cb);
132+
hn.call('search_by_date', {query: query, tags: 'poll'}, cb);
113133
},
114134

115135
// search popular/recent posts
@@ -122,21 +142,19 @@ var hn = {
122142

123143
// search popular/recent stories
124144
searchStories: function (query, cb) {
125-
hn.call('search?query=' + query + '&tags=story', cb);
145+
hn.call('search', {query: query, tags: 'story'}, cb);
126146
},
127147
searchLastStories: function (query, cb) {
128-
hn.call('search_by_date?query=' + query + '&tags=story', cb);
148+
hn.call('search_by_date', {query: query, tags: 'story'}, cb);
129149
},
130150

131151

132152
// search popular/recent
133153
search: function (obj, cb) {
134-
var params = querystring.stringify(obj);
135-
hn.call('search?' + params, cb);
154+
hn.call('search', obj, cb);
136155
},
137156
searchLast: function (obj, cb) {
138-
var params = querystring.stringify(obj);
139-
hn.call('search_by_date?' + params, cb);
157+
hn.call('search_by_date', obj, cb);
140158
}
141159
};
142160

0 commit comments

Comments
 (0)