Skip to content

Commit f983940

Browse files
committed
feat: add kient instance to structures
1 parent d2ed258 commit f983940

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// biome-ignore lint/style/useImportType: deepkit/type
2-
import { Channel } from '../../structures/channel'
3-
// biome-ignore lint/style/useImportType: deepkit/type
4-
import { User } from '../../structures/user'
2+
import { JSONObject } from '../../util/json-object.type'
53

64
export interface ChannelResponse {
75
account: {
8-
user: User
9-
channel: Channel
6+
user: JSONObject
7+
channel: JSONObject
108
}
119
}

src/api/channels/get-channel.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { cast } from '@deepkit/type'
22
import type Kient from 'Kient'
33
import { ofetch } from 'ofetch'
4-
// biome-ignore lint/style/useImportType: CommonAPIResponse used for deepkit/type runtime type information
4+
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
55
import { CommonAPIResponse, isSuccessResponse } from '../../util/common-api-response'
6-
// biome-ignore lint/style/useImportType: CommonAPIResponse used for deepkit/type runtime type information
6+
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
77
import { ChannelResponse } from './get-channel.response'
8+
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
9+
import { Channel } from '../../structures/channel'
10+
// biome-ignore lint/style/useImportType: deepkit/type runtime type information
11+
import { User } from '../../structures/user'
812

913
export async function getChannel(kient: Kient, slugOrId: string) {
1014
const response = await ofetch<CommonAPIResponse<ChannelResponse>>(
@@ -15,5 +19,11 @@ export async function getChannel(kient: Kient, slugOrId: string) {
1519
if (!isSuccessResponse(typedResponse)) {
1620
throw new Error(`Request failed: ${typedResponse.data}`)
1721
}
18-
return typedResponse
22+
23+
const channel = cast<Channel>({ ...typedResponse.data.account.channel, kient })
24+
const user = cast<User>({ ...typedResponse.data.account.user, kient })
25+
return {
26+
channel: channel,
27+
user: user,
28+
}
1929
}

src/structures/base.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type Kient from 'Kient'
2+
3+
// biome-ignore lint/suspicious/noExplicitAny: Decorator
4+
export function Expose(target: any, propertyKey: any) {
5+
if (!target.constructor.exposedProperties) {
6+
target.constructor.exposedProperties = []
7+
}
8+
target.constructor.exposedProperties.push(propertyKey)
9+
}
10+
11+
export class Base {
12+
kient!: Kient
13+
14+
raw(): Partial<Base> {
15+
const rawData: Partial<Base> = {}
16+
// biome-ignore lint/suspicious/noExplicitAny: Decorator magic
17+
const exposedProperties = (this.constructor as any).exposedProperties as string[]
18+
19+
for (const prop of exposedProperties) {
20+
rawData[prop as keyof Omit<Base, 'kient'>] = this[prop as keyof Omit<Base, 'kient'>]
21+
}
22+
23+
return rawData
24+
}
25+
26+
toJSON() {
27+
return this.raw()
28+
}
29+
}

src/structures/channel.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export interface Channel {
2-
id: string
3-
slug: string
4-
banner_picture: string
5-
description: string
6-
followers_count: number
1+
import { Base, Expose } from './base'
2+
3+
export class Channel extends Base {
4+
@Expose id!: string
5+
@Expose slug!: string
6+
@Expose banner_picture!: string
7+
@Expose description!: string
8+
@Expose followers_count!: number
79
}

src/structures/user.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
export interface User {
2-
id: string
3-
username: string
4-
is_verified: boolean
5-
profile_picture: string
1+
import { Base, Expose } from './base'
2+
3+
export class User extends Base {
4+
@Expose id!: string
5+
@Expose username!: string
6+
@Expose is_verified!: boolean
7+
@Expose profile_picture!: string
68
}

src/util/json-object.type.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// biome-ignore lint/suspicious/noExplicitAny: deepkit/type validation
2+
export type JSONObject = Record<string | number | symbol, any>

0 commit comments

Comments
 (0)