cbdc5b7774
**What** - Moves `sourceUpdatedNodes` and `sourceAllNodes` to a separate file, to prevent warning of unsupported exports. See #1455. - Adds warnings if `GET /store/products`, `GET /store/regions`, and `GET /store/collections` return empty arrays. This should help new users more easily figure out why their storefronts does not work as expected. - Adds schema to plugin, so that node types for products, regions and collections are always created. This will prevent errors such as `allMedusaRegions query failed` from happening in the storefront, as the query will be valid as the type exists, even if it returns nothing. This should make the gatsby plugin/starter easier to use. This error is an reoccurring issue in our discord, when new users try to run the Gatsby starter without seeding the storefront beforehand. Resolves #1455 Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com>
136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import axios, { AxiosPromise, AxiosRequestConfig } from "axios"
|
|
|
|
function medusaRequest(
|
|
storeURL: string,
|
|
path = "",
|
|
headers = {}
|
|
): AxiosPromise {
|
|
const options: AxiosRequestConfig = {
|
|
method: "GET",
|
|
withCredentials: true,
|
|
url: path,
|
|
headers: headers,
|
|
}
|
|
|
|
const client = axios.create({ baseURL: storeURL })
|
|
|
|
return client(options)
|
|
}
|
|
|
|
export const createClient = (options: MedusaPluginOptions): any => {
|
|
const { storeUrl, apiKey } = options
|
|
|
|
/**
|
|
* @param {string} _date used fetch products updated since the specified date
|
|
* @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 {
|
|
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)
|
|
|
|
if (!products.length && !_date) {
|
|
console.warn(
|
|
"[gatsby-source-medusa]: 📣 No products were retrieved. If this is a new store, please ensure that you have at least one published product in your store. You can create a product by using the Medusa admin dashboard."
|
|
)
|
|
}
|
|
|
|
return products
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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[]> {
|
|
let path = `/store/regions`
|
|
if (_date) {
|
|
path += `?updated_at[gt]=${_date}`
|
|
}
|
|
|
|
const regions = await medusaRequest(storeUrl, path).then(({ data }) => {
|
|
return data.regions
|
|
})
|
|
|
|
if (!regions.length && !_date) {
|
|
console.warn(
|
|
"[gatsby-source-medusa]: 📣 No regions were retrieved. If this is a new store, please ensure that you have configured at least one region in the Medusa admin dashboard."
|
|
)
|
|
}
|
|
|
|
return regions
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} _date used fetch regions updated since the specified date
|
|
* @return {Promise<any[]>} orders to create nodes from
|
|
*/
|
|
async function orders(_date?: string): Promise<any[]> {
|
|
const orders = await medusaRequest(storeUrl, `/admin/orders`, {
|
|
Authorization: `Bearer ${apiKey}`,
|
|
})
|
|
.then(({ data }) => {
|
|
return data.orders
|
|
})
|
|
.catch((error) => {
|
|
console.warn(`
|
|
📣 The following error status was produced while attempting to fetch orders: ${error}. \n
|
|
Make sure that the auth token you provided is valid.
|
|
`)
|
|
return []
|
|
})
|
|
return orders
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} _date used fetch regions updated since the specified date
|
|
* @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 {
|
|
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)
|
|
|
|
if (!collections.length && !_date) {
|
|
console.warn(
|
|
"[gatsby-source-medusa]: 📣 No collections were retrieved. You can create collections using the Medusa admin dasbboard."
|
|
)
|
|
}
|
|
|
|
return collections
|
|
}
|
|
|
|
return {
|
|
products,
|
|
collections,
|
|
regions,
|
|
orders,
|
|
}
|
|
}
|