Improve getAll webflow/printful client api
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Webflow } from "./util/types";
|
||||
import { type DeepPartial } from "./util/misc";
|
||||
import { type DeepPartial, type PagingOptions } from "./util/misc";
|
||||
import crypto from "node:crypto";
|
||||
|
||||
const parsePayload = (res: Response): Promise<unknown> =>
|
||||
@@ -95,14 +95,21 @@ class ProductsClient {
|
||||
return payload.product.id;
|
||||
}
|
||||
|
||||
async getAll(): Promise<Webflow.Products.ProductAndSkus[]> {
|
||||
const res = await fetch(`${this.creds.apiSitesUrl}/products`, {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeader,
|
||||
});
|
||||
if (!res.ok) throw new WebflowError("Failed to get all Webflow products", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as { items: Webflow.Products.ProductAndSkus[] };
|
||||
return payload.items;
|
||||
async getAll(opt: PagingOptions): Promise<Webflow.Products.ProductAndSkus[]> {
|
||||
const allWebflowProducts: Webflow.Products.ProductAndSkus[] = [];
|
||||
let offset = opt.offset ?? 0;
|
||||
do {
|
||||
const res = await fetch(`${this.creds.apiSitesUrl}/products?offset=${offset}&limit=${opt.limit ?? 100}`, {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeader,
|
||||
});
|
||||
if (!res.ok) throw new WebflowError("Failed to get all Webflow products", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Webflow.MetaDataMulti<"items", Webflow.Products.ProductAndSkus>;
|
||||
allWebflowProducts.push(...payload.items);
|
||||
offset = allWebflowProducts.length;
|
||||
if (allWebflowProducts.length >= payload.pagination.total) break;
|
||||
} while (opt.forceAll);
|
||||
return allWebflowProducts;
|
||||
}
|
||||
|
||||
async get(webflowProductId: string): Promise<Webflow.Products.ProductAndSkus | undefined> {
|
||||
@@ -149,17 +156,25 @@ class ProductsClient {
|
||||
class OrdersClient {
|
||||
constructor(private creds: Credentials) { }
|
||||
|
||||
async getAll(opt: { status?: Webflow.Orders.Order["status"] } = {}): Promise<Webflow.Orders.Order[]> {
|
||||
// TODO: pagination
|
||||
const params = new URLSearchParams();
|
||||
if (opt.status !== undefined) params.set("status", opt.status);
|
||||
const res = await fetch(`${this.creds.apiSitesUrl}/orders?${params.toString()}`, {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeader,
|
||||
});
|
||||
if (!res.ok) throw new WebflowError("Failed to get all Webflow orders", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as { orders: Webflow.Orders.Order[] };
|
||||
return payload.orders;
|
||||
async getAll(opt: PagingOptions & { status?: Webflow.Orders.Order["status"] }): Promise<Webflow.Orders.Order[]> {
|
||||
const allOrders: Webflow.Orders.Order[] = [];
|
||||
let offset = opt.offset ?? 0;
|
||||
do {
|
||||
const params = new URLSearchParams();
|
||||
if (opt.status !== undefined) params.set("status", opt.status);
|
||||
params.set("offset", String(offset));
|
||||
params.set("limit", String(opt.limit ?? 100));
|
||||
const res = await fetch(`${this.creds.apiSitesUrl}/orders?${params.toString()}`, {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeader,
|
||||
});
|
||||
if (!res.ok) throw new WebflowError("Failed to get all Webflow orders", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Webflow.MetaDataMulti<"orders", Webflow.Orders.Order>;
|
||||
allOrders.push(...payload.orders);
|
||||
offset = allOrders.length;
|
||||
if (allOrders.length >= payload.pagination.total) break;
|
||||
} while (opt.forceAll);
|
||||
return allOrders;
|
||||
}
|
||||
|
||||
async update(
|
||||
|
||||
Reference in New Issue
Block a user