fix(gatsby-source-medusa): adds support for incremental sourcing of medusa data (#1217)
This commit is contained in:
@@ -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<any[]>}
|
||||
* @return {Promise<any[]>} products to create nodes from
|
||||
*/
|
||||
async function products(_date?: string): Promise<any[]> {
|
||||
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<any[]>}
|
||||
* @param {string} _date used fetch regions updated since the specified date
|
||||
* @return {Promise<any[]>} regions to create nodes from
|
||||
*/
|
||||
async function regions(_date?: string): Promise<any[]> {
|
||||
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<any[]>}
|
||||
* @return {Promise<any[]>} orders to create nodes from
|
||||
*/
|
||||
async function orders(_date?: string): Promise<any[]> {
|
||||
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<any[]>}
|
||||
* @return {Promise<any[]>} collections to create nodes from
|
||||
*/
|
||||
async function collections(_date?: string): Promise<any[]> {
|
||||
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
|
||||
|
||||
@@ -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<void> {
|
||||
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<void> {
|
||||
await cache.set("timestamp", Date.now())
|
||||
}
|
||||
|
||||
export async function sourceNodes(
|
||||
gatsbyApi: SourceNodesArgs,
|
||||
pluginOptions: MedusaPluginOptions
|
||||
): Promise<void> {
|
||||
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 {
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user