mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 22:14:29 +09:00
## Description 1. What is this PR about (link the issue and add a short description) ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { json } from "@remix-run/server-runtime";
|
|
import { parseBuilderUrl } from "@webstudio-is/http-client";
|
|
import { loadAssetsByProject } from "@webstudio-is/asset-uploader/index.server";
|
|
import { toRuntimeAsset } from "@webstudio-is/sdk";
|
|
import { isBuilder } from "../router-utils";
|
|
import { createContext } from "../context.server";
|
|
|
|
/**
|
|
* System Resource that provides the list of assets for the current project.
|
|
* This allows assets to be dynamically referenced in the builder using the expression editor.
|
|
*/
|
|
export const loader = async ({ request }: { request: Request }) => {
|
|
if (isBuilder(request) === false) {
|
|
throw new Error(
|
|
"Asset resource loader can only be accessed from the builder interface"
|
|
);
|
|
}
|
|
|
|
const { projectId } = parseBuilderUrl(request.url);
|
|
|
|
if (projectId === undefined) {
|
|
throw new Error(
|
|
"Project ID is required to load assets. Ensure the request includes a valid project context."
|
|
);
|
|
}
|
|
|
|
const context = await createContext(request);
|
|
|
|
const assets = await loadAssetsByProject(projectId, context);
|
|
|
|
const requestUrl = new URL(request.url);
|
|
const origin = `${requestUrl.protocol}//${requestUrl.host}`;
|
|
|
|
// Convert array to object with asset IDs as keys
|
|
// Use /cgi/ endpoint URLs (relative paths)
|
|
const assetsById = Object.fromEntries(
|
|
assets.map((asset) => [asset.id, toRuntimeAsset(asset, origin)])
|
|
);
|
|
|
|
return json(assetsById);
|
|
};
|