Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: specify types from the undici package #649

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/api/src/lib/structures/api/ApiRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isNullishOrEmpty } from '@sapphire/utilities';
import type { Blob } from 'node:buffer';
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 +90,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 +99,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 +108,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 +124,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 +134,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 +143,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 +164,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 +184,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
27 changes: 14 additions & 13 deletions packages/api/src/lib/utils/_body/RequestProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { isNullish, isNullishOrEmpty } from '@sapphire/utilities';
import { Blob } from 'node:buffer';
import { arrayBuffer } from 'node:stream/consumers';
import { ReadableStream } from 'node:stream/web';
import type {
FormData,
Headers,
ReferrerPolicy,
Request,
RequestCache,
RequestCredentials,
RequestDestination,
RequestMode,
RequestRedirect
import {
Response,
type FormData,
type Headers,
type ReferrerPolicy,
type Request,
type RequestCache,
type RequestCredentials,
type RequestDestination,
type RequestMode,
type RequestRedirect
} from 'undici';
import type { ApiRequest } from '../../structures/api/ApiRequest';
import type { MethodName } from '../../structures/http/HttpMethods';
Expand Down Expand Up @@ -57,15 +58,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 +114,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