2022-01-07 20:07:38 +00:00
|
|
|
import { IncomingMessage } from "http";
|
|
|
|
import { parse } from "url";
|
|
|
|
import { ParsedRequest } from "./types";
|
|
|
|
|
|
|
|
export function parseRequest(req: IncomingMessage) {
|
|
|
|
console.log("HTTP " + req.url);
|
|
|
|
const { pathname, query } = parse(req.url || "/", true);
|
2022-01-10 05:50:31 +00:00
|
|
|
const {
|
|
|
|
fontSize,
|
|
|
|
images,
|
|
|
|
widths,
|
|
|
|
heights,
|
|
|
|
theme,
|
|
|
|
md,
|
|
|
|
|
|
|
|
// Attributes for Manifold card:
|
|
|
|
question,
|
|
|
|
probability,
|
|
|
|
metadata,
|
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
creatorAvatarUrl,
|
|
|
|
} = query || {};
|
2022-01-07 20:07:38 +00:00
|
|
|
|
|
|
|
if (Array.isArray(fontSize)) {
|
|
|
|
throw new Error("Expected a single fontSize");
|
|
|
|
}
|
|
|
|
if (Array.isArray(theme)) {
|
|
|
|
throw new Error("Expected a single theme");
|
|
|
|
}
|
|
|
|
|
|
|
|
const arr = (pathname || "/").slice(1).split(".");
|
|
|
|
let extension = "";
|
|
|
|
let text = "";
|
|
|
|
if (arr.length === 0) {
|
|
|
|
text = "";
|
|
|
|
} else if (arr.length === 1) {
|
|
|
|
text = arr[0];
|
|
|
|
} else {
|
|
|
|
extension = arr.pop() as string;
|
|
|
|
text = arr.join(".");
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:50:31 +00:00
|
|
|
// Take a url query param and return a single string
|
|
|
|
const getString = (stringOrArray: string[] | string | undefined): string => {
|
|
|
|
if (Array.isArray(stringOrArray)) {
|
|
|
|
// If the query param is an array, return the first element
|
|
|
|
return stringOrArray[0];
|
|
|
|
}
|
|
|
|
return stringOrArray || "";
|
|
|
|
};
|
|
|
|
|
2022-01-07 20:07:38 +00:00
|
|
|
const parsedRequest: ParsedRequest = {
|
|
|
|
fileType: extension === "jpeg" ? extension : "png",
|
|
|
|
text: decodeURIComponent(text),
|
|
|
|
theme: theme === "dark" ? "dark" : "light",
|
|
|
|
md: md === "1" || md === "true",
|
|
|
|
fontSize: fontSize || "96px",
|
|
|
|
images: getArray(images),
|
|
|
|
widths: getArray(widths),
|
|
|
|
heights: getArray(heights),
|
2022-01-10 05:50:31 +00:00
|
|
|
|
|
|
|
question:
|
|
|
|
getString(question) || "Will you create a prediction market on Manifold?",
|
2022-01-10 07:05:24 +00:00
|
|
|
probability: getString(probability) || "85%",
|
2022-01-10 05:50:31 +00:00
|
|
|
metadata: getString(metadata) || "Jan 1 • M$ 123 pool",
|
|
|
|
creatorName: getString(creatorName) || "Manifold Markets",
|
|
|
|
creatorUsername: getString(creatorUsername) || "ManifoldMarkets",
|
2022-01-10 07:05:24 +00:00
|
|
|
creatorAvatarUrl: getString(creatorAvatarUrl) || "",
|
2022-01-07 20:07:38 +00:00
|
|
|
};
|
|
|
|
parsedRequest.images = getDefaultImages(parsedRequest.images);
|
|
|
|
return parsedRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArray(stringOrArray: string[] | string | undefined): string[] {
|
|
|
|
if (typeof stringOrArray === "undefined") {
|
|
|
|
return [];
|
|
|
|
} else if (Array.isArray(stringOrArray)) {
|
|
|
|
return stringOrArray;
|
|
|
|
} else {
|
|
|
|
return [stringOrArray];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultImages(images: string[]): string[] {
|
|
|
|
const defaultImage = "https://manifold.markets/logo.png";
|
|
|
|
|
|
|
|
if (!images || !images[0]) {
|
|
|
|
return [defaultImage];
|
|
|
|
}
|
|
|
|
return images;
|
|
|
|
}
|