UNPKG

autumn-js

Version:

Autumn JS Library

169 lines (163 loc) 4.62 kB
import { createRouterWithOptions } from "./chunk-FRL4IWBU.mjs"; import "./chunk-QXJ7YDTB.mjs"; import "./chunk-NDN25XDG.mjs"; import "./chunk-NDS3EHT3.mjs"; import "./chunk-C2YFOJYP.mjs"; import { Autumn } from "./chunk-W63TY2DI.mjs"; import { autumnApiUrl } from "./chunk-KSG3E4Q2.mjs"; import "./chunk-6DZX6EAA.mjs"; // src/libraries/backend/tanstack.ts import { findRoute } from "rou3"; // node_modules/cookie-es/dist/index.mjs function splitSetCookieString(cookiesString) { if (Array.isArray(cookiesString)) { return cookiesString.flatMap((c) => splitSetCookieString(c)); } if (typeof cookiesString !== "string") { return []; } const cookiesStrings = []; let pos = 0; let start; let ch; let lastComma; let nextStart; let cookiesSeparatorFound; const skipWhitespace = () => { while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { pos += 1; } return pos < cookiesString.length; }; const notSpecialChar = () => { ch = cookiesString.charAt(pos); return ch !== "=" && ch !== ";" && ch !== ","; }; while (pos < cookiesString.length) { start = pos; cookiesSeparatorFound = false; while (skipWhitespace()) { ch = cookiesString.charAt(pos); if (ch === ",") { lastComma = pos; pos += 1; skipWhitespace(); nextStart = pos; while (pos < cookiesString.length && notSpecialChar()) { pos += 1; } if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") { cookiesSeparatorFound = true; pos = nextStart; cookiesStrings.push(cookiesString.slice(start, lastComma)); start = pos; } else { pos = lastComma + 1; } } else { pos += 1; } } if (!cookiesSeparatorFound || pos >= cookiesString.length) { cookiesStrings.push(cookiesString.slice(start, cookiesString.length)); } } return cookiesStrings; } // node_modules/@tanstack/start-client-core/dist/esm/headers.js function toHeadersInstance(init) { if (init instanceof Headers) { return new Headers(init); } else if (Array.isArray(init)) { return new Headers(init); } else if (typeof init === "object") { return new Headers(init); } else { return new Headers(); } } function mergeHeaders(...headers) { return headers.reduce((acc, header) => { const headersInstance = toHeadersInstance(header); for (const [key, value] of headersInstance.entries()) { if (key === "set-cookie") { const splitCookies = splitSetCookieString(value); splitCookies.forEach((cookie) => acc.append("set-cookie", cookie)); } else { acc.set(key, value); } } return acc; }, new Headers()); } // node_modules/@tanstack/start-client-core/dist/esm/json.js function json(payload, init) { return new Response(JSON.stringify(payload), { ...init, headers: mergeHeaders( { "content-type": "application/json" }, init == null ? void 0 : init.headers ) }); } // src/libraries/backend/tanstack.ts var autumnHandler = (options) => { const autumn = new Autumn({ url: autumnApiUrl, version: options.version }); const router = createRouterWithOptions(); const handleRequest = async (ctx) => { const { request } = ctx; const url = new URL(request.url); const searchParams = Object.fromEntries(url.searchParams); const pathname = url.pathname; const method = request.method; const match = findRoute(router, method, pathname); if (!match) { return new Response(JSON.stringify({ error: "Not found" }), { status: 404, headers: { "Content-Type": "application/json" } }); } const { data, params: pathParams } = match; const { handler } = data; let body = null; if (method === "POST" || method === "PUT" || method === "PATCH") { try { body = await request.json(); } catch (error) { } } try { const result = await handler({ autumn, body, path: pathname, getCustomer: async () => await options.identify(ctx), pathParams, searchParams }); return json(result.body, { status: result.statusCode }); } catch (error) { console.error("Autumn handler error:", error.message); return json({ error: error.message }, { status: 500 }); } }; return { GET: handleRequest, POST: handleRequest, PUT: handleRequest, PATCH: handleRequest, DELETE: handleRequest }; }; export { autumnHandler };