From 35340ffc8e1cbd317b829edc9ba79f78ac162933 Mon Sep 17 00:00:00 2001 From: Zhiyu You Date: Thu, 7 Sep 2023 16:40:08 +0800 Subject: [PATCH] fix(server): calculate default value before calling UX --- packages/server/src/providers/userInteraction.ts | 12 ++++++------ packages/server/src/utils.ts | 7 ++++++- packages/server/tests/utils.test.ts | 6 +++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/server/src/providers/userInteraction.ts b/packages/server/src/providers/userInteraction.ts index 4db7d36fa4..e58253c587 100644 --- a/packages/server/src/providers/userInteraction.ts +++ b/packages/server/src/providers/userInteraction.ts @@ -36,7 +36,7 @@ export default class ServerUserInteraction implements UserInteraction { async selectOption(config: SingleSelectConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.selectOption, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } @@ -44,7 +44,7 @@ export default class ServerUserInteraction implements UserInteraction { async selectOptions(config: MultiSelectConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.selectOptions, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } @@ -52,7 +52,7 @@ export default class ServerUserInteraction implements UserInteraction { async inputText(config: InputTextConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.inputText, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } @@ -70,7 +70,7 @@ export default class ServerUserInteraction implements UserInteraction { async selectFile(config: SelectFileConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.selectFile, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } @@ -78,7 +78,7 @@ export default class ServerUserInteraction implements UserInteraction { async selectFiles(config: SelectFilesConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.selectFiles, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } @@ -86,7 +86,7 @@ export default class ServerUserInteraction implements UserInteraction { async selectFolder(config: SelectFolderConfig): Promise> { const promise = this.connection.sendRequest( RequestTypes.ui.selectFolder, - convertUIConfigToJson(config) + await convertUIConfigToJson(config) ); return getResponseWithErrorHandling(promise); } diff --git a/packages/server/src/utils.ts b/packages/server/src/utils.ts index b8946fcf78..f7a42512de 100644 --- a/packages/server/src/utils.ts +++ b/packages/server/src/utils.ts @@ -67,7 +67,9 @@ export async function getResponseWithErrorHandling( }); } -export function convertUIConfigToJson(config: UIConfig): UIConfig { +export async function convertUIConfigToJson( + config: UIConfig +): Promise> { const newConfig = deepCopy(config); if ("options" in newConfig) { let options: StaticOptions = (newConfig as any).options; @@ -76,6 +78,9 @@ export function convertUIConfigToJson(config: UIConfig): UIConfig { (newConfig as any).options = options; } } + if (typeof config.default === "function") { + newConfig.default = await config.default(); + } if (config.validation) { const funcId = setFunc(config.validation); (newConfig as any).validation = { type: "ValidateFunc", id: funcId }; diff --git a/packages/server/tests/utils.test.ts b/packages/server/tests/utils.test.ts index 55f043a94b..25fc3e8828 100644 --- a/packages/server/tests/utils.test.ts +++ b/packages/server/tests/utils.test.ts @@ -88,17 +88,17 @@ describe("utils", () => { }); }); - it("convertUIConfigToJson", () => { + it("convertUIConfigToJson", async () => { const f = () => {}; const config = { name: "test name", title: "test title", - default: "test default value", + default: () => Promise.resolve("test default value"), options: ["option1", "option2"], validation: f, }; reset(); - const res = convertUIConfigToJson(config as UIConfig); + const res = await convertUIConfigToJson(config as UIConfig); const exp = { name: "test name", title: "test title",