Skip to content

Commit

Permalink
feat: add users api
Browse files Browse the repository at this point in the history
  • Loading branch information
zSoulweaver committed Feb 20, 2025
1 parent 7e15d8a commit 85570b6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/get-categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const kient = new Kient()
kient.setAuthToken(env.KICK_TOKEN as string)
const res = await kient.api.categories.query('fortnite')

console.log(res[0].toObject())
console.log(res[0].toJSON())
14 changes: 14 additions & 0 deletions example/get-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { env } from 'bun'
import { Kient } from 'kient'

const kient = new Kient()
kient.setAuthToken(env.KICK_TOKEN as string)

const currentUser = await kient.api.users.getAuthorisedUser()
console.log(currentUser.toJSON())

const multipleUsers = await kient.api.users.getByIds([1, 2, 3])
console.log(multipleUsers[0].toJSON())

const specificUser = await kient.api.users.getById(2)
console.log(specificUser.toJSON())
22 changes: 22 additions & 0 deletions src/api/users/get-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cast } from '@deepkit/type'
import type { Kient } from 'kient'
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
import { APIResponse } from '../../util/api-response'
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
import { User } from '../../structures/user'

export async function getUsersByIds(kient: Kient, ids?: number[]) {
const params = new URLSearchParams()
for (const id of ids || []) {
params.append('id', id.toString())
}
const response = await kient._apiClient.fetch<APIResponse<User[]>>(`/users?${params}`)

const responseKientInjected = response.data.map((user) => ({
...user,
kient,
}))

const typedResponse = cast<User[]>(responseKientInjected)
return typedResponse
}
34 changes: 34 additions & 0 deletions src/api/users/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { APIBase } from '../api-base'
import { getUsersByIds } from './get-users'

/**
* Description placeholder
*
* @group APIs
*/
export class UsersAPI extends APIBase {
/**
* Returns an array of users by an array of IDs
*
* @param {number[]} ids Accepts an user IDs that will be queried for
*/
getByIds(ids: number[]) {
return getUsersByIds(this.kient, ids)
}

/**
* Returns a singular user by ID
*
* @param {number} id Accepts a user ID that will be queried for
*/
async getById(id: number) {
return (await getUsersByIds(this.kient, [id]))[0]
}

/**
* Returns the currently authorised user's details
*/
async getAuthorisedUser() {
return (await getUsersByIds(this.kient))[0]
}
}
2 changes: 2 additions & 0 deletions src/kient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { APIClient, type APIClientOptions } from './api.client'
import type { KientEventEmitters } from './events'
import { WSClient, type WSClientOptions } from './ws.client'
import { CategoriesAPI } from './api/categories'
import { UsersAPI } from './api/users'

type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T

Expand Down Expand Up @@ -54,5 +55,6 @@ export class Kient extends EventEmitter<KientEventEmitters> {

api = {
categories: new CategoriesAPI(this),
users: new UsersAPI(this),
}
}
6 changes: 1 addition & 5 deletions src/structures/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export class Category {
public thumbnail: string,
) {}

toObject(): KientObject<Category> {
toJSON(): KientObject<Category> {
return serialize<Category>(this, { groupsExclude: ['exclude'] })
}

toJSON() {
return JSON.stringify(serialize<Category>(this, { groupsExclude: ['exclude'] }))
}
}
13 changes: 7 additions & 6 deletions src/structures/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// biome-ignore lint/style/useImportType: deepkit/type
import { Group, serialize } from '@deepkit/type'
import { Group, serialize, MapName } from '@deepkit/type'
import type { Kient } from '../kient'
import type { KientObject } from '../util/kient-object'

/**
* Data structure of a user
Expand All @@ -13,13 +14,13 @@ export class User {
/** @internal */
public kient: Kient & Group<'exclude'>,

public id: string,
public username: string,
public is_verified: boolean,
public profile_picture: string,
public id: string & MapName<'user_id'>,
public name: string,
public email: string,
public profilePicture: string & MapName<'profile_picture'>,
) {}

toJSON() {
toJSON(): KientObject<User> {
return serialize<User>(this, { groupsExclude: ['exclude'] })
}
}

0 comments on commit 85570b6

Please sign in to comment.