create-gramstax 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,7 +35,7 @@
35
35
  "lint:fix": "eslint . --fix"
36
36
  },
37
37
  "dependencies": {
38
- "gramstax": "^0.7.1",
38
+ "gramstax": "^0.8.1",
39
39
  "dayjs": "^1.11.13"
40
40
  },
41
41
  "peerDependencies": {
@@ -13,10 +13,10 @@ export class BotCore extends Gramstax {
13
13
  }
14
14
 
15
15
  async hookErrorPage(ctx: CtxCore, listenerName: string, error: any, isEdit: boolean) {
16
- await new GeneralStatusPage(ctx).toError(`${listenerName}:hookErrorPage`, error, isEdit)
16
+ await new GeneralStatusPage(ctx).error(`${listenerName}:hookErrorPage`, error, isEdit)
17
17
  }
18
18
 
19
19
  async hookErrorInputNotFoundPage(ctx: CtxCore) {
20
- await new GeneralStatusPage(ctx).toErrorInputNotFound()
20
+ await new GeneralStatusPage(ctx).errorInputNotFound()
21
21
  }
22
22
  }
@@ -4,14 +4,14 @@ import {UserNameNotFoundPage} from "~/pages/username-notfound"
4
4
 
5
5
  export class UserGuard extends GuardBase {
6
6
  async _catch(err: any, func: any, isEdit: boolean) {
7
- await new GeneralStatusPage(this).toError(func, err, isEdit)
7
+ await new GeneralStatusPage(this).error(func, err, isEdit)
8
8
  return null
9
9
  }
10
10
 
11
11
  async ensureUsername(edit: boolean) {
12
12
  try {
13
13
  if (!this.userName || this.userName.length == 0) {
14
- await new UserNameNotFoundPage(this).transition(edit)
14
+ await new UserNameNotFoundPage(this).entryPoint(edit)
15
15
  return null
16
16
  }
17
17
  return {status: true}
@@ -2,6 +2,33 @@ import {LogHe} from "~/helpers/log"
2
2
  import {PageBase} from "~/base/page"
3
3
  import {StartPage} from "./start"
4
4
 
5
+ class Helper {
6
+ static kb(ctx: GeneralStatusPage) {
7
+ return ctx.inlineKeyboardT([StartPage])
8
+ }
9
+ }
10
+
11
+ class Alert {
12
+ static async error(ctx: GeneralStatusPage, funcName: string, error: any, edit = true) {
13
+ LogHe.errorMake(error, ctx.constructorName, funcName)
14
+
15
+ await ctx.clearSession()
16
+ const kb = Helper.kb(ctx)
17
+ const da = {isNotFound: false}
18
+ if (edit) {
19
+ await ctx.edit(kb, da)
20
+ } else {
21
+ await ctx.reply(kb, da)
22
+ }
23
+ }
24
+
25
+ static async errorInputNotFound(ctx: GeneralStatusPage) {
26
+ const kb = Helper.kb(ctx)
27
+ const da = {isNotFound: true}
28
+ await ctx.reply(kb, da)
29
+ }
30
+ }
31
+
5
32
  export class GeneralStatusPage extends PageBase {
6
33
  constructorName?: string
7
34
 
@@ -10,23 +37,12 @@ export class GeneralStatusPage extends PageBase {
10
37
  this.constructorName = p?.constructor?.name
11
38
  }
12
39
 
13
- kb() {
14
- return this.inlineKeyboardT([StartPage])
15
- }
16
-
17
- async toError(funcName: string, error: any, edit = true) {
18
- LogHe.errorMake(error, this.constructorName, funcName)
19
-
20
- await this.clearSession()
21
- const kb = this.kb()
22
- const da = {isNotFound: false}
23
- if (edit) await this.edit(kb, da)
24
- else await this.reply(kb, da)
40
+ async error(funcName: string, error: any, edit = true) {
41
+ await Alert.error(this, funcName, error, edit)
25
42
  }
26
43
 
27
- async toErrorInputNotFound() {
28
- const kb = this.kb()
29
- await this.reply(kb, {isNotFound: true})
44
+ async errorInputNotFound() {
45
+ await Alert.errorInputNotFound(this)
30
46
  }
31
47
 
32
48
  static template = `
@@ -1,19 +1,24 @@
1
1
  import {PageBase} from "~/base/page"
2
2
  import {StartPage} from "./start"
3
3
 
4
- export class HelpPage extends PageBase {
5
- async show(edit = true) {
6
- const kb = this.inlineKeyboardT([StartPage])
7
- if (edit) await this.edit(kb)
8
- else await this.reply(kb)
4
+ class Perform {
5
+ static async entryPoint(ctx: HelpPage, edit = true) {
6
+ const kb = ctx.inlineKeyboardT([StartPage])
7
+ if (edit) {
8
+ await ctx.edit(kb)
9
+ } else {
10
+ await ctx.reply(kb)
11
+ }
9
12
  }
13
+ }
10
14
 
15
+ export class HelpPage extends PageBase {
11
16
  async handleCallbackData() {
12
- await this.show(true)
17
+ await Perform.entryPoint(this, true)
13
18
  }
14
19
 
15
20
  async handleCommandText() {
16
- await this.show(false)
21
+ await Perform.entryPoint(this, false)
17
22
  }
18
23
 
19
24
  static template = `
@@ -1,20 +1,29 @@
1
1
  import {PageBase} from "~/base/page"
2
2
  import {StartPage} from "./start"
3
3
 
4
+ class Perform {
5
+ static async entryPoint(ctx: RandomNumberPage, edit = true) {
6
+ const kb = ctx.inlineKeyboardT([this, StartPage])
7
+ const da = {result: Math.random() * 10 ** 18}
8
+ if (edit) {
9
+ await ctx.edit(kb, da)
10
+ } else {
11
+ await ctx.reply(kb, da)
12
+ }
13
+ }
14
+ }
15
+
4
16
  export class RandomNumberPage extends PageBase {
5
17
  async handleCallbackData() {
6
- await this.show(true)
18
+ await Perform.entryPoint(this, true)
7
19
  }
8
20
 
9
21
  async handleCommandText() {
10
- await this.show(false)
22
+ await Perform.entryPoint(this, false)
11
23
  }
12
24
 
13
- async show(edit = true) {
14
- const kb = this.inlineKeyboardT([this, StartPage])
15
- const da = {result: Math.random() * 10 ** 18}
16
- if (edit) await this.edit(kb, da)
17
- else await this.reply(kb, da)
25
+ async entryPoint(edit = true) {
26
+ await Perform.entryPoint(this, edit)
18
27
  }
19
28
 
20
29
  static template = `
@@ -3,21 +3,30 @@ import {PageBase} from "~/base/page"
3
3
  import {RandomNumberPage} from "./random-number"
4
4
  import {SumNumberPage} from "./sum-number"
5
5
 
6
+ class Perform {
7
+ static async entryPoint(ctx: StartPage, edit = true) {
8
+ await ctx.clearSession()
9
+ const kb = ctx.inlineKeyboardT([RandomNumberPage, SumNumberPage, HelpPage], `2`)
10
+ const da = {userName: ctx.userName}
11
+ if (edit) {
12
+ await ctx.edit(kb, da)
13
+ } else {
14
+ await ctx.reply(kb, da)
15
+ }
16
+ }
17
+ }
18
+
6
19
  export class StartPage extends PageBase {
7
20
  async handleCallbackData() {
8
- await this.show(true)
21
+ await Perform.entryPoint(this, true)
9
22
  }
10
23
 
11
24
  async handleCommandText() {
12
- await this.show(false)
25
+ await Perform.entryPoint(this, false)
13
26
  }
14
27
 
15
- async show(edit = true) {
16
- await this.clearSession()
17
- const kb = this.inlineKeyboardT([RandomNumberPage, SumNumberPage, HelpPage], `2`)
18
- const da = {userName: this.userName}
19
- if (edit) await this.edit(kb, da)
20
- else await this.reply(kb, da)
28
+ async entryPoint(edit = true) {
29
+ await Perform.entryPoint(this, edit)
21
30
  }
22
31
 
23
32
  static template = `
@@ -1,79 +1,98 @@
1
1
  import {PageBase} from "~/base/page"
2
2
  import {StartPage} from "./start"
3
3
 
4
- export class SumNumberPage extends PageBase {
5
- static BASE = {pleft: `pleft`, pright: `pright`, result: `result`}
4
+ class Base {
5
+ static pleft: `pleft`
6
+ static pright: `pright`
7
+ static result: `result`
8
+ }
6
9
 
7
- async handleCallbackData() {
8
- await this.first(true)
10
+ class Helper {
11
+ static getSessionParams(ctx: SumNumberPage) {
12
+ return (ctx.temp.session?.params as {step?: `left` | `right`; left?: number} | undefined) ?? {}
9
13
  }
10
14
 
11
- async handleCommandText() {
12
- await this.first(false)
15
+ static kb(ctx: SumNumberPage, baseId: string) {
16
+ return ctx.inlineKeyboardT([StartPage], `1`, baseId)
13
17
  }
18
+ }
14
19
 
15
- async handleFreeText() {
16
- if (!this.isSessionFreeText()) return null
17
- const {BASE} = SumNumberPage
18
- const {step, left} = this.getSessionParams()
19
- const input = this.msgText?.trim()
20
- const value = input ? Number(input) : NaN
20
+ class Flow {
21
+ static async step1Form(ctx: SumNumberPage, edit: boolean) {
22
+ await ctx.sessionFreeText({step: `left`})
23
+ const bi = Base.pleft
24
+ const kb = Helper.kb(ctx, bi)
25
+ const da = {}
26
+ if (edit) {
27
+ await ctx.edit(kb, da, bi)
28
+ } else {
29
+ await ctx.reply(kb, da, bi)
30
+ }
31
+ }
21
32
 
22
- if (step === `left`) {
23
- if (!Number.isFinite(value)) {
24
- await this.sessionFreeText({step: `left`})
25
- const bi = BASE.pleft
26
- const kb = this.kb(bi)
27
- return await this.reply(kb, {}, bi)
28
- }
29
-
30
- await this.sessionFreeText({step: `right`, left: value})
31
- const bi = BASE.pright
32
- const kb = this.kb(bi)
33
- return await this.reply(kb, {}, bi)
33
+ static async step1Answer(ctx: SumNumberPage, left: number) {
34
+ if (!Number.isFinite(left)) {
35
+ await Flow.step1Form(ctx, false)
36
+ } else {
37
+ await this.step2Form(ctx, left)
34
38
  }
39
+ }
40
+
41
+ static async step2Form(ctx: SumNumberPage, left: number) {
42
+ await ctx.sessionFreeText({step: `right`, left})
43
+ const bi = Base.pright
44
+ const kb = Helper.kb(ctx, bi)
45
+ const da = {}
46
+ await ctx.reply(kb, da, bi)
47
+ }
35
48
 
36
- if (step === `right`) {
37
- if (!Number.isFinite(value)) {
38
- await this.sessionFreeText({step: `right`, left})
39
- const bi = BASE.pright
40
- const kb = this.kb(bi)
41
- return await this.reply(kb, {}, bi)
42
- }
43
-
44
- if (!Number.isFinite(left ?? NaN)) {
45
- await this.clearSession()
46
- return await this.first(false)
47
- }
48
-
49
- await this.clearSession()
50
- const bi = BASE.result
51
- const kb = this.kb(bi)
52
- const da = {result: left! + value}
53
- await this.reply(kb, da, bi)
49
+ static async step2Answer(ctx: SumNumberPage, left: number, right: number) {
50
+ if (!Number.isFinite(right)) {
51
+ await Flow.step2Form(ctx, right)
52
+ } else if (!Number.isFinite(right ?? NaN)) {
53
+ await ctx.clearSession()
54
+ await Flow.step1Form(ctx, false)
55
+ } else {
56
+ await Alert.success(ctx, left, right)
54
57
  }
55
58
  }
59
+ }
56
60
 
57
- private async first(edit: boolean) {
58
- await this.sessionFreeText({step: `left`})
59
- const {BASE} = SumNumberPage
60
- const bi = BASE.pleft
61
- const kb = this.kb(bi)
61
+ class Alert {
62
+ static async success(ctx: SumNumberPage, left: number, right: number) {
63
+ await ctx.clearSession()
64
+ const bi = Base.result
65
+ const kb = Helper.kb(ctx, bi)
66
+ const da = {result: left! + right}
67
+ await ctx.reply(kb, da, bi)
68
+ }
69
+ }
62
70
 
63
- if (edit) await this.edit(kb, {}, bi)
64
- else await this.reply(kb, {}, bi)
71
+ export class SumNumberPage extends PageBase {
72
+ async handleCallbackData() {
73
+ await Flow.step1Form(this, true)
65
74
  }
66
75
 
67
- private kb(baseId: string) {
68
- return this.inlineKeyboardT([StartPage], `1`, baseId)
76
+ async handleCommandText() {
77
+ await Flow.step1Form(this, false)
69
78
  }
70
79
 
71
- private getSessionParams() {
72
- return (this.temp.session?.params as {step?: `left` | `right`; left?: number} | undefined) ?? {}
80
+ async handleFreeText() {
81
+ if (!this.isSessionFreeText()) {
82
+ return null
83
+ }
84
+ const {step, left} = Helper.getSessionParams(this)
85
+ const input = this.msgText?.trim()
86
+ const value = input ? Number(input) : NaN
87
+ if (step === `left`) {
88
+ await Flow.step1Answer(this, value)
89
+ } else if (step === `right`) {
90
+ await Flow.step2Answer(this, left!, value)
91
+ }
73
92
  }
74
93
 
75
94
  static template = `
76
- <base id="${this.BASE.pleft}">
95
+ <base id="${Base.pleft}">
77
96
  ${this.kbk.cancel()}
78
97
  <message>
79
98
  <b>📚 Input Left Number</b>
@@ -86,7 +105,8 @@ export class SumNumberPage extends PageBase {
86
105
  Contoh: 23
87
106
  </message>
88
107
  </base>
89
- <base id="${this.BASE.pright}">
108
+
109
+ <base id="${Base.pright}">
90
110
  ${this.kbk.cancel()}
91
111
  <message>
92
112
  <b>📚 Input Right Number</b>
@@ -99,7 +119,8 @@ export class SumNumberPage extends PageBase {
99
119
  Contoh: 10
100
120
  </message>
101
121
  </base>
102
- <base id="${this.BASE.result}">
122
+
123
+ <base id="${Base.result}">
103
124
  ${this.kbk.home()}
104
125
  <message>
105
126
  <b>📚 Result Sum</b>
@@ -1,11 +1,20 @@
1
1
  import {HelpPage} from "./help"
2
2
  import {PageBase} from "~/base/page"
3
3
 
4
+ class Perform {
5
+ static async entryPoint(ctx: UserNameNotFoundPage, edit = true) {
6
+ const kb = ctx.inlineKeyboardT([HelpPage])
7
+ if (edit) {
8
+ await ctx.edit(kb)
9
+ } else {
10
+ await ctx.reply(kb)
11
+ }
12
+ }
13
+ }
14
+
4
15
  export class UserNameNotFoundPage extends PageBase {
5
- async transition(edit = true) {
6
- const kb = this.inlineKeyboardT([HelpPage])
7
- if (edit) await this.edit(kb)
8
- else await this.reply(kb)
16
+ async entryPoint(edit = true) {
17
+ await Perform.entryPoint(this, edit)
9
18
  }
10
19
 
11
20
  static template = `
@@ -1,7 +1,5 @@
1
1
  {
2
2
  "compilerOptions": {
3
- // Short relative path
4
- "baseUrl": ".",
5
3
  "paths": {
6
4
  "~/*": [
7
5
  "./src/*"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-gramstax",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public",