Skip to content

Commit

Permalink
fix: specify types from the undici package
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Oct 27, 2024
1 parent 518ac87 commit 40b42c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions packages/api/src/lib/structures/api/ApiRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isNullishOrEmpty } from '@sapphire/utilities';
import { IncomingMessage } from 'node:http';
import type { FormData, Request } from 'undici';
import type { MimeType } from '../../utils/MimeType';
import { RequestProxy } from '../../utils/_body/RequestProxy';
import type { Route } from '../Route';
Expand Down Expand Up @@ -88,7 +89,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBody() {
public readBody(): Promise<unknown> {
return this.#isFormContentType ? this.readBodyFormData() : this.readBodyJson();
}

Expand All @@ -97,7 +98,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBodyArrayBuffer() {
public readBodyArrayBuffer(): Promise<ArrayBuffer> {
return this.asWeb().arrayBuffer();
}

Expand All @@ -106,7 +107,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBodyBlob() {
public readBodyBlob(): Promise<Blob> {
return this.asWeb().blob();
}

Expand All @@ -122,7 +123,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBodyFormData() {
public readBodyFormData(): Promise<FormData> {
return this.asWeb().formData(); // NOSONAR
}

Expand All @@ -132,7 +133,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBodyJson() {
public readBodyJson(): Promise<unknown> {
return this.asWeb().json();
}

Expand All @@ -141,7 +142,7 @@ export class ApiRequest extends IncomingMessage {
*
* @returns The result of the body parsing
*/
public readBodyText() {
public readBodyText(): Promise<string> {
return this.asWeb().text();
}

Expand All @@ -162,7 +163,7 @@ export class ApiRequest extends IncomingMessage {
* @param validator The validator function to use on the body parsing result
* @returns The validated body
*/
public readValidatedBodyFormData<Type extends FormData>(validator: ValidatorFunction<FormData, Type>) {
public readValidatedBodyFormData<Type>(validator: ValidatorFunction<FormData, Type>) {
return this.readBodyFormData().then(validator);
}

Expand All @@ -182,9 +183,9 @@ export class ApiRequest extends IncomingMessage {
* @param validator The validator function to use on the body parsing result
* @returns The validated body
*/
public readValidatedBodyText<Type extends string>(validator: ValidatorFunction<string, Type>) {
public readValidatedBodyText<Type>(validator: ValidatorFunction<string, Type>) {
return this.readBodyText().then(validator);
}
}

export type ValidatorFunction<Data, Type extends Data> = (data: Data) => Type | ((data: Data) => data is Type);
export type ValidatorFunction<Data, Type> = (data: Data) => Type;
2 changes: 1 addition & 1 deletion packages/api/src/lib/utils/_body/RequestHeadersProxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isNullishOrEmpty } from '@sapphire/utilities';
import { splitSetCookieString } from 'cookie-es';
import type { Headers, SpecIterableIterator } from 'undici-types';
import type { Headers, SpecIterableIterator } from 'undici';
import type { ApiRequest } from '../../structures/api/ApiRequest';
import { NodeUtilInspectSymbol } from '../constants';

Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/lib/utils/_body/RequestProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export class RequestProxy implements Request {
return this.#cachedMethod;
}

public get signal() {
public get signal(): AbortSignal {
this.#abortController ??= new AbortController();
return this.#abortController.signal;
}

public get body(): ReadableStream<Uint8Array> | null {
if (!this.hasBody) return null;

this.#bodyStream ??= new ReadableStream({
this.#bodyStream ??= new ReadableStream<Uint8Array>({
start: (controller) => {
this.#request
.on('data', (chunk) => controller.enqueue(chunk))
Expand Down Expand Up @@ -113,7 +113,7 @@ export class RequestProxy implements Request {
return new RequestProxy(this.#request);
}

private get hasBody() {
private get hasBody(): boolean {
if (this.#cachedHasBody !== null) return this.#cachedHasBody;

const contentLengthString = this.headers.get('content-length');
Expand Down

0 comments on commit 40b42c4

Please sign in to comment.