Improve getAll webflow/printful client api

This commit is contained in:
Dominic Ferrando
2026-06-24 16:32:03 -04:00
parent d415685a81
commit 43089c03b6
5 changed files with 104 additions and 61 deletions
+15 -13
View File
@@ -1,5 +1,5 @@
import type { Printful } from "./util/types";
import { type DeepPartial } from "./util/misc";
import { type DeepPartial, type PagingOptions } from "./util/misc";
const parsePayload = (res: Response): Promise<unknown> =>
res.json().catch(() => res.text().catch(() => undefined));
@@ -35,7 +35,7 @@ class VariantsClient {
});
if (res.status === 404 || res.status === 400) return;
if (!res.ok) throw new PrintfulError("Failed to get Printful variant", res.status, await parsePayload(res));
const payload = (await res.json()) as Printful.Products.MetaDataSingle<Printful.Products.SyncVariant>;
const payload = (await res.json()) as Printful.MetaDataSingle<Printful.Products.SyncVariant>;
return payload.result;
}
}
@@ -47,19 +47,20 @@ class ProductsClient {
this.Variants = new VariantsClient(creds);
}
async getAll(offset = 0): Promise<Printful.Products.SyncProduct[]> {
async getAll(opt: PagingOptions): Promise<Printful.Products.SyncProduct[]> {
const allSyncProducts: Printful.Products.SyncProduct[] = [];
while (true) {
const res = await fetch(`${this.creds.apiUrl}/store/products?offset=${offset}`, {
let offset = opt.offset ?? 0;
do {
const res = await fetch(`${this.creds.apiUrl}/store/products?offset=${offset}&limit=${opt.limit ?? 100}`, {
method: "GET",
headers: this.creds.authHeaders,
});
if (!res.ok) throw new PrintfulError("Failed to get all Printful products", res.status, await parsePayload(res));
const payload = (await res.json()) as Printful.Products.MetaDataMulti<Printful.Products.SyncProduct>;
const payload = (await res.json()) as Printful.MetaDataMulti<Printful.Products.SyncProduct>;
allSyncProducts.push(...payload.result);
offset = allSyncProducts.length;
if (allSyncProducts.length >= payload.paging.total) break;
}
} while (opt.forceAll);
return allSyncProducts;
}
@@ -70,7 +71,7 @@ class ProductsClient {
});
if (res.status === 404 || res.status === 400) return;
if (!res.ok) throw new PrintfulError("Failed to get Printful product", res.status, await parsePayload(res));
const payload = (await res.json()) as Printful.Products.MetaDataSingle<Printful.Products.Product>;
const payload = (await res.json()) as Printful.MetaDataSingle<Printful.Products.Product>;
return payload.result;
}
@@ -102,19 +103,20 @@ class OrdersClient {
if (!res.ok) throw new PrintfulError("Failed to create Printful order", res.status, await parsePayload(res));
}
async getAll(offset = 0): Promise<Printful.Orders.Order[]> {
async getAll(opt: PagingOptions): Promise<Printful.Orders.Order[]> {
const allPrintfulOrders: Printful.Orders.Order[] = [];
while (true) {
const res = await fetch(`${this.creds.apiUrl}/orders?offset=${offset}`, {
let offset = opt.offset ?? 0;
do {
const res = await fetch(`${this.creds.apiUrl}/orders?offset=${offset}&limit=${opt.limit ?? 100}`, {
method: "GET",
headers: this.creds.authHeaders,
});
if (!res.ok) throw new PrintfulError("Failed to get all Printful orders", res.status, await parsePayload(res));
const payload = (await res.json()) as Printful.Products.MetaDataMulti<Printful.Orders.Order>;
const payload = (await res.json()) as Printful.MetaDataMulti<Printful.Orders.Order>;
allPrintfulOrders.push(...payload.result);
offset = allPrintfulOrders.length;
if (allPrintfulOrders.length >= payload.paging.total) break;
}
} while (opt.forceAll);
return allPrintfulOrders;
}
}