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

Allow typing the body of a RequestError response #2312

Closed
1 task done
dcaldastp opened this issue Nov 29, 2023 · 0 comments · Fixed by #2325
Closed
1 task done

Allow typing the body of a RequestError response #2312

dcaldastp opened this issue Nov 29, 2023 · 0 comments · Fixed by #2325
Labels
enhancement This change will extend Got features good for beginner This issue is easy to fix ✭ help wanted ✭

Comments

@dcaldastp
Copy link

What problem are you trying to solve?

I want to type the response body of a RequestError. Currently because RequestError does not accept a generic it's impossible to type the underlying response body.

export declare class RequestError extends Error {
    code?: string;
    stack: string;
    readonly options: NormalizedOptions;
    readonly response?: Response;
    readonly request?: Request;
    readonly timings?: Timings;
    constructor(message: string, error: Partial<Error & {
        code?: string;
    }>, self: Request | NormalizedOptions);
}

A valid workaround at the moment is something like:

type MyErrorBodyType = { reason: string; code: number; metadata: any }

type MyRequestError = Omit<RequestError, 'response'> & {
  response: Response<MyErrorBodyType>
}

This way ☝️ the body would be typed properly.

Describe the feature

I would like to be able to inject a type to the Request, like so 👇

export declare class RequestError<T> extends Error {
    code?: string;
    stack: string;
    readonly options: NormalizedOptions;
    readonly response?: Response<T>;
    readonly request?: Request;
    readonly timings?: Timings;
    constructor(message: string, error: Partial<Error & {
        code?: string;
    }>, self: Request | NormalizedOptions);
}

whereby T is the type fo pass along to the respective response body.

Checklist

  • I have read the documentation and made sure this feature doesn't already exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features good for beginner This issue is easy to fix ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants