Skip to content

Commit

Permalink
chore: add support to OnValidateError actionReturn
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Aug 30, 2024
1 parent 83ab571 commit f3c5fad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 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.2",
"version": "1.1.3",
"description": "Prompt plugin for GramIO",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function prompt<GlobalData = never>(options?: {
if (typeof prompt.onValidateError === "string")
return context.send(prompt.onValidateError);
// @ts-ignore
if (prompt.onValidateError) return prompt.onValidateError(context);
if (prompt.onValidateError) return prompt.onValidateError(context, prompt.actionReturn);
if (prompt.text)
return context.send(prompt.text, prompt.sendParams);
return;
Expand Down
20 changes: 17 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,27 @@ export type ValidateFunction<Event extends EventsUnion> = (
export type TransformFunction<Event extends EventsUnion, Data> = (
context: PromptAnswer<Event, never>,
) => MaybePromise<Data>;
export type OnValidateErrorFunction<Event extends EventsUnion, Data> = (
export type OnValidateErrorFunction<
Event extends EventsUnion,
Data,
ActionReturn = never,
> = (
context: PromptAnswer<Event>,
...data: IsNever<ActionReturn> extends true
? []
: [actionReturn: ActionReturn]
) => any;

interface PromptData<Event extends EventsUnion, Data = never> {
interface PromptData<
Event extends EventsUnion,
Data = never,
ActionReturn = any,
> {
resolve: (context: PromptAnswer<Event, Data>) => void;
events?: Event[];
validate?: ValidateFunction<Event>;
onValidateError?: string | OnValidateErrorFunction<Event, Data>;
actionReturn?: ActionReturn;
transform?: TransformFunction<Event, Data>;
sendParams?: Optional<SendMessageParams, "chat_id" | "text">;
text?: string;
Expand Down Expand Up @@ -154,7 +166,9 @@ export interface WaitWithActionFunction<GlobalData = never> {
| {
validate?: ValidateFunction<Event>;
transform?: TransformFunction<Event, Data>;
onValidateError?: string | OnValidateErrorFunction<Event, Data>;
onValidateError?:
| string
| OnValidateErrorFunction<Event, Data, ActionReturn>;
},
): Promise<[PromptAnswer<Event, Data>, ActionReturn]>;
}
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export function getWaitWithAction(

return new Promise<[PromptAnswer<Event>, ActionReturn]>((resolve) => {
prompts.set(id, {
actionReturn,
// @ts-expect-error
resolve: resolve,
events: Array.isArray(events) ? events : [events],
Expand Down

0 comments on commit f3c5fad

Please sign in to comment.