From 82323323fbd57a413da9a419d317525b9852871f Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 12 Aug 2020 21:46:28 -0700 Subject: [PATCH] feat: fetch results from swiftype for the node and save the data --- plugins/gatsby-source-swiftype/gatsby-node.js | 44 ++++++++++++------- src/data/related-pages.json | 23 ++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 src/data/related-pages.json diff --git a/plugins/gatsby-source-swiftype/gatsby-node.js b/plugins/gatsby-source-swiftype/gatsby-node.js index 6d902c113..8fa90c626 100644 --- a/plugins/gatsby-source-swiftype/gatsby-node.js +++ b/plugins/gatsby-source-swiftype/gatsby-node.js @@ -1,4 +1,5 @@ const fs = require('fs'); +const fetch = require('node-fetch'); const { appendTrailingSlash, stripTrailingSlash } = require('./src/utils/url'); const data = {}; @@ -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) => { diff --git a/src/data/related-pages.json b/src/data/related-pages.json new file mode 100644 index 000000000..77c693623 --- /dev/null +++ b/src/data/related-pages.json @@ -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" + ] + } + } + } +} \ No newline at end of file