5.0.0 • Published 2 years ago

@airgram/use-models v5.0.0

Weekly downloads
21
License
-
Repository
github
Last release
2 years ago

Using models in Airgram

This small utility converts plain JSON objects to class instances.

Install

$ npm i @airgram/use-models

Usage

For example, lets add some features to the Chat:

import { Airgram } from 'airgram'
import { ApiMethods } from '@airgram/core'
import { CHAT_TYPE, UPDATE } from '@airgram/constants'
import { ChatBaseModel, useModels } from '@airgram/use-models'

class ChatModel extends ChatBaseModel {
  get isBasicGroup (): boolean {
    return this.type._ === CHAT_TYPE.chatTypeBasicGroup
  }

  get isSupergroup (): boolean {
    return this.type._ === CHAT_TYPE.chatTypeSupergroup
  }

  get isPrivateChat (): boolean {
    return this.type._ === CHAT_TYPE.chatTypePrivate
  }

  get isSecretChat (): boolean {
    return this.type._ === CHAT_TYPE.chatTypeSecret
  }
}

// This is important for correct typings
declare module '@airgram/core/types/outputs/Chat' {
  export interface Chat extends ChatModel {}
}

const airgram = new Airgram({
  models: useModels({
    chat: ChatModel
  })
})

airgram.updates.on(UPDATE.updateNewChat, async ({ update }) => {
  const { chat } = update
  console.info('isBasicGroup: ', chat.isBasicGroup)
  console.info('isSupergroup: ', chat.isSupergroup)
  console.info('isPrivateChat: ', chat.isPrivateChat)
  console.info('isSecretChat: ', chat.isSecretChat)
})

License

The source code is licensed under GPL v3. License is available here.

5.1.1

2 years ago

5.0.0

2 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.3.0

3 years ago

3.2.1

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0-next.10

4 years ago

2.2.0-next.9

4 years ago

2.2.0-next.5

4 years ago

2.2.0-next.7

4 years ago

2.2.0

4 years ago

2.2.0-next.3

4 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.2-next.1

5 years ago

2.0.2-next.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-next.2

5 years ago

2.0.0-1

5 years ago

2.0.0-0

5 years ago

1.2.0

5 years ago