Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dll/editor-bricks-helper/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"getCurrentTheme",
"getHistory",
"getMockInfo",
"getRealValue",
"getRuntime",
"getRuntimeMisc",
"handleHttpError",
Expand Down
3 changes: 3 additions & 0 deletions etc/brick-kit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ export const getMockInfo: (requestUrl: string, method: string) => {
mockId: string;
} | undefined;

// @public
export function getRealValue(value: unknown): unknown;

// @public
export function getRuntime(): Runtime;

Expand Down
1 change: 1 addition & 0 deletions packages/brick-dll/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18541,6 +18541,7 @@
"getCurrentTheme",
"getHistory",
"getMockInfo",
"getRealValue",
"getRuntime",
"getRuntimeMisc",
"handleHttpError",
Expand Down
35 changes: 35 additions & 0 deletions packages/brick-kit/src/getRealValue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PluginRuntimeContext } from "@next-core/brick-types";
import * as runtime from "./core/Runtime";
import { getRealValue } from "./getRealValue";

const mockCurrentContext = jest.spyOn(runtime, "_internalApiGetCurrentContext");

describe("getRealValue", () => {
const context: PluginRuntimeContext = {
query: new URLSearchParams({
quality: "good",
}),
};

beforeEach(() => {
mockCurrentContext.mockReturnValue(context);
});

it.each<[unknown, unknown]>([
["${QUERY.quality}", "good"],
[["<% QUERY.quality %>"], ["good"]],
[
{
"<% QUERY.quality %>": "<% QUERY.quality.toUpperCase() %>",
},
{
good: "GOOD",
},
],
["oops", "oops"],
[2, 2],
])("test getRealValue(%j)", (input, output) => {
const result = getRealValue(input);
expect(result).toEqual(output);
});
});
39 changes: 39 additions & 0 deletions packages/brick-kit/src/getRealValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { PluginRuntimeContext } from "@next-core/brick-types";
import { inject, isEvaluable, isObject } from "@next-core/brick-utils";
import { _internalApiGetCurrentContext } from "./core/exports";
import { evaluate } from "./internal/evaluate";
import { haveBeenInjected, recursiveMarkAsInjected } from "./internal/injected";

/**
* An equivalent of `computeRealValue` but for external usages, with no custom
* context or options.
*
* @param value - Any value which may contains evaluations or placeholders.
* @returns Computed real value.
*/
export function getRealValue(value: unknown): unknown {
const compute = (data: unknown, ctx: PluginRuntimeContext): unknown => {
if (typeof data === "string") {
if (isEvaluable(data)) {
const result = evaluate(data);
recursiveMarkAsInjected(result);
return result;
}
return inject(data, ctx);
}

if (!isObject(data) || haveBeenInjected(value)) {
return data;
}

if (Array.isArray(data)) {
return data.map((v) => compute(v, ctx));
}

return Object.fromEntries(
Object.entries(data).map(([k, v]) => [compute(k, ctx), compute(v, ctx)])
);
};

return compute(value, _internalApiGetCurrentContext());
}
1 change: 1 addition & 0 deletions packages/brick-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ export { getMockInfo } from "./core/MockRegistry";
export * from "./hooks";
export * from "./internal/misc";
export * from "./abortController";
export * from "./getRealValue";