-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from zazuko/http-ts
barnard59-http converted to TS
- Loading branch information
Showing
20 changed files
with
137 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"barnard59-http": patch | ||
--- | ||
|
||
Added TS declarations |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.ts | ||
!*.d.ts | ||
test/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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): 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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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): 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.