Skip to content

Commit

Permalink
fix: needs runtime node-fetch import
Browse files Browse the repository at this point in the history
`import ... from "node-fetch"` will throw in Edge runtime and browser
because the library attempts to make `node:...` package imports.

The only way to consume this package is to load it when it's needed with
`await import(...)`, and not as an import statement at the top of the
context.

It is the difference between always loading it and only using it when
needed, and only loading it and using it when needed.

closes #50
cc @transmissions11
  • Loading branch information
ctjlewis committed Jun 22, 2023
1 parent 1e9c399 commit fefa768
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/lib/backoff.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/* eslint-disable no-console */
import nodeFetch, { RequestInfo as NodeFetchRequestInfo, RequestInit as NodeFetchRequestInit } from "node-fetch";
import type NodeFetch from "node-fetch";
import type { RequestInfo as NodeFetchRequestInfo, RequestInit as NodeFetchRequestInit } from "node-fetch";

type Fetch = typeof NodeFetch | typeof fetch;

export interface BackoffOptions {
maxRetries: number;
delay: number;
}

const globalFetch = typeof fetch === "undefined" ? nodeFetch : fetch;

export const fetchWithBackoff = async (
input: RequestInfo & NodeFetchRequestInfo,
init?: RequestInit & NodeFetchRequestInit,
fetch = globalFetch,
fetch?: Fetch,
{ delay, maxRetries }: BackoffOptions = {
delay: 500,
maxRetries: 7
}
) => {
let _fetch: Fetch | undefined = fetch;

if (!_fetch) {
const { default: nodeFetch } = await import("node-fetch");

This comment has been minimized.

Copy link
@ctjlewis

ctjlewis Jun 22, 2023

Author Collaborator

Whoops!

_fetch = nodeFetch;
}

for (let i = 0; i <= maxRetries; i++) {
try {
if (!fetch) {
throw new Error("No fetch implementation.");
}

const response = await fetch(input, init);
const response = await _fetch(input, init);

if (!response.ok) {
const errorData = await response.json();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ export interface OpenAIOptions extends OpenAIStreamOptions {
*/
controller?: AbortController;
/**
* An optional custom fetch implementation, which will be used to replace the default fetch/node-fetch
* call used for making API requests in edge/dom and node environments respectively.
* An optional custom fetch implementation, which will be used to replace the
* default fetch/node-fetch call used for making API requests in edge/dom and
* node environments respectively.
*/
fetch?: typeof fetch | typeof nodeFetch;
}
Expand Down

1 comment on commit fefa768

@vercel
Copy link

@vercel vercel bot commented on fefa768 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.