Skip to content

Commit f0327c6

Browse files
committed
feat: types
1 parent 2bc1ec2 commit f0327c6

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

doc/api.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Error code is `"ESESS"`.
141141
* [.processForm(name, chatID, ref, [options])](#FormSet+processForm)
142142
* [.process(chatID, text, ref)](#FormSet+process)
143143
* [.cancel(chatID)](#FormSet+cancel)
144+
* [._emitQuery(question, ref)](#FormSet+_emitQuery)
144145

145146
<a name="new_FormSet_new"></a>
146147

@@ -274,6 +275,18 @@ Cancel current form processing for chat.
274275
| --- | --- | --- |
275276
| chatID | <code>String</code> \| <code>Number</code> | Unique identifier for the originating chat |
276277

278+
<a name="FormSet+_emitQuery"></a>
279+
280+
### formSet.\_emitQuery(question, ref)
281+
Emits the `query` event.
282+
283+
**Kind**: instance method of [<code>FormSet</code>](#FormSet)
284+
285+
| Param | Type | Description |
286+
| --- | --- | --- |
287+
| question | <code>Object</code> | Query |
288+
| ref | <code>Object</code> | Reference |
289+
277290

278291
* * *
279292

index.d.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
export namespace constants {
2+
const SESSION_VERSION: number;
3+
}
4+
5+
export namespace errors {
6+
class BaseError {}
7+
8+
const BusyErrorCode: string;
9+
class BusyError {}
10+
11+
const FormNotFoundErrorCode: string;
12+
class FormNotFoundError {}
13+
14+
const I18nErrorCode: string;
15+
class I18nError {}
16+
17+
const QueryNotFoundErrorCode: string;
18+
class QueryNotFoundError {}
19+
20+
const SessionErrorCode: string;
21+
class SessionError {}
22+
}
23+
24+
interface AddFormOptions<Answers, Ref> {
25+
cb?: (
26+
answers: Answers,
27+
ref: Ref,
28+
) => Promise<any>;
29+
i18n?: (
30+
text: string,
31+
answers: Partial<Answers>,
32+
ref: Ref,
33+
) => Promise<any>;
34+
}
35+
36+
type ChatId = number | string;
37+
38+
export interface Form<Answers, Ref> {
39+
name: string;
40+
queries: Query<Answers, Ref>[];
41+
options: AddFormOptions<Answers, Ref>;
42+
}
43+
44+
export class FormSet<Answers, Ref> {
45+
addForm(
46+
formName: string,
47+
queries: Query<Answers, Ref>[],
48+
options?: AddFormOptions<Answers, Ref>,
49+
): Form<Answers, Ref>;
50+
51+
cancel(chatId: ChatId): Promise<boolean>;
52+
53+
getForms(): Form<Answers, Ref>[];
54+
55+
on(
56+
event: "query",
57+
cb: (
58+
question: {
59+
choices: Array<{ text: string }>;
60+
text: string;
61+
},
62+
ref: Ref,
63+
) => void,
64+
): void;
65+
66+
process(
67+
chatId: ChatId,
68+
text: string,
69+
ref: Ref,
70+
): Promise<void>;
71+
72+
processForm(
73+
formName: string,
74+
chatId: ChatId,
75+
ref: Ref,
76+
options?: {
77+
answers?: Answers;
78+
},
79+
): Promise<void>;
80+
}
81+
82+
export interface Query<Answers, Ref> {
83+
name?: keyof Answers;
84+
85+
post?(
86+
this: QueryController<Answers, Ref>,
87+
answer: string,
88+
): Promise<any>;
89+
90+
pre?(this: QueryController<Answers, Ref>): Promise<any>;
91+
92+
question?: {
93+
choices?: (string | { id: number | string; text: string })[];
94+
strict?: boolean;
95+
retryText?: string;
96+
text?: string;
97+
};
98+
99+
text?: string;
100+
}
101+
102+
export interface QueryController<Answers, Ref> {
103+
form: Form<Answers, Ref>;
104+
formset: FormSet<Answers, Ref>;
105+
ref: Ref;
106+
107+
do<Key extends keyof Answers>(name: Key): Promise<never>;
108+
109+
getAnswer<Key extends keyof Answers>(): Answers[Key];
110+
getAnswer<Key extends keyof Answers>(name: Key, defaultValue?: any): Answers[Key];
111+
112+
getAnswers(): Partial<Answers>;
113+
114+
goto<Key extends keyof Answers>(name: Key): Promise<never>;
115+
116+
post(): Promise<never>;
117+
118+
retry(text?: string): Promise<never>;
119+
120+
send(text: string): Promise<void>;
121+
122+
setAnswer<Key extends keyof Answers>(key: Key, value: Answers[Key]): void;
123+
setAnswer<Key extends keyof Answers>(value: Answers[Key]): void;
124+
125+
setText(text: string): void;
126+
127+
skip(): Promise<never>;
128+
129+
stop(): Promise<never>;
130+
131+
text(text: string, ctx?: Partial<Answers>): Promise<string>;
132+
133+
unsetAnswer(): void;
134+
unsetAnswer<Key extends keyof Answers>(name: Key): void;
135+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"url": "https://mugo.gocho.live"
1313
},
1414
"main": "lib/index.js",
15+
"types": "index.d.ts",
1516
"engines": {
1617
"node": ">=7"
1718
},

0 commit comments

Comments
 (0)