Skip to content

Commit

Permalink
feat: fetch results from swiftype for the node and save the data
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 65b83c6 commit 8232332
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
44 changes: 28 additions & 16 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const fetch = require('node-fetch');
const { appendTrailingSlash, stripTrailingSlash } = require('./src/utils/url');

const data = {};
Expand Down Expand Up @@ -28,24 +29,35 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => {

const { page: pageFilters = {} } = params.filters || {};

const allParams = {
...params,
engine_key: engineKey,
per_page: pageLimit,
filters: {
...params.filters,
page: {
...pageFilters,
url: [
`!${appendTrailingSlash(url)}`,
`!${stripTrailingSlash(url)}`,
...(pageFilters.url || []),
],
const result = await fetch(
'https://search-api.swiftype.com/api/v1/public/engines/search.json',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
},
};
body: JSON.stringify({
...params,
engine_key: engineKey,
per_page: pageLimit,
filters: {
...params.filters,
page: {
...pageFilters,
url: [
`!${appendTrailingSlash(url)}`,
`!${stripTrailingSlash(url)}`,
...(pageFilters.url || []),
],
},
},
}),
}
);

const { records } = await result.json();

data[pathname] = allParams;
data[pathname] = records.page;
};

exports.onPostBootstrap = (_, pluginOptions) => {
Expand Down
23 changes: 23 additions & 0 deletions src/data/related-pages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"/explore-docs/intro-to-sdk": {
"q": "Intro to New Relic One API components",
"search_fields": {
"page": [
"tags^10",
"body^5",
"title^1.5",
"*"
]
},
"engine_key": "Ad9HfGjDw4GRkcmJjUut",
"per_page": 5,
"filters": {
"page": {
"url": [
"!https://developer.newrelic.com/explore-docs/intro-to-sdk/",
"!https://developer.newrelic.com/explore-docs/intro-to-sdk"
]
}
}
}
}

0 comments on commit 8232332

Please sign in to comment.