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

Use builtin fetch for Node if exists #60

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading