From 23f8399c16e3f3c7a01a6846b2b96750dde55a60 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 18 Mar 2022 15:00:36 +0100 Subject: [PATCH] fix(gatsby-source-medusa): adds support for incremental sourcing of medusa data (#1217) --- packages/gatsby-source-medusa/src/client.ts | 78 ++++++++----------- .../gatsby-source-medusa/src/gatsby-node.ts | 42 ++++------ .../gatsby-source-medusa/src/operations.ts | 24 +++--- .../gatsby-source-medusa/types/interface.d.ts | 8 +- 4 files changed, 60 insertions(+), 92 deletions(-) diff --git a/packages/gatsby-source-medusa/src/client.ts b/packages/gatsby-source-medusa/src/client.ts index 60701384cf..eac092aac6 100644 --- a/packages/gatsby-source-medusa/src/client.ts +++ b/packages/gatsby-source-medusa/src/client.ts @@ -1,5 +1,4 @@ import axios, { AxiosPromise, AxiosRequestConfig } from "axios" -import { Reporter } from "gatsby-cli/lib/reporter/reporter" function medusaRequest( storeURL: string, @@ -18,34 +17,28 @@ function medusaRequest( return client(options) } -export const createClient = ( - options: MedusaPluginOptions, - reporter: Reporter -): any => { +export const createClient = (options: MedusaPluginOptions): any => { const { storeUrl, authToken } = options as any /** - * * @param {string} _date used fetch products updated since the specified date - * @return {Promise} + * @return {Promise} products to create nodes from */ async function products(_date?: string): Promise { let products: any[] = [] let offset = 0 let count = 1 do { - await medusaRequest(storeUrl, `/store/products?offset=${offset}`) - .then(({ data }) => { - products = [...products, ...data.products] - count = data.count - offset = data.products.length - }) - .catch((error) => { - reporter.error( - `"The following error status was produced while attempting to fetch products: ${error}` - ) - return [] - }) + let path = `/store/products?offset=${offset}` + if (_date) { + path += `&updated_at[gt]=${_date}` + } + + await medusaRequest(storeUrl, path).then(({ data }) => { + products = [...products, ...data.products] + count = data.count + offset = data.products.length + }) } while (products.length < count) return products @@ -53,27 +46,25 @@ export const createClient = ( /** * - * @param {string} date used fetch regions updated since the specified date - * @return {Promise} + * @param {string} _date used fetch regions updated since the specified date + * @return {Promise} regions to create nodes from */ async function regions(_date?: string): Promise { - const regions = await medusaRequest(storeUrl, `/store/regions`) - .then(({ data }) => { - return data.regions - }) - .catch((error) => { - console.warn(` - "The following error status was produced while attempting to fetch regions: ${error} - `) - return [] - }) + let path = `/store/regions` + if (_date) { + path += `?updated_at[gt]=${_date}` + } + + const regions = await medusaRequest(storeUrl, path).then(({ data }) => { + return data.regions + }) return regions } /** * * @param {string} _date used fetch regions updated since the specified date - * @return {Promise} + * @return {Promise} orders to create nodes from */ async function orders(_date?: string): Promise { const orders = await medusaRequest(storeUrl, `/admin/orders`, { @@ -95,25 +86,22 @@ export const createClient = ( /** * * @param {string} _date used fetch regions updated since the specified date - * @return {Promise} + * @return {Promise} collections to create nodes from */ async function collections(_date?: string): Promise { let collections: any[] = [] let offset = 0 let count = 1 do { - await medusaRequest(storeUrl, `/store/collections?offset=${offset}`) - .then(({ data }) => { - collections = [...collections, ...data.collections] - count = data.count - offset = data.collections.length - }) - .catch((error) => { - reporter.error( - `"The following error status was produced while attempting to fetch products: ${error}` - ) - return [] - }) + let path = `/store/collections?offset=${offset}` + if (_date) { + path += `&updated_at[gt]=${_date}` + } + await medusaRequest(storeUrl, path).then(({ data }) => { + collections = [...collections, ...data.collections] + count = data.count + offset = data.collections.length + }) } while (collections.length < count) return collections diff --git a/packages/gatsby-source-medusa/src/gatsby-node.ts b/packages/gatsby-source-medusa/src/gatsby-node.ts index 0573c87235..8b2627ec44 100644 --- a/packages/gatsby-source-medusa/src/gatsby-node.ts +++ b/packages/gatsby-source-medusa/src/gatsby-node.ts @@ -27,7 +27,7 @@ async function sourceAllNodes( createRegionsOperation, createOrdersOperation, createCollectionsOperation, - } = createOperations(pluginOptions, gatsbyApi) + } = createOperations(pluginOptions) const operations = [ createProductsOperation, @@ -54,22 +54,17 @@ const medusaNodeTypes = [ "MedusaCollections", ] -export async function sourceUpdatedNodes( +async function sourceUpdatedNodes( gatsbyApi: SourceNodesArgs, - pluginOptions: MedusaPluginOptions + pluginOptions: MedusaPluginOptions, + lastBuildTime: string ): Promise { const { incrementalProductsOperation, incrementalRegionsOperation, incrementalOrdersOperation, incrementalCollectionsOperation, - } = createOperations(pluginOptions, gatsbyApi) - - const lastBuildTime = new Date( - gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`]?.[ - `lastBuildTime` - ] - ) + } = createOperations(pluginOptions) for (const nodeType of medusaNodeTypes) { gatsbyApi @@ -94,36 +89,25 @@ export async function sourceUpdatedNodes( } } +export async function onPostBuild({ cache }: { cache: any }): Promise { + await cache.set("timestamp", Date.now()) +} + export async function sourceNodes( gatsbyApi: SourceNodesArgs, pluginOptions: MedusaPluginOptions ): Promise { - const pluginStatus = - gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`] - - const lastBuildTime = pluginStatus?.[`lastBuildTime`] + const { cache } = gatsbyApi + const lastBuildTime = await cache.get("timestamp") if (lastBuildTime !== undefined) { - gatsbyApi.reporter.info( - `Cache is warm, but incremental builds are currently not supported. Running a clean build.` - ) - await sourceAllNodes(gatsbyApi, pluginOptions) + await sourceUpdatedNodes(gatsbyApi, pluginOptions, lastBuildTime) } else { gatsbyApi.reporter.info(`Cache is cold, running a clean build.`) await sourceAllNodes(gatsbyApi, pluginOptions) } - gatsbyApi.reporter.info(`Finished sourcing nodes, caching last build time`) - gatsbyApi.actions.setPluginStatus( - pluginStatus !== undefined - ? { - ...pluginStatus, - [`lastBuildTime`]: Date.now(), - } - : { - [`lastBuildTime`]: Date.now(), - } - ) + gatsbyApi.reporter.info(`Finished sourcing nodes`) } export function createResolvers({ createResolvers }: CreateResolversArgs): any { diff --git a/packages/gatsby-source-medusa/src/operations.ts b/packages/gatsby-source-medusa/src/operations.ts index 98be1ae91a..36d260187b 100644 --- a/packages/gatsby-source-medusa/src/operations.ts +++ b/packages/gatsby-source-medusa/src/operations.ts @@ -1,11 +1,7 @@ -import { SourceNodesArgs } from "gatsby" import { createClient } from "./client" -export function createOperations( - options: MedusaPluginOptions, - { reporter }: SourceNodesArgs -): IOperations { - const client = createClient(options, reporter) +export function createOperations(options: MedusaPluginOptions): IOperations { + const client = createClient(options) function createOperation( name: "products" | "collections" | "regions" | "orders", @@ -22,13 +18,13 @@ export function createOperations( createCollectionsOperation: createOperation("collections"), createRegionsOperation: createOperation("regions"), createOrdersOperation: createOperation("orders"), - incrementalProductsOperation: (date: Date): any => - createOperation("products", date.toISOString()), - incrementalCollectionsOperation: (date: Date): any => - createOperation("collections", date.toISOString()), - incrementalRegionsOperation: (date: Date): any => - createOperation("regions", date.toISOString()), - incrementalOrdersOperation: (date: Date): any => - createOperation("orders", date.toISOString()), + incrementalProductsOperation: (date: string): any => + createOperation("products", date), + incrementalCollectionsOperation: (date: string): any => + createOperation("collections", date), + incrementalRegionsOperation: (date: string): any => + createOperation("regions", date), + incrementalOrdersOperation: (date: string): any => + createOperation("orders", date), } } diff --git a/packages/gatsby-source-medusa/types/interface.d.ts b/packages/gatsby-source-medusa/types/interface.d.ts index 56ed7c00d5..ee7b33a5dc 100644 --- a/packages/gatsby-source-medusa/types/interface.d.ts +++ b/packages/gatsby-source-medusa/types/interface.d.ts @@ -22,8 +22,8 @@ interface IOperations { createCollectionsOperation: IMedusaOperation createRegionsOperation: IMedusaOperation createOrdersOperation: IMedusaOperation - incrementalProductsOperation: (date: Date) => IMedusaOperation - incrementalCollectionsOperation: (date: Date) => IMedusaOperation - incrementalRegionsOperation: (date: Date) => IMedusaOperation - incrementalOrdersOperation: (date: Date) => IMedusaOperation + incrementalProductsOperation: (date: string) => IMedusaOperation + incrementalCollectionsOperation: (date: string) => IMedusaOperation + incrementalRegionsOperation: (date: string) => IMedusaOperation + incrementalOrdersOperation: (date: string) => IMedusaOperation }