Skip to content

Commit

Permalink
refactor: remove HTTPError class and improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
iShibi committed Oct 24, 2021
1 parent 16ae503 commit 513a47d
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/rest/APIRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class APIRequest {
client: Client;
isStreaming?: boolean;

constructor(rest: RESTManager, method: string, path: string, options: ExtendedRequestData<string, unknown>) {
constructor(rest: RESTManager, method: string, path: string, options: ExtendedRequestData<unknown, unknown>) {
this.rest = rest;
this.method = method;
this.path = path;
Expand All @@ -24,7 +24,7 @@ export class APIRequest {
this.client = rest.client;
this.isStreaming = options.isStreaming;

if (options.query) {
if (options.query && typeof options.query === 'object') {
const queryString = Object.entries(options.query)
.filter(([, value]) => value !== null && typeof value !== 'undefined')
.map(([key, value]) => (Array.isArray(value) ? `${key}=${value.join(',')}` : `${key}=${value}`))
Expand Down
2 changes: 1 addition & 1 deletion src/rest/APIRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function buildRoute(manager: RESTManager): any {
routeBucket.push(currentDirectory);
}
}
return (options: RequestData<string, unknown>) =>
return (options: RequestData<unknown, unknown>) =>
manager.request(property, path.join('/'), Object.assign({ route: routeBucket.join('/') }, options));
}
path.push(property);
Expand Down
28 changes: 0 additions & 28 deletions src/rest/HTTPError.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/rest/RESTManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class RESTManager {
async request(
method: string,
path: string,
options: ExtendedRequestData<string, unknown>,
options: ExtendedRequestData<unknown, unknown>,
): Promise<Record<string, unknown> | ArrayBuffer | Response> {
const apiRequest = new APIRequest(this, method, path, options);
let handler = this.requestHandlers.get(apiRequest.route);
Expand Down
24 changes: 2 additions & 22 deletions src/rest/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { errors } from 'undici';
import { HTTPError } from './HTTPError';
import { setTimeout } from 'timers/promises';
import { AsyncQueue } from '@sapphire/async-queue';
import { TwitterAPIError } from './TwitterAPIError';
Expand Down Expand Up @@ -35,16 +33,7 @@ export class RequestHandler {
}

async execute(request: APIRequest): Promise<Record<string, unknown> | ArrayBuffer | Response> {
let res: Response;
try {
res = await request.make();
} catch (error) {
if (error instanceof errors.UndiciError) {
throw new HTTPError(request.method, error.message, error.name, request.path);
} else {
throw error;
}
}
const res = await request.make();

if (res.ok) {
if (request.isStreaming) return res;
Expand Down Expand Up @@ -73,16 +62,7 @@ export class RequestHandler {
await setTimeout(20000); // TODO: Remove this once the latency issue has been fixed
return this.execute(request);
}
let apiError: APIProblem;
try {
apiError = (await parseResponse(res)) as APIProblem;
} catch (error) {
if (error instanceof errors.UndiciError) {
throw new HTTPError(request.method, error.message, error.name, request.path);
} else {
throw error;
}
}
const apiError = (await parseResponse(res)) as APIProblem;
throw new TwitterAPIError(apiError);
}
}
Expand Down

0 comments on commit 513a47d

Please sign in to comment.