Skip to content

Commit 599ae74

Browse files
committed
Code style
1 parent d83dadd commit 599ae74

File tree

6 files changed

+52
-71
lines changed

6 files changed

+52
-71
lines changed

source/core/errors.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ An error to be thrown when a request fails.
1717
Contains a `code` property with error class code, like `ECONNREFUSED`.
1818
*/
1919
export class RequestError<T = unknown> extends Error {
20+
override name = 'RequestError';
21+
code = 'ERR_GOT_REQUEST_ERROR';
2022
input?: string;
21-
22-
code: string;
2323
override stack!: string;
2424
declare readonly options: Options;
2525
readonly response?: Response<T>;
@@ -30,8 +30,10 @@ export class RequestError<T = unknown> extends Error {
3030
super(message, {cause: error});
3131
Error.captureStackTrace(this, this.constructor);
3232

33-
this.name = 'RequestError';
34-
this.code = error.code ?? 'ERR_GOT_REQUEST_ERROR';
33+
if (error.code) {
34+
this.code = error.code;
35+
}
36+
3537
this.input = (error as any).input;
3638

3739
if (isRequest(self)) {
@@ -73,14 +75,14 @@ An error to be thrown when the server redirects you more than ten times.
7375
Includes a `response` property.
7476
*/
7577
export class MaxRedirectsError extends RequestError {
78+
override name = 'MaxRedirectsError';
79+
override code = 'ERR_TOO_MANY_REDIRECTS';
7680
declare readonly response: Response;
7781
declare readonly request: Request;
7882
declare readonly timings: Timings;
7983

8084
constructor(request: Request) {
8185
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
82-
this.name = 'MaxRedirectsError';
83-
this.code = 'ERR_TOO_MANY_REDIRECTS';
8486
}
8587
}
8688

@@ -91,14 +93,14 @@ Includes a `response` property.
9193
// TODO: Change `HTTPError<T = any>` to `HTTPError<T = unknown>` in the next major version to enforce type usage.
9294
// eslint-disable-next-line @typescript-eslint/naming-convention
9395
export class HTTPError<T = any> extends RequestError<T> {
96+
override name = 'HTTPError';
97+
override code = 'ERR_NON_2XX_3XX_RESPONSE';
9498
declare readonly response: Response<T>;
9599
declare readonly request: Request;
96100
declare readonly timings: Timings;
97101

98102
constructor(response: PlainResponse) {
99103
super(`Request failed with status code ${response.statusCode} (${response.statusMessage!}): ${response.request.options.method} ${response.request.options.url!.toString()}`, {}, response.request);
100-
this.name = 'HTTPError';
101-
this.code = 'ERR_NON_2XX_3XX_RESPONSE';
102104
}
103105
}
104106

@@ -107,25 +109,29 @@ An error to be thrown when a cache method fails.
107109
For example, if the database goes down or there's a filesystem error.
108110
*/
109111
export class CacheError extends RequestError {
112+
override name = 'CacheError';
110113
declare readonly request: Request;
111114

112115
constructor(error: Error, request: Request) {
113116
super(error.message, error, request);
114-
this.name = 'CacheError';
115-
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_CACHE_ACCESS' : this.code;
117+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
118+
this.code = 'ERR_CACHE_ACCESS';
119+
}
116120
}
117121
}
118122

119123
/**
120124
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
121125
*/
122126
export class UploadError extends RequestError {
127+
override name = 'UploadError';
123128
declare readonly request: Request;
124129

125130
constructor(error: Error, request: Request) {
126131
super(error.message, error, request);
127-
this.name = 'UploadError';
128-
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_UPLOAD' : this.code;
132+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
133+
this.code = 'ERR_UPLOAD';
134+
}
129135
}
130136
}
131137

@@ -134,13 +140,13 @@ An error to be thrown when the request is aborted due to a timeout.
134140
Includes an `event` and `timings` property.
135141
*/
136142
export class TimeoutError extends RequestError {
143+
override name = 'TimeoutError';
137144
declare readonly request: Request;
138145
override readonly timings: Timings;
139146
readonly event: string;
140147

141148
constructor(error: TimedOutTimeoutError, timings: Timings, request: Request) {
142149
super(error.message, error, request);
143-
this.name = 'TimeoutError';
144150
this.event = error.event;
145151
this.timings = timings;
146152
}
@@ -150,35 +156,39 @@ export class TimeoutError extends RequestError {
150156
An error to be thrown when reading from response stream fails.
151157
*/
152158
export class ReadError extends RequestError {
159+
override name = 'ReadError';
153160
declare readonly request: Request;
154161
declare readonly response: Response;
155162
declare readonly timings: Timings;
156163

157164
constructor(error: Error, request: Request) {
158165
super(error.message, error, request);
159-
this.name = 'ReadError';
160-
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_READING_RESPONSE_STREAM' : this.code;
166+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
167+
this.code = 'ERR_READING_RESPONSE_STREAM';
168+
}
161169
}
162170
}
163171

164172
/**
165173
An error which always triggers a new retry when thrown.
166174
*/
167175
export class RetryError extends RequestError {
176+
override name = 'RetryError';
177+
override code = 'ERR_RETRYING';
178+
168179
constructor(request: Request) {
169180
super('Retrying', {}, request);
170-
this.name = 'RetryError';
171-
this.code = 'ERR_RETRYING';
172181
}
173182
}
174183

175184
/**
176185
An error to be thrown when the request is aborted by AbortController.
177186
*/
178187
export class AbortError extends RequestError {
188+
override name = 'AbortError';
189+
override code = 'ERR_ABORTED';
190+
179191
constructor(request: Request) {
180192
super('This operation was aborted.', {}, request);
181-
this.code = 'ERR_ABORTED';
182-
this.name = 'AbortError';
183193
}
184194
}

source/core/index.ts

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
AbortError,
4545
} from './errors.js';
4646
import {
47-
type RequestId,
4847
generateRequestId,
4948
publishRequestCreate,
5049
publishRequestStart,
@@ -187,34 +186,34 @@ export default class Request extends Duplex implements RequestEvents<Request> {
187186
options: Options;
188187
response?: PlainResponse;
189188
requestUrl?: URL;
190-
redirectUrls: URL[];
191-
retryCount: number;
192-
_stopReading: boolean;
189+
redirectUrls: URL[] = [];
190+
retryCount = 0;
191+
_stopReading = false;
193192

194193
declare private _requestOptions: NativeRequestOptions;
195194

196-
private _stopRetry: () => void;
197-
private _downloadedSize: number;
198-
private _uploadedSize: number;
199-
private readonly _pipedServerResponses: Set<ServerResponse>;
195+
private _stopRetry = noop;
196+
private _downloadedSize = 0;
197+
private _uploadedSize = 0;
198+
private readonly _pipedServerResponses = new Set<ServerResponse>();
200199
private _request?: ClientRequest;
201200
private _responseSize?: number;
202201
private _bodySize?: number;
203-
private _unproxyEvents: () => void;
202+
private _unproxyEvents = noop;
204203
private _isFromCache?: boolean;
205-
private _triggerRead: boolean;
206-
declare private readonly _jobs: Array<() => void>;
207-
private _cancelTimeouts: () => void;
208-
private readonly _removeListeners: () => void;
204+
private _triggerRead = false;
205+
private readonly _jobs: Array<() => void> = [];
206+
private _cancelTimeouts = noop;
207+
private readonly _removeListeners = noop;
209208
private _nativeResponse?: IncomingMessageWithTimings;
210-
private _flushed: boolean;
211-
private _aborted: boolean;
209+
private _flushed = false;
210+
private _aborted = false;
212211
private _expectedContentLength?: number;
213212
private _byteCounter?: ByteCounter;
214-
private readonly _requestId: RequestId;
213+
private readonly _requestId = generateRequestId();
215214

216215
// We need this because `this._request` if `undefined` when using cache
217-
private _requestInitialized: boolean;
216+
private _requestInitialized = false;
218217

219218
constructor(url: UrlType, options?: OptionsType, defaults?: DefaultsType) {
220219
super({
@@ -224,25 +223,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
224223
highWaterMark: 0,
225224
});
226225

227-
this._downloadedSize = 0;
228-
this._uploadedSize = 0;
229-
this._stopReading = false;
230-
this._pipedServerResponses = new Set<ServerResponse>();
231-
this._unproxyEvents = noop;
232-
this._triggerRead = false;
233-
this._cancelTimeouts = noop;
234-
this._removeListeners = noop;
235-
this._jobs = [];
236-
this._flushed = false;
237-
this._requestInitialized = false;
238-
this._aborted = false;
239-
240-
this.redirectUrls = [];
241-
this.retryCount = 0;
242-
243-
this._stopRetry = noop;
244-
this._requestId = generateRequestId();
245-
246226
this.on('pipe', (source: NodeJS.ReadableStream & {headers?: Record<string, string | string[] | undefined>}) => {
247227
if (this.options.copyPipedHeaders && source?.headers) {
248228
Object.assign(this.options.headers, source.headers);

source/core/options.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ const init = (options: OptionsInit, withOptions: OptionsInit, self: Options): vo
12931293
export default class Options {
12941294
private _unixOptions?: NativeRequestOptions;
12951295
private readonly _internals: InternalsType;
1296-
private _merging: boolean;
1296+
private _merging = false;
12971297
private readonly _init: OptionsInit[];
12981298

12991299
constructor(input?: string | URL | OptionsInit, options?: OptionsInit, defaults?: Options) {
@@ -1307,8 +1307,6 @@ export default class Options {
13071307

13081308
this._internals = cloneInternals(defaults?._internals ?? defaults ?? defaultInternals);
13091309
this._init = [...(defaults?._init ?? [])];
1310-
this._merging = false;
1311-
this._unixOptions = undefined;
13121310

13131311
// This rule allows `finally` to be considered more important.
13141312
// Meaning no matter the error thrown in the `try` block,

source/core/response.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ An error to be thrown when server response code is 2xx, and parsing body fails.
126126
Includes a `response` property.
127127
*/
128128
export class ParseError extends RequestError {
129+
override name = 'ParseError';
130+
override code = 'ERR_BODY_PARSE_FAILURE';
129131
declare readonly response: Response;
130132

131133
constructor(error: Error, response: Response) {
132134
const {options} = response.request;
133135

134136
super(`${error.message} in "${options.url!.toString()}"`, error, response.request);
135-
this.name = 'ParseError';
136-
this.code = 'ERR_BODY_PARSE_FAILURE';
137137
}
138138
}
139139

source/core/timed-out.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ export type ErrorCode =
3333
| 'EAI_AGAIN';
3434

3535
export class TimeoutError extends Error {
36-
code: ErrorCode;
36+
override name = 'TimeoutError';
37+
code: ErrorCode = 'ETIMEDOUT';
3738

3839
constructor(threshold: number, public event: string) {
3940
super(`Timeout awaiting '${event}' for ${threshold}ms`);
40-
41-
this.name = 'TimeoutError';
42-
this.code = 'ETIMEDOUT';
4341
}
4442
}
4543

source/core/utils/weakable-map.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
export default class WeakableMap<K, V> {
2-
weakMap: WeakMap<Record<string, unknown>, V>;
3-
map: Map<K, V>;
4-
5-
constructor() {
6-
this.weakMap = new WeakMap();
7-
this.map = new Map();
8-
}
2+
weakMap = new WeakMap<Record<string, unknown>, V>();
3+
map = new Map<K, V>();
94

105
set(key: K, value: V): void {
116
if (typeof key === 'object') {

0 commit comments

Comments
 (0)