@@ -125,43 +125,53 @@ class Replicate {
125125 * Make a request to the Replicate API.
126126 *
127127 * @param {string } route - REST API endpoint path
128- * @param {object } parameters - Request parameters
129- * @param {string } [parameters.method] - HTTP method. Defaults to GET
130- * @param {object } [parameters.params] - Query parameters
131- * @param {object } [parameters.data] - Body parameters
132- * @returns {Promise<object> } - Resolves with the API response data
128+ * @param {object } options - Request parameters
129+ * @param {string } [options.method] - HTTP method. Defaults to GET
130+ * @param {object } [options.params] - Query parameters
131+ * @param {object|Headers } [options.headers] - HTTP headers
132+ * @param {object } [options.data] - Body parameters
133+ * @returns {Promise<Response> } - Resolves with the response object
133134 * @throws {ApiError } If the request failed
134135 */
135- async request ( route , parameters ) {
136+ async request ( route , options ) {
136137 const { auth, baseUrl, userAgent } = this ;
137138
138- const url = new URL (
139- route . startsWith ( '/' ) ? route . slice ( 1 ) : route ,
140- baseUrl . endsWith ( '/' ) ? baseUrl : `${ baseUrl } /`
141- ) ;
139+ let url ;
140+ if ( route instanceof URL ) {
141+ url = route ;
142+ } else {
143+ url = new URL (
144+ route . startsWith ( '/' ) ? route . slice ( 1 ) : route ,
145+ baseUrl . endsWith ( '/' ) ? baseUrl : `${ baseUrl } /`
146+ ) ;
147+ }
142148
143- const { method = 'GET' , params = { } , data } = parameters ;
149+ const { method = 'GET' , params = { } , data } = options ;
144150
145151 Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
146152 url . searchParams . append ( key , value ) ;
147153 } ) ;
148154
149- const headers = {
150- Authorization : `Token ${ auth } ` ,
151- 'Content-Type' : 'application/json' ,
152- 'User-Agent' : userAgent ,
153- } ;
155+ const headers = new Headers ( ) ;
156+ headers . append ( 'Authorization' , `Token ${ auth } ` ) ;
157+ headers . append ( 'Content-Type' , 'application/json' ) ;
158+ headers . append ( 'User-Agent' , userAgent ) ;
159+ if ( options . headers ) {
160+ options . headers . forEach ( ( value , key ) => {
161+ headers . append ( key , value ) ;
162+ } ) ;
163+ }
154164
155- const options = {
165+ const init = {
156166 method,
157167 headers,
158168 body : data ? JSON . stringify ( data ) : undefined ,
159169 } ;
160170
161- const response = await this . fetch ( url , options ) ;
171+ const response = await this . fetch ( url , init ) ;
162172
163173 if ( ! response . ok ) {
164- const request = new Request ( url , options ) ;
174+ const request = new Request ( url , init ) ;
165175 const responseText = await response . text ( ) ;
166176 throw new ApiError (
167177 `Request to ${ url } failed with status ${ response . status } ${ response . statusText } : ${ responseText } .` ,
@@ -170,7 +180,7 @@ class Replicate {
170180 ) ;
171181 }
172182
173- return response . json ( ) ;
183+ return response ;
174184 }
175185
176186 /**
@@ -188,7 +198,7 @@ class Replicate {
188198 const response = await endpoint ( ) ;
189199 yield response . results ;
190200 if ( response . next ) {
191- const nextPage = ( ) => this . request ( response . next , { method : 'GET' } ) ;
201+ const nextPage = ( ) => this . request ( response . next , { method : 'GET' } ) . then ( ( r ) => r . json ( ) ) ;
192202 yield * this . paginate ( nextPage ) ;
193203 }
194204 }
0 commit comments