Skip to content

Commit

Permalink
chore: fix rawREST event and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Oct 27, 2023
1 parent a44ced2 commit 56c27a7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
15 changes: 7 additions & 8 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IncomingMessage } from 'http';
import type { SlashCommand } from './command';
import type { CommandContext } from './structures/interfaces/commandContext';
import type { RespondFunction, TransformedRequest } from './server';
Expand All @@ -7,6 +6,7 @@ import type { MessageData } from './structures/message';
import type { AutocompleteContext } from './structures/interfaces/autocompleteContext';
import type { ModalInteractionContext } from './structures/interfaces/modalInteractionContext';
import type { BaseSlashCreator } from './creator';
import type { FileContent } from './rest/requestHandler';

export const VERSION: string = require('../package.json').version;

Expand Down Expand Up @@ -298,14 +298,13 @@ export interface GuildApplicationCommandPermissions extends PartialApplicationCo

/** @private */
export interface RawRequest {
method: string;
url: string;
auth: boolean;
body: any;
route: string;
reason?: string;
short: boolean;
resp: IncomingMessage;
body: Record<string, any> | undefined;
files: FileContent[] | undefined;
latency: number;
method: string;
response: Response;
url: URL;
}

/** Any interaction request from Discord. */
Expand Down
2 changes: 1 addition & 1 deletion src/rest/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface RequestOptions {
reason?: string;
}

/** @hidden */
/** @private */
export interface FileContent {
file: any;
name: string;
Expand Down
21 changes: 9 additions & 12 deletions src/rest/sequentialBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,15 @@ export class SequentialBucket {
return this.#execute(request, ++attempts);
}

// TODO replace
// if (this.#handler.rest.listenerCount('response')) {
// this.#handler.rest.emit('response', {
// auth: request.options.auth ?? false,
// body: request.options.body,
// files: request.options.files,
// latency: latency,
// method: request.method,
// response: res.clone(),
// url: request.url
// });
// }
this.#handler.creator?.emit('rawREST', {
auth: request.options.auth ?? false,
body: request.options.body,
files: request.options.files,
latency: latency,
method: request.method,
response: res.clone() as any,
url: request.url
});

const retryAfter = this.#handle(request, res, latency);
this.#handler.creator?.emit(
Expand Down
11 changes: 9 additions & 2 deletions src/servers/cfworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ export class CloudflareWorkerServer extends Server {
super({ alreadyListening: true });
}

cfEndpoint = async (request: WorkerRequest, env: Record<string, object>, ctx: ExecutionContext) => {
/**
* The fetch handler for the server. Export this as your fetch handler to utilize this server.
* @example
* export const workerServer = new CloudflareWorkerServer();
* creator.withServer(workerServer);
* export default { fetch: workerServer.fetchHandler }
*/
readonly fetchHandler = async (request: WorkerRequest, env: Record<string, object>, ctx: ExecutionContext) => {
if (!this._handler) return new Response('Server has no handler.', { status: 503 });
if (request.method !== 'POST') return new Response('Server only supports POST requests.', { status: 405 });
const body = await request.text();
Expand Down Expand Up @@ -50,7 +57,7 @@ export class CloudflareWorkerServer extends Server {
}
).catch(reject)
);
});
}) as Promise<Response>;
};

/** @private */
Expand Down

0 comments on commit 56c27a7

Please sign in to comment.