Skip to content

Commit

Permalink
chore: some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Aug 30, 2024
1 parent 160c77b commit 83ab571
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gramio/prompt",
"version": "1.1.1",
"version": "1.1.2",
"description": "Prompt plugin for GramIO",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ export interface WaitWithActionFunction<GlobalData = never> {
<const Event extends EventsUnion, Data = GlobalData, ActionReturn = any>(
event: MaybeArray<Event>,
action: () => MaybePromise<ActionReturn>,
validateOrOptions?:
| ValidateFunction<Event>
| {
validate?: ValidateFunction<Event>;
transform?: TransformFunction<Event, Data>;
onValidateError?: string | OnValidateErrorFunction<Event, Data>;
},
): Promise<[PromptAnswer<Event, Data>, ActionReturn]>;
}

Expand Down
42 changes: 25 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,43 @@ export function getWaitWithAction(
) {
const actionReturn = await action();

const transform =
typeof validateOrOptions === "object"
? validateOrOptions.transform
: undefined;
const validate =
typeof validateOrOptions === "object"
? validateOrOptions.validate
: undefined;
const onValidateError =
typeof validateOrOptions === "object"
? validateOrOptions.onValidateError
: undefined;
const options = deepMerge(
defaults,
typeof validateOrOptions === "function"
? {
validate: validateOrOptions,
}
: validateOrOptions ?? {},
);

// const transform =
// typeof validateOrOptions === "object"
// ? validateOrOptions.transform
// : undefined;
// const validate =
// typeof validateOrOptions === "object"
// ? validateOrOptions.validate
// : undefined;
// const onValidateError =
// typeof validateOrOptions === "object"
// ? validateOrOptions.onValidateError
// : undefined;

return new Promise<[PromptAnswer<Event>, ActionReturn]>((resolve) => {
prompts.set(id, {
// @ts-expect-error
resolve: resolve,
events: Array.isArray(events) ? events : [events],
validate: validate,
validate: options.validate,
// @ts-expect-error
transform: async (context) => {
const transformedContext = transform
? // @ts-expect-error
await transform(context)
const transformedContext = options.transform
? await options.transform(context)
: context;

return [transformedContext, actionReturn];
},
onValidateError: onValidateError,
onValidateError: options.onValidateError,
});
});
}
Expand Down

0 comments on commit 83ab571

Please sign in to comment.