1
- import { nanoid } from "nanoid" ;
2
-
3
1
// settings & const
4
2
const DEFAULT_HEADERS = {
5
3
"Content-Type" : "application/json" ,
@@ -21,6 +19,14 @@ class CustomRequest extends Request {
21
19
}
22
20
}
23
21
22
+ /**
23
+ * Returns a cheap, non-cryptographically-secure random ID
24
+ * Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
25
+ */
26
+ export function randomID ( ) {
27
+ return Math . random ( ) . toString ( 36 ) . slice ( 2 , 11 ) ;
28
+ }
29
+
24
30
/**
25
31
* Create an openapi-fetch client.
26
32
* @type {import("./index.js").default }
@@ -84,56 +90,63 @@ export default function createClient(clientOptions) {
84
90
requestInit . headers . delete ( "Content-Type" ) ;
85
91
}
86
92
87
- const id = nanoid ( ) ;
93
+ let id ;
94
+ let options ;
88
95
let request = new CustomRequest ( createFinalURL ( schemaPath , { baseUrl, params, querySerializer } ) , requestInit ) ;
89
96
90
- // middleware (request)
91
- const options = Object . freeze ( {
92
- baseUrl,
93
- fetch,
94
- parseAs,
95
- querySerializer,
96
- bodySerializer,
97
- } ) ;
98
- for ( const m of middlewares ) {
99
- if ( m && typeof m === "object" && typeof m . onRequest === "function" ) {
100
- const result = await m . onRequest ( {
101
- request,
102
- schemaPath,
103
- params,
104
- options,
105
- id,
106
- } ) ;
107
- if ( result ) {
108
- if ( ! ( result instanceof Request ) ) {
109
- throw new Error ( "onRequest: must return new Request() when modifying the request" ) ;
97
+ if ( middlewares . length ) {
98
+ id = randomID ( ) ;
99
+
100
+ // middleware (request)
101
+ options = Object . freeze ( {
102
+ baseUrl,
103
+ fetch,
104
+ parseAs,
105
+ querySerializer,
106
+ bodySerializer,
107
+ } ) ;
108
+ for ( const m of middlewares ) {
109
+ if ( m && typeof m === "object" && typeof m . onRequest === "function" ) {
110
+ const result = await m . onRequest ( {
111
+ request,
112
+ schemaPath,
113
+ params,
114
+ options,
115
+ id,
116
+ } ) ;
117
+ if ( result ) {
118
+ if ( ! ( result instanceof Request ) ) {
119
+ throw new Error ( "onRequest: must return new Request() when modifying the request" ) ;
120
+ }
121
+ request = result ;
110
122
}
111
- request = result ;
112
123
}
113
124
}
114
125
}
115
126
116
127
// fetch!
117
- let response = await fetch ( request . url , request ) ;
128
+ let response = await fetch ( request ) ;
118
129
119
130
// middleware (response)
120
131
// execute in reverse-array order (first priority gets last transform)
121
- for ( let i = middlewares . length - 1 ; i >= 0 ; i -- ) {
122
- const m = middlewares [ i ] ;
123
- if ( m && typeof m === "object" && typeof m . onResponse === "function" ) {
124
- const result = await m . onResponse ( {
125
- request,
126
- response,
127
- schemaPath,
128
- params,
129
- options,
130
- id,
131
- } ) ;
132
- if ( result ) {
133
- if ( ! ( result instanceof Response ) ) {
134
- throw new Error ( "onResponse: must return new Response() when modifying the response" ) ;
132
+ if ( middlewares . length ) {
133
+ for ( let i = middlewares . length - 1 ; i >= 0 ; i -- ) {
134
+ const m = middlewares [ i ] ;
135
+ if ( m && typeof m === "object" && typeof m . onResponse === "function" ) {
136
+ const result = await m . onResponse ( {
137
+ request,
138
+ response,
139
+ schemaPath,
140
+ params,
141
+ options,
142
+ id,
143
+ } ) ;
144
+ if ( result ) {
145
+ if ( ! ( result instanceof Response ) ) {
146
+ throw new Error ( "onResponse: must return new Response() when modifying the response" ) ;
147
+ }
148
+ response = result ;
135
149
}
136
- response = result ;
137
150
}
138
151
}
139
152
}
0 commit comments