Cors
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { calcAttributeLevels, calcPlayerLevel, computeLevels } from "./calculator/calc.ts";
|
import { calcAttributeLevels, calcPlayerLevel, computeLevels } from "./calculator/calc.ts";
|
||||||
import { activities, Activity, BenchmarkPerformance, Gender, genders } from "./calculator/util.ts";
|
import { Activity, BenchmarkPerformance, Gender } from "./calculator/util.ts";
|
||||||
import { productRecords } from "./data/product-records.ts";
|
import { productRecords } from "./data/product-records.ts";
|
||||||
import { Printful } from "./printful.ts"
|
import { Printful } from "./printful.ts"
|
||||||
import { Webflow } from "./webflow.ts";
|
import { Webflow } from "./webflow.ts";
|
||||||
@@ -16,10 +16,30 @@ interface CalcRequest {
|
|||||||
}[]
|
}[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ClientResponse extends Response {
|
||||||
|
constructor(body?: BodyInit | null, init: ResponseInit = {}) {
|
||||||
|
const headers = new Headers(init.headers);
|
||||||
|
headers.set("Access-Control-Allow-Origin", "*");
|
||||||
|
headers.set("Access-Control-Allow-Methods", "POST, OPTIONS");
|
||||||
|
headers.set("Access-Control-Allow-Headers", "Content-Type");
|
||||||
|
super(body, { ...init, headers });
|
||||||
|
}
|
||||||
|
|
||||||
|
static json(data: unknown, init: ResponseInit = {}) {
|
||||||
|
const body = JSON.stringify(data);
|
||||||
|
const headers = new Headers(init.headers);
|
||||||
|
headers.set("Content-Type", "application/json");
|
||||||
|
return new ClientResponse(body, { ...init, headers });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
routes: {
|
routes: {
|
||||||
"/calc": {
|
"/calc": {
|
||||||
async GET(req) {
|
OPTIONS() {
|
||||||
|
return new ClientResponse(undefined, { status: 204 });
|
||||||
|
},
|
||||||
|
async POST(req) {
|
||||||
const calcReq: CalcRequest = await req.json();
|
const calcReq: CalcRequest = await req.json();
|
||||||
const { player, performances } = calcReq;
|
const { player, performances } = calcReq;
|
||||||
|
|
||||||
@@ -39,15 +59,18 @@ const server = Bun.serve({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.json({
|
return ClientResponse.json({
|
||||||
levels: {
|
levels: {
|
||||||
attributes: calcAttributeLevels(computedPerformances),
|
attributes: calcAttributeLevels(computedPerformances),
|
||||||
player: calcPlayerLevel(computedPerformances)
|
player: calcPlayerLevel(computedPerformances)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/webhook/printful": {
|
"/webhook/printful": {
|
||||||
|
OPTIONS() {
|
||||||
|
return new ClientResponse(undefined, { status: 204 });
|
||||||
|
},
|
||||||
async POST(req) {
|
async POST(req) {
|
||||||
const payload: Printful.Webhook.EventPayload = await req.json()
|
const payload: Printful.Webhook.EventPayload = await req.json()
|
||||||
console.log(JSON.stringify(payload));
|
console.log(JSON.stringify(payload));
|
||||||
@@ -76,10 +99,10 @@ const server = Bun.serve({
|
|||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
Response.error();
|
ClientResponse.error();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.json("");
|
return ClientResponse.json("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user