Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extendable routes, mockApi and clientOptions in storage #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Vishtar
Copy link

@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
Owner

jehy commented Jun 8, 2024

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

@Vishtar
Copy link
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
Owner

jehy commented Jun 18, 2024

Great! Gonna take a closer look today and merge.

@coveralls
Copy link

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

Coverage Status

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


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

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