Skip to content

Commit b339725

Browse files
authored
Replace axios with native fetch (#31)
* Replace axios with cross-fetch * Use native, overridable fetch
1 parent 6218765 commit b339725

File tree

5 files changed

+94
-4544
lines changed

5 files changed

+94
-4544
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ declare module 'replicate' {
6666
auth: string;
6767
userAgent?: string;
6868
baseUrl?: string;
69-
private instance: any;
69+
fetch: Function;
7070

7171
run(
7272
identifier: `${string}/${string}:${string}`,

index.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const axios = require('axios');
2-
31
const collections = require('./lib/collections');
42
const models = require('./lib/models');
53
const predictions = require('./lib/predictions');
@@ -32,20 +30,14 @@ class Replicate {
3230
* @param {string} options.auth - Required. API access token
3331
* @param {string} options.userAgent - Identifier of your app
3432
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
33+
* @param {Function} [options.fetch] - Defaults to native fetch
3534
*/
3635
constructor(options) {
3736
this.auth = options.auth;
3837
this.userAgent =
3938
options.userAgent || `replicate-javascript/${packageJSON.version}`;
4039
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-
});
40+
this.fetch = fetch;
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 this.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
/**

index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
import { expect, jest, test } from '@jest/globals';
23
import Replicate, { Prediction } from 'replicate';
34
import nock from 'nock';
5+
import fetch from 'cross-fetch';
46

57
describe('Replicate client', () => {
68
let client: Replicate;
@@ -9,6 +11,7 @@ describe('Replicate client', () => {
911

1012
beforeEach(() => {
1113
client = new Replicate({ auth: 'test-token' });
14+
client.fetch = fetch;
1215
});
1316

1417
describe('constructor', () => {

0 commit comments

Comments
 (0)