Marcos Susaña 87cdad0b03
Next: A complete Biscuit rewrite (#131)
* FINALLY WE REACHED AN AGREEMENT

* chore: d-types adapt

* websocket sucks, rest

* changes

* new look for core

* 💀

* fix cdn routes, more structures

Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>

* CDN routes

* chore: change to rome

Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>

* Oh shit, here we go again

Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>

* fixes

* mixin, handler events, ws
Co-authored-by: Yuzu
<yuzudev@users.noreply.github.com>
Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>

* change type

* Error system (#133)

* Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>

* chore: biscuit rebase

* token leak goes brrrr

* fix: events

* chore: road to raw data

* fix: managers typing

* chore: fix gateway typing

* feat: helpers

* style: linter

* Types for routes (#134)

* typing for routes

* managers

Co-authored-by: Marcos Susaña <marcosjgs03@gmail.com>

* Types for routes (#134)

* I wanna cry

* Next (#136)

* Merge #137

* chore: lineWidth to 140

* chore: README update

---------

Co-authored-by: Yuzu <yuzuru@programmer.net>
Co-authored-by: Free 公園 <FreeAoi@users.noreply.github.com>
Co-authored-by: ThisIsAName <46913407+NejireSupremacy@users.noreply.github.com>
Co-authored-by: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com>
Co-authored-by: MARCROCK22 <marcos22dev@gmail.com>
2023-05-29 22:34:47 -04:00

83 lines
2.5 KiB
TypeScript

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<T>(route: string, options?: RequestObject<RequestMethod.Get>): Promise<T> {
const data = await this.api.get(route as `/${string}`, {
...options,
query: options?.query ? new URLSearchParams(options.query) : undefined
});
return data as T;
}
async post<T>(route: string, body?: RequestObject<RequestMethod.Post>): Promise<T> {
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<T>(route: string, body?: RequestObject<RequestMethod.Put>): Promise<T> {
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<T>(route: string, body?: RequestObject<RequestMethod.Patch>): Promise<T> {
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<T>(route: string, options?: RequestObject<RequestMethod.Delete>): Promise<T> {
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<ConstructorParameters<typeof REST>[0] & { token: string }>;
export type RequestOptions = Pick<RequestData, 'passThroughBody' | 'reason'>;
export type RequestObject<M extends RequestMethod, B = Record<string, any>, Q = Record<string, any>> = {
query?: Q;
} & RequestOptions &
(M extends `${RequestMethod.Get}`
? unknown
: {
body?: B;
files?: RawFile[];
});
export type RestArguments<M extends RequestMethod, B = any, Q extends never | Record<string, any> = any> = M extends RequestMethod.Get
? Q extends never
? RequestObject<M, never, B>
: never
: RequestObject<M, B, Q>;