|
1 | | -const axios = require('axios'); |
2 | | - |
3 | 1 | const collections = require('./lib/collections'); |
4 | 2 | const models = require('./lib/models'); |
5 | 3 | const predictions = require('./lib/predictions'); |
@@ -32,20 +30,14 @@ class Replicate { |
32 | 30 | * @param {string} options.auth - Required. API access token |
33 | 31 | * @param {string} options.userAgent - Identifier of your app |
34 | 32 | * @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1 |
| 33 | + * @param {Function} [options.fetch] - Defaults to native fetch |
35 | 34 | */ |
36 | 35 | constructor(options) { |
37 | 36 | this.auth = options.auth; |
38 | 37 | this.userAgent = |
39 | 38 | options.userAgent || `replicate-javascript/${packageJSON.version}`; |
40 | 39 | 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; |
49 | 41 |
|
50 | 42 | this.collections = { |
51 | 43 | get: collections.get.bind(this), |
@@ -115,12 +107,43 @@ class Replicate { |
115 | 107 | * Make a request to the Replicate API. |
116 | 108 | * |
117 | 109 | * @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 |
119 | 114 | * @returns {Promise<object>} - Resolves with the API response data |
120 | 115 | */ |
121 | 116 | 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(); |
124 | 147 | } |
125 | 148 |
|
126 | 149 | /** |
|
0 commit comments