2024-09-05 22:29:12 +02:00
|
|
|
import { parse as parseCookie, serialize as serializeCookie } from "cookie";
|
2024-10-03 16:53:26 +02:00
|
|
|
import { API_BASE, INTERNAL_API_BASE } from "~/env.server";
|
2024-09-05 22:29:12 +02:00
|
|
|
import { ApiError, ErrorCode } from "./api/error";
|
2024-09-30 20:14:03 +02:00
|
|
|
import { tokenCookieName } from "~/lib/utils";
|
2024-09-05 22:29:12 +02:00
|
|
|
|
|
|
|
export type RequestParams = {
|
|
|
|
token?: string;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
body?: any;
|
|
|
|
headers?: Record<string, string>;
|
2024-10-01 21:25:51 +02:00
|
|
|
isInternal?: boolean;
|
2024-09-05 22:29:12 +02:00
|
|
|
};
|
|
|
|
|
2024-10-03 16:53:26 +02:00
|
|
|
export type RequestMethod = "GET" | "POST" | "PATCH" | "DELETE";
|
|
|
|
|
2024-10-02 02:46:39 +02:00
|
|
|
export async function baseRequest(
|
2024-10-03 16:53:26 +02:00
|
|
|
method: RequestMethod,
|
2024-09-05 22:29:12 +02:00
|
|
|
path: string,
|
|
|
|
params: RequestParams = {},
|
2024-10-01 21:25:51 +02:00
|
|
|
): Promise<Response> {
|
2024-10-03 16:53:26 +02:00
|
|
|
// Internal requests, unauthenticated requests, and GET requests bypass the rate limiting proxy.
|
|
|
|
// All other requests go through the proxy, and are rate limited.
|
|
|
|
let base = params.isInternal || !params.token || method === "GET" ? INTERNAL_API_BASE : API_BASE;
|
|
|
|
base += params.isInternal ? "/internal" : "/v2";
|
2024-10-01 21:25:51 +02:00
|
|
|
|
|
|
|
const url = `${base}${path}`;
|
2024-09-05 22:29:12 +02:00
|
|
|
const resp = await fetch(url, {
|
|
|
|
method,
|
|
|
|
body: params.body ? JSON.stringify(params.body) : undefined,
|
|
|
|
headers: {
|
|
|
|
...params.headers,
|
|
|
|
...(params.token ? { Authorization: params.token } : {}),
|
2024-11-03 02:07:07 +01:00
|
|
|
...(params.body ? { "Content-Type": "application/json" } : {}),
|
2024-09-05 22:29:12 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.headers.get("Content-Type")?.indexOf("application/json") === -1) {
|
|
|
|
// If we don't get a JSON response, the server almost certainly encountered an internal error it couldn't recover from
|
|
|
|
// (that, or the reverse proxy, which should also be treated as a 500 error)
|
|
|
|
throw {
|
|
|
|
status: 500,
|
|
|
|
code: ErrorCode.InternalServerError,
|
|
|
|
message: "Internal server error",
|
|
|
|
} as ApiError;
|
|
|
|
}
|
|
|
|
|
2024-09-06 15:01:44 +02:00
|
|
|
if (resp.status < 200 || resp.status >= 400) throw (await resp.json()) as ApiError;
|
2024-10-01 21:25:51 +02:00
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-10-03 16:53:26 +02:00
|
|
|
export async function fastRequest(method: RequestMethod, path: string, params: RequestParams = {}) {
|
2024-10-02 02:46:39 +02:00
|
|
|
await baseRequest(method, path, params);
|
2024-10-01 21:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function serverRequest<T>(
|
2024-10-03 16:53:26 +02:00
|
|
|
method: RequestMethod,
|
2024-10-01 21:25:51 +02:00
|
|
|
path: string,
|
|
|
|
params: RequestParams = {},
|
|
|
|
) {
|
2024-10-02 02:46:39 +02:00
|
|
|
const resp = await baseRequest(method, path, params);
|
2024-09-05 22:29:12 +02:00
|
|
|
return (await resp.json()) as T;
|
|
|
|
}
|
|
|
|
|
2024-09-30 20:14:03 +02:00
|
|
|
export const getToken = (req: Request) => getCookie(req, tokenCookieName);
|
2024-09-11 19:13:54 +02:00
|
|
|
|
2024-09-06 15:01:44 +02:00
|
|
|
export function getCookie(req: Request, cookieName: string): string | undefined {
|
2024-09-05 22:29:12 +02:00
|
|
|
const header = req.headers.get("Cookie");
|
|
|
|
if (!header) return undefined;
|
|
|
|
|
|
|
|
const cookie = parseCookie(header);
|
|
|
|
return cookieName in cookie ? cookie[cookieName] : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const YEAR = 365 * 86400;
|
|
|
|
|
2024-09-06 15:01:44 +02:00
|
|
|
export const writeCookie = (cookieName: string, value: string, maxAge: number | undefined = YEAR) =>
|
2024-09-05 22:29:12 +02:00
|
|
|
serializeCookie(cookieName, value, {
|
|
|
|
maxAge,
|
|
|
|
path: "/",
|
|
|
|
sameSite: "lax",
|
|
|
|
httpOnly: true,
|
2024-09-30 20:14:03 +02:00
|
|
|
secure: true,
|
2024-09-05 22:29:12 +02:00
|
|
|
});
|