🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
5
node_modules/astro/dist/core/routing/index.d.ts
generated
vendored
Normal file
5
node_modules/astro/dist/core/routing/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
export { createRouteManifest } from './manifest/create.js';
|
||||
export { deserializeRouteData, serializeRouteData } from './manifest/serialization.js';
|
||||
export { matchAllRoutes, matchRoute } from './match.js';
|
||||
export { getParams } from './params.js';
|
||||
export { validateDynamicRouteModule, validateGetStaticPathsResult } from './validation.js';
|
15
node_modules/astro/dist/core/routing/index.js
generated
vendored
Normal file
15
node_modules/astro/dist/core/routing/index.js
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { createRouteManifest } from "./manifest/create.js";
|
||||
import { deserializeRouteData, serializeRouteData } from "./manifest/serialization.js";
|
||||
import { matchAllRoutes, matchRoute } from "./match.js";
|
||||
import { getParams } from "./params.js";
|
||||
import { validateDynamicRouteModule, validateGetStaticPathsResult } from "./validation.js";
|
||||
export {
|
||||
createRouteManifest,
|
||||
deserializeRouteData,
|
||||
getParams,
|
||||
matchAllRoutes,
|
||||
matchRoute,
|
||||
serializeRouteData,
|
||||
validateDynamicRouteModule,
|
||||
validateGetStaticPathsResult
|
||||
};
|
14
node_modules/astro/dist/core/routing/manifest/create.d.ts
generated
vendored
Normal file
14
node_modules/astro/dist/core/routing/manifest/create.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
/// <reference types="node" />
|
||||
import type { AstroSettings, ManifestData } from '../../../@types/astro';
|
||||
import type { LogOptions } from '../../logger/core';
|
||||
import nodeFs from 'node:fs';
|
||||
export interface CreateRouteManifestParams {
|
||||
/** Astro Settings object */
|
||||
settings: AstroSettings;
|
||||
/** Current working directory */
|
||||
cwd?: string;
|
||||
/** fs module, for testing */
|
||||
fsMod?: typeof nodeFs;
|
||||
}
|
||||
/** Create manifest of all static routes */
|
||||
export declare function createRouteManifest({ settings, cwd, fsMod }: CreateRouteManifestParams, logging: LogOptions): ManifestData;
|
343
node_modules/astro/dist/core/routing/manifest/create.js
generated
vendored
Normal file
343
node_modules/astro/dist/core/routing/manifest/create.js
generated
vendored
Normal file
|
@ -0,0 +1,343 @@
|
|||
import { createRequire } from "module";
|
||||
import nodeFs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { getPrerenderDefault } from "../../../prerender/utils.js";
|
||||
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from "../../constants.js";
|
||||
import { warn } from "../../logger/core.js";
|
||||
import { removeLeadingForwardSlash, slash } from "../../path.js";
|
||||
import { resolvePages } from "../../util.js";
|
||||
import { getRouteGenerator } from "./generator.js";
|
||||
const require2 = createRequire(import.meta.url);
|
||||
function countOccurrences(needle, haystack) {
|
||||
let count = 0;
|
||||
for (const hay of haystack) {
|
||||
if (hay === needle)
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
function getParts(part, file) {
|
||||
const result = [];
|
||||
part.split(/\[(.+?\(.+?\)|.+?)\]/).map((str, i) => {
|
||||
if (!str)
|
||||
return;
|
||||
const dynamic = i % 2 === 1;
|
||||
const [, content] = dynamic ? /([^(]+)$/.exec(str) || [null, null] : [null, str];
|
||||
if (!content || dynamic && !/^(\.\.\.)?[a-zA-Z0-9_$]+$/.test(content)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 parameter name must match /^[a-zA-Z0-9_$]+$/`);
|
||||
}
|
||||
result.push({
|
||||
content,
|
||||
dynamic,
|
||||
spread: dynamic && /^\.{3}.+$/.test(content)
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
function getPattern(segments, base, addTrailingSlash) {
|
||||
const pathname = segments.map((segment) => {
|
||||
if (segment.length === 1 && segment[0].spread) {
|
||||
return "(?:\\/(.*?))?";
|
||||
} else {
|
||||
return "\\/" + segment.map((part) => {
|
||||
if (part.spread) {
|
||||
return "(.*?)";
|
||||
} else if (part.dynamic) {
|
||||
return "([^/]+?)";
|
||||
} else {
|
||||
return part.content.normalize().replace(/\?/g, "%3F").replace(/#/g, "%23").replace(/%5B/g, "[").replace(/%5D/g, "]").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
}).join("");
|
||||
}
|
||||
}).join("");
|
||||
const trailing = addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : "$";
|
||||
let initial = "\\/";
|
||||
if (addTrailingSlash === "never" && base !== "/") {
|
||||
initial = "";
|
||||
}
|
||||
return new RegExp(`^${pathname || initial}${trailing}`);
|
||||
}
|
||||
function getTrailingSlashPattern(addTrailingSlash) {
|
||||
if (addTrailingSlash === "always") {
|
||||
return "\\/$";
|
||||
}
|
||||
if (addTrailingSlash === "never") {
|
||||
return "$";
|
||||
}
|
||||
return "\\/?$";
|
||||
}
|
||||
function isSpread(str) {
|
||||
const spreadPattern = /\[\.{3}/g;
|
||||
return spreadPattern.test(str);
|
||||
}
|
||||
function validateSegment(segment, file = "") {
|
||||
if (!file)
|
||||
file = segment;
|
||||
if (/\]\[/.test(segment)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
|
||||
}
|
||||
if (countOccurrences("[", segment) !== countOccurrences("]", segment)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
|
||||
}
|
||||
if ((/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) && file.endsWith(".astro")) {
|
||||
throw new Error(`Invalid route ${file} \u2014 rest parameter must be a standalone segment`);
|
||||
}
|
||||
}
|
||||
function comparator(a, b) {
|
||||
if (a.isIndex !== b.isIndex) {
|
||||
if (a.isIndex)
|
||||
return isSpread(a.file) ? 1 : -1;
|
||||
return isSpread(b.file) ? -1 : 1;
|
||||
}
|
||||
const max = Math.max(a.parts.length, b.parts.length);
|
||||
for (let i = 0; i < max; i += 1) {
|
||||
const aSubPart = a.parts[i];
|
||||
const bSubPart = b.parts[i];
|
||||
if (!aSubPart)
|
||||
return 1;
|
||||
if (!bSubPart)
|
||||
return -1;
|
||||
if (aSubPart.spread && bSubPart.spread) {
|
||||
return a.isIndex ? 1 : -1;
|
||||
}
|
||||
if (aSubPart.spread !== bSubPart.spread)
|
||||
return aSubPart.spread ? 1 : -1;
|
||||
if (aSubPart.dynamic !== bSubPart.dynamic) {
|
||||
return aSubPart.dynamic ? 1 : -1;
|
||||
}
|
||||
if (!aSubPart.dynamic && aSubPart.content !== bSubPart.content) {
|
||||
return bSubPart.content.length - aSubPart.content.length || (aSubPart.content < bSubPart.content ? -1 : 1);
|
||||
}
|
||||
}
|
||||
if (a.isPage !== b.isPage) {
|
||||
return a.isPage ? 1 : -1;
|
||||
}
|
||||
return a.file < b.file ? -1 : 1;
|
||||
}
|
||||
function injectedRouteToItem({ config, cwd }, { pattern, entryPoint }) {
|
||||
const resolved = require2.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||
const ext = path.extname(pattern);
|
||||
const type = resolved.endsWith(".astro") ? "page" : "endpoint";
|
||||
const isPage = type === "page";
|
||||
return {
|
||||
basename: pattern,
|
||||
ext,
|
||||
parts: getParts(pattern, resolved),
|
||||
file: resolved,
|
||||
isDir: false,
|
||||
isIndex: true,
|
||||
isPage,
|
||||
routeSuffix: pattern.slice(pattern.indexOf("."), -ext.length)
|
||||
};
|
||||
}
|
||||
function createRouteManifest({ settings, cwd, fsMod }, logging) {
|
||||
var _a;
|
||||
const components = [];
|
||||
const routes = [];
|
||||
const validPageExtensions = /* @__PURE__ */ new Set([
|
||||
".astro",
|
||||
...SUPPORTED_MARKDOWN_FILE_EXTENSIONS,
|
||||
...settings.pageExtensions
|
||||
]);
|
||||
const validEndpointExtensions = /* @__PURE__ */ new Set([".js", ".ts"]);
|
||||
const localFs = fsMod ?? nodeFs;
|
||||
const prerender = getPrerenderDefault(settings.config);
|
||||
const foundInvalidFileExtensions = /* @__PURE__ */ new Set();
|
||||
function walk(fs, dir, parentSegments, parentParams) {
|
||||
let items = [];
|
||||
fs.readdirSync(dir).forEach((basename) => {
|
||||
const resolved = path.join(dir, basename);
|
||||
const file = slash(path.relative(cwd || fileURLToPath(settings.config.root), resolved));
|
||||
const isDir = fs.statSync(resolved).isDirectory();
|
||||
const ext = path.extname(basename);
|
||||
const name = ext ? basename.slice(0, -ext.length) : basename;
|
||||
if (name[0] === "_") {
|
||||
return;
|
||||
}
|
||||
if (basename[0] === "." && basename !== ".well-known") {
|
||||
return;
|
||||
}
|
||||
if (!isDir && !validPageExtensions.has(ext) && !validEndpointExtensions.has(ext)) {
|
||||
if (!foundInvalidFileExtensions.has(ext)) {
|
||||
foundInvalidFileExtensions.add(ext);
|
||||
warn(logging, "astro", `Invalid file extension for Pages: ${ext}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const segment = isDir ? basename : name;
|
||||
validateSegment(segment, file);
|
||||
const parts = getParts(segment, file);
|
||||
const isIndex = isDir ? false : basename.startsWith("index.");
|
||||
const routeSuffix = basename.slice(basename.indexOf("."), -ext.length);
|
||||
const isPage = validPageExtensions.has(ext);
|
||||
items.push({
|
||||
basename,
|
||||
ext,
|
||||
parts,
|
||||
file: file.replace(/\\/g, "/"),
|
||||
isDir,
|
||||
isIndex,
|
||||
isPage,
|
||||
routeSuffix
|
||||
});
|
||||
});
|
||||
items = items.sort(comparator);
|
||||
items.forEach((item) => {
|
||||
const segments = parentSegments.slice();
|
||||
if (item.isIndex) {
|
||||
if (item.routeSuffix) {
|
||||
if (segments.length > 0) {
|
||||
const lastSegment = segments[segments.length - 1].slice();
|
||||
const lastPart = lastSegment[lastSegment.length - 1];
|
||||
if (lastPart.dynamic) {
|
||||
lastSegment.push({
|
||||
dynamic: false,
|
||||
spread: false,
|
||||
content: item.routeSuffix
|
||||
});
|
||||
} else {
|
||||
lastSegment[lastSegment.length - 1] = {
|
||||
dynamic: false,
|
||||
spread: false,
|
||||
content: `${lastPart.content}${item.routeSuffix}`
|
||||
};
|
||||
}
|
||||
segments[segments.length - 1] = lastSegment;
|
||||
} else {
|
||||
segments.push(item.parts);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
segments.push(item.parts);
|
||||
}
|
||||
const params = parentParams.slice();
|
||||
params.push(...item.parts.filter((p) => p.dynamic).map((p) => p.content));
|
||||
if (item.isDir) {
|
||||
walk(fsMod ?? fs, path.join(dir, item.basename), segments, params);
|
||||
} else {
|
||||
components.push(item.file);
|
||||
const component = item.file;
|
||||
const trailingSlash = item.isPage ? settings.config.trailingSlash : "never";
|
||||
const pattern = getPattern(segments, settings.config.base, trailingSlash);
|
||||
const generate = getRouteGenerator(segments, trailingSlash);
|
||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join("/")}` : null;
|
||||
const route = `/${segments.map(([{ dynamic, content }]) => dynamic ? `[${content}]` : content).join("/")}`.toLowerCase();
|
||||
routes.push({
|
||||
route,
|
||||
type: item.isPage ? "page" : "endpoint",
|
||||
pattern,
|
||||
segments,
|
||||
params,
|
||||
component,
|
||||
generate,
|
||||
pathname: pathname || void 0,
|
||||
prerender
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
const { config } = settings;
|
||||
const pages = resolvePages(config);
|
||||
if (localFs.existsSync(pages)) {
|
||||
walk(localFs, fileURLToPath(pages), [], []);
|
||||
} else if (settings.injectedRoutes.length === 0) {
|
||||
const pagesDirRootRelative = pages.href.slice(settings.config.root.href.length);
|
||||
warn(logging, "astro", `Missing pages directory: ${pagesDirRootRelative}`);
|
||||
}
|
||||
(_a = settings.injectedRoutes) == null ? void 0 : _a.sort(
|
||||
(a, b) => (
|
||||
// sort injected routes in the same way as user-defined routes
|
||||
comparator(injectedRouteToItem({ config, cwd }, a), injectedRouteToItem({ config, cwd }, b))
|
||||
)
|
||||
).reverse().forEach(({ pattern: name, entryPoint, prerender: prerenderInjected }) => {
|
||||
let resolved;
|
||||
try {
|
||||
resolved = require2.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||
} catch (e) {
|
||||
resolved = fileURLToPath(new URL(entryPoint, config.root));
|
||||
}
|
||||
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
|
||||
const segments = removeLeadingForwardSlash(name).split(path.posix.sep).filter(Boolean).map((s) => {
|
||||
validateSegment(s);
|
||||
return getParts(s, component);
|
||||
});
|
||||
const type = resolved.endsWith(".astro") ? "page" : "endpoint";
|
||||
const isPage = type === "page";
|
||||
const trailingSlash = isPage ? config.trailingSlash : "never";
|
||||
const pattern = getPattern(segments, settings.config.base, trailingSlash);
|
||||
const generate = getRouteGenerator(segments, trailingSlash);
|
||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join("/")}` : null;
|
||||
const params = segments.flat().filter((p) => p.dynamic).map((p) => p.content);
|
||||
const route = `/${segments.map(([{ dynamic, content }]) => dynamic ? `[${content}]` : content).join("/")}`.toLowerCase();
|
||||
const collision = routes.find(({ route: r }) => r === route);
|
||||
if (collision) {
|
||||
throw new Error(
|
||||
`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}".
|
||||
This route collides with: "${collision.component}".`
|
||||
);
|
||||
}
|
||||
routes.unshift({
|
||||
type,
|
||||
route,
|
||||
pattern,
|
||||
segments,
|
||||
params,
|
||||
component,
|
||||
generate,
|
||||
pathname: pathname || void 0,
|
||||
prerender: prerenderInjected ?? prerender
|
||||
});
|
||||
});
|
||||
Object.entries(settings.config.redirects).forEach(([from, to]) => {
|
||||
const trailingSlash = config.trailingSlash;
|
||||
const segments = removeLeadingForwardSlash(from).split(path.posix.sep).filter(Boolean).map((s) => {
|
||||
validateSegment(s);
|
||||
return getParts(s, from);
|
||||
});
|
||||
const pattern = getPattern(segments, settings.config.base, trailingSlash);
|
||||
const generate = getRouteGenerator(segments, trailingSlash);
|
||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join("/")}` : null;
|
||||
const params = segments.flat().filter((p) => p.dynamic).map((p) => p.content);
|
||||
const route = `/${segments.map(([{ dynamic, content }]) => dynamic ? `[${content}]` : content).join("/")}`.toLowerCase();
|
||||
const routeData = {
|
||||
type: "redirect",
|
||||
route,
|
||||
pattern,
|
||||
segments,
|
||||
params,
|
||||
component: from,
|
||||
generate,
|
||||
pathname: pathname || void 0,
|
||||
prerender: false,
|
||||
redirect: to,
|
||||
redirectRoute: routes.find((r) => r.route === to)
|
||||
};
|
||||
const lastSegmentIsDynamic = (r) => {
|
||||
var _a2, _b;
|
||||
return !!((_b = (_a2 = r.segments.at(-1)) == null ? void 0 : _a2.at(-1)) == null ? void 0 : _b.dynamic);
|
||||
};
|
||||
const redirBase = path.posix.dirname(route);
|
||||
const dynamicRedir = lastSegmentIsDynamic(routeData);
|
||||
let i = 0;
|
||||
for (const existingRoute of routes) {
|
||||
if (existingRoute.route === route) {
|
||||
routes.splice(i + 1, 0, routeData);
|
||||
return;
|
||||
}
|
||||
const base = path.posix.dirname(existingRoute.route);
|
||||
if (base === redirBase && !dynamicRedir && lastSegmentIsDynamic(existingRoute)) {
|
||||
routes.splice(i, 0, routeData);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
routes.push(routeData);
|
||||
});
|
||||
return {
|
||||
routes
|
||||
};
|
||||
}
|
||||
export {
|
||||
createRouteManifest
|
||||
};
|
2
node_modules/astro/dist/core/routing/manifest/generator.d.ts
generated
vendored
Normal file
2
node_modules/astro/dist/core/routing/manifest/generator.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import type { AstroConfig, RoutePart } from '../../../@types/astro';
|
||||
export declare function getRouteGenerator(segments: RoutePart[][], addTrailingSlash: AstroConfig['trailingSlash']): import("path-to-regexp").PathFunction<object>;
|
23
node_modules/astro/dist/core/routing/manifest/generator.js
generated
vendored
Normal file
23
node_modules/astro/dist/core/routing/manifest/generator.js
generated
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { compile } from "path-to-regexp";
|
||||
function getRouteGenerator(segments, addTrailingSlash) {
|
||||
const template = segments.map((segment) => {
|
||||
return "/" + segment.map((part) => {
|
||||
if (part.spread) {
|
||||
return `:${part.content.slice(3)}(.*)?`;
|
||||
} else if (part.dynamic) {
|
||||
return `:${part.content}`;
|
||||
} else {
|
||||
return part.content.normalize().replace(/\?/g, "%3F").replace(/#/g, "%23").replace(/%5B/g, "[").replace(/%5D/g, "]").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
}).join("");
|
||||
}).join("");
|
||||
let trailing = "";
|
||||
if (addTrailingSlash === "always" && segments.length) {
|
||||
trailing = "/";
|
||||
}
|
||||
const toPath = compile(template + trailing);
|
||||
return toPath;
|
||||
}
|
||||
export {
|
||||
getRouteGenerator
|
||||
};
|
3
node_modules/astro/dist/core/routing/manifest/serialization.d.ts
generated
vendored
Normal file
3
node_modules/astro/dist/core/routing/manifest/serialization.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import type { AstroConfig, RouteData, SerializedRouteData } from '../../../@types/astro';
|
||||
export declare function serializeRouteData(routeData: RouteData, trailingSlash: AstroConfig['trailingSlash']): SerializedRouteData;
|
||||
export declare function deserializeRouteData(rawRouteData: SerializedRouteData): RouteData;
|
26
node_modules/astro/dist/core/routing/manifest/serialization.js
generated
vendored
Normal file
26
node_modules/astro/dist/core/routing/manifest/serialization.js
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { getRouteGenerator } from "./generator.js";
|
||||
function serializeRouteData(routeData, trailingSlash) {
|
||||
return {
|
||||
...routeData,
|
||||
generate: void 0,
|
||||
pattern: routeData.pattern.source,
|
||||
_meta: { trailingSlash }
|
||||
};
|
||||
}
|
||||
function deserializeRouteData(rawRouteData) {
|
||||
return {
|
||||
route: rawRouteData.route,
|
||||
type: rawRouteData.type,
|
||||
pattern: new RegExp(rawRouteData.pattern),
|
||||
params: rawRouteData.params,
|
||||
component: rawRouteData.component,
|
||||
generate: getRouteGenerator(rawRouteData.segments, rawRouteData._meta.trailingSlash),
|
||||
pathname: rawRouteData.pathname || void 0,
|
||||
segments: rawRouteData.segments,
|
||||
prerender: rawRouteData.prerender
|
||||
};
|
||||
}
|
||||
export {
|
||||
deserializeRouteData,
|
||||
serializeRouteData
|
||||
};
|
5
node_modules/astro/dist/core/routing/match.d.ts
generated
vendored
Normal file
5
node_modules/astro/dist/core/routing/match.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
import type { ManifestData, RouteData } from '../../@types/astro';
|
||||
/** Find matching route from pathname */
|
||||
export declare function matchRoute(pathname: string, manifest: ManifestData): RouteData | undefined;
|
||||
/** Finds all matching routes from pathname */
|
||||
export declare function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[];
|
10
node_modules/astro/dist/core/routing/match.js
generated
vendored
Normal file
10
node_modules/astro/dist/core/routing/match.js
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
function matchRoute(pathname, manifest) {
|
||||
return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname)));
|
||||
}
|
||||
function matchAllRoutes(pathname, manifest) {
|
||||
return manifest.routes.filter((route) => route.pattern.test(pathname));
|
||||
}
|
||||
export {
|
||||
matchAllRoutes,
|
||||
matchRoute
|
||||
};
|
13
node_modules/astro/dist/core/routing/params.d.ts
generated
vendored
Normal file
13
node_modules/astro/dist/core/routing/params.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
import type { GetStaticPathsItem, Params, RouteData } from '../../@types/astro';
|
||||
/**
|
||||
* given an array of params like `['x', 'y', 'z']` for
|
||||
* src/routes/[x]/[y]/[z]/svelte, create a function
|
||||
* that turns a RegExpExecArray into ({ x, y, z })
|
||||
*/
|
||||
export declare function getParams(array: string[]): (match: RegExpExecArray) => Params;
|
||||
/**
|
||||
* given a route's Params object, validate parameter
|
||||
* values and create a stringified key for the route
|
||||
* that can be used to match request routes
|
||||
*/
|
||||
export declare function stringifyParams(params: GetStaticPathsItem['params'], route: RouteData): string;
|
28
node_modules/astro/dist/core/routing/params.js
generated
vendored
Normal file
28
node_modules/astro/dist/core/routing/params.js
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { validateGetStaticPathsParameter } from "./validation.js";
|
||||
function getParams(array) {
|
||||
const fn = (match) => {
|
||||
const params = {};
|
||||
array.forEach((key, i) => {
|
||||
if (key.startsWith("...")) {
|
||||
params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : void 0;
|
||||
} else {
|
||||
params[key] = decodeURIComponent(match[i + 1]);
|
||||
}
|
||||
});
|
||||
return params;
|
||||
};
|
||||
return fn;
|
||||
}
|
||||
function stringifyParams(params, route) {
|
||||
const validatedParams = Object.entries(params).reduce((acc, next) => {
|
||||
validateGetStaticPathsParameter(next, route.component);
|
||||
const [key, value] = next;
|
||||
acc[key] = value == null ? void 0 : value.toString();
|
||||
return acc;
|
||||
}, {});
|
||||
return JSON.stringify(route.generate(validatedParams));
|
||||
}
|
||||
export {
|
||||
getParams,
|
||||
stringifyParams
|
||||
};
|
12
node_modules/astro/dist/core/routing/validation.d.ts
generated
vendored
Normal file
12
node_modules/astro/dist/core/routing/validation.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
import type { ComponentInstance, GetStaticPathsResult, RouteData } from '../../@types/astro';
|
||||
import type { LogOptions } from '../logger/core';
|
||||
/** Throws error for invalid parameter in getStaticPaths() response */
|
||||
export declare function validateGetStaticPathsParameter([key, value]: [string, any], route: string): void;
|
||||
/** Warn or error for deprecated or malformed route components */
|
||||
export declare function validateDynamicRouteModule(mod: ComponentInstance, { ssr, logging, route, }: {
|
||||
ssr: boolean;
|
||||
logging: LogOptions;
|
||||
route: RouteData;
|
||||
}): void;
|
||||
/** Throw error and log warnings for malformed getStaticPaths() response */
|
||||
export declare function validateGetStaticPathsResult(result: GetStaticPathsResult, logging: LogOptions, route: RouteData): void;
|
87
node_modules/astro/dist/core/routing/validation.js
generated
vendored
Normal file
87
node_modules/astro/dist/core/routing/validation.js
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
import { bold } from "kleur/colors";
|
||||
import { AstroError, AstroErrorData } from "../errors/index.js";
|
||||
import { warn } from "../logger/core.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,
|
||||
logging,
|
||||
route
|
||||
}) {
|
||||
if (ssr && mod.getStaticPaths && !route.prerender) {
|
||||
warn(
|
||||
logging,
|
||||
"getStaticPaths",
|
||||
`getStaticPaths() in ${bold(route.component)} is ignored when "output: server" is set.`
|
||||
);
|
||||
}
|
||||
if ((!ssr || route.prerender) && !mod.getStaticPaths) {
|
||||
throw new AstroError({
|
||||
...AstroErrorData.GetStaticPathsRequired,
|
||||
location: { file: route.component }
|
||||
});
|
||||
}
|
||||
}
|
||||
function validateGetStaticPathsResult(result, logging, route) {
|
||||
if (!Array.isArray(result)) {
|
||||
throw new AstroError({
|
||||
...AstroErrorData.InvalidGetStaticPathsReturn,
|
||||
message: AstroErrorData.InvalidGetStaticPathsReturn.message(typeof result),
|
||||
location: {
|
||||
file: route.component
|
||||
}
|
||||
});
|
||||
}
|
||||
result.forEach((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
|
||||
}
|
||||
});
|
||||
}
|
||||
if (typeof pathObject.params !== "object") {
|
||||
throw new AstroError({
|
||||
...AstroErrorData.InvalidGetStaticPathParam,
|
||||
message: AstroErrorData.InvalidGetStaticPathParam.message(typeof pathObject.params),
|
||||
location: {
|
||||
file: route.component
|
||||
}
|
||||
});
|
||||
}
|
||||
for (const [key, val] of Object.entries(pathObject.params)) {
|
||||
if (!(typeof val === "undefined" || typeof val === "string" || typeof val === "number")) {
|
||||
warn(
|
||||
logging,
|
||||
"getStaticPaths",
|
||||
`invalid path param: ${key}. A string, number or undefined value was expected, but got \`${JSON.stringify(
|
||||
val
|
||||
)}\`.`
|
||||
);
|
||||
}
|
||||
if (typeof val === "string" && val === "") {
|
||||
warn(
|
||||
logging,
|
||||
"getStaticPaths",
|
||||
`invalid path param: ${key}. \`undefined\` expected for an optional param, but got empty string.`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
export {
|
||||
validateDynamicRouteModule,
|
||||
validateGetStaticPathsParameter,
|
||||
validateGetStaticPathsResult
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue