Skip to content

Commit

Permalink
rest: some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniaky committed Sep 9, 2023
1 parent b6a5d5b commit c941290
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/rest/src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { RawFile } from "@discloudapp/util";
import { Agent, BodyInit, Dispatcher, File, request } from "undici";
import { Agent, BodyInit, Dispatcher, File, request, HeadersInit } from "undici";
import { RequestMethod } from "../@enum";

export type RouteLike = `/${string}`

export type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>
export type RequestOptions = Parameters<typeof request>[1]

export interface RESTOptions {
/**
Expand Down Expand Up @@ -52,7 +52,7 @@ export interface RequestData {
/**
* The body to send to this request.
*/
body?: BodyInit | unknown
body?: BodyInit
/**
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class REST extends EventEmitter {
* @param options - Request options
*/
raw(options: InternalRequest) {
const { fetchOptions, url } = this.requestManager.resolveRequest(options);
return this.requestManager.request(url, fetchOptions);
const request = this.requestManager.resolveRequest(options);
return this.requestManager.request(request.url, request.fetchOptions);
}
}
20 changes: 10 additions & 10 deletions packages/rest/src/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface RequestManager {

export class RequestManager extends EventEmitter {
#token!: string;
options: RESTOptions;
readonly options: RESTOptions;

/**
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
Expand Down Expand Up @@ -91,12 +91,10 @@ export class RequestManager extends EventEmitter {
resolveRequest(request: InternalRequest) {
const headers: RequestHeaders = Object.assign({}, this.options.headers, { "api-token": this.#token });

const query = request.query?.toString() ? `?${request.query}` : "";

const url = `${this.options.api}/v${this.options.version}${request.fullRoute}${query}`;
const url = `${this.options.api}/v${this.options.version}${request.fullRoute}`;

const additionalHeaders: Record<string, string> = {};
const additionalOptions: Partial<RequestOptions> = {};
const additionalOptions: Partial<RequestOptions> = { query: request.query };
const formData = new FormData();

if (request.file) {
Expand All @@ -119,12 +117,12 @@ export class RequestManager extends EventEmitter {

const fetchOptions: RequestOptions = Object.assign({
headers: Object.assign({}, request.headers, additionalHeaders, headers),
method: request.method.toUpperCase() as Dispatcher.HttpMethod,
method: request.method.toUpperCase(),
}, additionalOptions);

if (request.body)
if (request.file) {
for (const [key, value] of Object.entries(request.body as Record<string, unknown>))
for (const [key, value] of Object.entries(request.body))
formData.append(key, value);
} else {
fetchOptions.body = JSON.stringify(request.body);
Expand All @@ -139,6 +137,8 @@ export class RequestManager extends EventEmitter {
}

async request(url: string, options: RequestOptions) {
if (!options) options = {};

while (this.globalLimited) {
this.emit(RESTEvents.RateLimited, <RateLimitData>{
global: this.globalLimited,
Expand All @@ -156,9 +156,9 @@ export class RequestManager extends EventEmitter {
const limit = Number(res.headers["ratelimit-limit"]);
const remaining = Number(res.headers["ratelimit-remaining"]);
const reset = Number(res.headers["ratelimit-reset"]);
if (!isNaN(limit)) this.globalLimit = Number(res.headers["ratelimit-limit"]);
if (!isNaN(remaining)) this.globalRemaining = Number(res.headers["ratelimit-remaining"]);
if (!isNaN(reset)) this.globalReset = Number(res.headers["ratelimit-reset"]);
if (!isNaN(limit)) this.globalLimit = limit;
if (!isNaN(remaining)) this.globalRemaining = remaining;
if (!isNaN(reset)) this.globalReset = reset;

if (this.globalLimited) {
this.emit(RESTEvents.RateLimited, <RateLimitData>{
Expand Down

0 comments on commit c941290

Please sign in to comment.