Skip to content

Commit

Permalink
fix: remove retyping of modules with types (#125)
Browse files Browse the repository at this point in the history
The types are incorrect and cause errors with modules that check the types of their deps.

Types for `is-electron` can be removed once cheton/is-electron#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
  • Loading branch information
achingbrain authored May 4, 2021
1 parent ca301b0 commit f6406c5
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 252 deletions.
14 changes: 2 additions & 12 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +88,7 @@ class HTTP {
*
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
* @returns {Promise<ExtendedResponse>}
*/
async fetch (resource, options = {}) {
/** @type {HTTPOptions} */
Expand Down Expand Up @@ -168,7 +168,6 @@ class HTTP {
/**
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
*/
post (resource, options = {}) {
return this.fetch(resource, { ...options, method: 'POST' })
Expand All @@ -177,7 +176,6 @@ class HTTP {
/**
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
*/
get (resource, options = {}) {
return this.fetch(resource, { ...options, method: 'GET' })
Expand All @@ -186,7 +184,6 @@ class HTTP {
/**
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
*/
put (resource, options = {}) {
return this.fetch(resource, { ...options, method: 'PUT' })
Expand All @@ -195,7 +192,6 @@ class HTTP {
/**
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
*/
delete (resource, options = {}) {
return this.fetch(resource, { ...options, method: 'DELETE' })
Expand All @@ -204,7 +200,6 @@ class HTTP {
/**
* @param {string | Request} resource
* @param {HTTPOptions} options
* @returns {Promise<Response>}
*/
options (resource, options = {}) {
return this.fetch(resource, { ...options, method: 'OPTIONS' })
Expand Down Expand Up @@ -335,35 +330,30 @@ HTTP.streamToAsyncIterator = fromStream
/**
* @param {string | Request} resource
* @param {HTTPOptions} [options]
* @returns {Promise<Response>}
*/
HTTP.post = (resource, options) => new HTTP(options).post(resource, options)

/**
* @param {string | Request} resource
* @param {HTTPOptions} [options]
* @returns {Promise<Response>}
*/
HTTP.get = (resource, options) => new HTTP(options).get(resource, options)

/**
* @param {string | Request} resource
* @param {HTTPOptions} [options]
* @returns {Promise<Response>}
*/
HTTP.put = (resource, options) => new HTTP(options).put(resource, options)

/**
* @param {string | Request} resource
* @param {HTTPOptions} [options]
* @returns {Promise<Response>}
*/
HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options)

/**
* @param {string | Request} resource
* @param {HTTPOptions} [options]
* @returns {Promise<Response>}
*/
HTTP.options = (resource, options) => new HTTP(options).options(resource, options)

Expand Down
2 changes: 1 addition & 1 deletion src/http/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/http/fetch.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,5 @@ class ResponseWithURL extends Response {
module.exports = {
fetch: fetchWith,
Request,
Headers,
ResponseWithURL
Headers
}
1 change: 0 additions & 1 deletion src/http/fetch.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RequestInit, Response } from '../types/native-fetch'
interface ProgressStatus {
total: number
loaded: number
Expand Down Expand Up @@ -51,3 +50,9 @@ export interface HTTPOptions extends FetchOptions {
*/
handleError?: (rsp: Response) => Promise<void>
}

export interface ExtendedResponse extends Response {
iterator: () => AsyncGenerator<Uint8Array, void, undefined>

ndjson: () => AsyncGenerator<any, void, undefined>
}
117 changes: 0 additions & 117 deletions types/electron-fetch/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/iso-url/index.d.ts

This file was deleted.

117 changes: 0 additions & 117 deletions types/native-fetch/index.d.ts

This file was deleted.

0 comments on commit f6406c5

Please sign in to comment.