Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: fix types
  • Loading branch information
achingbrain committed Jun 19, 2022
commit 499fe15f0bc4be22bdae988f4711f2da92833f5d
12 changes: 9 additions & 3 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict'

/**
* @typedef {globalThis.Headers} Headers
* @typedef {globalThis.Request} Request
* @typedef {globalThis.Response} Response
*/

const { isElectronMain } = require('./env')

// use window.fetch if it is available, fall back to node-fetch if not
let fetch = 'native-fetch'
let impl = 'native-fetch'

if (isElectronMain) {
fetch = 'electron-fetch'
impl = 'electron-fetch'
}

module.exports = require(fetch)
module.exports = require(impl)
9 changes: 7 additions & 2 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class HTTP {
async fetch (resource, options = {}) {
/** @type {HTTPOptions} */
const opts = merge(this.opts, options)
// @ts-expect-error
const headers = new Headers(opts.headers)

// validate resource type
// @ts-expect-error
if (typeof resource !== 'string' && !(resource instanceof URL || resource instanceof Request)) {
throw new TypeError('`resource` must be a string, URL, or Request')
}
Expand Down Expand Up @@ -125,12 +127,15 @@ class HTTP {
// @ts-ignore
const signal = anySignal([abortController.signal, opts.signal])

/** @type {ExtendedResponse} */
// @ts-expect-error additional fields are assigned below
const response = await timeout(
fetch(
url.toString(),
{
...opts,
signal,
// @ts-expect-error non-browser fetch implementations may take extra options
timeout: undefined,
headers
}
Expand All @@ -146,8 +151,8 @@ class HTTP {
throw new HTTPError(response)
}

response.iterator = function () {
return fromStream(response.body)
response.iterator = async function * () {
yield * fromStream(response.body)
}

response.ndjson = async function * () {
Expand Down
1 change: 1 addition & 0 deletions src/http/fetch.browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { TimeoutError, AbortError } = require('./error')
// @ts-expect-error
const { Response, Request, Headers, default: fetch } = require('../fetch')

/**
Expand Down
19 changes: 16 additions & 3 deletions src/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
'use strict'

/**
* @typedef {object} fetchImpl
* @property {globalThis.fetch} fetchImpl.fetch
* @property {globalThis.Request} fetchImpl.Request
* @property {globalThis.Response} fetchImpl.Response
* @property {globalThis.Headers} fetchImpl.Headers
*/

let implName = './fetch.node'

if (typeof XMLHttpRequest === 'function') {
// Electron has `XMLHttpRequest` and should get the browser implementation
// instead of node.
module.exports = require('./fetch.browser')
} else {
module.exports = require('./fetch.node')
implName = './fetch.browser'
}

/** @type {fetchImpl} */
const fetch = require(implName)

module.exports = fetch
2 changes: 2 additions & 0 deletions src/http/fetch.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const withUploadProgress = (options) => {
// and `ArrayBuffer`s to strings.
const content = normalizeBody(body)

// @ts-expect-error this is node-fetch
const rsp = new Response(content)
// @ts-expect-error this is node-fetch
const source = iterateBodyWithProgress(/** @type {NodeReadableStream} */(rsp.body), onUploadProgress)
return {
...options,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"emitDeclarationOnly": true
},
"include": [
"test",
Expand Down