diff --git a/packages/cookie-store/src/CookieStore.ts b/packages/cookie-store/src/CookieStore.ts index 126e5c87fa4..c524682676d 100644 --- a/packages/cookie-store/src/CookieStore.ts +++ b/packages/cookie-store/src/CookieStore.ts @@ -22,7 +22,7 @@ export class CookieStore extends EventTarget { } async get( - init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions, + init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions | undefined, ): Promise { if (init == null) { throw new TypeError('CookieStoreGetOptions must not be empty'); diff --git a/packages/cookie-store/src/types.ts b/packages/cookie-store/src/types.ts index 76d92122aae..ba2a90c6b00 100644 --- a/packages/cookie-store/src/types.ts +++ b/packages/cookie-store/src/types.ts @@ -16,21 +16,21 @@ export interface CookieStoreDeleteOptions { } export interface CookieStoreGetOptions { - name?: string; + name?: string | undefined; url?: string; } export type CookieSameSite = 'strict' | 'lax' | 'none'; export interface CookieListItem { - name?: string; - value?: string; + name?: string | undefined; + value?: string | undefined; domain: string | null; - path?: string; + path?: string | undefined; expires: Date | number | null; - secure?: boolean; - sameSite?: CookieSameSite; - httpOnly?: boolean; + secure?: boolean | undefined; + sameSite?: CookieSameSite | undefined; + httpOnly?: boolean | undefined; } export type CookieList = CookieListItem[]; diff --git a/packages/disposablestack/src/AsyncDisposableStack.ts b/packages/disposablestack/src/AsyncDisposableStack.ts index 128f3b48af4..602bd666551 100644 --- a/packages/disposablestack/src/AsyncDisposableStack.ts +++ b/packages/disposablestack/src/AsyncDisposableStack.ts @@ -43,7 +43,7 @@ export class PonyfillAsyncDisposableStack implements AsyncDisposableStack { return this[DisposableSymbols.asyncDispose](); } - private _error?: Error; + private _error?: Error | undefined; private _iterateCallbacks(): MaybePromise { const cb = this.callbacks.pop(); diff --git a/packages/disposablestack/src/DisposableStack.ts b/packages/disposablestack/src/DisposableStack.ts index d77e87e6134..43fec12762b 100644 --- a/packages/disposablestack/src/DisposableStack.ts +++ b/packages/disposablestack/src/DisposableStack.ts @@ -41,7 +41,7 @@ export class PonyfillDisposableStack implements DisposableStack { return this[DisposableSymbols.dispose](); } - private _error?: Error; + private _error?: Error | undefined; private _iterateCallbacks(): void { const cb = this.callbacks.pop(); diff --git a/packages/node-fetch/src/Body.ts b/packages/node-fetch/src/Body.ts index 88b1dd29e61..840def2800e 100644 --- a/packages/node-fetch/src/Body.ts +++ b/packages/node-fetch/src/Body.ts @@ -60,11 +60,11 @@ export class PonyfillBody implements Body { this._buffer = buffer; } - private bodyType?: BodyInitType; + private bodyType?: BodyInitType | undefined; private _bodyFactory: () => PonyfillReadableStream | null = () => null; private _generatedBody: PonyfillReadableStream | null = null; - private _buffer?: Buffer; + private _buffer?: Buffer | undefined; private generateBody(): PonyfillReadableStream | null { if (this._generatedBody?.readable?.destroyed && this._buffer) { diff --git a/packages/node-fetch/src/Request.ts b/packages/node-fetch/src/Request.ts index 0f4e951699e..221f8916188 100644 --- a/packages/node-fetch/src/Request.ts +++ b/packages/node-fetch/src/Request.ts @@ -10,11 +10,11 @@ function isRequest(input: any): input is PonyfillRequest { export type RequestPonyfillInit = PonyfillBodyOptions & Omit & { - body?: BodyPonyfillInit | null; - duplex?: 'half' | 'full'; - headers?: PonyfillHeadersInit; - headersSerializer?: HeadersSerializer; - agent?: HTTPAgent | HTTPSAgent | false; + body?: BodyPonyfillInit | null | undefined; + duplex?: 'half' | 'full' | undefined; + headers?: PonyfillHeadersInit | undefined; + headersSerializer?: HeadersSerializer | undefined; + agent?: HTTPAgent | HTTPSAgent | false | undefined; }; type HeadersSerializer = ( @@ -96,7 +96,7 @@ export class PonyfillRequest extends PonyfillBody implements } } - headersSerializer?: HeadersSerializer; + headersSerializer?: HeadersSerializer | undefined; cache: RequestCache; credentials: RequestCredentials; destination: RequestDestination; diff --git a/packages/node-fetch/src/Response.ts b/packages/node-fetch/src/Response.ts index 958ef7251e7..44d06e731e5 100644 --- a/packages/node-fetch/src/Response.ts +++ b/packages/node-fetch/src/Response.ts @@ -4,10 +4,10 @@ import { isHeadersLike, PonyfillHeaders, PonyfillHeadersInit } from './Headers.j export type ResponsePonyfilInit = PonyfillBodyOptions & Omit & { - url?: string; - redirected?: boolean; - headers?: PonyfillHeadersInit; - type?: ResponseType; + url?: string | undefined; + redirected?: boolean | undefined; + headers?: PonyfillHeadersInit | undefined; + type?: ResponseType | undefined; }; const JSON_CONTENT_TYPE = 'application/json; charset=utf-8'; @@ -15,7 +15,7 @@ const JSON_CONTENT_TYPE = 'application/json; charset=utf-8'; export class PonyfillResponse extends PonyfillBody implements Response { headers: Headers; - constructor(body?: BodyPonyfillInit | null, init?: ResponsePonyfilInit) { + constructor(body?: BodyPonyfillInit | null | undefined, init?: ResponsePonyfilInit) { super(body || null, init); this.headers = init?.headers && isHeadersLike(init.headers) diff --git a/packages/node-fetch/src/fetchNodeHttp.ts b/packages/node-fetch/src/fetchNodeHttp.ts index 7993eb9f92f..61259e29baf 100644 --- a/packages/node-fetch/src/fetchNodeHttp.ts +++ b/packages/node-fetch/src/fetchNodeHttp.ts @@ -1,4 +1,4 @@ -import { request as httpRequest } from 'http'; +import { request as httpRequest, STATUS_CODES } from 'http'; import { request as httpsRequest } from 'https'; import { PassThrough, Readable, promises as streamPromises } from 'stream'; import { createBrotliDecompress, createGunzip, createInflate, createInflateRaw } from 'zlib'; @@ -124,9 +124,14 @@ export function fetchNodeHttp( }) .catch(reject); + const statusCode = nodeResponse.statusCode || 200; + let statusText = nodeResponse.statusMessage || STATUS_CODES[statusCode]; + if (statusText == null) { + statusText = ''; + } const ponyfillResponse = new PonyfillResponse(outputStream, { - status: nodeResponse.statusCode, - statusText: nodeResponse.statusMessage, + status: statusCode, + statusText, headers: nodeResponse.headers as Record, url: fetchRequest.url, }); diff --git a/packages/server/src/utils.ts b/packages/server/src/utils.ts index 77e0f0cebc6..4c3302d4cc0 100644 --- a/packages/server/src/utils.ts +++ b/packages/server/src/utils.ts @@ -11,19 +11,19 @@ export function isAsyncIterable(body: any): body is AsyncIterable { } export interface NodeRequest { - protocol?: string; - hostname?: string; - body?: any; - url?: string; - originalUrl?: string; - method?: string; - headers?: any; - req?: IncomingMessage | Http2ServerRequest; - raw?: IncomingMessage | Http2ServerRequest; - socket?: Socket; - query?: any; + protocol?: string | undefined; + hostname?: string | undefined; + body?: any | undefined; + url?: string | undefined; + originalUrl?: string | undefined; + method?: string | undefined; + headers?: any | undefined; + req?: IncomingMessage | Http2ServerRequest | undefined; + raw?: IncomingMessage | Http2ServerRequest | undefined; + socket?: Socket | undefined; + query?: any | undefined; once?(event: string, listener: (...args: any[]) => void): void; - aborted?: boolean; + aborted?: boolean | undefined; } export type NodeResponse = ServerResponse | Http2ServerResponse; @@ -183,7 +183,7 @@ export function normalizeNodeRequest( return new fetchAPI.Request(fullUrl, { method: nodeRequest.method, headers: normalizedHeaders, - signal, + signal: signal || null, }); } @@ -197,16 +197,16 @@ export function normalizeNodeRequest( if (maybeParsedBody != null && Object.keys(maybeParsedBody).length > 0) { if (isRequestBody(maybeParsedBody)) { return new fetchAPI.Request(fullUrl, { - method: nodeRequest.method, + method: nodeRequest.method || 'GET', headers: normalizedHeaders, body: maybeParsedBody, - signal, + signal: signal || null, }); } const request = new fetchAPI.Request(fullUrl, { - method: nodeRequest.method, + method: nodeRequest.method || 'GET', headers: normalizedHeaders, - signal, + signal: signal || null, }); if (!request.headers.get('content-type')?.includes('json')) { request.headers.set('content-type', 'application/json; charset=utf-8'); diff --git a/tsconfig.json b/tsconfig.json index 9f595c3ff3e..ca315771499 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "declaration": true, "downlevelIteration": true, "incremental": true, + "exactOptionalPropertyTypes": true, "skipLibCheck": false,