Skip to content

Commit

Permalink
refactor: export kient properly
Browse files Browse the repository at this point in the history
  • Loading branch information
zSoulweaver committed Jul 29, 2024
1 parent f983940 commit c273848
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion example/get-channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Kient from 'Kient'
import { Kient } from 'kient'
import { inspect } from 'bun'

const kient = new Kient()
Expand Down
2 changes: 1 addition & 1 deletion example/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Kient from 'Kient'
import { Kient } from 'kient'
import { Events } from '../src/events'

// const kient = new Kient({
Expand Down
2 changes: 1 addition & 1 deletion src/api/channels/get-channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cast } from '@deepkit/type'
import type Kient from 'Kient'
import type { Kient } from 'kient'
import { ofetch } from 'ofetch'
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
import { CommonAPIResponse, isSuccessResponse } from '../../util/common-api-response'
Expand Down
51 changes: 1 addition & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1 @@
import defu from 'defu'
import { getChannel } from './api/channels/get-channel'
import { WSClient } from './ws.client'
import { EventEmitter } from 'tseep'
import type { KientEventEmitters } from './events'

type DeepRequired<T> = {
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>>
}

export interface KientOptions {
connectToWebsocket?: boolean
pusher?: {
appKey?: string
cluster?: string
}
}

const defaultKientOptions: DeepRequired<KientOptions> = {
connectToWebsocket: true,
pusher: {
appKey: '32cbd69e4b950bf97679',
cluster: 'us2',
},
}

export default class Kient extends EventEmitter<KientEventEmitters> {
private readonly kientOptions!: DeepRequired<KientOptions>
private wsClient?: WSClient

constructor(options?: KientOptions) {
super()
this.kientOptions = defu(options, defaultKientOptions)

this.kientOptions.connectToWebsocket ? this.connectWebsocket() : null
}

connectWebsocket() {
this.wsClient = new WSClient(this, {
pusher: {
appKey: this.kientOptions.pusher.appKey,
cluster: this.kientOptions.pusher.cluster,
},
})
}

api = {
getChannel: (slugOrId: string) => getChannel(this, slugOrId),
}
}
export { Kient } from './kient'
50 changes: 50 additions & 0 deletions src/kient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import defu from 'defu'
import { getChannel } from './api/channels/get-channel'
import { WSClient } from './ws.client'
import { EventEmitter } from 'tseep'
import type { KientEventEmitters } from './events'

type DeepRequired<T> = {
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>>
}

export interface KientOptions {
connectToWebsocket?: boolean
pusher?: {
appKey?: string
cluster?: string
}
}

const defaultKientOptions: DeepRequired<KientOptions> = {
connectToWebsocket: true,
pusher: {
appKey: '32cbd69e4b950bf97679',
cluster: 'us2',
},
}

export class Kient extends EventEmitter<KientEventEmitters> {
private readonly kientOptions!: DeepRequired<KientOptions>
private wsClient?: WSClient

constructor(options?: KientOptions) {
super()
this.kientOptions = defu(options, defaultKientOptions)

this.kientOptions.connectToWebsocket ? this.connectWebsocket() : null
}

connectWebsocket() {
this.wsClient = new WSClient(this, {
pusher: {
appKey: this.kientOptions.pusher.appKey,
cluster: this.kientOptions.pusher.cluster,
},
})
}

api = {
getChannel: (slugOrId: string) => getChannel(this, slugOrId),
}
}
2 changes: 1 addition & 1 deletion src/structures/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Kient from 'Kient'
import type { Kient } from '../kient'

// biome-ignore lint/suspicious/noExplicitAny: Decorator
export function Expose(target: any, propertyKey: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/ws.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Kient from 'Kient'
import type { Kient } from 'kient'
import Pusher, { type Channel, type Options } from 'pusher-js'
import { Events } from './events'

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"paths": {
"Kient": [
"kient": [
"./src/index.ts"
]
},
Expand Down

0 comments on commit c273848

Please sign in to comment.