Skip to content

Commit

Permalink
refactor: use classes to expose api funcs
Browse files Browse the repository at this point in the history
- allows for jsdoc comments to show in ide, arrow functions were stripping these
  • Loading branch information
zSoulweaver committed Jul 29, 2024
1 parent c273848 commit 6bbfceb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/get-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Kient } from 'kient'
import { inspect } from 'bun'

const kient = new Kient()
const channel = await kient.api.getChannel('channel_01GFZ7V9JR2H2AM2B3ZP7EDVKF')
const channel = await kient.api.channel.get('channel_01GFZ7V9JR2H2AM2B3ZP7EDVKF')
console.log(
inspect(channel, {
colors: true,
Expand Down
9 changes: 9 additions & 0 deletions src/api/api-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Kient } from '../kient'

export class APIBase {
readonly kient: Kient

constructor(kient: Kient) {
this.kient = kient
}
}
13 changes: 13 additions & 0 deletions src/api/channels/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { APIBase } from '../api-base'
import { getChannel } from './get-channel'

export class ChannelsAPI extends APIBase {
/**
* Returns channel and user information of the specified channel
*
* @param {string} slugOrId Accepts either a channel slug, i.e `xqc` or, an id such as `channel_ULID`
*/
get(slugOrId: string) {
return getChannel(this.kient, slugOrId)
}
}
4 changes: 2 additions & 2 deletions src/kient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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'
import { ChannelsAPI } from './api/channels'

type DeepRequired<T> = {
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>>
Expand Down Expand Up @@ -45,6 +45,6 @@ export class Kient extends EventEmitter<KientEventEmitters> {
}

api = {
getChannel: (slugOrId: string) => getChannel(this, slugOrId),
channel: new ChannelsAPI(this),
}
}

0 comments on commit 6bbfceb

Please sign in to comment.