From f4c4fc6ecdb3f67d10c0d0ca97d304cfce40c9cc Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 6 Aug 2024 21:41:14 +0200 Subject: [PATCH] refactor: fetch-compatible signature --- .changeset/red-crews-draw.md | 5 +++++ packages/http/get.ts | 11 +++++++++-- packages/http/lib/writableFetch.ts | 6 +++--- packages/http/post.ts | 11 +++++++++-- packages/http/test/get.test.js | 16 +++++++++++++++- packages/http/test/post.test.js | 24 +++++++++++++++++++++++- 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 .changeset/red-crews-draw.md diff --git a/.changeset/red-crews-draw.md b/.changeset/red-crews-draw.md new file mode 100644 index 00000000..6b836f5f --- /dev/null +++ b/.changeset/red-crews-draw.md @@ -0,0 +1,5 @@ +--- +"barnard59-http": minor +--- + +Add overload to `get` and `post` to match the signature of native `fetch` diff --git a/packages/http/get.ts b/packages/http/get.ts index 7da2d279..9b9f777e 100644 --- a/packages/http/get.ts +++ b/packages/http/get.ts @@ -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 +function get(url: string, options?: RequestInit): ReturnType +function get(urlOrOptions: GetInit | string, options: RequestInit = {}): ReturnType { + if (typeof urlOrOptions === 'string') { + return fetch({ ...options, url: urlOrOptions }) + } + + return fetch(urlOrOptions) } export default get diff --git a/packages/http/lib/writableFetch.ts b/packages/http/lib/writableFetch.ts index be6392b3..d734959a 100644 --- a/packages/http/lib/writableFetch.ts +++ b/packages/http/lib/writableFetch.ts @@ -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 } @@ -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) { diff --git a/packages/http/post.ts b/packages/http/post.ts index 974a3594..b98617b7 100644 --- a/packages/http/post.ts +++ b/packages/http/post.ts @@ -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 +function post(url: string, options: RequestInit): ReturnType +function post(urlOrOptions: PostInit | string, options: RequestInit = {}): ReturnType { + if (typeof urlOrOptions === 'string') { + return writableFetch({ ...options, url: urlOrOptions }) + } + + return writableFetch(urlOrOptions) } export default post diff --git a/packages/http/test/get.test.js b/packages/http/test/get.test.js index c6f75e1f..cb874c83 100644 --- a/packages/http/test/get.test.js +++ b/packages/http/test/get.test.js @@ -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' @@ -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') + }) + }) }) diff --git a/packages/http/test/post.test.js b/packages/http/test/post.test.js index b691b091..b5f4acbd 100644 --- a/packages/http/test/post.test.js +++ b/packages/http/test/post.test.js @@ -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' @@ -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) + }) + }) })