kjelsrud.dev/node_modules/astro/dist/core/request.js
2023-07-19 21:31:30 +02:00

50 lines
1.3 KiB
JavaScript

import { warn } from "./logger/core.js";
const clientAddressSymbol = Symbol.for("astro.clientAddress");
const clientLocalsSymbol = Symbol.for("astro.locals");
function createRequest({
url,
headers,
clientAddress,
method = "GET",
body = void 0,
logging,
ssr,
locals
}) {
let headersObj = headers instanceof Headers ? headers : new Headers(Object.entries(headers));
const request = new Request(url.toString(), {
method,
headers: headersObj,
body
});
Object.defineProperties(request, {
params: {
get() {
warn(logging, "deprecation", `Astro.request.params has been moved to Astro.params`);
return void 0;
}
}
});
if (!ssr) {
const _headers = request.headers;
const headersDesc = Object.getOwnPropertyDescriptor(request, "headers") || {};
Object.defineProperty(request, "headers", {
...headersDesc,
get() {
warn(
logging,
"ssg",
`Headers are not exposed in static (SSG) output mode. To enable headers: set \`output: "server"\` in your config file.`
);
return _headers;
}
});
} else if (clientAddress) {
Reflect.set(request, clientAddressSymbol, clientAddress);
}
Reflect.set(request, clientLocalsSymbol, locals ?? {});
return request;
}
export {
createRequest
};