|
1 | | -const axios = require('axios'); |
| 1 | +const fetch = require('cross-fetch'); |
2 | 2 |
|
3 | 3 | const collections = require('./lib/collections'); |
4 | 4 | const models = require('./lib/models'); |
@@ -37,14 +37,6 @@ class Replicate { |
37 | 37 | this.userAgent = |
38 | 38 | options.userAgent || `replicate-javascript/${packageJSON.version}`; |
39 | 39 | 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 | | - }); |
48 | 40 |
|
49 | 41 | this.collections = { |
50 | 42 | get: collections.get.bind(this), |
@@ -107,12 +99,35 @@ class Replicate { |
107 | 99 | * Make a request to the Replicate API. |
108 | 100 | * |
109 | 101 | * @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 |
111 | 106 | * @returns {Promise<object>} - Resolves with the API response data |
112 | 107 | */ |
113 | 108 | async request(route, parameters) { |
114 | | - const response = await this.instance(route, parameters); |
115 | | - return response.data; |
| 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 | + } |
116 | 131 | } |
117 | 132 |
|
118 | 133 | /** |
|
0 commit comments