@coursebuilder/core
Version:
Core package for Course Builder
145 lines (143 loc) • 4.37 kB
JavaScript
import {
isCourseBuilderAction
} from "./chunk-UMBG64VH.js";
import {
logger
} from "./chunk-B73XG2UO.js";
import {
CourseBuilderError
} from "./chunk-F32UWPXN.js";
import {
__name,
__publicField
} from "./chunk-VLQXSCFN.js";
// src/lib/utils/web.ts
import { parse as parseCookie, serialize } from "cookie";
async function getBody(req, config) {
const headers = Object.fromEntries(req.headers);
const isStripeWebhook = [
"stripe-signature"
].every((prop) => {
return prop in headers;
});
if (isStripeWebhook) {
let parsedBody;
const stripeProvider = config.providers.find((p) => p.id === "stripe");
parsedBody = await req.text();
stripeProvider?.options?.paymentsAdapter.verifyWebhookSignature(parsedBody, headers["stripe-signature"]);
parsedBody = JSON.parse(parsedBody);
return parsedBody;
}
if (!("body" in req) || !req.body || req.method !== "POST")
return;
const contentType = req.headers.get("content-type");
if (contentType?.includes("application/json")) {
try {
return await req.json();
} catch (e) {
logger.error(e);
return void 0;
}
} else if (contentType?.includes("application/x-www-form-urlencoded")) {
const params = new URLSearchParams(await req.text());
return Object.fromEntries(params);
}
}
__name(getBody, "getBody");
async function toInternalRequest(req, config) {
try {
config.basePath ?? (config.basePath = "/coursebuilder");
const url = new URL(req.url);
const { action, providerId } = parseActionAndProviderId(url.pathname, config.basePath);
console.debug({
url,
action,
providerId
});
return {
url,
action,
providerId,
method: req.method,
headers: Object.fromEntries(req.headers),
body: req.body ? await getBody(req, config) : void 0,
cookies: parseCookie(req.headers.get("cookie") ?? "") ?? {},
error: url.searchParams.get("error") ?? void 0,
query: Object.fromEntries(url.searchParams)
};
} catch (e) {
logger.error(e);
logger.debug("request", req);
}
}
__name(toInternalRequest, "toInternalRequest");
function parseActionAndProviderId(pathname, base) {
const a = pathname.match(new RegExp(`^${base}(.+)`));
if (a === null)
throw new UnknownAction(`Cannot parse action at ${pathname}`);
const [_, actionAndProviderId] = a;
const b = actionAndProviderId.replace(/^\//, "").split("/");
if (b.length !== 1 && b.length !== 2)
throw new UnknownAction(`**Cannot parse action at ${pathname}`);
const [action, providerId] = b;
if (!isCourseBuilderAction(action))
throw new UnknownAction(`***Cannot parse action at ${pathname}`);
if (providerId && ![
"webhook",
"srt",
"session",
"subscribe-to-list",
"checkout",
"redeem",
"subscriber",
"lookup",
"claimed",
"nameUpdate",
"transfer",
"refund",
"create-magic-link"
].includes(action))
throw new UnknownAction(`**** Cannot parse action at ${pathname}. add it to the list in web.ts?`);
return {
action,
providerId
};
}
__name(parseActionAndProviderId, "parseActionAndProviderId");
var _UnknownAction = class _UnknownAction extends CourseBuilderError {
};
__name(_UnknownAction, "UnknownAction");
__publicField(_UnknownAction, "type", "UnknownAction");
var UnknownAction = _UnknownAction;
function toResponse(res) {
const headers = new Headers(res.headers);
res.cookies?.forEach((cookie) => {
const { name, value, options } = cookie;
const cookieHeader = serialize(name, value, options);
if (headers.has("Set-Cookie"))
headers.append("Set-Cookie", cookieHeader);
else
headers.set("Set-Cookie", cookieHeader);
});
let body = res.body;
if (headers.get("content-type") === "application/json")
body = JSON.stringify(res.body);
else if (headers.get("content-type") === "application/x-www-form-urlencoded")
body = new URLSearchParams(res.body).toString();
const status = res.redirect ? 302 : res.status ?? 200;
const response = new Response(body, {
headers,
status
});
if (res.redirect)
response.headers.set("Location", res.redirect);
return response;
}
__name(toResponse, "toResponse");
export {
toInternalRequest,
parseActionAndProviderId,
UnknownAction,
toResponse
};
//# sourceMappingURL=chunk-CSOW4WEB.js.map