fix: remplacer status=any par deux appels active+draft (API REST Shopify)
This commit is contained in:
+11
-3
@@ -54,18 +54,26 @@ function extractNextUrl(linkHeader: string | null): string | null {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Récupère tous les produits Shopify (actifs + drafts), en paginant.
|
* Récupère tous les produits Shopify (actifs + drafts), en paginant.
|
||||||
* Limite : 250 par page via cursor-based pagination.
|
* L'API REST ne supporte pas status=any — on fait deux passes (active + draft).
|
||||||
*/
|
*/
|
||||||
export async function fetchAllShopifyProducts(): Promise<ShopifyProductFull[]> {
|
export async function fetchAllShopifyProducts(): Promise<ShopifyProductFull[]> {
|
||||||
|
const [active, draft] = await Promise.all([
|
||||||
|
fetchByStatus('active'),
|
||||||
|
fetchByStatus('draft'),
|
||||||
|
])
|
||||||
|
return [...active, ...draft]
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchByStatus(status: 'active' | 'draft'): Promise<ShopifyProductFull[]> {
|
||||||
const { baseUrl, headers } = getConfig()
|
const { baseUrl, headers } = getConfig()
|
||||||
const all: ShopifyProductFull[] = []
|
const all: ShopifyProductFull[] = []
|
||||||
|
|
||||||
let url: string | null =
|
let url: string | null =
|
||||||
`${baseUrl}/products.json?limit=250&fields=id,title,handle,status,body_html,variants&status=any`
|
`${baseUrl}/products.json?limit=250&fields=id,title,handle,status,body_html,variants&status=${status}`
|
||||||
|
|
||||||
while (url) {
|
while (url) {
|
||||||
const res = await fetch(url, { headers })
|
const res = await fetch(url, { headers })
|
||||||
if (!res.ok) throw new Error(`Shopify fetch error: ${res.status}`)
|
if (!res.ok) throw new Error(`Shopify fetch error (${status}): ${res.status}`)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
all.push(...(data.products as ShopifyAPIProduct[]).map(toSyncProduct))
|
all.push(...(data.products as ShopifyAPIProduct[]).map(toSyncProduct))
|
||||||
url = extractNextUrl(res.headers.get('Link'))
|
url = extractNextUrl(res.headers.get('Link'))
|
||||||
|
|||||||
Reference in New Issue
Block a user