From 03709a5c61d77d8dd8f4b80520f3aa2e2903826a Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Sat, 13 Apr 2024 16:49:07 -0700 Subject: [PATCH] Use builtin fetch for Node if exists --- src/client.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/client.js b/src/client.js index 10def72..8870b7b 100644 --- a/src/client.js +++ b/src/client.js @@ -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} */ 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 @@ -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) {