-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update search tests and lintings (#40)
* fix linting * include descriptions; and flatten resolved data * cover new api endpoints
- Loading branch information
Showing
5 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ module.exports = { | |
conf, | ||
download, | ||
paths, | ||
search | ||
search, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,46 @@ | ||
const http = require('http'); | ||
|
||
const getSearchUrl = pkg => `http://npmsearch.com/query\?q\=${pkg}%20AND%20\(keywords:dext-theme%20OR%20keywords:dext-plugin\)\&fields\=name`; | ||
/** | ||
* Retrieves the API url | ||
* | ||
* @param {String} q - The keyword to search | ||
* @return {String} | ||
*/ | ||
const getSearchUrl = q => `https://npmsearch.com/query?q=${q}%20AND%20(keywords:dext-theme%20OR%20keywords:dext-plugin)&fields=name,description`; | ||
|
||
/** | ||
* Searches for package marked with the keywords 'dext-plugin' or 'dext-theme' | ||
* Searches for packages marked with the keywords 'dext-plugin' or 'dext-theme' | ||
* | ||
* { name, desc } | ||
* | ||
* @param {String} pkg - The name of the npm package | ||
* @return {Promise} - The search results | ||
* @param {String} q - A keyword to search for (queried by package name) | ||
* @return {Promise} - Resolves an array of the package names and descriptions | ||
*/ | ||
const searchPackages = (pkg) => new Promise((resolve) => { | ||
const searchPackages = q => new Promise((resolve) => { | ||
let body = ''; | ||
const endpoint = getSearchUrl(q); | ||
// Retrieve the search results | ||
return http.get(getSearchUrl(pkg), (res) => { | ||
return http.get(endpoint, (res) => { | ||
res.on('data', (chunk) => { | ||
body += chunk; | ||
}); | ||
res.on('end', () => { | ||
const results = JSON.parse(body); | ||
if (!results.results) { | ||
resolve([]); | ||
return; | ||
} | ||
const resultsFlat = results.results.map(c => ({ | ||
name: c.name[0], | ||
desc: c.description ? c.description[0] : '', | ||
})); | ||
// Return the results part of the HTTP response | ||
resolve(results.results); | ||
resolve(resultsFlat); | ||
}); | ||
}); | ||
}); | ||
|
||
module.exports = { | ||
getSearchUrl, | ||
searchPackages | ||
searchPackages, | ||
}; |