Api cleanup
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
+50
-49
@@ -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,30 +109,29 @@ 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) };
|
}, {
|
||||||
}, {
|
body: t.Object({
|
||||||
body: t.Object({
|
player: PlayerSchema,
|
||||||
player: PlayerSchema,
|
activityPerformances: t.Array(ActivityPerformanceSchema)
|
||||||
activityPerformances: t.Array(ActivityPerformanceSchema)
|
}),
|
||||||
}),
|
})
|
||||||
})
|
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
const token = auth.value && await jwt.verify(auth.value);
|
||||||
const token = auth.value && await jwt.verify(auth.value);
|
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() };
|
})
|
||||||
})
|
.get("/standards/config", async () => {
|
||||||
.get("/standards/config", async () => {
|
return await s.Calculator.Standards.Config.get();
|
||||||
return await s.Calculator.Standards.Config.get();
|
})
|
||||||
})
|
.post("/standards/config/switch", async ({ body: { standardsConfigId } }) => {
|
||||||
.post("/standards/config/switch", async ({ body: { standardsConfigId } }) => {
|
await s.Calculator.Standards.Config.switch(standardsConfigId);
|
||||||
await s.Calculator.Standards.Config.switch(standardsConfigId);
|
}, {
|
||||||
}, {
|
body: t.Object({ standardsConfigId: t.String() })
|
||||||
body: t.Object({ standardsConfigId: t.String() })
|
})
|
||||||
})
|
|
||||||
)
|
)
|
||||||
.group("/standards", (app) => app
|
.group("/standards", (app) => app
|
||||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||||
@@ -177,15 +178,15 @@ export const app = new Elysia()
|
|||||||
)
|
)
|
||||||
|
|
||||||
// COMMERCE
|
// COMMERCE
|
||||||
.group("/products",
|
.group("/commerce", (app) => app
|
||||||
(app) => app
|
.group("/products",
|
||||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
(app) => app
|
||||||
const token = auth.value && await jwt.verify(auth.value);
|
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
const token = auth.value && await jwt.verify(auth.value);
|
||||||
return { sessionId: token.sessionId.toString() };
|
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||||
})
|
return { sessionId: token.sessionId.toString() };
|
||||||
.group("/sync", (app) =>
|
})
|
||||||
app
|
.group("/sync", (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);
|
||||||
@@ -205,24 +206,24 @@ export const app = new Elysia()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
|
}, { params: t.Object({ pProductId: t.Optional(t.Numeric()) }) }),
|
||||||
)
|
)
|
||||||
.get("/:pProductId", async ({ params: { pProductId } }) => {
|
.get("/:pProductId", async ({ params: { pProductId } }) => {
|
||||||
const pProduct = await s.Commerce.Printful.Products.get(pProductId);
|
const pProduct = await s.Commerce.Printful.Products.get(pProductId);
|
||||||
if (!pProduct) throw new NotFoundError("Missing printful product");
|
if (!pProduct) throw new NotFoundError("Missing printful product");
|
||||||
|
|
||||||
const wProductId = pProduct.sync_product.external_id.split("-")[0];
|
const wProductId = pProduct.sync_product.external_id.split("-")[0];
|
||||||
if (!wProductId) throw new NotFoundError("Missing webflow product ID");
|
if (!wProductId) throw new NotFoundError("Missing webflow product ID");
|
||||||
|
|
||||||
const wProduct = await s.Commerce.Webflow.Products.get(wProductId);
|
const wProduct = await s.Commerce.Webflow.Products.get(wProductId);
|
||||||
|
|
||||||
return { pProduct, wProduct };
|
return { pProduct, wProduct };
|
||||||
}, {
|
}, {
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user