From f6406c5af4abb83fa11113202f785e8a7c9f6941 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 4 May 2021 09:02:09 +0100 Subject: [PATCH] fix: remove retyping of modules with types (#125) The types are incorrect and cause errors with modules that check the types of their deps. Types for `is-electron` can be removed once https://github.com/cheton/is-electron/issues/7 is resolved. Fixes #109 BREAKING CHANGE: `ResponseWithURL` type is not exported any more as it uses a private name and causes an inconsistency between node and the browser --- src/http.js | 14 +--- src/http/error.js | 2 +- src/http/fetch.browser.js | 3 +- src/http/fetch.node.js | 1 - src/types.d.ts | 7 +- types/electron-fetch/index.d.ts | 117 -------------------------------- types/iso-url/index.d.ts | 1 - types/native-fetch/index.d.ts | 117 -------------------------------- 8 files changed, 10 insertions(+), 252 deletions(-) delete mode 100644 types/electron-fetch/index.d.ts delete mode 100644 types/iso-url/index.d.ts delete mode 100644 types/native-fetch/index.d.ts diff --git a/src/http.js b/src/http.js index c08e751..ba88fdf 100644 --- a/src/http.js +++ b/src/http.js @@ -9,7 +9,7 @@ const { AbortController } = require('native-abort-controller') const anySignal = require('any-signal') /** - * @typedef {import('native-fetch').Response} Response + * @typedef {import('./types').ExtendedResponse} ExtendedResponse * @typedef {import('stream').Readable} NodeReadableStream * @typedef {import('stream').Duplex} NodeDuplexStream * @typedef {import('./types').HTTPOptions} HTTPOptions @@ -88,7 +88,7 @@ class HTTP { * * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} + * @returns {Promise} */ async fetch (resource, options = {}) { /** @type {HTTPOptions} */ @@ -168,7 +168,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ post (resource, options = {}) { return this.fetch(resource, { ...options, method: 'POST' }) @@ -177,7 +176,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ get (resource, options = {}) { return this.fetch(resource, { ...options, method: 'GET' }) @@ -186,7 +184,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ put (resource, options = {}) { return this.fetch(resource, { ...options, method: 'PUT' }) @@ -195,7 +192,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ delete (resource, options = {}) { return this.fetch(resource, { ...options, method: 'DELETE' }) @@ -204,7 +200,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ options (resource, options = {}) { return this.fetch(resource, { ...options, method: 'OPTIONS' }) @@ -335,35 +330,30 @@ HTTP.streamToAsyncIterator = fromStream /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.post = (resource, options) => new HTTP(options).post(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.get = (resource, options) => new HTTP(options).get(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.put = (resource, options) => new HTTP(options).put(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.options = (resource, options) => new HTTP(options).options(resource, options) diff --git a/src/http/error.js b/src/http/error.js index aa5c9d6..d839809 100644 --- a/src/http/error.js +++ b/src/http/error.js @@ -18,7 +18,7 @@ exports.AbortError = AbortError class HTTPError extends Error { /** - * @param {import('native-fetch').Response} response + * @param {Response} response */ constructor (response) { super(response.statusText) diff --git a/src/http/fetch.browser.js b/src/http/fetch.browser.js index 4c70938..ee59420 100644 --- a/src/http/fetch.browser.js +++ b/src/http/fetch.browser.js @@ -136,6 +136,5 @@ class ResponseWithURL extends Response { module.exports = { fetch: fetchWith, Request, - Headers, - ResponseWithURL + Headers } diff --git a/src/http/fetch.node.js b/src/http/fetch.node.js index 27d8ee8..ae0ed0f 100644 --- a/src/http/fetch.node.js +++ b/src/http/fetch.node.js @@ -5,7 +5,6 @@ const { Request, Response, Headers, default: nativeFetch } = require('../fetch') const toStream = require('it-to-stream') const { Buffer } = require('buffer') /** - * @typedef {import('native-fetch').BodyInit} BodyInit * @typedef {import('stream').Readable} NodeReadableStream * * @typedef {import('../types').FetchOptions} FetchOptions diff --git a/src/types.d.ts b/src/types.d.ts index 910ec56..2ee6223 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,4 +1,3 @@ -import type { RequestInit, Response } from '../types/native-fetch' interface ProgressStatus { total: number loaded: number @@ -51,3 +50,9 @@ export interface HTTPOptions extends FetchOptions { */ handleError?: (rsp: Response) => Promise } + +export interface ExtendedResponse extends Response { + iterator: () => AsyncGenerator + + ndjson: () => AsyncGenerator +} diff --git a/types/electron-fetch/index.d.ts b/types/electron-fetch/index.d.ts deleted file mode 100644 index 596d04a..0000000 --- a/types/electron-fetch/index.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Readable as NodeReadableStream } from 'stream' - -export default function fetch (input: RequestInfo, init?: RequestInit): Promise - -export interface Body { - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} -export class Headers extends globalThis.Headers {} - -/** This Fetch API interface represents the response to a request. */ -export class Response extends globalThis.Response { - constructor (body?: BodyInit | null, init?: ResponseInit) - readonly headers: Headers - readonly ok: boolean - readonly redirected: boolean - readonly status: number - readonly statusText: string - readonly trailer: Promise - readonly type: ResponseType - readonly url: string - clone: () => Response - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise - iterator: () => AsyncIterable - ndjson: () => AsyncIterable - - static error (): Response - static redirect (url: string, status?: number): Response -} - -export class Request extends globalThis.Request { - constructor (input: RequestInfo, init?: RequestInit) - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} - -export type RequestInfo = Request | string - -export type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string | Buffer | NodeReadableStream - -export interface RequestInit { - /** - * A BodyInit object or null to set request's body. - */ - body?: BodyInit | null - /** - * A string indicating how the request will interact with the browser's cache to set request's cache. - */ - cache?: RequestCache - /** - * A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. - */ - credentials?: RequestCredentials - /** - * A Headers object, an object literal, or an array of two-item arrays to set request's headers. - */ - headers?: HeadersInit - /** - * A cryptographic hash of the resource to be fetched by request. Sets request's integrity. - */ - integrity?: string - /** - * A boolean to set request's keepalive. - */ - keepalive?: boolean - /** - * A string to set request's method. - */ - method?: string - /** - * A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. - */ - mode?: RequestMode - /** - * A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. - */ - redirect?: RequestRedirect - /** - * A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. - */ - referrer?: string - /** - * A referrer policy to set request's referrerPolicy. - */ - referrerPolicy?: ReferrerPolicy - /** - * An AbortSignal to set request's signal. - */ - signal?: AbortSignal | null - /** - * Can only be null. Used to disassociate request from any Window. - */ - window?: any -} diff --git a/types/iso-url/index.d.ts b/types/iso-url/index.d.ts deleted file mode 100644 index eac8a7e..0000000 --- a/types/iso-url/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export { URL, URLSearchParams } diff --git a/types/native-fetch/index.d.ts b/types/native-fetch/index.d.ts deleted file mode 100644 index 596d04a..0000000 --- a/types/native-fetch/index.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Readable as NodeReadableStream } from 'stream' - -export default function fetch (input: RequestInfo, init?: RequestInit): Promise - -export interface Body { - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} -export class Headers extends globalThis.Headers {} - -/** This Fetch API interface represents the response to a request. */ -export class Response extends globalThis.Response { - constructor (body?: BodyInit | null, init?: ResponseInit) - readonly headers: Headers - readonly ok: boolean - readonly redirected: boolean - readonly status: number - readonly statusText: string - readonly trailer: Promise - readonly type: ResponseType - readonly url: string - clone: () => Response - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise - iterator: () => AsyncIterable - ndjson: () => AsyncIterable - - static error (): Response - static redirect (url: string, status?: number): Response -} - -export class Request extends globalThis.Request { - constructor (input: RequestInfo, init?: RequestInit) - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} - -export type RequestInfo = Request | string - -export type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string | Buffer | NodeReadableStream - -export interface RequestInit { - /** - * A BodyInit object or null to set request's body. - */ - body?: BodyInit | null - /** - * A string indicating how the request will interact with the browser's cache to set request's cache. - */ - cache?: RequestCache - /** - * A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. - */ - credentials?: RequestCredentials - /** - * A Headers object, an object literal, or an array of two-item arrays to set request's headers. - */ - headers?: HeadersInit - /** - * A cryptographic hash of the resource to be fetched by request. Sets request's integrity. - */ - integrity?: string - /** - * A boolean to set request's keepalive. - */ - keepalive?: boolean - /** - * A string to set request's method. - */ - method?: string - /** - * A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. - */ - mode?: RequestMode - /** - * A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. - */ - redirect?: RequestRedirect - /** - * A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. - */ - referrer?: string - /** - * A referrer policy to set request's referrerPolicy. - */ - referrerPolicy?: ReferrerPolicy - /** - * An AbortSignal to set request's signal. - */ - signal?: AbortSignal | null - /** - * Can only be null. Used to disassociate request from any Window. - */ - window?: any -}