Skip to content

Commit 69e9d8f

Browse files
committed
Replace axios with cross-fetch
1 parent ab89817 commit 69e9d8f

File tree

3 files changed

+111
-166
lines changed

3 files changed

+111
-166
lines changed

index.js

Lines changed: 28 additions & 13 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');
@@ -37,14 +37,6 @@ class Replicate {
3737
this.userAgent =
3838
options.userAgent || `replicate-javascript/${packageJSON.version}`;
3939
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';
40-
this.instance = axios.create({
41-
baseURL: this.baseUrl,
42-
headers: {
43-
Authorization: `Token ${this.auth}`,
44-
'User-Agent': this.userAgent,
45-
'Content-Type': 'application/json',
46-
},
47-
});
4840

4941
this.collections = {
5042
get: collections.get.bind(this),
@@ -107,12 +99,35 @@ class Replicate {
10799
* Make a request to the Replicate API.
108100
*
109101
* @param {string} route - REST API endpoint path
110-
* @param {object} parameters - URL, query, and request body parameters for the given route
102+
* @param {object} parameters - Request parameters
103+
* @param {string} [parameters.method] - HTTP method. Defaults to GET
104+
* @param {object} [parameters.params] - Query parameters
105+
* @param {object} [parameters.data] - Body parameters
111106
* @returns {Promise<object>} - Resolves with the API response data
112107
*/
113-
async request(route, parameters) {
114-
const response = await this.instance(route, parameters);
115-
return response.data;
108+
async function(route, parameters) {
109+
const url = new URL(route, this.baseUrl);
110+
const { method = 'GET', params = {}, data } = parameters;
111+
112+
Object.entries(params).forEach(([key, value]) => {
113+
url.searchParams.append(key, value);
114+
});
115+
116+
const headers = {
117+
Authorization: `Token ${this.auth}`,
118+
'Content-Type': 'application/json',
119+
'User-Agent': this.userAgent,
120+
};
121+
122+
const response = await fetch(url, {
123+
method,
124+
headers,
125+
body: data ? JSON.stringify(data) : undefined,
126+
});
127+
128+
if (!response.ok) {
129+
throw new Error(`API request failed: ${response.statusText}`);
130+
}
116131
}
117132

118133
/**

0 commit comments

Comments
 (0)