Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: minimal IFetchComponent interface #88

Open
hugoArregui opened this issue May 17, 2024 · 1 comment
Open

Proposal: minimal IFetchComponent interface #88

hugoArregui opened this issue May 17, 2024 · 1 comment

Comments

@hugoArregui
Copy link
Contributor

Currently, we depend on node-fetch to define the IFetchInterface, which is commonly used across WKC. I wanted to discuss the idea of keeping a minimal interface here and then extend this interface in the fetcher(s) package(s).

We could define something like:

import { URL, UrlObject } from 'url'

import { Readable } from 'stream'

export type Body = Readable & {
  text(): Promise<string>
  json(): Promise<unknown>
}

  export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';


export type IFetchResponse = { statusCode: number; headers: Headers; body: Body }
export type Headers = Record<string, string | string[] | undefined>

export type IFetchComponent = {
  request(
    method: HttpMethod,
    url: string | URL | UrlObject,
    headers: Headers,
    body?: string | Buffer | Uint8Array | Readable | null
  ): Promise<IFetchResponse>
}

which is a contract simple enough to be implemented with undici or node-fetch or whatever, and perhaps the fetch package could define its own interface according to the underline implementation. Wdyt? @menduz @marianogoldman @aleortega

@aleortega
Copy link
Contributor

I like the idea, also I would like to adapt the fetch from WKC to return this FetchResponse in case of error instead of throwing since we are seeing more use cases where that is needed to prevent re-work of handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants