Skip to content

Commit

Permalink
feat: add support to events array
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Aug 30, 2024
1 parent 6993e50 commit 160c77b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 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.0",
"version": "1.1.1",
"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 @@ -84,7 +84,7 @@ export function prompt<GlobalData = never>(options?: {
const prompt = prompts.get(id);

if (prompt) {
if (prompt?.event && !context.is(prompt.event)) return next();
if (prompt?.events && !context.is(prompt.events)) return next();
// @ts-ignore
if (prompt.validate && !(await prompt.validate(context))) {
if (typeof prompt.onValidateError === "string")
Expand Down
24 changes: 13 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export type OnValidateErrorFunction<Event extends EventsUnion, Data> = (

interface PromptData<Event extends EventsUnion, Data = never> {
resolve: (context: PromptAnswer<Event, Data>) => void;
event?: Event;
events?: Event[];
validate?: ValidateFunction<Event>;
onValidateError?: string | OnValidateErrorFunction<Event, Data>;
transform?: TransformFunction<Event, Data>;
Expand All @@ -111,8 +111,8 @@ export interface PromptFunction<GlobalData = never> {
params?: PromptFunctionParams<EventsUnion, Data>,
): Promise<PromptAnswer<EventsUnion, Data>>;
/** Send message and wait answer ignoring events not listed */
<Event extends EventsUnion, Data = GlobalData>(
event: Event,
<const Event extends EventsUnion, Data = GlobalData>(
event: MaybeArray<Event>,
text: Stringable,
params?: PromptFunctionParams<Event, Data>,
): Promise<PromptAnswer<Event, Data>>;
Expand All @@ -122,32 +122,34 @@ export interface WaitFunction<GlobalData = never> {
/** Wait for the next event from the user */
<Data = GlobalData>(): Promise<PromptAnswer<EventsUnion, Data>>;
/** Wait for the next event from the user ignoring events not listed */
<Event extends EventsUnion, Data = GlobalData>(
event: Event,
<const Event extends EventsUnion, Data = GlobalData>(
event: MaybeArray<Event>,
): Promise<PromptAnswer<Event, Data>>;
/** Wait for the next event from the user ignoring non validated answers */
<Data = GlobalData>(
validate: ValidateFunction<EventsUnion>,
): Promise<PromptAnswer<EventsUnion, Data>>;
/** Wait for the next event from the user ignoring non validated answers and not listed events with transformer */
<Event extends EventsUnion, Data = GlobalData>(
event: Event,
<const Event extends EventsUnion, Data = GlobalData>(
event: MaybeArray<Event>,
options: {
validate?: ValidateFunction<Event>;
transform?: TransformFunction<Event, Data>;
},
): Promise<PromptAnswer<Event, Data>>;
/** Wait for the next event from the user ignoring non validated answers and not listed events */
<Event extends EventsUnion, Data = GlobalData>(
event: Event,
<const Event extends EventsUnion, Data = GlobalData>(
event: MaybeArray<Event>,
validate: ValidateFunction<Event>,
): Promise<PromptAnswer<Event, Data>>;
}

export interface WaitWithActionFunction<GlobalData = never> {
// biome-ignore lint/style/useShorthandFunctionType: <explanation>
<Event extends EventsUnion, Data = GlobalData, ActionReturn = any>(
event: Event,
<const Event extends EventsUnion, Data = GlobalData, ActionReturn = any>(
event: MaybeArray<Event>,
action: () => MaybePromise<ActionReturn>,
): Promise<[PromptAnswer<Event, Data>, ActionReturn]>;
}

export type MaybeArray<T> = T | T[];
33 changes: 21 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormattableString } from "gramio";
import type {
EventsUnion,
MaybeArray,
OnValidateErrorFunction,
PromptAnswer,
PromptFunction,
Expand Down Expand Up @@ -45,8 +46,11 @@ export const events = [
] as const;

function isEvent(
maybeEvent: EventsUnion | Stringable,
): maybeEvent is EventsUnion {
maybeEvent: MaybeArray<EventsUnion> | Stringable,
): maybeEvent is EventsUnion | EventsUnion[] {
if (Array.isArray(maybeEvent))
return maybeEvent.every((event) => events.includes(event));

return events.includes(maybeEvent.toString() as EventsUnion);
}

Expand All @@ -57,7 +61,7 @@ export function getPrompt(
defaults: PromptFunctionParams<any, any>,
): PromptFunction {
async function prompt<Event extends EventsUnion, Data>(
eventOrText: Event | Stringable,
eventOrText: MaybeArray<Event> | Stringable,
textOrParams?: Stringable | PromptFunctionParams<Event, Data>,
params?: PromptFunctionParams<Event, Data>,
) {
Expand All @@ -81,11 +85,13 @@ export function getPrompt(

await context.send(text, sendParams);

const events = isEvent(eventOrText) ? eventOrText : undefined;

return new Promise<PromptAnswer<Event, Data>>((resolve) => {
prompts.set(id, {
// @ts-expect-error
resolve: resolve,
event: isEvent(eventOrText) ? eventOrText : undefined,
events: Array.isArray(events) ? events : events ? [events] : undefined,
validate,
// @ts-expect-error
transform,
Expand All @@ -101,7 +107,7 @@ export function getPrompt(

export function getWait(prompts: PromptsType, id: number): WaitFunction {
async function wait<Event extends EventsUnion, Data>(
eventOrValidate?: Event | ValidateFunction<Event>,
eventOrValidate?: MaybeArray<Event> | ValidateFunction<Event>,
validateOrOptions?:
| ValidateFunction<Event>
| {
Expand All @@ -110,15 +116,18 @@ export function getWait(prompts: PromptsType, id: number): WaitFunction {
onValidateError?: string | OnValidateErrorFunction<Event, Data>;
},
) {
const events =
eventOrValidate &&
typeof eventOrValidate !== "function" &&
isEvent(eventOrValidate)
? eventOrValidate
: undefined;

return new Promise<PromptAnswer<Event>>((resolve) => {
prompts.set(id, {
// @ts-expect-error
resolve: resolve,
event:
// @ts-expect-error
eventOrValidate && isEvent(eventOrValidate)
? eventOrValidate
: undefined,
events: Array.isArray(events) ? events : events ? [events] : undefined,
validate:
typeof eventOrValidate === "function"
? eventOrValidate
Expand Down Expand Up @@ -151,7 +160,7 @@ export function getWaitWithAction(
Data = never,
ActionReturn = any,
>(
event: EventsUnion,
events: MaybeArray<EventsUnion>,
action: () => ActionReturn,
validateOrOptions?:
| ValidateFunction<Event>
Expand Down Expand Up @@ -180,7 +189,7 @@ export function getWaitWithAction(
prompts.set(id, {
// @ts-expect-error
resolve: resolve,
event,
events: Array.isArray(events) ? events : [events],
validate: validate,
// @ts-expect-error
transform: async (context) => {
Expand Down

0 comments on commit 160c77b

Please sign in to comment.