You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
909 B
JavaScript
33 lines
909 B
JavaScript
8 years ago
|
'use strict';
|
||
|
|
||
|
const Promise = require("bluebird");
|
||
|
const bhttp = require("bhttp");
|
||
|
const querystring = require("querystring");
|
||
|
const defaultValue = require("default-value");
|
||
|
|
||
|
module.exports = function(autocompleteKey) {
|
||
|
function createQueryString(query, options) {
|
||
|
return querystring.stringify({
|
||
|
autocomplete_key: autocompleteKey,
|
||
|
_: Date.now(),
|
||
|
query: query,
|
||
|
num_results: defaultValue(options.resultCount, 20)
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function createUrl(query, options) {
|
||
|
return `https://ac.cnstrc.com/autocomplete/${encodeURIComponent(query)}?${createQueryString(query, options)}`;
|
||
|
}
|
||
|
|
||
|
return function doSearch(query, options) {
|
||
|
return Promise.try(() => {
|
||
|
return bhttp.get(createUrl(query, options));
|
||
|
}).then((response) => {
|
||
|
if (response.statusCode === 200) {
|
||
|
return response.body;
|
||
|
} else {
|
||
|
throw new Error(`Non-200 status code encountered: ${response.statusCode}`)
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|