75 lines
2.4 KiB
Plaintext
75 lines
2.4 KiB
Plaintext
import { AstroError, AstroErrorData } from "../errors/index.js";
|
|
const VALID_PARAM_TYPES = ["string", "number", "undefined"];
|
|
function validateGetStaticPathsParameter([key, value], route) {
|
|
if (!VALID_PARAM_TYPES.includes(typeof value)) {
|
|
throw new AstroError({
|
|
...AstroErrorData.GetStaticPathsInvalidRouteParam,
|
|
message: AstroErrorData.GetStaticPathsInvalidRouteParam.message(key, value, typeof value),
|
|
location: {
|
|
file: route
|
|
}
|
|
});
|
|
}
|
|
}
|
|
function validateDynamicRouteModule(mod, {
|
|
ssr,
|
|
route
|
|
}) {
|
|
if ((!ssr || route.prerender) && !mod.getStaticPaths) {
|
|
throw new AstroError({
|
|
...AstroErrorData.GetStaticPathsRequired,
|
|
location: { file: route.component }
|
|
});
|
|
}
|
|
}
|
|
function validateGetStaticPathsResult(result, logger, route) {
|
|
if (!Array.isArray(result)) {
|
|
throw new AstroError({
|
|
...AstroErrorData.InvalidGetStaticPathsReturn,
|
|
message: AstroErrorData.InvalidGetStaticPathsReturn.message(typeof result),
|
|
location: {
|
|
file: route.component
|
|
}
|
|
});
|
|
}
|
|
result.forEach((pathObject) => {
|
|
if (typeof pathObject === "object" && Array.isArray(pathObject) || pathObject === null) {
|
|
throw new AstroError({
|
|
...AstroErrorData.InvalidGetStaticPathsEntry,
|
|
message: AstroErrorData.InvalidGetStaticPathsEntry.message(
|
|
Array.isArray(pathObject) ? "array" : typeof pathObject
|
|
)
|
|
});
|
|
}
|
|
if (pathObject.params === void 0 || pathObject.params === null || pathObject.params && Object.keys(pathObject.params).length === 0) {
|
|
throw new AstroError({
|
|
...AstroErrorData.GetStaticPathsExpectedParams,
|
|
location: {
|
|
file: route.component
|
|
}
|
|
});
|
|
}
|
|
for (const [key, val] of Object.entries(pathObject.params)) {
|
|
if (!(typeof val === "undefined" || typeof val === "string" || typeof val === "number")) {
|
|
logger.warn(
|
|
"router",
|
|
`getStaticPaths() returned an invalid path param: "${key}". A string, number or undefined value was expected, but got \`${JSON.stringify(
|
|
val
|
|
)}\`.`
|
|
);
|
|
}
|
|
if (typeof val === "string" && val === "") {
|
|
logger.warn(
|
|
"router",
|
|
`getStaticPaths() returned an invalid path param: "${key}". \`undefined\` expected for an optional param, but got empty string.`
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
export {
|
|
validateDynamicRouteModule,
|
|
validateGetStaticPathsParameter,
|
|
validateGetStaticPathsResult
|
|
};
|