Skip to content

Commit e2022e6

Browse files
committed
Replace axios with cross-fetch
1 parent 6218765 commit e2022e6

File tree

3 files changed

+84
-4538
lines changed

3 files changed

+84
-4538
lines changed

index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const axios = require('axios');
1+
const fetch = require('cross-fetch');
22

33
const collections = require('./lib/collections');
44
const models = require('./lib/models');
@@ -38,14 +38,6 @@ class Replicate {
3838
this.userAgent =
3939
options.userAgent || `replicate-javascript/${packageJSON.version}`;
4040
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';
41-
this.instance = axios.create({
42-
baseURL: this.baseUrl,
43-
headers: {
44-
Authorization: `Token ${this.auth}`,
45-
'User-Agent': this.userAgent,
46-
'Content-Type': 'application/json',
47-
},
48-
});
4941

5042
this.collections = {
5143
get: collections.get.bind(this),
@@ -115,12 +107,43 @@ class Replicate {
115107
* Make a request to the Replicate API.
116108
*
117109
* @param {string} route - REST API endpoint path
118-
* @param {object} parameters - URL, query, and request body parameters for the given route
110+
* @param {object} parameters - Request parameters
111+
* @param {string} [parameters.method] - HTTP method. Defaults to GET
112+
* @param {object} [parameters.params] - Query parameters
113+
* @param {object} [parameters.data] - Body parameters
119114
* @returns {Promise<object>} - Resolves with the API response data
120115
*/
121116
async request(route, parameters) {
122-
const response = await this.instance(route, parameters);
123-
return response.data;
117+
const { auth, baseUrl, userAgent } = this;
118+
119+
const url = new URL(
120+
route.startsWith('/') ? route.slice(1) : route,
121+
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`
122+
);
123+
124+
const { method = 'GET', params = {}, data } = parameters;
125+
126+
Object.entries(params).forEach(([key, value]) => {
127+
url.searchParams.append(key, value);
128+
});
129+
130+
const headers = {
131+
Authorization: `Token ${auth}`,
132+
'Content-Type': 'application/json',
133+
'User-Agent': userAgent,
134+
};
135+
136+
const response = await fetch(url, {
137+
method,
138+
headers,
139+
body: data ? JSON.stringify(data) : undefined,
140+
});
141+
142+
if (!response.ok) {
143+
throw new Error(`API request failed: ${response.statusText}`);
144+
}
145+
146+
return response.json();
124147
}
125148

126149
/**

0 commit comments

Comments
 (0)