@@ -2,58 +2,73 @@ import defu from 'defu'
22import { EventEmitter } from 'tseep'
33import { APIClient , type APIClientOptions } from './api.client'
44import type { KientEventEmitters } from './events'
5- import { WSClient , type WSClientOptions } from './ws.client'
65import { CategoryAPI } from './api/category'
76import { UserAPI } from './api/user'
87import { ChannelAPI } from './api/channel'
98import { ChatAPI } from './api/chat'
109import { MiscAPI } from './api/misc'
10+ import { WebhookAPI } from './api/webhook'
11+ import { WebhookServer } from './webhook.server'
12+ import { WebhookHandler } from './webhook.handler'
13+ import type { WebhookEvent } from './structures/base-event'
1114
1215type DeepPartial < T > = T extends object ? { [ P in keyof T ] ?: DeepPartial < T [ P ] > } : T
1316
1417export interface KientOptions {
15- connectToWebsocket : boolean
1618 apiClient : APIClientOptions
17- wsClient : WSClientOptions
19+ webhookServer : {
20+ enable : boolean
21+ }
1822}
1923
2024const defaultKientOptions : KientOptions = {
21- connectToWebsocket : true ,
2225 apiClient : {
2326 ofetch : {
2427 baseURL : 'https://api.kick.com/public/v1' ,
2528 } ,
2629 } ,
27- wsClient : {
28- pusher : {
29- appKey : '32cbd69e4b950bf97679' ,
30- cluster : 'us2' ,
31- } ,
30+ webhookServer : {
31+ enable : true ,
3232 } ,
3333}
3434
3535export class Kient extends EventEmitter < KientEventEmitters > {
3636 private readonly kientOptions : KientOptions
37- _wsClient ?: WSClient
37+ private _webhookServer ?: WebhookServer
38+ _webhookHandler : WebhookHandler
3839 _apiClient : APIClient
40+ _kickPublicKey ?: string
3941
4042 constructor ( options ?: DeepPartial < KientOptions > ) {
4143 super ( )
4244 this . kientOptions = defu ( options as KientOptions , defaultKientOptions )
4345
44- this . kientOptions . connectToWebsocket ? this . connectWebsocket ( ) : null
46+ if ( this . kientOptions . webhookServer . enable ) {
47+ this . createWebhookServer ( )
48+ }
4549
4650 this . _apiClient = new APIClient ( this , this . kientOptions . apiClient )
51+ this . _webhookHandler = new WebhookHandler ( this )
4752 }
4853
49- connectWebsocket ( ) {
50- this . _wsClient = new WSClient ( this , this . kientOptions . wsClient )
54+ createWebhookServer ( ) {
55+ this . _webhookServer = new WebhookServer ( this )
5156 }
5257
53- setAuthToken ( token : string ) {
58+ async setAuthToken ( token : string ) {
5459 this . _apiClient . setHeaders ( {
5560 Authorization : `Bearer ${ token } ` ,
5661 } )
62+
63+ this . _kickPublicKey = await this . api . misc . getPublicKey ( )
64+ }
65+
66+ get webhookServerFetch ( ) {
67+ return this . _webhookServer ?. fetch
68+ }
69+
70+ handleWebhookEvent ( event : WebhookEvent ) {
71+ this . _webhookHandler . handleEvent ( event )
5772 }
5873
5974 api = {
@@ -62,5 +77,6 @@ export class Kient extends EventEmitter<KientEventEmitters> {
6277 user : new UserAPI ( this ) ,
6378 channel : new ChannelAPI ( this ) ,
6479 chat : new ChatAPI ( this ) ,
80+ webhook : new WebhookAPI ( this ) ,
6581 }
6682}
0 commit comments