Skip to content

Commit 8f44bb5

Browse files
HaroenvKyleAMathews
authored andcommitted
chore(npm source): use index.browse (#4861)
* chore(npm source): use index.browse This will get all of the hits, even if they are on multiple pages, and is actually better for this usecase Signed-off-by: Haroen Viaene <[email protected]> * chore(search): update API key with unique one for gatsby Signed-off-by: Haroen Viaene <[email protected]> * simplify and fix Signed-off-by: Haroen Viaene <[email protected]>
1 parent 489c578 commit 8f44bb5

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

packages/gatsby-source-npm-package-search/src/gatsby-node.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const algoliasearch = require(`algoliasearch`)
22
const crypto = require(`crypto`)
33

4-
const client = algoliasearch(`OFCNCOG2CU`, `f54e21fa3a2a0160595bb058179bfb1e`)
4+
const client = algoliasearch(`OFCNCOG2CU`, `6fbcaeafced8913bf0e4d39f0b541957`)
55
var index = client.initIndex(`npm-search`)
66

77
const createContentDigest = obj =>
@@ -10,6 +10,17 @@ const createContentDigest = obj =>
1010
.update(JSON.stringify(obj))
1111
.digest(`hex`)
1212

13+
function browse({ index, ...params }) {
14+
let hits = []
15+
const browser = index.browseAll(params)
16+
17+
return new Promise((resolve, reject) => {
18+
browser.on(`result`, content => (hits = hits.concat(content.hits)))
19+
browser.on(`end`, () => resolve(hits))
20+
browser.on(`error`, err => reject(err))
21+
})
22+
}
23+
1324
exports.sourceNodes = async (
1425
{ boundActionCreators, createNodeId },
1526
{ keywords }
@@ -18,19 +29,15 @@ exports.sourceNodes = async (
1829

1930
console.log(`Grabbing local NPM packages...`)
2031

21-
let buildFilter = []
22-
23-
keywords.forEach(keyword => {
24-
buildFilter.push(`keywords:${keyword}`)
25-
})
32+
const buildFilter = keywords.map(keyword => `keywords:${keyword}`)
2633

27-
const data = await index.search({
28-
query: ``,
34+
const hits = await browse({
35+
index,
2936
filters: `(${buildFilter.join(` OR `)})`,
3037
hitsPerPage: 1000,
3138
})
3239

33-
data.hits.forEach(hit => {
40+
hits.forEach(hit => {
3441
// commented changed remove all badges and images from readme content to keep the creation of the node from failing below
3542
// if (hit.readme.includes(`![`)) {
3643
// hit.readme = hit.readme.replace(/[[]?!\[.*\b/gi, ``)
@@ -52,7 +59,6 @@ exports.sourceNodes = async (
5259
readmeNode.internal.contentDigest = createContentDigest(readmeNode)
5360
// Remove unneeded data
5461
delete hit.readme
55-
delete hit._highlightResult
5662
delete hit.versions
5763

5864
const node = {

www/src/components/searchbar-body.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class SearchBar extends Component {
416416
return (
417417
<div>
418418
<InstantSearch
419-
apiKey="f54e21fa3a2a0160595bb058179bfb1e"
419+
apiKey="ae43b69014c017e05950a1cd4273f404"
420420
appId="OFCNCOG2CU"
421421
indexName="npm-search"
422422
searchState={this.state.searchState}

0 commit comments

Comments
 (0)