Skip to content

Commit

Permalink
feat: convert to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 6, 2024
1 parent 61a1095 commit cb4223b
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-lamps-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59-http": patch
---

Added TS declarations
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
3 changes: 3 additions & 0 deletions packages/http/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.ts
!*.d.ts
test/
3 changes: 2 additions & 1 deletion packages/http/get.js → packages/http/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GetInit } from './lib/fetch.js'
import fetch from './lib/fetch.js'

function get(options) {
function get(options: GetInit) {
return fetch(options)
}

Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions packages/http/lib/fetch.js → packages/http/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { SpanStatusCode } from '@opentelemetry/api'
import toReadable from 'duplex-to/readable.js'
import type { RequestInit } from 'node-fetch'
import nodeFetch from 'node-fetch'
import tracer from './tracer.js'

async function fetch({ method = 'GET', url, ...options } = {}) {
export type GetInit = RequestInit & { url: string }

export default async function fetch({ method = 'GET', url, ...options }: GetInit) {
return await tracer.startActiveSpan('fetch', async span => {
try {
const response = await nodeFetch(url, { method, ...options })

return toReadable(response.body)
} catch (e) {
return toReadable(response.body as any)
} catch (e: any) {
span.recordException(e)
span.setStatus({ code: SpanStatusCode.ERROR, message: e.message })
} finally {
span.end()
}
})
}

export default fetch
File renamed without changes.
25 changes: 0 additions & 25 deletions packages/http/lib/writableFetch.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/http/lib/writableFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Duplex } from 'node:stream'
import { PassThrough, Readable } from 'node:stream'
import duplexify from 'duplexify'
import tracer from './tracer.js'

export type PostInit = RequestInit & { url: string }

export default async function writableFetch({ method = 'POST', url, ...options }: PostInit): Promise<Duplex> {
const inputStream: any = new PassThrough()
const outputStream = new PassThrough()

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

Readable.fromWeb(response.body as any).pipe(outputStream)
} catch (err) {
outputStream.emit('error', err)
} finally {
span.end()
}
}, 0))

return duplexify(inputStream, outputStream)
}
5 changes: 4 additions & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "mocha"
"test": "mocha",
"build": "tsc",
"prepack": "npm run build"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +29,7 @@
"readable-stream": ">=3"
},
"devDependencies": {
"@types/duplexify": "^3.6.4",
"express-as-promise": "^1.2.0",
"get-stream": "^6.0.1",
"is-stream": "^3.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/http/post.js → packages/http/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PostInit } from './lib/writableFetch.js'
import writableFetch from './lib/writableFetch.js'

function post(options) {
function post(options: PostInit) {
return writableFetch(options)
}

Expand Down
7 changes: 7 additions & 0 deletions packages/http/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"files": ["./index.ts"],
"compilerOptions": {
"skipLibCheck": true
}
}

0 comments on commit cb4223b

Please sign in to comment.