experimental: Allow json in template literals (#3886)

## Description
closes #3872


- [x] - Builder support
- [x] - Publish support

## Steps for reproduction

Example

https://add-json.staging.webstudio.is/builder/8160e16d-5ab4-4249-86de-0c98a64526a3

https://test-json-ki9yj.wstd.work/

## 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:
5de6)
- [ ] 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
This commit is contained in:
Ivan Starkov authored and GitHub committed 2024-08-11 11:50:18 +03:00
1 parent 403d7b2328
commit 13da170ef4
38 files changed
+888 -173

No files matched your search

+1
View File
@@ -20,3 +20,4 @@ export * from "./resources-generator";
export * from "./page-meta-generator";
export * from "./url-pattern";
export * from "./form-fields";
export * from "./to-string";
+27
View File
@@ -0,0 +1,27 @@
export const createJsonStringifyProxy = <T extends object>(target: T): T => {
return new Proxy(target, {
get(target, prop, receiver) {
if (prop === "toString") {
return function () {
return JSON.stringify(target);
};
}
const value = Reflect.get(target, prop, receiver);
if (typeof value === "object" && value !== null) {
return createJsonStringifyProxy(value);
}
return value;
},
});
};
export const isPlainObject = (value: unknown): value is object => {
return (
Object.prototype.toString.call(value) === "[object Object]" &&
(Object.getPrototypeOf(value) === null ||
Object.getPrototypeOf(value) === Object.prototype)
);
};