Skip to content

Commit

Permalink
Merge pull request #9873 from OfficeDev/zhiyou/vs/merge
Browse files Browse the repository at this point in the history
fix(server): calculate default value before calling UX
  • Loading branch information
adashen authored Sep 7, 2023
2 parents 5651693 + 35340ff commit 5f7a01a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
12 changes: 6 additions & 6 deletions packages/server/src/providers/userInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ export default class ServerUserInteraction implements UserInteraction {
async selectOption(config: SingleSelectConfig): Promise<Result<SingleSelectResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.selectOption,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}

async selectOptions(config: MultiSelectConfig): Promise<Result<MultiSelectResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.selectOptions,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}

async inputText(config: InputTextConfig): Promise<Result<InputTextResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.inputText,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}
Expand All @@ -70,23 +70,23 @@ export default class ServerUserInteraction implements UserInteraction {
async selectFile(config: SelectFileConfig): Promise<Result<SelectFileResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.selectFile,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}

async selectFiles(config: SelectFilesConfig): Promise<Result<SelectFilesResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.selectFiles,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}

async selectFolder(config: SelectFolderConfig): Promise<Result<SelectFolderResult, FxError>> {
const promise = this.connection.sendRequest(
RequestTypes.ui.selectFolder,
convertUIConfigToJson(config)
await convertUIConfigToJson(config)
);
return getResponseWithErrorHandling(promise);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export async function getResponseWithErrorHandling<T>(
});
}

export function convertUIConfigToJson<T>(config: UIConfig<T>): UIConfig<T> {
export async function convertUIConfigToJson<T extends string | string[]>(
config: UIConfig<T>
): Promise<UIConfig<T>> {
const newConfig = deepCopy(config);
if ("options" in newConfig) {
let options: StaticOptions = (newConfig as any).options;
Expand All @@ -76,6 +78,9 @@ export function convertUIConfigToJson<T>(config: UIConfig<T>): UIConfig<T> {
(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 = <CustomizeFuncRequestType>{ type: "ValidateFunc", id: funcId };
Expand Down
6 changes: 3 additions & 3 deletions packages/server/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>);
const res = await convertUIConfigToJson(config as UIConfig<string>);
const exp = {
name: "test name",
title: "test title",
Expand Down

0 comments on commit 5f7a01a

Please sign in to comment.