2.1.2 • Published 5 years ago
@airgram-dev/use-models v2.1.2
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 { 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.
2.2.0-next.1
5 years ago
2.2.0-next.0
5 years ago
2.1.3-next.6
5 years ago
2.1.3-next.5
5 years ago
2.1.3-next.3
5 years ago
2.1.3-next.0
5 years ago
2.1.2
5 years ago
2.1.2-next.0
5 years ago
2.1.1
5 years ago
2.1.1-next.0
5 years ago
2.0.2
5 years ago
1.3.0-next.2
5 years ago
2.0.5-next.4
5 years ago
2.0.5-next.3
5 years ago
2.0.5-next.0
5 years ago
2.0.4
5 years ago
2.0.3
5 years ago
2.0.3-next.1
5 years ago
2.0.3-next.0
5 years ago