File tree 4 files changed +35
-5
lines changed
4 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -206,16 +206,17 @@ And it expects either:
206
206
### onResponse
207
207
208
208
``` ts
209
- onResponse (res , options ) {
209
+ onResponse (res , options , req ) {
210
210
// …
211
211
}
212
212
```
213
213
214
- ` onResponse() ` also takes 2 params:
214
+ ` onResponse() ` also takes 3 params:
215
215
| Name | Type | Description |
216
216
| :-------- | :-----------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
217
- | ` req ` | ` MiddlewareRequest ` | A standard [ Response] ( https://developer.mozilla.org/en-US/docs/Web/API/Response ) . |
217
+ | ` req ` | ` Response ` | A standard [ Response] ( https://developer.mozilla.org/en-US/docs/Web/API/Response ) . |
218
218
| ` options ` | ` MergedOptions ` | Combination of [ createClient] ( /openapi-fetch/api#create-client ) options + [ fetch overrides] ( /openapi-fetch/api#fetch-options ) |
219
+ | ` req ` | ` MiddlewareRequest ` | A standard [ Request] ( https://developer.mozilla.org/en-US/docs/Web/API/Request ) with ` schemaPath ` (OpenAPI pathname) and ` params ` ([ params] ( /openapi-fetch/api#fetch-options ) object) |
219
220
220
221
And it expects either:
221
222
Original file line number Diff line number Diff line change @@ -144,7 +144,11 @@ export function onRequest(
144
144
req : MiddlewareRequest ,
145
145
options : MergedOptions ,
146
146
) : Request | undefined | Promise < Request | undefined > ;
147
- export function onResponse ( res : Response , options : MergedOptions ) : Response | undefined | Promise < Response | undefined > ;
147
+ export function onResponse (
148
+ res : Response ,
149
+ options : MergedOptions ,
150
+ req : MiddlewareRequest ,
151
+ ) : Response | undefined | Promise < Response | undefined > ;
148
152
149
153
export interface Middleware {
150
154
onRequest ?: typeof onRequest ;
Original file line number Diff line number Diff line change @@ -114,7 +114,9 @@ export default function createClient(clientOptions) {
114
114
for ( let i = middlewares . length - 1 ; i >= 0 ; i -- ) {
115
115
const m = middlewares [ i ] ;
116
116
if ( m && typeof m === "object" && typeof m . onResponse === "function" ) {
117
- const result = await m . onResponse ( response , mergedOptions ) ;
117
+ request . schemaPath = url ; // (re)attach original URL
118
+ request . params = params ; // (re)attach params
119
+ const result = await m . onResponse ( response , mergedOptions , request ) ;
118
120
if ( result ) {
119
121
if ( ! ( result instanceof Response ) ) {
120
122
throw new Error ( "Middleware must return new Response() when modifying the response" ) ;
Original file line number Diff line number Diff line change @@ -1013,6 +1013,29 @@ describe("client", () => {
1013
1013
expect ( requestBaseUrl ) . toBe ( "https://api.foo.bar/v1" ) ;
1014
1014
} ) ;
1015
1015
1016
+ it ( "receives the original request" , async ( ) => {
1017
+ useMockRequestHandler ( {
1018
+ baseUrl : "https://api.foo.bar/v1/" ,
1019
+ method : "get" ,
1020
+ path : "/self" ,
1021
+ status : 200 ,
1022
+ body : { } ,
1023
+ } ) ;
1024
+
1025
+ const client = createClient < paths > ( {
1026
+ baseUrl : "https://api.foo.bar/v1/" ,
1027
+ } ) ;
1028
+ client . use ( {
1029
+ onResponse ( res , options , req ) {
1030
+ expect ( req ) . toBeInstanceOf ( Request ) ;
1031
+
1032
+ return undefined ;
1033
+ } ,
1034
+ } ) ;
1035
+
1036
+ await client . GET ( "/self" ) ;
1037
+ } ) ;
1038
+
1016
1039
it ( "receives OpenAPI options passed in from parent" , async ( ) => {
1017
1040
useMockRequestHandler ( {
1018
1041
method : "put" ,
You can’t perform that action at this time.
0 commit comments