Files
webstudio/apps/builder/app/services/api-router.server.ts
T
Oleg IsonenandCodex f30bdb7679 fix: Keep malformed styles from blocking publish (#6665)
Malformed saved CSS can stop a site from publishing. This restores
Vite's stylesheet pipeline with Lightning CSS and lets publishing
continue when generated CSS is invalid.

## What changed

- Save validation uses the shared CSS parser for touched declarations
and now rejects malformed style state selectors. The CSS editor already
reports malformed pasted declarations and omits them from saved styles.
- Publish skips older malformed declarations and warns without including
their values.
- Generated CSS is imported by Remix, React Router, and Vike, so Vite
processes the stylesheet and emits its asset.
- Before Vite runs, the CLI checks generated CSS with Lightning CSS and
esbuild. If the stylesheet cannot build, it warns and uses preset and
font CSS; if that also fails, it uses an empty stylesheet.
- The domain publish check parses the current project CSS and shows a
warning without blocking publish. The parser catches the malformed cases
reproduced with Lightning CSS and esbuild; the CLI's compiler check
remains the final guard. It skips malformed declarations just as the CLI
does. The early check reads assets and generates and parses CSS once
more per validation. Content Block styles created during publishing and
static template publishing are covered by the CLI fallback, but not by
this early warning.
- The temporary hashed public stylesheet path and its larger fixture
size allowances are removed. The direct PostCSS dependency remains
removed.

## Checklist

- [x] Restore Vite CSS imports with Lightning CSS in every site
template.
- [x] Keep malformed CSS from failing publish and warn when fallback CSS
is used.
- [x] Test the reported escaped property in React Router and Cloudflare
Remix builds.
- [x] Test malformed CSS recovery in React Router and Vike builds.
- [x] Verify the prerendered Vike page links to the generated CSS asset.
- [x] Regenerate fixtures and review the diff.
- [x] Review documentation impact: the existing publishing guide needs
no change for this warning in the publish flow.
- [x] Run CLI typecheck, lint, formatting, package boundaries, CLI
build, and frozen lockfile check.
- [x] Confirm CSS compiler failure cases with focused tests.
- [x] Validate CSS-affecting inputs at save, including style states that
bypass the editor picker.
- [x] Warn in domain pre-publish validation when current project CSS
fails compiler checks.
- [x] Verify pasted CSS diagnostics and save behavior in the editor.
- [x] Review the new diff and rerun relevant tests, typechecks, and
Builder build.
- [x] Remove the Builder runtime's native Lightning CSS dependency and
test the known compiler failure cases with CSS-tree.
- [x] Verify staging Builder and CLI workflows for the latest commit.
- [ ] Verify a site publish on staging with a project compatible with
this branch. The first attempt used a project with a style source type
this branch cannot read.
- [x] Reject CSS-tree recoverable parse errors in declaration validation
and add a regression test.
- [x] Keep project-build within its existing package entrypoints and
rerun the affected checks.
- [x] Monitor required PR checks to completion.

## Verification

Local: affected tests, four package typechecks, Builder and CLI builds,
root lint, formatting, package boundaries, and frozen lockfile check
passed. The fixture pipeline passed for the earlier commit; this commit
changes no fixture inputs or outputs. The new invalid-declaration
regression test failed when its filter was removed, then passed when
restored. The incomplete `rgb()` parser test failed before the review
fix and passed afterward. Full css-data and project-build test suites
passed. The Builder runtime uses CSS-tree for the early warning because
Vercel omitted Lightning CSS's native binary from the deployed function;
the CLI still runs Lightning CSS and esbuild during publishing. The
replacement staging deployment, fixture checks, both CLI R2 workflows,
main workflow, and all required PR checks passed.

Staging publish attempt: CLI sync stopped at `build.styleSources[520]`
before CSS generation. This branch accepts only `token` and `local`
sources; open PR #6663 adds `tagStyle` and the CSS generation it needs.
The failing record could not be read directly with the available share
token, so the exact saved type remains unconfirmed. Use a project
without Tag styles, or test a combined integration branch after an
explicit scope decision.

Replaces #6662.

---------

Co-authored-by: Codex <codex@users.noreply.github.com>
2026-11-09 18:02:11 +01:00

1372 lines
40 KiB
TypeScript

import { z } from "zod";
import {
AuthorizationError,
authorizeProject,
procedure,
router,
type AppContext,
type AuthPermit,
} from "@webstudio-is/trpc-interface/index.server";
import {
loadById,
setMarketplaceApprovalStatus,
} from "@webstudio-is/project/index.server";
import { loadDevBuildByProjectId } from "@webstudio-is/project-build/server";
import { hasCssSyntaxError } from "@webstudio-is/css-data";
import { parseGeneratedStyleDeclaration } from "@webstudio-is/project-build/persistence";
import { singlePlayerVersionMismatchError } from "@webstudio-is/project/build-patch-core.server";
import {
createProjectDomain,
deleteProjectDomain,
getDefaultPublishDomains,
getVerifiedPublishDomains,
getProjectPublishJob,
listProjectDomains,
listProjectPublishes,
publishProject,
updateProjectDomain,
unpublishProjectDomains,
verifyProjectDomain,
} from "@webstudio-is/domain/index.server";
import {
getBuilderRuntimeOperationInputSchema,
getBuildIntegrityIssues,
formatBuildIntegrityError,
paginateOutput,
paginatedOutputInputSchema,
} from "@webstudio-is/project-build/runtime";
import {
getDetailedAssetResourceQueryError,
assetQueryDiagnosticIssue,
assetQueryRequest,
assetQuerySetupIssue,
getAssetQueryWhereMetrics,
validateAssetQueryAgainstCatalog,
type AssetResourceQueryError,
} from "@webstudio-is/content-engine";
import {
loadBuilderAssetFieldCatalog,
loadAssetDataByProject,
loadAssetFoldersByProject,
PostgresAssetRepository,
} from "@webstudio-is/asset-uploader/server";
import { createAssetContentSession } from "@webstudio-is/content-engine/asset-content-session";
import { buildPatchTransaction } from "@webstudio-is/protocol/schema";
import {
publicApiContractVersion,
publicApiOperationRequiresServerSupport,
publicApiOperations,
issueReportInput,
} from "@webstudio-is/protocol";
import {
assertApiProjectPermit,
getTokenPermits,
loadApiToken,
} from "./api-permits.server";
import { componentMetas } from "~/shared/component-metas.server";
import {
generateCss,
getStyleDeclKey,
type Asset,
type AssetFolder,
} from "@webstudio-is/sdk";
import {
createContentBlockApplication,
type BuilderRuntimeContext,
} from "@webstudio-is/project-build/runtime";
import type { CompactBuild } from "@webstudio-is/project-build";
import {
runtimeOperationContracts,
type RuntimeOperationId,
} from "@webstudio-is/project-build/contracts";
import { builderNamespaces } from "@webstudio-is/project-build/contracts";
import type { BuilderNamespace } from "@webstudio-is/project-build/contracts";
import type { BuilderApiCapability } from "@webstudio-is/project-build/contracts";
import {
buildGetInput,
buildPatchInput,
commitBuildPatch,
commitBuildTransactions,
createBuildSnapshot,
loadBuildByProjectVersion,
loadReadableDevBuild,
serializeProjectSummary,
} from "./api-build.server";
import { throwApiError } from "./api-errors.server";
import { assertContentOrBuildTransactions } from "./api-content-permissions.server";
import { validateProjectSettingsUpdate } from "./project-settings-mutation.server";
import {
createBuilderRuntimeState,
executeApiRuntimeMutation,
executeApiRuntimeOperation,
} from "./api-runtime.server";
import { createAssetClient } from "../shared/asset-client";
import { previewProjectAssetQuery } from "./asset-query-preview.server";
import { simulateFormSubmission } from "./simulate-form.server";
import { publishConfiguredIssueReport } from "./github-issue-report.server";
import { loadContentDatabasePublishDiagnostics } from "./content-database-publish-diagnostics.server";
const assertApiPublishDomains = ({
auth,
domains,
project,
}: {
auth: Awaited<ReturnType<typeof assertApiProjectPermit>>;
domains: string[];
project: { domain: string };
}) => {
if (auth.type === "user") {
return;
}
const { token } = auth;
if (token.canPublish === true) {
return;
}
if (
token.relation === "builders" &&
domains.length > 0 &&
domains.every((domain) => domain === project.domain)
) {
return;
}
throw new AuthorizationError(
"Authorization token does not have publish permission"
);
};
const projectIdInput = z.object({ projectId: z.string() });
const assetQueryInput = projectIdInput.extend(assetQueryRequest.shape);
const assetQueryValidationInput = projectIdInput.extend({
query: assetQueryRequest.shape.query,
});
const asRecord = (value: unknown): Record<string, unknown> | undefined =>
typeof value === "object" && value !== null && Array.isArray(value) === false
? (value as Record<string, unknown>)
: undefined;
const getAssetQueryApiIssues = (error: AssetResourceQueryError) => {
const details = asRecord(error.details);
const sourceDiagnostics = Array.isArray(details?.diagnostics)
? details.diagnostics
: details?.path === undefined
? []
: [details];
const sourceIssues = sourceDiagnostics.flatMap((value) => {
const diagnostic = asRecord(value);
if (diagnostic === undefined || typeof diagnostic.path !== "string") {
return [];
}
const code =
typeof diagnostic.code === "string" ? diagnostic.code : error.code;
const message =
typeof diagnostic.message === "string"
? diagnostic.message
: error.message;
const detail = [
typeof diagnostic.line === "number"
? `Line ${diagnostic.line}${
typeof diagnostic.column === "number"
? `, column ${diagnostic.column}`
: ""
}`
: undefined,
typeof diagnostic.scope === "string"
? `Scope: ${diagnostic.scope}`
: undefined,
typeof diagnostic.severity === "string"
? `Severity: ${diagnostic.severity}`
: undefined,
typeof diagnostic.assetId === "string"
? `Asset: ${diagnostic.assetId}`
: undefined,
]
.filter((item) => item !== undefined)
.join(". ");
const parsedSourceRange =
assetQueryDiagnosticIssue.shape.sourceRange.safeParse(
diagnostic.sourceRange
);
const sourceRange = parsedSourceRange.success
? parsedSourceRange.data
: undefined;
const severity = diagnostic.severity === "warning" ? "warning" : "error";
const scope = diagnostic.scope === "database" ? "database" : "query";
const phase =
diagnostic.phase === "metadata" || diagnostic.phase === "reference"
? diagnostic.phase
: "source";
return [
{
code,
path: [diagnostic.path],
message,
constraint: code,
...(detail === "" ? {} : { detail }),
severity,
scope,
phase,
file: diagnostic.path,
...(typeof diagnostic.assetId === "string"
? { assetId: diagnostic.assetId }
: {}),
...(typeof diagnostic.line === "number"
? { line: diagnostic.line }
: {}),
...(typeof diagnostic.column === "number"
? { column: diagnostic.column }
: {}),
...(typeof diagnostic.reference === "string"
? { reference: diagnostic.reference }
: {}),
...(typeof diagnostic.nodeType === "string"
? { nodeType: diagnostic.nodeType }
: {}),
...(typeof diagnostic.reason === "string"
? { reason: diagnostic.reason }
: {}),
...(sourceRange === undefined ? {} : { sourceRange }),
},
];
});
const queryIssues = Array.isArray(details?.issues)
? details.issues.flatMap((value) => {
const parsedIssue = assetQuerySetupIssue.safeParse(value);
if (parsedIssue.success === false) {
return [];
}
const issue = parsedIssue.data;
return [
{
code: issue.code,
path: issue.path,
message: issue.message,
constraint: issue.code,
severity: issue.severity,
...(issue.severity === "warning"
? { detail: "Severity: warning" }
: {}),
},
];
})
: [];
return [...sourceIssues, ...queryIssues];
};
const throwAssetQueryApiError = (error: unknown): never => {
const queryError = getDetailedAssetResourceQueryError(error);
if (queryError !== undefined) {
const issues = getAssetQueryApiIssues(queryError);
return throwApiError(
queryError.status === 409 ? "CONFLICT" : "BAD_REQUEST",
queryError.message,
{
webstudioCode: queryError.code,
...(issues.length === 0 ? {} : { issues }),
}
);
}
throw error;
};
const paginatedProjectInput = projectIdInput.extend(
paginatedOutputInputSchema.shape
);
const paginateProjectItems = <Item>(
items: readonly Item[],
input: z.infer<typeof paginatedProjectInput>
) =>
paginateOutput({
items,
cursor: input.cursor,
limit: input.limit,
verbose: input.verbose,
filters: {},
});
const withProjectId = <Schema extends z.ZodTypeAny>(
input: Schema
): z.ZodType<z.infer<Schema> & { projectId: string }> =>
z.intersection(projectIdInput, input) as z.ZodType<
z.infer<Schema> & { projectId: string }
>;
const runtimeProjectInput = (id: RuntimeOperationId) =>
withProjectId(getBuilderRuntimeOperationInputSchema(id));
type ProjectApiCapability = Extract<AuthPermit, BuilderApiCapability>;
type ServerOnlyPublicApiOperationMeta = {
command: string;
client: string;
invalidatesNamespaces?: readonly BuilderNamespace[];
};
const publicApiMeta = (
permit: BuilderApiCapability,
operation?: ServerOnlyPublicApiOperationMeta
) => ({
publicApiPermit: permit,
publicApiOperation: operation,
});
const applyBuildPayload = async <Result extends Record<string, unknown> = {}>(
ctx: AppContext,
build: Awaited<ReturnType<typeof loadDevBuildByProjectId>>,
projectId: string,
payload: z.infer<typeof buildPatchTransaction>["payload"],
result?: Result
) => ({
...(await commitBuildPatch({ build, ctx, projectId, payload })),
...(result ?? {}),
});
const projectQuery = <Schema extends z.ZodType<{ projectId: string }>, Result>(
input: Schema,
permit: ProjectApiCapability,
handler: (args: {
ctx: AppContext;
input: z.infer<Schema>;
auth: Awaited<ReturnType<typeof assertApiProjectPermit>>;
}) => Promise<Result>,
operation?: ServerOnlyPublicApiOperationMeta
) =>
procedure
.meta(publicApiMeta(permit, operation))
.input(input)
.query(async ({ ctx, input }) => {
const parsedInput = input as z.infer<Schema>;
return await handler({
ctx,
input: parsedInput,
auth: await assertApiProjectPermit(ctx, parsedInput.projectId, permit),
});
});
const projectMutation = <
Schema extends z.ZodType<{ projectId: string }>,
Result,
>(
input: Schema,
permit: ProjectApiCapability,
handler: (args: {
ctx: AppContext;
input: z.infer<Schema>;
auth: Awaited<ReturnType<typeof assertApiProjectPermit>>;
}) => Promise<Result>,
operation?: ServerOnlyPublicApiOperationMeta
) =>
procedure
.meta(publicApiMeta(permit, operation))
.input(input)
.mutation(async ({ ctx, input }) => {
const parsedInput = input as z.infer<Schema>;
return await handler({
ctx,
input: parsedInput,
auth: await assertApiProjectPermit(ctx, parsedInput.projectId, permit),
});
});
const buildQuery = <Schema extends z.ZodType<{ projectId: string }>, Result>(
input: Schema,
handler: (args: {
ctx: AppContext;
input: z.infer<Schema>;
build: Awaited<ReturnType<typeof loadDevBuildByProjectId>>;
}) => Promise<Result>
) =>
procedure
.meta(publicApiMeta("view"))
.input(input)
.query(async ({ ctx, input }) => {
const parsedInput = input as z.infer<Schema>;
return await handler({
ctx,
input: parsedInput,
build: await loadReadableDevBuild(ctx, parsedInput.projectId),
});
});
const runtimeBuildQuery = <Result = unknown>(id: RuntimeOperationId) =>
buildQuery(runtimeProjectInput(id), async ({ input, build }) =>
executeApiRuntimeOperation<Result>({
id,
build,
input,
})
);
const withConfirm = <Schema extends z.ZodTypeAny>(input: Schema) =>
z.intersection(input, z.object({ confirm: z.literal(true) })) as z.ZodType<
z.infer<Schema> & { confirm: true }
>;
const runtimeMutationInput = (
id: RuntimeOperationId,
requiresConfirm: boolean
) => {
const input = runtimeProjectInput(id);
return requiresConfirm ? withConfirm(input) : input;
};
const buildMutation = <Schema extends z.ZodType<{ projectId: string }>, Result>(
input: Schema,
handler: (args: {
ctx: AppContext;
auth: Awaited<ReturnType<typeof assertApiProjectPermit>>;
input: z.infer<Schema>;
build: Awaited<ReturnType<typeof loadDevBuildByProjectId>>;
commit: BuildCommit;
}) => Promise<Result>
) =>
procedure
.meta(publicApiMeta("build"))
.input(input)
.mutation(async ({ ctx, input }) => {
const parsedInput = input as z.infer<Schema>;
const auth = await assertApiProjectPermit(
ctx,
parsedInput.projectId,
"build"
);
const build = await loadDevBuildByProjectId(ctx, parsedInput.projectId);
return await handler({
ctx,
auth,
input: parsedInput,
build,
commit: async <CommitResult extends Record<string, unknown> = {}>(
payload: z.infer<typeof buildPatchTransaction>["payload"],
result?: CommitResult
) =>
(await applyBuildPayload(
ctx,
build,
parsedInput.projectId,
payload,
result
)) as { version: number } & CommitResult,
});
});
const runtimeBuildMutation = <Result extends Record<string, unknown> = {}>(
id: RuntimeOperationId,
requiresConfirm: boolean
) =>
buildMutation(
runtimeMutationInput(id, requiresConfirm),
async ({ ctx, input, build, commit }) => {
const error = await runtimeMutationValidators[id]?.({
input,
build,
context: ctx,
});
if (error !== undefined) {
return throwApiError("BAD_REQUEST", error);
}
return commitRuntimeMutation<Result>({
id,
build,
input,
commit,
});
}
);
const runtimeMutationValidators: Partial<
Record<
RuntimeOperationId,
(args: {
input: unknown;
build: CompactBuild;
context: AppContext;
}) => Promise<string | undefined>
>
> = {
"projectSettings.update": validateProjectSettingsUpdate,
};
type BuildCommit = <CommitResult extends Record<string, unknown> = {}>(
payload: z.infer<typeof buildPatchTransaction>["payload"],
result?: CommitResult,
assetFolders?: readonly AssetFolder[]
) => Promise<{ version: number } & CommitResult>;
const commitRuntimeMutation = async <
Result extends Record<string, unknown> = Record<string, unknown>,
>({
id,
build,
assets,
assetFolders,
input,
commit,
context,
}: {
id: RuntimeOperationId;
build: Awaited<ReturnType<typeof loadDevBuildByProjectId>>;
assets?: Asset[];
assetFolders?: AssetFolder[];
input: unknown;
commit: BuildCommit;
context?: Partial<BuilderRuntimeContext>;
}) => {
const mutation = await executeApiRuntimeMutation<Result>({
id,
build,
assets,
assetFolders,
input,
context,
});
if (mutation.noop || mutation.payload.length === 0) {
return {
version: build.version,
...mutation.result,
};
}
return await commit(mutation.payload, mutation.result);
};
const contentOrBuildMutation = <
Schema extends z.ZodType<{ projectId: string }>,
Result,
>(
input: Schema,
handler: (args: {
ctx: AppContext;
auth: Awaited<ReturnType<typeof assertApiProjectPermit>>;
input: z.infer<Schema>;
build: Awaited<ReturnType<typeof loadDevBuildByProjectId>>;
commit: BuildCommit;
}) => Promise<Result>
) =>
procedure
.meta(publicApiMeta("edit"))
.input(input)
.mutation(async ({ ctx, input }) => {
const parsedInput = input as z.infer<Schema>;
const auth = await assertApiProjectPermit(
ctx,
parsedInput.projectId,
"edit"
);
const build = await loadDevBuildByProjectId(ctx, parsedInput.projectId);
return await handler({
ctx,
auth,
input: parsedInput,
build,
commit: async <CommitResult extends Record<string, unknown> = {}>(
payload: z.infer<typeof buildPatchTransaction>["payload"],
result?: CommitResult,
assetFolders?: readonly AssetFolder[]
) => {
assertContentOrBuildTransactions({
auth,
build,
transactions: [{ id: "api-content-mode", payload }],
assetFolders,
});
return (await applyBuildPayload(
ctx,
build,
parsedInput.projectId,
payload,
result
)) as { version: number } & CommitResult;
},
});
});
const runtimeContentOrBuildMutation = <
Result extends Record<string, unknown> = {},
>(
id: RuntimeOperationId,
requiresConfirm: boolean
) =>
contentOrBuildMutation(
runtimeMutationInput(id, requiresConfirm),
async ({ input, build, commit }) =>
commitRuntimeMutation<Result>({
id,
build,
input,
commit,
})
);
const loadRuntimeAssetData = async (ctx: AppContext, projectId: string) => {
return loadAssetDataByProject(projectId, ctx, {
skipPermissionsCheck: true,
});
};
const canPublishBuildCss = async (build: CompactBuild, assets: Asset[]) => {
const usedComponents = new Set(
build.instances.map((instance) => instance.component)
);
const { cssText } = generateCss({
assets: new Map(assets.map((asset) => [asset.id, asset])),
instances: new Map(
build.instances.map((instance) => [instance.id, instance])
),
props: new Map(build.props.map((prop) => [prop.id, prop])),
breakpoints: new Map(
build.breakpoints.map((breakpoint) => [breakpoint.id, breakpoint])
),
styles: new Map(
build.styles
.filter(
(style) => parseGeneratedStyleDeclaration(style).errors.length === 0
)
.map((style) => [getStyleDeclKey(style), style])
),
styleSourceSelections: new Map(
build.styleSourceSelections.map((selection) => [
selection.instanceId,
selection,
])
),
componentMetas: new Map(
[...componentMetas].filter(([name]) => usedComponents.has(name))
),
assetBaseUrl: "",
atomic:
build.projectSettings?.compiler?.atomicStyles ??
build.pages?.compiler?.atomicStyles ??
true,
});
return hasCssSyntaxError(cssText) === false;
};
const createServerContentBlockApplication = ({
ctx,
projectId,
build,
assets,
assetFolders,
}: {
ctx: AppContext;
projectId: string;
build: CompactBuild;
assets: Asset[];
assetFolders: AssetFolder[];
}) => {
const state = createBuilderRuntimeState(build, assets, assetFolders);
const repository = new PostgresAssetRepository({
projectId,
context: ctx,
assetStore: createAssetClient(),
});
const session = createAssetContentSession({
repository,
authorize: ({ assetId }) => state.assets?.has(assetId) === true,
});
return createContentBlockApplication({
projectId,
session,
metas: componentMetas,
});
};
const runtimeAssetsQuery = <Result = unknown>(id: RuntimeOperationId) =>
projectQuery(runtimeProjectInput(id), "view", async ({ ctx, input }) => {
const [{ assets, assetFolders }, build] = await Promise.all([
loadRuntimeAssetData(ctx, input.projectId),
loadDevBuildByProjectId(ctx, input.projectId),
]);
const contentBlockApplication = id.startsWith("contentBlocks.")
? createServerContentBlockApplication({
ctx,
projectId: input.projectId,
build,
assets,
assetFolders,
})
: undefined;
return executeApiRuntimeOperation<Result>({
id,
build,
assets,
assetFolders,
input,
context: { contentBlockApplication },
});
});
const runtimeAssetsMutation = <Result extends Record<string, unknown> = {}>(
operation: RuntimeOperation
) => {
const { id, permit, requiresConfirm } = operation;
const mutation = permit === "edit" ? contentOrBuildMutation : buildMutation;
return mutation(
runtimeMutationInput(id, permit === "edit" ? requiresConfirm : true),
async ({ ctx, auth, input, build, commit }) => {
const { assets, assetFolders } = await loadRuntimeAssetData(
ctx,
input.projectId
);
return await commitRuntimeMutation<Result>({
id,
build,
assets,
assetFolders,
input:
id === "pageTemplates.createPage" &&
auth.permits.includes("build") === false
? { ...input, contentMode: true }
: input,
commit:
id === "assetFolders.create"
? (payload, result) => commit(payload, result, assetFolders)
: commit,
});
}
);
};
const runtimeContentBlockMutation = <
Result extends Record<string, unknown> = {},
>(
id: RuntimeOperationId
) =>
contentOrBuildMutation(
runtimeMutationInput(id, false),
async ({ ctx, input, build, commit }) => {
const { assets, assetFolders } = await loadRuntimeAssetData(
ctx,
input.projectId
);
return await commitRuntimeMutation<Result>({
id,
build,
assets,
assetFolders,
input,
commit,
context: {
contentBlockApplication: createServerContentBlockApplication({
ctx,
projectId: input.projectId,
build,
assets,
assetFolders,
}),
},
});
}
);
type RuntimeOperationRouteTree = Record<string, unknown>;
type RouterRecord = Parameters<typeof router>[0];
type RuntimeOperation = (typeof runtimeOperationContracts)[number];
const addRuntimeOperationRoute = (
tree: RuntimeOperationRouteTree,
operation: RuntimeOperation
) => {
const path = operation.id.split(".");
const routeName = path.at(-1);
if (routeName === undefined || path.length < 2) {
throw new Error(`Runtime operation "${operation.id}" must be namespaced.`);
}
let namespace = tree;
for (const segment of path.slice(0, -1)) {
const existing = namespace[segment];
if (existing === undefined) {
namespace[segment] = {};
}
namespace = namespace[segment] as RuntimeOperationRouteTree;
}
if (namespace[routeName] !== undefined) {
throw new Error(`Duplicate runtime operation route "${operation.id}".`);
}
namespace[routeName] = createRuntimeOperationProcedure(operation);
};
const createRuntimeOperationProcedure = (operation: RuntimeOperation) => {
const { id } = operation;
if (operation.requiresAssets) {
return operation.kind === "read"
? runtimeAssetsQuery(id)
: id.startsWith("contentBlocks.")
? runtimeContentBlockMutation(id)
: runtimeAssetsMutation(operation);
}
if (operation.kind === "read") {
return runtimeBuildQuery(id);
}
if (operation.permit === "edit") {
return runtimeContentOrBuildMutation(id, operation.requiresConfirm);
}
return runtimeBuildMutation(id, operation.requiresConfirm);
};
const createRuntimeOperationRouters = () => {
const tree: RuntimeOperationRouteTree = {};
for (const operation of runtimeOperationContracts) {
addRuntimeOperationRoute(tree, operation);
}
const createRouters = (routes: RuntimeOperationRouteTree): RouterRecord =>
Object.fromEntries(
Object.entries(routes).map(([name, value]) => {
if (
typeof value === "object" &&
value !== null &&
"_def" in value === false
) {
return [
name,
router(createRouters(value as RuntimeOperationRouteTree)),
];
}
return [name, value];
})
) as RouterRecord;
return createRouters(tree);
};
// Runtime routes are generated from contracts; api-router tests verify their
// concrete paths against the public operation catalog.
const runtimeOperationRouters = createRuntimeOperationRouters();
export const apiRouter = router({
auth: router({
me: procedure
.meta(
publicApiMeta("view", {
command: "whoami",
client: "getApiTokenInfo",
})
)
.query(async ({ ctx }) => {
const token = await loadApiToken(ctx);
const permits = getTokenPermits(token, ctx);
return {
actor: { type: "token" as const, tokenId: token.token },
projectId: token.projectId,
relation: token.relation,
permits,
};
}),
}),
forms: router({
simulateSubmission: projectMutation(
projectIdInput.extend({
formId: z.string().min(1),
path: z.string().min(1).max(2048),
siteOrigin: z.string().url().optional(),
values: z
.array(
z.object({
name: z.string().min(1).max(256),
value: z.string().max(10_000),
})
)
.max(100),
arrayNames: z.array(z.string().min(1).max(256)).max(100).default([]),
}),
"build",
async ({ ctx, input }) => {
if (
new TextEncoder().encode(JSON.stringify(input.values)).byteLength >
100_000
) {
throw new Error("Form simulation values are too large");
}
const result = await simulateFormSubmission({
context: ctx,
...input,
});
return {
success: result.success,
status: result.status,
results: result.results.map(({ status, resourceId }) => ({
status,
resourceId,
})),
errors: result.errors.map(
({ status, message, code, resourceId }) => ({
status,
message,
...(resourceId === undefined ? {} : { resourceId }),
...(code === undefined ? {} : { code }),
})
),
};
},
{ command: "simulate-form-submission", client: "simulateFormSubmission" }
),
}),
projects: router({
permissions: procedure
.meta(
publicApiMeta("view", {
command: "permissions",
client: "getProjectPermissions",
})
)
.input(projectIdInput)
.query(async ({ ctx, input }) => {
const token = await loadApiToken(ctx);
if (token.projectId !== input.projectId) {
throw new AuthorizationError(
"Authorization token is not valid for project"
);
}
const permits = getTokenPermits(token, ctx);
const canUseProject = await authorizeProject.hasProjectPermit(
{ projectId: input.projectId, permit: "view" },
ctx
);
if (canUseProject === false) {
throw new AuthorizationError("You don't have access to this project");
}
return {
relation: token.relation,
permits,
canView: permits.includes("view"),
canEdit: permits.includes("edit"),
canBuild: permits.includes("build"),
canAdmin: permits.includes("admin"),
canOwn: false,
canUseApi: permits.includes("api"),
canPublish: token.canPublish === true,
canPublishProjectDomain:
token.canPublish === true || token.relation === "builders",
canPublishCustomDomains: token.canPublish === true,
apiContract: {
version: publicApiContractVersion,
operationIds: publicApiOperations.flatMap((operation) =>
publicApiOperationRequiresServerSupport(operation)
? [operation.id]
: []
),
},
};
}),
get: projectQuery(
projectIdInput,
"view",
async ({ ctx, input }) => {
const project = await loadById(input.projectId, ctx);
const build = await loadDevBuildByProjectId(ctx, input.projectId);
return {
...serializeProjectSummary(project),
buildId: build.id,
version: build.version,
homePageId: build.pages.homePageId,
features: ctx.planFeatures,
};
},
{ command: "inspect", client: "getProjectInfo" }
),
submitMarketplaceProduct: projectMutation(
projectIdInput.extend({
acknowledgePublicSubmission: z
.literal(true)
.describe(
"Acknowledge that submission starts public marketplace review"
),
}),
"edit",
async ({ ctx, input }) => {
const build = await loadDevBuildByProjectId(ctx, input.projectId);
if (build.marketplaceProduct === undefined) {
return throwApiError(
"BAD_REQUEST",
"Complete the marketplace product metadata before submitting it for review"
);
}
const project = await setMarketplaceApprovalStatus(
{
projectId: input.projectId,
marketplaceApprovalStatus: "PENDING",
},
ctx
);
return {
marketplaceApprovalStatus: project.marketplaceApprovalStatus,
};
},
{
command: "submit-marketplace-product",
client: "submitMarketplaceProduct",
}
),
}),
reports: router({
issue: projectMutation(
withProjectId(issueReportInput),
"view",
async ({ ctx, input }) => {
const { projectId: _projectId, ...report } = input;
return await publishConfiguredIssueReport({
...report,
...(report.runtime === undefined
? {}
: {
runtime: {
...report.runtime,
...(ctx.apiClient.version === undefined
? {}
: { cliVersion: ctx.apiClient.version }),
serverApiContractVersion: publicApiContractVersion,
},
}),
});
},
{ command: "report-issue", client: "reportIssue" }
),
}),
build: router({
get: projectQuery(
buildGetInput,
"view",
async ({ ctx, input }) => {
const build =
input.version === undefined
? await loadDevBuildByProjectId(ctx, input.projectId)
: await loadBuildByProjectVersion(
ctx,
input.projectId,
input.version
);
if (build.projectId !== input.projectId) {
throwApiError("NOT_FOUND", "Build not found for project");
}
const include = new Set(input.include ?? []);
const snapshot = createBuildSnapshot({
build,
include,
projectId: input.projectId,
});
if (include.has("assets")) {
const { assets, assetFolders } = await loadAssetDataByProject(
input.projectId,
ctx
);
snapshot.assets = assets;
if (include.has("assetFolders")) {
snapshot.assetFolders = assetFolders;
}
} else if (include.has("assetFolders")) {
snapshot.assetFolders = await loadAssetFoldersByProject(
input.projectId,
ctx
);
}
return snapshot;
},
{ command: "snapshot", client: "getBuildSnapshot" }
),
patch: projectMutation(
buildPatchInput,
"edit",
async ({ ctx, input, auth }) => {
const build = await loadDevBuildByProjectId(ctx, input.projectId);
if (input.baseVersion > build.version) {
throwApiError("CONFLICT", singlePlayerVersionMismatchError);
}
// Older versions cannot write; let persistence handle retries and conflicts.
if (input.baseVersion === build.version) {
assertContentOrBuildTransactions({
auth,
build,
transactions: input.transactions,
scope: "text-and-props",
});
}
return commitBuildTransactions({
ctx,
projectId: input.projectId,
buildId: build.id,
clientVersion: input.baseVersion,
transactions: input.transactions,
});
},
{
command: "apply-patch",
client: "applyBuildPatch",
invalidatesNamespaces: builderNamespaces,
}
),
}),
...runtimeOperationRouters,
assetQueries: router({
validate: projectQuery(
assetQueryValidationInput,
"view",
async ({ ctx, input }) => {
try {
const catalog = await loadBuilderAssetFieldCatalog({
projectId: input.projectId,
context: ctx,
assetClient: createAssetClient(),
});
const validated = validateAssetQueryAgainstCatalog({
query: input.query,
catalog,
});
return {
valid: true as const,
referencedFieldPaths: validated.referencedFieldPaths,
filterCount: getAssetQueryWhereMetrics(validated.query.where)
.filters,
sortCount: validated.query.sort.length,
warnings: validated.warnings,
issues: validated.warningIssues,
};
} catch (error) {
return throwAssetQueryApiError(error);
}
},
{
command: "validate-asset-query",
client: "validateAssetQuery",
}
),
preview: projectQuery(
assetQueryInput,
"view",
async ({ ctx, input }) => {
try {
const { projectId, ...request } = input;
return await previewProjectAssetQuery({
projectId,
request,
context: ctx,
});
} catch (error) {
return throwAssetQueryApiError(error);
}
},
{
command: "preview-asset-query",
client: "previewAssetQuery",
}
),
fieldCatalog: projectQuery(
projectIdInput,
"view",
async ({ ctx, input }) =>
await loadBuilderAssetFieldCatalog({
projectId: input.projectId,
context: ctx,
assetClient: createAssetClient(),
}),
{
command: "get-asset-field-catalog",
client: "getAssetFieldCatalog",
}
),
}),
publish: router({
validate: projectQuery(
projectIdInput.extend({
target: z.enum(["staging", "production"]),
domains: z.array(z.string()).optional(),
}),
"edit",
async ({ auth, ctx, input }) => {
const project = await loadById(input.projectId, ctx);
const domains =
input.domains ?? getDefaultPublishDomains(project, input.target);
assertApiPublishDomains({ auth, domains, project });
if (domains.length === 0) {
throwApiError(
"BAD_REQUEST",
"Select at least one domain to publish."
);
}
const verifiedDomains = getVerifiedPublishDomains(project, domains);
const invalidDomains = domains.filter(
(domain) => verifiedDomains.includes(domain) === false
);
if (invalidDomains.length > 0) {
throwApiError(
"BAD_REQUEST",
`Publish domains must belong to this project and be active and verified: ${invalidDomains.join(", ")}`
);
}
const build = await loadDevBuildByProjectId(ctx, input.projectId);
const issues = getBuildIntegrityIssues(build);
if (issues[0] !== undefined) {
throwApiError(
"BAD_REQUEST",
formatBuildIntegrityError(issues[0], "Cannot publish"),
{ issues }
);
}
const diagnostics = await loadContentDatabasePublishDiagnostics(
input.projectId,
ctx,
{ build }
);
const { assets } = await loadRuntimeAssetData(ctx, input.projectId);
const cssValid = await canPublishBuildCss(build, assets);
return {
valid: true,
target: input.target,
domains,
diagnostics,
cssValid,
};
},
{ command: "validate-publish", client: "validatePublish" }
),
list: projectQuery(
paginatedProjectInput,
"view",
async ({ ctx, input }) => {
const { publishes } = await listProjectPublishes(input.projectId, ctx);
const { items, ...pagination } = paginateProjectItems(publishes, input);
return { publishes: items, ...pagination };
},
{ command: "list-publishes", client: "listPublishes" }
),
create: projectMutation(
projectIdInput.extend({
target: z.enum(["staging", "production"]),
domains: z.array(z.string()).optional(),
message: z.string().optional(),
idempotencyKey: z.string().optional(),
}),
"edit",
async ({ auth, ctx, input }) => {
const project = await loadById(input.projectId, ctx);
const domains =
input.domains ?? getDefaultPublishDomains(project, input.target);
assertApiPublishDomains({ auth, domains, project });
const { build, deploymentNotImplemented } = await publishProject(
{ project, domains, target: input.target },
ctx
);
return {
jobId: build.id,
warning: deploymentNotImplemented
? "Publish was recorded locally, but the deployment service is not available in this environment."
: undefined,
};
},
{ command: "publish", client: "publish" }
),
getJob: projectQuery(
projectIdInput.extend({ jobId: z.string() }),
"view",
async ({ ctx, input }) => {
const publishJob = await getProjectPublishJob(input, ctx);
if (publishJob === undefined) {
return throwApiError("NOT_FOUND", "Publish job not found");
}
return publishJob;
},
{ command: "get-publish-job", client: "getPublishJob" }
),
unpublish: projectMutation(
projectIdInput.extend({
target: z.enum(["staging", "production"]),
domains: z.array(z.string()).optional(),
message: z.string().optional(),
idempotencyKey: z.string().optional(),
confirm: z.literal(true),
}),
"edit",
async ({ auth, ctx, input }) => {
const project = await loadById(input.projectId, ctx);
const domains =
input.domains ?? getDefaultPublishDomains(project, input.target);
assertApiPublishDomains({ auth, domains, project });
await unpublishProjectDomains(
{ projectId: input.projectId, domains },
ctx
);
return { unpublishedDomains: domains };
},
{ command: "unpublish", client: "unpublish" }
),
}),
domains: router({
list: projectQuery(
paginatedProjectInput,
"view",
async ({ ctx, input }) => {
const { items, ...pagination } = paginateProjectItems(
await listProjectDomains(input.projectId, ctx),
input
);
return { domains: items, ...pagination };
},
{ command: "list-domains", client: "listDomains" }
),
create: projectMutation(
projectIdInput.extend({ domain: z.string() }),
"admin",
async ({ ctx, input }) => {
return await createProjectDomain(input, ctx);
},
{ command: "create-domain", client: "createDomain" }
),
update: projectMutation(
projectIdInput.extend({
domainId: z.string(),
updates: z.object({ domain: z.string().optional() }),
}),
"admin",
async ({ ctx, input }) => {
return await updateProjectDomain(input, ctx);
},
{ command: "update-domain", client: "updateDomain" }
),
delete: projectMutation(
projectIdInput.extend({
domainId: z.string(),
confirm: z.literal(true),
}),
"admin",
async ({ ctx, input }) => {
return await deleteProjectDomain(input, ctx);
},
{ command: "delete-domain", client: "deleteDomain" }
),
verify: projectMutation(
projectIdInput.extend({ domainId: z.string() }),
"admin",
async ({ ctx, input }) => {
return await verifyProjectDomain(input, ctx);
},
{ command: "verify-domain", client: "verifyDomain" }
),
}),
});
export const __testing__ = {
assertApiPublishDomains,
};