Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Use builtin fetch for Node if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Apr 13, 2024
1 parent e33a2f3 commit 03709a5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const VERSION = '0.0.3';
const RETRY_STATUS_CODES = [429, 500, 502, 503, 504];
const ENDPOINT = 'https://api.mistral.ai';

let mistralFetch;

/**
* Initialize fetch
* @return {Promise<void>}
*/
async function initializeFetch() {
if (typeof window === 'undefined' ||
if (typeof window === 'undefined' &&
typeof globalThis.fetch === 'undefined') {
const nodeFetch = await import('node-fetch');
fetch = nodeFetch.default;
mistralFetch = nodeFetch.default;
isNode = true;
} else {
fetch = globalThis.fetch;
mistralFetch = globalThis.fetch;
}
}

initializeFetch();
await initializeFetch();

/**
* MistralAPIError
Expand Down Expand Up @@ -90,7 +92,7 @@ class MistralClient {

for (let attempts = 0; attempts < this.maxRetries; attempts++) {
try {
const response = await fetch(url, options);
const response = await mistralFetch(url, options);

if (response.ok) {
if (request?.stream) {
Expand Down

0 comments on commit 03709a5

Please sign in to comment.