Files
webstudio/apps/builder/app/shared/system.ts
Oleg Isonen df7ed327ab refactor: clarify pathname rule matchers (#6446)
Authentication and custom response headers use the same route-rule
syntax, but the shared matcher and validator had auth-specific names.
Give those functions generic names in `wsauth` and update the Builder
header UI and SDK response-header schema to call them directly. All
basic-auth logic remains in `wsauth`; this change adds no package or
re-export.

The project-build page matcher is different: it uses `URLPattern` and
returns decoded parameters. Rename it to `matchUrlPattern` and document
the behavior difference. For example, `/docs/*` matches `/docs` for
authentication and headers, but not for the page matcher. Tests cover
both behaviors.

This renames the two public `wsauth` route exports. Published workers
pinned to the older package retain the old API; the SaaS worker can
adopt the new names after this package is released.

Validation: `wsauth` tests, including its packaged consumer test; SDK
page and response-header tests; project-build route and redirect tests;
SDK, `wsauth`, project-build, and Builder typechecks.
2026-09-26 11:28:23 +02:00

105 lines
3.1 KiB
TypeScript

import { atom, computed } from "nanostores";
import { getPagePath, isPage, type Page, type System } from "@webstudio-is/sdk";
import {
compilePathnamePattern,
matchUrlPattern,
tokenizePathnamePattern,
} from "@webstudio-is/project-build/runtime";
import { $selectedPage } from "./nano-states/pages";
import { $pages } from "./sync/data-stores";
import { $publishedOrigin } from "./nano-states/misc";
import { executeRuntimeMutation } from "./instance-utils/data";
export const $systemDataByPage = atom(
new Map<Page["id"], Pick<System, "search" | "params">>()
);
const extractParams = (
pattern: string,
path?: string,
fallbackPattern?: string
) => {
const params: System["params"] = {};
const tokens = tokenizePathnamePattern(pattern);
// try to match the first item in history to let user
// see the page without manually entering params
// or selecting them in address bar
const matchedParams = path
? (matchUrlPattern(pattern, path) ??
(fallbackPattern ? matchUrlPattern(fallbackPattern, path) : undefined))
: undefined;
for (const token of tokens) {
if (token.type === "param") {
params[token.name] = matchedParams?.[token.name] ?? undefined;
}
}
return params;
};
export const $currentSystem = computed(
[$publishedOrigin, $selectedPage, $pages, $systemDataByPage],
(origin, page, pages, systemByPage) => {
const system: System = {
search: {},
params: {},
pathname: "/",
origin,
};
if (page === undefined || pages === undefined || !isPage(page)) {
return system;
}
const systemData = systemByPage.get(page.id);
const pagePath = getPagePath(page.id, pages);
const extractedParams = extractParams(
pagePath,
page.history?.[0],
page.path
);
const params = { ...extractedParams, ...systemData?.params };
const pathname = compilePath(pagePath, params) || "/";
return {
search: { ...system.search, ...systemData?.search },
params,
pathname,
origin,
};
}
);
const compilePath = (pattern: string, params: System["params"]) => {
const tokens = tokenizePathnamePattern(pattern);
return compilePathnamePattern(tokens, params);
};
/**
* put new path into the beginning of history
* and drop paths in the end when exceeded 20
*/
const savePathInHistory = (pageId: string, path: string) => {
executeRuntimeMutation({
id: "pages.savePathInHistory",
input: {
pageId,
path,
},
});
};
export const updateCurrentSystem = (
update: Partial<Pick<System, "search" | "params">>
) => {
const page = $selectedPage.get();
if (!isPage(page)) {
return;
}
const systemDataByPage = new Map($systemDataByPage.get());
const systemData = systemDataByPage.get(page.id);
const search = update.search ?? systemData?.search ?? {};
const params = update.params ?? systemData?.params ?? {};
systemDataByPage.set(page.id, { search, params });
$systemDataByPage.set(systemDataByPage);
const pages = $pages.get();
const pagePath = pages ? getPagePath(page.id, pages) : page.path;
savePathInHistory(page.id, compilePath(pagePath, params));
};