-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core-client] Switch _response property for callback. #13132
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
InternalPipelineOptions | ||
} from "@azure/core-https"; | ||
import { | ||
OperationResponse, | ||
OperationArguments, | ||
OperationSpec, | ||
OperationRequest, | ||
|
@@ -128,10 +127,10 @@ export class ServiceClient { | |
* @param {OperationArguments} operationArguments The arguments that the HTTP request's templated values will be populated from. | ||
* @param {OperationSpec} operationSpec The OperationSpec to use to populate the httpRequest. | ||
*/ | ||
async sendOperationRequest( | ||
async sendOperationRequest<T>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider adding doc for type parameter? https://typedoc.org/guides/doccomments/#%40typeparam-%3Cparam-name%3E There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, didn't know about this tag. Added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: It is also supported by TSDoc: https://tsdoc.org/pages/tags/typeparam/ |
||
operationArguments: OperationArguments, | ||
operationSpec: OperationSpec | ||
): Promise<OperationResponse> { | ||
): Promise<T> { | ||
const baseUri: string | undefined = operationSpec.baseUrl || this._baseUri; | ||
if (!baseUri) { | ||
throw new Error( | ||
|
@@ -200,7 +199,14 @@ export class ServiceClient { | |
|
||
try { | ||
const rawResponse = await this.sendRequest(request); | ||
return flattenResponse(rawResponse, operationSpec.responses[rawResponse.status]); | ||
const flatResponse = flattenResponse( | ||
rawResponse, | ||
operationSpec.responses[rawResponse.status] | ||
) as T; | ||
if (options?.onResponse) { | ||
options.onResponse(rawResponse, flatResponse); | ||
} | ||
return flatResponse; | ||
} catch (error) { | ||
if (error.response) { | ||
error.details = flattenResponse( | ||
|
@@ -285,29 +291,18 @@ export function createClientPipeline(options: ClientPipelineOptions = {}): Pipel | |
function flattenResponse( | ||
fullResponse: FullOperationResponse, | ||
responseSpec: OperationResponseMap | undefined | ||
): OperationResponse { | ||
): unknown { | ||
const parsedHeaders = fullResponse.parsedHeaders; | ||
const bodyMapper = responseSpec && responseSpec.bodyMapper; | ||
|
||
function addResponse<T extends object>( | ||
obj: T | ||
): T & { readonly _response: FullOperationResponse } { | ||
return Object.defineProperty(obj, "_response", { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: fullResponse | ||
}); | ||
} | ||
|
||
if (bodyMapper) { | ||
const typeName = bodyMapper.type.name; | ||
if (typeName === "Stream") { | ||
return addResponse({ | ||
return { | ||
...parsedHeaders, | ||
blobBody: fullResponse.blobBody, | ||
readableStreamBody: fullResponse.readableStreamBody | ||
}); | ||
}; | ||
} | ||
|
||
const modelProperties = | ||
|
@@ -330,14 +325,14 @@ function flattenResponse( | |
arrayResponse[key] = parsedHeaders[key]; | ||
} | ||
} | ||
return addResponse(arrayResponse); | ||
return arrayResponse; | ||
} | ||
|
||
if (typeName === "Composite" || typeName === "Dictionary") { | ||
return addResponse({ | ||
return { | ||
...parsedHeaders, | ||
...fullResponse.parsedBody | ||
}); | ||
}; | ||
} | ||
} | ||
|
||
|
@@ -346,16 +341,16 @@ function flattenResponse( | |
fullResponse.request.method === "HEAD" || | ||
isPrimitiveType(fullResponse.parsedBody) | ||
) { | ||
return addResponse({ | ||
return { | ||
...parsedHeaders, | ||
body: fullResponse.parsedBody | ||
}); | ||
}; | ||
} | ||
|
||
return addResponse({ | ||
return { | ||
...parsedHeaders, | ||
...fullResponse.parsedBody | ||
}); | ||
}; | ||
} | ||
|
||
function getCredentialScopes(options: ServiceClientOptions): string | string[] | undefined { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make the second argument optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional here would mean the caller doesn't need to pass it, but we should always have something to pass along, right?
The implementer of the callback doesn't need to receive the parameter or do anything with it, e.g. this works