Skip to content

Replace axios with native fetch #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ declare module 'replicate' {
auth: string;
userAgent?: string;
baseUrl?: string;
private instance: any;
fetch: Function;

run(
identifier: `${string}/${string}:${string}`,
Expand Down
49 changes: 36 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const axios = require('axios');

const collections = require('./lib/collections');
const models = require('./lib/models');
const predictions = require('./lib/predictions');
Expand Down Expand Up @@ -32,20 +30,14 @@ class Replicate {
* @param {string} options.auth - Required. API access token
* @param {string} options.userAgent - Identifier of your app
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
* @param {Function} [options.fetch] - Defaults to native fetch
*/
constructor(options) {
this.auth = options.auth;
this.userAgent =
options.userAgent || `replicate-javascript/${packageJSON.version}`;
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';
this.instance = axios.create({
baseURL: this.baseUrl,
headers: {
Authorization: `Token ${this.auth}`,
'User-Agent': this.userAgent,
'Content-Type': 'application/json',
},
});
this.fetch = fetch;

this.collections = {
get: collections.get.bind(this),
Expand Down Expand Up @@ -115,12 +107,43 @@ class Replicate {
* Make a request to the Replicate API.
*
* @param {string} route - REST API endpoint path
* @param {object} parameters - URL, query, and request body parameters for the given route
* @param {object} parameters - Request parameters
* @param {string} [parameters.method] - HTTP method. Defaults to GET
* @param {object} [parameters.params] - Query parameters
* @param {object} [parameters.data] - Body parameters
* @returns {Promise<object>} - Resolves with the API response data
*/
async request(route, parameters) {
const response = await this.instance(route, parameters);
return response.data;
const { auth, baseUrl, userAgent } = this;

const url = new URL(
route.startsWith('/') ? route.slice(1) : route,
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`
);

const { method = 'GET', params = {}, data } = parameters;

Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, value);
});

const headers = {
Authorization: `Token ${auth}`,
'Content-Type': 'application/json',
'User-Agent': userAgent,
};

const response = await this.fetch(url, {
method,
headers,
body: data ? JSON.stringify(data) : undefined,
});

if (!response.ok) {
throw new Error(`API request failed: ${response.statusText}`);
}

return response.json();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import { expect, jest, test } from '@jest/globals';
import Replicate, { Prediction } from 'replicate';
import nock from 'nock';
import fetch from 'cross-fetch';

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

beforeEach(() => {
client = new Replicate({ auth: 'test-token' });
client.fetch = fetch;
});

describe('constructor', () => {
Expand Down
Loading