Api cleanup

This commit is contained in:
Dominic Ferrando
2026-07-10 14:45:16 -04:00
parent 105d2b6241
commit a09d11e8e4
3 changed files with 57 additions and 56 deletions
+3 -3
View File
@@ -1,10 +1,10 @@
import { Printful, PrintfulClient, PrintfulError, Webflow, WebflowClient, WebflowError } from "@blade-and-brawn/commerce"; import { Printful, PrintfulClient, PrintfulError, Webflow, WebflowClient, WebflowError } from "@blade-and-brawn/commerce";
import { env } from "../util"; import { env } from "../util";
const BASE_URL = "http://dev.api.bladeandbrawn.com"; const DOMAIN = "dev.api.bladeandbrawn.com";
const PRINTFUL_WEBHOOK_URL = `${BASE_URL}/webhook/printful`; const PRINTFUL_WEBHOOK_URL = `http://${DOMAIN}/webhooks/printful`;
const WEBFLOW_WEBHOOK_URL = `${BASE_URL}/webhook/webflow`; const WEBFLOW_WEBHOOK_URL = `https://${DOMAIN}/webhooks/webflow`;
function printError(err: unknown): never { function printError(err: unknown): never {
if (err instanceof WebflowError || err instanceof PrintfulError) if (err instanceof WebflowError || err instanceof PrintfulError)
+10 -9
View File
@@ -75,8 +75,10 @@ export const app = new Elysia()
}) })
.onAfterResponse(({ request, status, path }) => { .onAfterResponse(({ request, status, path }) => {
const skip: Record<string, string[]> = { "/products/sync/": ["GET"] }; if (env.NODE_ENV === "development") {
if (env.NODE_ENV === "development" && skip[path]?.includes(request.method)) return; const skip: Record<string, string[]> = { "/commerce/products/sync/": ["GET"] };
if (skip[path]?.includes(request.method)) return;
}
log.info({ log.info({
method: request.method, method: request.method,
@@ -107,8 +109,7 @@ export const app = new Elysia()
}, { body: t.Object({ password: t.String() }) }) }, { body: t.Object({ password: t.String() }) })
// CALCULATOR // CALCULATOR
.group("/calculator", .group("/calculator", (app) => app
(app) => app
// Non-authenticated // Non-authenticated
.post("/calculate", async ({ body }) => { .post("/calculate", async ({ body }) => {
return { levels: await s.Calculator.calculate(body.player, body.activityPerformances) }; return { levels: await s.Calculator.calculate(body.player, body.activityPerformances) };
@@ -177,6 +178,7 @@ export const app = new Elysia()
) )
// COMMERCE // COMMERCE
.group("/commerce", (app) => app
.group("/products", .group("/products",
(app) => app (app) => app
.resolve(async ({ jwt, cookie: { auth } }) => { .resolve(async ({ jwt, cookie: { auth } }) => {
@@ -184,8 +186,7 @@ export const app = new Elysia()
if (!token || !token.sessionId) throw status(401, "Unauthorized"); if (!token || !token.sessionId) throw status(401, "Unauthorized");
return { sessionId: token.sessionId.toString() }; return { sessionId: token.sessionId.toString() };
}) })
.group("/sync", (app) => .group("/sync", (app) => app
app
// Sync status // Sync status
.get("/", async ({ sessionId }) => { .get("/", async ({ sessionId }) => {
const latestSync = await s.Commerce.Sync.getLatest(sessionId); const latestSync = await s.Commerce.Sync.getLatest(sessionId);
@@ -219,10 +220,10 @@ export const app = new Elysia()
}, { }, {
params: t.Object({ pProductId: t.Numeric() }) params: t.Object({ pProductId: t.Numeric() })
}) })
) ))
// WEBHOOKS // WEBHOOKS
.post("/webhook/printful", async ({ body }) => { .post("/webhooks/printful", async ({ body }) => {
// https://webflow.com/integrations/printful // https://webflow.com/integrations/printful
const payload = body as Printful.Webhook.EventPayload; const payload = body as Printful.Webhook.EventPayload;
@@ -267,7 +268,7 @@ export const app = new Elysia()
log.warn({ type: (payload as any).type }, "printful webhook: unhandled event type"); log.warn({ type: (payload as any).type }, "printful webhook: unhandled event type");
} }
}) })
.post("/webhook/webflow", async ({ request, body }) => { .post("/webhooks/webflow", async ({ request, body }) => {
if (!s.Commerce.Webflow.Util.verifyWebflowSignature(request, body)) if (!s.Commerce.Webflow.Util.verifyWebflowSignature(request, body))
throw status(400, "Invalid signature"); throw status(400, "Invalid signature");
@@ -14,7 +14,7 @@
return this.status === "active" || this.status === "queued"; return this.status === "active" || this.status === "queued";
}, },
poll: async function () { poll: async function () {
const res = await api.products.sync.get(); const res = await api.commerce.products.sync.get();
if (res.error) { if (res.error) {
synchronizer.status = "none"; synchronizer.status = "none";
synchronizer.syncingPProductIds = []; synchronizer.syncingPProductIds = [];
@@ -32,11 +32,11 @@
}, },
syncAll: async () => { syncAll: async () => {
synchronizer.startPolling(); synchronizer.startPolling();
await api.products.sync.post(); await api.commerce.products.sync.post();
}, },
sync: async (pProductId: number) => { sync: async (pProductId: number) => {
synchronizer.startPolling(); synchronizer.startPolling();
await api.products.sync({ pProductId }).post(); await api.commerce.products.sync({ pProductId }).post();
}, },
}); });
@@ -86,7 +86,7 @@
<td <td
><button ><button
onclick={async () => { onclick={async () => {
const res = await api const res = await api.commerce
.products({ .products({
pProductId: pProductId:
pProduct.id, pProduct.id,