11// settings & const
22const PATH_PARAM_RE = / \{ [ ^ { } ] + \} / g;
33
4- /** Add custom parameters to Request object */
5- class CustomRequest extends Request {
6- constructor ( input , init ) {
7- super ( input , init ) ;
8-
9- // add custom parameters
10- for ( const key in init ) {
11- if ( ! ( key in this ) ) {
12- this [ key ] = init [ key ] ;
13- }
14- }
15- }
16- }
17-
184/**
195 * Returns a cheap, non-cryptographically-secure random ID
206 * Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
@@ -30,6 +16,7 @@ export function randomID() {
3016export default function createClient ( clientOptions ) {
3117 let {
3218 baseUrl = "" ,
19+ Request : CustomRequest = globalThis . Request ,
3320 fetch : baseFetch = globalThis . fetch ,
3421 querySerializer : globalQuerySerializer ,
3522 bodySerializer : globalBodySerializer ,
@@ -48,6 +35,7 @@ export default function createClient(clientOptions) {
4835 const {
4936 baseUrl : localBaseUrl ,
5037 fetch = baseFetch ,
38+ Request = CustomRequest ,
5139 headers,
5240 params = { } ,
5341 parseAs = "json" ,
@@ -98,6 +86,13 @@ export default function createClient(clientOptions) {
9886 let options ;
9987 let request = new CustomRequest ( createFinalURL ( schemaPath , { baseUrl, params, querySerializer } ) , requestInit ) ;
10088
89+ /** Add custom parameters to Request object */
90+ for ( const key in init ) {
91+ if ( ! ( key in request ) ) {
92+ request [ key ] = init [ key ] ;
93+ }
94+ }
95+
10196 if ( middlewares . length ) {
10297 id = randomID ( ) ;
10398
@@ -119,7 +114,7 @@ export default function createClient(clientOptions) {
119114 id,
120115 } ) ;
121116 if ( result ) {
122- if ( ! ( result instanceof Request ) ) {
117+ if ( ! ( result instanceof CustomRequest ) ) {
123118 throw new Error ( "onRequest: must return new Request() when modifying the request" ) ;
124119 }
125120 request = result ;
0 commit comments