Skip to content

Commit

Permalink
Add debug listener to rest
Browse files Browse the repository at this point in the history
To use this you have to enable `useDebugRest` on the client options
  • Loading branch information
Didas-git committed Apr 13, 2024
1 parent 8163ac5 commit ad438b4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { REST } from "./http/rest.js";
import { CachingManager } from "./cache/manager.js";
import { DebugREST, REST } from "./http/rest.js";
import { WebSocketManager } from "#ws";

import {
Expand Down Expand Up @@ -49,7 +49,7 @@ export interface Client<T extends Transformers> {
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class Client<T extends Transformers = Transformers, C extends CacheManagerStructure = CacheManagerStructure> {
public readonly rest: REST = new REST();
public readonly rest: REST;
public readonly cache: C;

readonly #ws: WebSocketManager;
Expand All @@ -58,6 +58,7 @@ export class Client<T extends Transformers = Transformers, C extends CacheManage
protected readonly ready: boolean = false;

public constructor(options: BaseClientOptions<T>, debug?: DebugFunction) {
this.rest = options.useDebugRest === true ? new DebugREST(debug) : new REST();
this.cache = typeof options.caching?.manager !== "undefined" ? <C>options.caching.manager : <C><unknown> new CachingManager();
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.#debug = debug ?? (() => {});
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/enums/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export const enum DebugIdentifier {
ZombieConnection = "ZOMBIE",
AttemptingResume = "ATTEMPTING_RESUME",
UnknownCode = "UNKNOWN_CODE",
CompiledListeners = "LISTENERS"
CompiledListeners = "LISTENERS",
RESTCall = "REST"
}
25 changes: 24 additions & 1 deletion packages/core/src/http/rest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@ts-expect-error We don't want the package.json being added to dist
import packageJson from "../../package.json" with { type: "json" };
import { DebugIdentifier } from "#enums";

import type { AuditLogEvent, MFALevel, OnboardingMode, PrivacyLevel } from "#enums";

import type {
LocalizationGlobalApplicationCommandStructure,
LocalizationGuildApplicationCommandStructure,
Expand Down Expand Up @@ -68,6 +68,7 @@ import type {
EmojiStructure,
UserStructure,
RoleStructure,
DebugFunction,
ErrorMessage,
BanStructure,
ImageData
Expand Down Expand Up @@ -1196,3 +1197,25 @@ export class REST {
}
//#endregion
}

export class DebugREST extends REST {
readonly #debug: DebugFunction;

public constructor(debug?: DebugFunction, token?: string) {
super(token);
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.#debug = debug ?? (() => {});
}

public override async makeAPIRequest<T>(method: "GET" | "POST" | "PATCH" | "DELETE" | "PUT", path: string, data: FormData, reason?: string | undefined): Promise<T>;
public override async makeAPIRequest<T>(method: "GET" | "POST" | "PATCH" | "DELETE" | "PUT", path: string, data?: Record<string, any> | undefined, files?: Array<LilybirdAttachment> | undefined): Promise<T>;
public override async makeAPIRequest<T>(
method: "GET" | "POST" | "PATCH" | "DELETE" | "PUT",
path: string,
data?: FormData | Record<string, any> | undefined,
filesOrReason?: string | Array<LilybirdAttachment> | undefined
): Promise<T> {
this.#debug(DebugIdentifier.RESTCall, { method, path, data, filesOrReason });
return super.makeAPIRequest(<never>method, <never>path, <never> data, <never>filesOrReason);
}
}
1 change: 1 addition & 0 deletions packages/core/src/typings/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export interface BaseClientOptions<T extends Transformers> {
transformers?: T;
presence?: UpdatePresenceStructure;
caching?: (DefaultCache | ExternalCache | TransformersCache) & ApplyTransformers;
useDebugRest?: boolean;
setup?: (client: Client) => Awaitable<any>;
}

Expand Down

0 comments on commit ad438b4

Please sign in to comment.