Skip to content

Commit

Permalink
refactor: fetch-compatible signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 6, 2024
1 parent 3c51a24 commit f4c4fc6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-crews-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59-http": minor
---

Add overload to `get` and `post` to match the signature of native `fetch`
11 changes: 9 additions & 2 deletions packages/http/get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { RequestInit } from 'node-fetch'
import type { GetInit } from './lib/fetch.js'
import fetch from './lib/fetch.js'

function get(options: GetInit) {
return fetch(options)
function get(options: GetInit): ReturnType<typeof fetch>
function get(url: string, options?: RequestInit): ReturnType<typeof fetch>
function get(urlOrOptions: GetInit | string, options: RequestInit = {}): ReturnType<typeof fetch> {
if (typeof urlOrOptions === 'string') {
return fetch({ ...options, url: urlOrOptions })
}

return fetch(urlOrOptions)
}

export default get
6 changes: 3 additions & 3 deletions packages/http/lib/writableFetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Duplex } from 'node:stream'
import { PassThrough } from 'readable-stream'
import duplexify from 'duplexify'
import type { RequestInit } from 'node-fetch'
import fetch from 'node-fetch'
import nodeFetch from 'node-fetch'
import duplexify from 'duplexify'
import tracer from './tracer.js'

export type PostInit = RequestInit & { url: string }
Expand All @@ -14,7 +14,7 @@ export default async function writableFetch({ method = 'POST', url, ...options }

tracer.startActiveSpan('writableFetch', span => setTimeout(async () => {
try {
const response = await fetch(url, { method, body: inputStream, ...options })
const response = await nodeFetch(url, { method, body: inputStream, ...options })

response.body!.pipe(outputStream)
} catch (err) {
Expand Down
11 changes: 9 additions & 2 deletions packages/http/post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { RequestInit } from 'node-fetch'
import type { PostInit } from './lib/writableFetch.js'
import writableFetch from './lib/writableFetch.js'

function post(options: PostInit) {
return writableFetch(options)
function post(options: PostInit): ReturnType<typeof writableFetch>
function post(url: string, options: RequestInit): ReturnType<typeof writableFetch>
function post(urlOrOptions: PostInit | string, options: RequestInit = {}): ReturnType<typeof writableFetch> {
if (typeof urlOrOptions === 'string') {
return writableFetch({ ...options, url: urlOrOptions })
}

return writableFetch(urlOrOptions)
}

export default post
16 changes: 15 additions & 1 deletion packages/http/test/get.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { strictEqual } from 'assert'
import { strictEqual } from 'node:assert'
import withServer from 'express-as-promise/withServer.js'
import getStream from 'get-stream'
import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
Expand Down Expand Up @@ -32,4 +32,18 @@ describe('get', () => {
strictEqual(content, expected.content)
})
})

it('can be called with 2 arguments', async () => {
await withServer(async server => {
server.app.get('/', (req, res) => {
res.send(req.headers['x-test'])
})

const baseUrl = await server.listen()
const response = await get(baseUrl, { headers: { 'x-test': 'test header' } })
const content = await getStream(response)

strictEqual(content, 'test header')
})
})
})
24 changes: 23 additions & 1 deletion packages/http/test/post.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { strictEqual } from 'assert'
import { strictEqual } from 'node:assert'
import withServer from 'express-as-promise/withServer.js'
import getStream from 'get-stream'
import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
Expand Down Expand Up @@ -45,4 +45,26 @@ describe('post', () => {
strictEqual(content, expected.content)
})
})

it('can be called with 2 arguments', async () => {
await withServer(async server => {
let response
const expected = chunksAndContent()

server.app.post('/', async (req, res) => {
response = await getStream(req)

res.status(204).end()
})

const baseUrl = await server.listen()
const stream = await post(baseUrl)

expected.stream.pipe(stream)

await getStream(stream)

strictEqual(response, expected.content)
})
})
})

0 comments on commit f4c4fc6

Please sign in to comment.