2022-08-25 10:52:19 +00:00
|
|
|
import { Middleware } from "retes";
|
|
|
|
|
2022-09-01 13:17:48 +00:00
|
|
|
import { createMiddlewareDebug } from "./middleware-debug";
|
|
|
|
|
|
|
|
const debug = createMiddlewareDebug("withBaseURL");
|
|
|
|
|
2022-08-25 10:52:19 +00:00
|
|
|
export const withBaseURL: Middleware = (handler) => async (request) => {
|
|
|
|
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
|
|
|
|
|
2022-09-01 13:17:48 +00:00
|
|
|
debug("Middleware called with host: %s, protocol %s", host, request.headers["x-forwarded-proto"]);
|
|
|
|
|
2022-08-25 10:52:19 +00:00
|
|
|
request.context.baseURL = `${protocol}://${host}`;
|
|
|
|
|
2022-09-01 13:17:48 +00:00
|
|
|
debug("context.baseURL resolved to be: \"%s\"", request.context.baseURL);
|
|
|
|
|
2022-08-25 10:52:19 +00:00
|
|
|
return handler(request);
|
|
|
|
};
|