UNPKG

autumn-js

Version:
165 lines (163 loc) 5.25 kB
import { secretKeyCheck } from "./chunk-UNZHJTEY.mjs"; import { createRouterWithOptions } from "./chunk-4MEEJLXG.mjs"; import "./chunk-PO4EL4BW.mjs"; import "./chunk-MHIUO3ST.mjs"; import "./chunk-USQ76FYI.mjs"; import "./chunk-LOSIWWM2.mjs"; import "./chunk-35N7BIAE.mjs"; import { Autumn } from "./chunk-QOJMX7ML.mjs"; import "./chunk-2TEL6LR5.mjs"; import "./chunk-KSG3E4Q2.mjs"; // src/libraries/backend/convex.ts import { findRoute } from "rou3"; function autumnHandler(options) { const router = createRouterWithOptions(); return options.httpAction(async (ctx, request) => { const headers = request.headers; if (headers.get("Origin") !== null && headers.get("Access-Control-Request-Method") !== null && headers.get("Access-Control-Request-Headers") !== null) { return new Response(null, { headers: new Headers({ // e.g. https://mywebsite.com, configured on your Convex dashboard, or passed in as an option "Access-Control-Allow-Origin": process.env.CLIENT_ORIGIN ?? "http://localhost:3000", "Access-Control-Allow-Methods": "POST, GET, PATCH, DELETE, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Digest, Authorization, Cookie", "Access-Control-Allow-Credentials": "true", "Access-Control-Max-Age": "86400" }) }); } let { found, error: resError } = secretKeyCheck(options.secretKey); if (!found && !options.secretKey) { return Response.json(resError, { status: resError.statusCode }); } const method = request.method; const url = new URL(request.url); const searchParams = Object.fromEntries(url.searchParams); const pathname = url.pathname; const match = findRoute(router, method, pathname); if (!match) { return Response.json({ error: "Not found" }, { status: 404 }); } const { data, params: pathParams } = match; const { handler } = data; const autumn = new Autumn({ secretKey: options.secretKey || void 0, url: options.url }); let body = null; if (method === "POST" || method === "PUT" || method === "PATCH") { try { body = await request.json(); } catch (error) { } } const result = await handler({ autumn, body, path: url.pathname, getCustomer: async () => await options.identify(ctx, request), pathParams, searchParams }); return Response.json(result.body, { status: result.statusCode, headers: new Headers({ "Access-Control-Allow-Origin": process.env.CLIENT_ORIGIN ?? "http://localhost:3000", "Access-Control-Allow-Credentials": "true", Vary: "origin" }) }); }); } function convexHandler(options) { const router = createRouterWithOptions(); const corsHeaders = { "Access-Control-Allow-Origin": options.corsOrigin ?? process.env.CLIENT_ORIGIN ?? "http://localhost:3000", "Access-Control-Allow-Methods": "POST, GET, PATCH, DELETE, OPTIONS", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Credentials": "true", "Access-Control-Max-Age": "86400", "Vary": "Origin" }; return async (ctx, request) => { const headers = request.headers; if (request.method === "OPTIONS") { return new Response(null, { headers: new Headers(corsHeaders) }); } let { found, error: resError } = secretKeyCheck(options.secretKey); if (!found && !options.secretKey) { return new Response(JSON.stringify(resError), { status: resError.statusCode, headers: { "Content-Type": "application/json", ...corsHeaders } }); } const method = request.method; const url = new URL(request.url); const searchParams = Object.fromEntries(url.searchParams); const pathname = url.pathname; const match = findRoute(router, method, pathname); if (!match) { return new Response(JSON.stringify({ error: "Not found" }), { status: 404, headers: { "Content-Type": "application/json", ...corsHeaders } }); } const { data, params: pathParams } = match; const { handler } = data; const autumn = new Autumn({ secretKey: options.secretKey || void 0, url: options.url }); let body = null; if (method === "POST" || method === "PUT" || method === "PATCH") { try { body = await request.json(); } catch (error) { } } try { console.log("Identity in convex.ts:", options.identity); const result = await handler({ autumn, body, path: url.pathname, getCustomer: async () => options.identity, pathParams, searchParams }); return new Response(JSON.stringify(result.body), { status: result.statusCode, headers: { "Content-Type": "application/json", ...corsHeaders } }); } catch (error) { return new Response(JSON.stringify({ error: "Internal server error" }), { status: 500, headers: { "Content-Type": "application/json", ...corsHeaders } }); } }; } export { autumnHandler, convexHandler };