Setup dev printful/webflow webhooks
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@ export const env = {
|
||||
WEBFLOW_SITE_ID: requireEnv("WEBFLOW_SITE_ID"),
|
||||
WEBFLOW_COLLECTION_ID: requireEnv("WEBFLOW_COLLECTION_ID"),
|
||||
WEBFLOW_AUTH: requireEnv("WEBFLOW_AUTH"),
|
||||
WEBFLOW_WEBHOOK_SECRET: optionEnv("WEBFLOW_WEBHOOK_SECRET"),
|
||||
WEBFLOW_WEBHOOK_SECRET: requireEnv("WEBFLOW_WEBHOOK_SECRET"),
|
||||
NODE_ENV: optionEnv("NODE_ENV", "development"),
|
||||
LOG_LEVEL: optionEnv("LOG_LEVEL", "info")
|
||||
};
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Printful, PrintfulClient } from "@blade-and-brawn/commerce";
|
||||
import { env } from "../env";
|
||||
|
||||
const printful = new PrintfulClient({
|
||||
token: env.PRINTFUL_AUTH,
|
||||
storeId: env.PRINTFUL_STORE_ID,
|
||||
});
|
||||
|
||||
await printful.Webhooks.register("https://dev.bladeandbrawn.com/webhook/printful", [
|
||||
Printful.Webhook.Event.ProductUpdated,
|
||||
Printful.Webhook.Event.ProductDeleted,
|
||||
Printful.Webhook.Event.PackageShipped,
|
||||
]);
|
||||
|
||||
const config = await printful.Webhooks.getConfig();
|
||||
console.log(config);
|
||||
@@ -162,6 +162,7 @@ export const app = new Elysia()
|
||||
|
||||
// WEBHOOKS
|
||||
.post("/webhook/printful", async ({ body }) => {
|
||||
// https://webflow.com/integrations/printful
|
||||
const payload = body as Printful.Webhook.EventPayload;
|
||||
|
||||
switch (payload.type) {
|
||||
|
||||
@@ -121,9 +121,43 @@ class OrdersClient {
|
||||
}
|
||||
}
|
||||
|
||||
class WebhooksClient {
|
||||
constructor(private creds: Credentials) { }
|
||||
|
||||
async getConfig(): Promise<Printful.Webhook.Config | undefined> {
|
||||
const res = await fetch(`${this.creds.apiUrl}/webhooks`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...this.creds.authHeaders,
|
||||
}
|
||||
});
|
||||
if (res.status === 404 || res.status === 400) return;
|
||||
if (!res.ok) throw new PrintfulError("Failed to get webhook configuration", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Printful.Webhook.Config;
|
||||
return payload;
|
||||
}
|
||||
|
||||
async register(url: string, events: Printful.Webhook.Event[]) {
|
||||
const res = await fetch(`${this.creds.apiUrl}/webhooks`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...this.creds.authHeaders,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"url": url,
|
||||
"types": events
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new PrintfulError("Failed to register webhook URL", res.status, await parsePayload(res));
|
||||
}
|
||||
}
|
||||
|
||||
export class PrintfulClient {
|
||||
readonly Products: ProductsClient;
|
||||
readonly Orders: OrdersClient;
|
||||
readonly Webhooks: WebhooksClient;
|
||||
|
||||
constructor(options: ClientOptions) {
|
||||
const creds: Credentials = {
|
||||
@@ -135,6 +169,7 @@ export class PrintfulClient {
|
||||
};
|
||||
this.Products = new ProductsClient(creds);
|
||||
this.Orders = new OrdersClient(creds);
|
||||
this.Webhooks = new WebhooksClient(creds);
|
||||
}
|
||||
|
||||
static Util = {
|
||||
|
||||
@@ -133,6 +133,11 @@ export namespace Printful {
|
||||
PackageShipped = "package_shipped",
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
url: string,
|
||||
types: Event[]
|
||||
}
|
||||
|
||||
export interface ProductUpdated extends MetaData<
|
||||
Event.ProductUpdated,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user