import type { RawFile, RequestData } from '@discordjs/rest'; import { REST } from '@discordjs/rest'; import type { Identify } from '@biscuitland/common'; import type { RequestMethod } from './Router'; export class BiscuitREST { api: REST; constructor(public options: BiscuitRESTOptions) { const { token, ...restOptions } = this.options; this.api = new REST(restOptions).setToken(token); } async get(route: string, options?: RequestObject): Promise { const data = await this.api.get(route as `/${string}`, { ...options, query: options?.query ? new URLSearchParams(options.query) : undefined }); return data as T; } async post(route: string, body?: RequestObject): Promise { const data = await this.api.post(route as `/${string}`, { ...body, body: body?.body, query: body?.query ? new URLSearchParams(body.query) : undefined, files: body?.files }); return data as T; } async put(route: string, body?: RequestObject): Promise { const data = await this.api.put(route as `/${string}`, { ...body, body: body?.body, query: body?.query ? new URLSearchParams(body.query) : undefined, files: body?.files }); return data as T; } async patch(route: string, body?: RequestObject): Promise { const data = await this.api.patch(route as `/${string}`, { ...body, body: body?.body, query: body?.query ? new URLSearchParams(body.query) : undefined, files: body?.files }); return data as T; } async delete(route: string, options?: RequestObject): Promise { const data = await this.api.delete(route as `/${string}`, { ...options, query: options?.query ? new URLSearchParams(options.query) : undefined }); return data as T; } } export type BiscuitRESTOptions = Identify[0] & { token: string }>; export type RequestOptions = Pick; export type RequestObject, Q = Record> = { query?: Q; } & RequestOptions & (M extends `${RequestMethod.Get}` ? unknown : { body?: B; files?: RawFile[]; }); export type RestArguments = any> = M extends RequestMethod.Get ? Q extends never ? RequestObject : never : RequestObject;