autumn-js
Version:
Autumn JS Library
180 lines (174 loc) • 5.11 kB
JavaScript
import {
secretKeyCheck
} from "./chunk-UNZHJTEY.mjs";
import {
createRouterWithOptions
} from "./chunk-2RZ2FZX2.mjs";
import "./chunk-5EKNB4IJ.mjs";
import "./chunk-3OLXYDCU.mjs";
import "./chunk-K7JGEYUX.mjs";
import "./chunk-HHMIVOXX.mjs";
import "./chunk-5H6HVCOP.mjs";
import {
Autumn
} from "./chunk-IOSEAUDJ.mjs";
import "./chunk-45WVZY23.mjs";
import {
autumnApiUrl
} from "./chunk-KSG3E4Q2.mjs";
import "./chunk-6DZX6EAA.mjs";
// src/libraries/backend/tanstack.ts
import { findRoute } from "rou3";
// ../node_modules/.pnpm/cookie-es@1.2.2/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/.pnpm/@tanstack+start-client-core@1.121.34/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/.pnpm/@tanstack+start-client-core@1.121.34/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();
let { found, error: resError } = secretKeyCheck(options?.secretKey);
const handleRequest = async (ctx) => {
const { request } = ctx;
if (!found && !options.secretKey) {
return new Response(JSON.stringify(resError), {
status: resError.statusCode
});
}
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
};