Skip to content

Extendable routes, mockApi and clientOptions in storage#88

Open
Vishtar wants to merge 4 commits intojehy:masterfrom
Vishtar:PR
Open

Extendable routes, mockApi and clientOptions in storage#88
Vishtar wants to merge 4 commits intojehy:masterfrom
Vishtar:PR

Conversation

@Vishtar
Copy link
Copy Markdown

@Vishtar Vishtar commented May 27, 2024

I need more APIs than this library has, but there is no easy way to add new routes and store mock data for new APIs.

Mock data can be stored in an extended class, etc. But the native way seems better.

Example of expandable routes:

// Generic type
export class TelegramServerWrapper extends TelegramServer<{
  getChat: Record<number | string, Partial<ChatFromGetChat>>
  getChatAdministrators: Record<number | string, Partial<ChatMemberAdministrator>[]>
  getChatMembersCount: Record<number | string, number>
}> {
  constructor(config?: Partial<TelegramServerConfig>) {
    super(config, {
      // Custom routes
      routes: [getChat, getChatAdministrators, getChatMembersCount],
    })
  }
}

And custom route:

export const getChat: TExpressRoute = (app, telegramServer) => {
  handle(app, '/bot:token/getChat', (req, res, _next) => {
    const isChatIdNumber = typeof req.body.chat_id === 'number'
    const chat_id = isChatIdNumber ? req.body.chat_id : req.body.chat_id.replace('@', '')
    // There is an example of mocked data
    const data = { ok: true, result: telegramServer.storage.mockApi.getChat[chat_id] }
    // @ts-expect-error .sendResult() in any
    res.sendResult(data)
  })
}

I also apretiate the ability to store exist client options to use it in mocks:

tgServer.storage.mockApi.getChatAdministrators = {
      [testChannel]: [
        {
          user: {
            id: tgClient.userId,
            is_bot: false,
            first_name: tgClient.firstName,
          },
        },
      ],
    }

What do you think?

@jehy
Copy link
Copy Markdown
Owner

jehy commented Jun 8, 2024

Hi! Looks really good. Can you add information about those features to readme?

@Vishtar
Copy link
Copy Markdown
Author

Vishtar commented Jun 18, 2024

Hi! Looks really good. Can you add information about those features to readme?

Ofc! Added almost the same text to the bottom of the readme.

@jehy
Copy link
Copy Markdown
Owner

jehy commented Jun 18, 2024

Great! Gonna take a closer look today and merge.

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 90.541% (-0.1%) from 90.685%
when pulling 1dc9913 on Vishtar:PR
into f1e75d2 on jehy:master.

1 similar comment
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 90.541% (-0.1%) from 90.685%
when pulling 1dc9913 on Vishtar:PR
into f1e75d2 on jehy:master.

Comment thread README.md

```typescript
export const getChat: TExpressRoute = (app, telegramServer) => {
handle(app, '/bot:token/getChat', (req, res, _next) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's handle coming from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants