@@ -13,7 +13,6 @@ import {
13
13
InternalPipelineOptions
14
14
} from "@azure/core-https" ;
15
15
import {
16
- OperationResponse ,
17
16
OperationArguments ,
18
17
OperationSpec ,
19
18
OperationRequest ,
@@ -125,13 +124,14 @@ export class ServiceClient {
125
124
126
125
/**
127
126
* Send an HTTP request that is populated using the provided OperationSpec.
127
+ * @typeParam T The typed result of the request, based on the OperationSpec.
128
128
* @param {OperationArguments } operationArguments The arguments that the HTTP request's templated values will be populated from.
129
129
* @param {OperationSpec } operationSpec The OperationSpec to use to populate the httpRequest.
130
130
*/
131
- async sendOperationRequest (
131
+ async sendOperationRequest < T > (
132
132
operationArguments : OperationArguments ,
133
133
operationSpec : OperationSpec
134
- ) : Promise < OperationResponse > {
134
+ ) : Promise < T > {
135
135
const baseUri : string | undefined = operationSpec . baseUrl || this . _baseUri ;
136
136
if ( ! baseUri ) {
137
137
throw new Error (
@@ -200,7 +200,14 @@ export class ServiceClient {
200
200
201
201
try {
202
202
const rawResponse = await this . sendRequest ( request ) ;
203
- return flattenResponse ( rawResponse , operationSpec . responses [ rawResponse . status ] ) ;
203
+ const flatResponse = flattenResponse (
204
+ rawResponse ,
205
+ operationSpec . responses [ rawResponse . status ]
206
+ ) as T ;
207
+ if ( options ?. onResponse ) {
208
+ options . onResponse ( rawResponse , flatResponse ) ;
209
+ }
210
+ return flatResponse ;
204
211
} catch ( error ) {
205
212
if ( error . response ) {
206
213
error . details = flattenResponse (
@@ -285,29 +292,18 @@ export function createClientPipeline(options: ClientPipelineOptions = {}): Pipel
285
292
function flattenResponse (
286
293
fullResponse : FullOperationResponse ,
287
294
responseSpec : OperationResponseMap | undefined
288
- ) : OperationResponse {
295
+ ) : unknown {
289
296
const parsedHeaders = fullResponse . parsedHeaders ;
290
297
const bodyMapper = responseSpec && responseSpec . bodyMapper ;
291
298
292
- function addResponse < T extends object > (
293
- obj : T
294
- ) : T & { readonly _response : FullOperationResponse } {
295
- return Object . defineProperty ( obj , "_response" , {
296
- configurable : false ,
297
- enumerable : false ,
298
- writable : false ,
299
- value : fullResponse
300
- } ) ;
301
- }
302
-
303
299
if ( bodyMapper ) {
304
300
const typeName = bodyMapper . type . name ;
305
301
if ( typeName === "Stream" ) {
306
- return addResponse ( {
302
+ return {
307
303
...parsedHeaders ,
308
304
blobBody : fullResponse . blobBody ,
309
305
readableStreamBody : fullResponse . readableStreamBody
310
- } ) ;
306
+ } ;
311
307
}
312
308
313
309
const modelProperties =
@@ -330,14 +326,14 @@ function flattenResponse(
330
326
arrayResponse [ key ] = parsedHeaders [ key ] ;
331
327
}
332
328
}
333
- return addResponse ( arrayResponse ) ;
329
+ return arrayResponse ;
334
330
}
335
331
336
332
if ( typeName === "Composite" || typeName === "Dictionary" ) {
337
- return addResponse ( {
333
+ return {
338
334
...parsedHeaders ,
339
335
...fullResponse . parsedBody
340
- } ) ;
336
+ } ;
341
337
}
342
338
}
343
339
@@ -346,16 +342,16 @@ function flattenResponse(
346
342
fullResponse . request . method === "HEAD" ||
347
343
isPrimitiveType ( fullResponse . parsedBody )
348
344
) {
349
- return addResponse ( {
345
+ return {
350
346
...parsedHeaders ,
351
347
body : fullResponse . parsedBody
352
- } ) ;
348
+ } ;
353
349
}
354
350
355
- return addResponse ( {
351
+ return {
356
352
...parsedHeaders ,
357
353
...fullResponse . parsedBody
358
- } ) ;
354
+ } ;
359
355
}
360
356
361
357
function getCredentialScopes ( options : ServiceClientOptions ) : string | string [ ] | undefined {
0 commit comments