@azure/core-client
Advanced tools
Comparing version 1.0.0-alpha.20201203.2 to 1.0.0-alpha.20201210.1
@@ -8,2 +8,3 @@ // Copyright (c) Microsoft Corporation. | ||
export { deserializationPolicy, deserializationPolicyName } from "./deserializationPolicy"; | ||
export { serializationPolicy, serializationPolicyName } from "./serializationPolicy"; | ||
//# sourceMappingURL=index.js.map |
@@ -5,10 +5,8 @@ // Copyright (c) Microsoft Corporation. | ||
import { DefaultHttpsClient, createPipelineRequest, createPipelineFromOptions, bearerTokenAuthenticationPolicy } from "@azure/core-https"; | ||
import { XML_CHARKEY, XML_ATTRKEY } from "./interfaces"; | ||
import { getPathStringFromParameter, isStreamOperation } from "./interfaceHelpers"; | ||
import { MapperTypeNames } from "./serializer"; | ||
import { isStreamOperation } from "./interfaceHelpers"; | ||
import { getRequestUrl } from "./urlHelpers"; | ||
import { isPrimitiveType } from "./utils"; | ||
import { getOperationArgumentValueFromParameter } from "./operationHelpers"; | ||
import { deserializationPolicy } from "./deserializationPolicy"; | ||
import { URL } from "./url"; | ||
import { serializationPolicy } from "./serializationPolicy"; | ||
/** | ||
@@ -28,3 +26,2 @@ * Initializes a new instance of the ServiceClient. | ||
this._httpsClient = options.httpsClient || new DefaultHttpsClient(); | ||
this._stringifyXML = options.stringifyXML; | ||
const credentialScopes = getCredentialScopes(options); | ||
@@ -36,3 +33,4 @@ this._pipeline = | ||
credential: options.credential, | ||
parseXML: options.parseXML | ||
parseXML: options.parseXML, | ||
stringifyXML: options.stringifyXML | ||
}); | ||
@@ -70,2 +68,3 @@ } | ||
request.additionalInfo.operationSpec = operationSpec; | ||
request.additionalInfo.operationArguments = operationArguments; | ||
const contentType = operationSpec.contentType || this._requestContentType; | ||
@@ -75,20 +74,2 @@ if (contentType) { | ||
} | ||
if (operationSpec.headerParameters) { | ||
for (const headerParameter of operationSpec.headerParameters) { | ||
let headerValue = getOperationArgumentValueFromParameter(operationArguments, headerParameter); | ||
if (headerValue !== null && headerValue !== undefined) { | ||
headerValue = operationSpec.serializer.serialize(headerParameter.mapper, headerValue, getPathStringFromParameter(headerParameter)); | ||
const headerCollectionPrefix = headerParameter.mapper | ||
.headerCollectionPrefix; | ||
if (headerCollectionPrefix) { | ||
for (const key of Object.keys(headerValue)) { | ||
request.headers.set(headerCollectionPrefix + key, headerValue[key]); | ||
} | ||
} | ||
else { | ||
request.headers.set(headerParameter.mapper.serializedName || getPathStringFromParameter(headerParameter), headerValue); | ||
} | ||
} | ||
} | ||
} | ||
const options = operationArguments.options; | ||
@@ -123,3 +104,2 @@ if (options) { | ||
} | ||
serializeRequestBody(request, operationArguments, operationSpec, this._stringifyXML); | ||
if (request.streamResponseBody === undefined) { | ||
@@ -141,81 +121,2 @@ request.streamResponseBody = isStreamOperation(operationSpec); | ||
} | ||
/** | ||
* @internal @ignore | ||
*/ | ||
export function serializeRequestBody(request, operationArguments, operationSpec, stringifyXML = function () { | ||
throw new Error("XML serialization unsupported!"); | ||
}) { | ||
var _a, _b, _c, _d, _e; | ||
const serializerOptions = (_a = operationArguments.options) === null || _a === void 0 ? void 0 : _a.serializerOptions; | ||
const updatedOptions = { | ||
xml: { | ||
rootName: (_b = serializerOptions === null || serializerOptions === void 0 ? void 0 : serializerOptions.xml.rootName) !== null && _b !== void 0 ? _b : "", | ||
includeRoot: (_c = serializerOptions === null || serializerOptions === void 0 ? void 0 : serializerOptions.xml.includeRoot) !== null && _c !== void 0 ? _c : false, | ||
xmlCharKey: (_d = serializerOptions === null || serializerOptions === void 0 ? void 0 : serializerOptions.xml.xmlCharKey) !== null && _d !== void 0 ? _d : XML_CHARKEY | ||
} | ||
}; | ||
const xmlCharKey = updatedOptions.xml.xmlCharKey; | ||
if (operationSpec.requestBody && operationSpec.requestBody.mapper) { | ||
request.body = getOperationArgumentValueFromParameter(operationArguments, operationSpec.requestBody); | ||
const bodyMapper = operationSpec.requestBody.mapper; | ||
const { required, serializedName, xmlName, xmlElementName, xmlNamespace, xmlNamespacePrefix } = bodyMapper; | ||
const typeName = bodyMapper.type.name; | ||
try { | ||
if (request.body || required) { | ||
const requestBodyParameterPathString = getPathStringFromParameter(operationSpec.requestBody); | ||
request.body = operationSpec.serializer.serialize(bodyMapper, request.body, requestBodyParameterPathString, updatedOptions); | ||
const isStream = typeName === MapperTypeNames.Stream; | ||
if (operationSpec.isXML) { | ||
const xmlnsKey = xmlNamespacePrefix ? `xmlns:${xmlNamespacePrefix}` : "xmlns"; | ||
const value = getXmlValueWithNamespace(xmlNamespace, xmlnsKey, typeName, request.body, updatedOptions); | ||
if (typeName === MapperTypeNames.Sequence) { | ||
request.body = stringifyXML(prepareXMLRootList(value, xmlElementName || xmlName || serializedName, xmlnsKey, xmlNamespace), { rootName: xmlName || serializedName, xmlCharKey }); | ||
} | ||
else if (!isStream) { | ||
request.body = stringifyXML(value, { | ||
rootName: xmlName || serializedName, | ||
xmlCharKey | ||
}); | ||
} | ||
} | ||
else if (typeName === MapperTypeNames.String && | ||
(((_e = operationSpec.contentType) === null || _e === void 0 ? void 0 : _e.match("text/plain")) || operationSpec.mediaType === "text")) { | ||
// the String serializer has validated that request body is a string | ||
// so just send the string. | ||
return; | ||
} | ||
else if (!isStream) { | ||
request.body = JSON.stringify(request.body); | ||
} | ||
} | ||
} | ||
catch (error) { | ||
throw new Error(`Error "${error.message}" occurred in serializing the payload - ${JSON.stringify(serializedName, undefined, " ")}.`); | ||
} | ||
} | ||
else if (operationSpec.formDataParameters && operationSpec.formDataParameters.length > 0) { | ||
request.formData = {}; | ||
for (const formDataParameter of operationSpec.formDataParameters) { | ||
const formDataParameterValue = getOperationArgumentValueFromParameter(operationArguments, formDataParameter); | ||
if (formDataParameterValue !== undefined && formDataParameterValue !== null) { | ||
const formDataParameterPropertyName = formDataParameter.mapper.serializedName || getPathStringFromParameter(formDataParameter); | ||
request.formData[formDataParameterPropertyName] = operationSpec.serializer.serialize(formDataParameter.mapper, formDataParameterValue, getPathStringFromParameter(formDataParameter), updatedOptions); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Adds an xml namespace to the xml serialized object if needed, otherwise it just returns the value itself | ||
*/ | ||
function getXmlValueWithNamespace(xmlNamespace, xmlnsKey, typeName, serializedValue, options) { | ||
// Composite and Sequence schemas already got their root namespace set during serialization | ||
// We just need to add xmlns to the other schema types | ||
if (xmlNamespace && !["Composite", "Sequence", "Dictionary"].includes(typeName)) { | ||
const result = {}; | ||
result[options.xml.xmlCharKey] = serializedValue; | ||
result[XML_ATTRKEY] = { [xmlnsKey]: xmlNamespace }; | ||
return result; | ||
} | ||
return serializedValue; | ||
} | ||
function createDefaultPipeline(options = {}) { | ||
@@ -229,2 +130,5 @@ const credentialOptions = options.credential && options.credentialScopes | ||
parseXML: options.parseXML | ||
}, | ||
serializationOptions: { | ||
stringifyXML: options.stringifyXML | ||
} | ||
@@ -247,16 +151,8 @@ }); | ||
} | ||
pipeline.addPolicy(deserializationPolicy(options.deserializationOptions), { phase: "Serialize" }); | ||
pipeline.addPolicy(serializationPolicy(options.serializationOptions), { phase: "Serialize" }); | ||
pipeline.addPolicy(deserializationPolicy(options.deserializationOptions), { | ||
phase: "Deserialize" | ||
}); | ||
return pipeline; | ||
} | ||
function prepareXMLRootList(obj, elementName, xmlNamespaceKey, xmlNamespace) { | ||
if (!Array.isArray(obj)) { | ||
obj = [obj]; | ||
} | ||
if (!xmlNamespaceKey || !xmlNamespace) { | ||
return { [elementName]: obj }; | ||
} | ||
const result = { [elementName]: obj }; | ||
result[XML_ATTRKEY] = { [xmlNamespaceKey]: xmlNamespace }; | ||
return result; | ||
} | ||
function flattenResponse(fullResponse, responseSpec) { | ||
@@ -263,0 +159,0 @@ var _a, _b; |
{ | ||
"name": "@azure/core-client", | ||
"version": "1.0.0-alpha.20201203.2", | ||
"version": "1.0.0-alpha.20201210.1", | ||
"description": "Core library for interfacing with AutoRest generated code", | ||
@@ -5,0 +5,0 @@ "sdk-type": "client", |
@@ -88,2 +88,6 @@ import { AbortSignalLike } from '@azure/abort-controller'; | ||
deserializationOptions?: DeserializationPolicyOptions; | ||
/** | ||
* Options to customize serializationPolicy. | ||
*/ | ||
serializationOptions?: serializationPolicyOptions; | ||
} | ||
@@ -318,2 +322,6 @@ export declare interface CompositeMapper extends BaseMapper { | ||
/** | ||
* Used to encode the request. | ||
*/ | ||
operationArguments?: OperationArguments; | ||
/** | ||
* A function that returns the proper OperationResponseMap for the given OperationSpec and | ||
@@ -485,2 +493,24 @@ * PipelineResponse combination. If this is undefined, then a simple status code lookup will | ||
/** | ||
* This policy handles assembling the request body and headers using | ||
* an OperationSpec and OperationArguments on the request. | ||
*/ | ||
export declare function serializationPolicy(options?: serializationPolicyOptions): PipelinePolicy; | ||
/** | ||
* The programmatic identifier of the serializationPolicy. | ||
*/ | ||
export declare const serializationPolicyName = "serializationPolicy"; | ||
/** | ||
* Options to configure API request serialization. | ||
*/ | ||
export declare interface serializationPolicyOptions { | ||
/** | ||
* A function that is able to write XML. Required for XML support. | ||
*/ | ||
stringifyXML?: (obj: any, opts?: XmlOptions) => string; | ||
/** | ||
* Configures behavior of xml parser and builder. | ||
*/ | ||
serializerOptions?: SerializerOptions; | ||
} | ||
/** | ||
* Used to map raw response objects to final shapes. | ||
@@ -523,6 +553,2 @@ * Mostly useful for unpacking/packing Dates and other encoded types that | ||
/** | ||
* Decoupled method for processing XML into a string. | ||
*/ | ||
private readonly _stringifyXML?; | ||
/** | ||
* The HTTP client that will be used to send requests. | ||
@@ -529,0 +555,0 @@ */ |
@@ -90,2 +90,6 @@ import { AbortSignalLike } from '@azure/abort-controller'; | ||
deserializationOptions?: DeserializationPolicyOptions; | ||
/** | ||
* Options to customize serializationPolicy. | ||
*/ | ||
serializationOptions?: serializationPolicyOptions; | ||
} | ||
@@ -344,2 +348,6 @@ | ||
/** | ||
* Used to encode the request. | ||
*/ | ||
operationArguments?: OperationArguments; | ||
/** | ||
* A function that returns the proper OperationResponseMap for the given OperationSpec and | ||
@@ -522,2 +530,27 @@ * PipelineResponse combination. If this is undefined, then a simple status code lookup will | ||
/** | ||
* This policy handles assembling the request body and headers using | ||
* an OperationSpec and OperationArguments on the request. | ||
*/ | ||
export declare function serializationPolicy(options?: serializationPolicyOptions): PipelinePolicy; | ||
/** | ||
* The programmatic identifier of the serializationPolicy. | ||
*/ | ||
export declare const serializationPolicyName = "serializationPolicy"; | ||
/** | ||
* Options to configure API request serialization. | ||
*/ | ||
export declare interface serializationPolicyOptions { | ||
/** | ||
* A function that is able to write XML. Required for XML support. | ||
*/ | ||
stringifyXML?: (obj: any, opts?: XmlOptions) => string; | ||
/** | ||
* Configures behavior of xml parser and builder. | ||
*/ | ||
serializerOptions?: SerializerOptions; | ||
} | ||
/** | ||
* Used to map raw response objects to final shapes. | ||
@@ -562,6 +595,2 @@ * Mostly useful for unpacking/packing Dates and other encoded types that | ||
/** | ||
* Decoupled method for processing XML into a string. | ||
*/ | ||
private readonly _stringifyXML?; | ||
/** | ||
* The HTTP client that will be used to send requests. | ||
@@ -568,0 +597,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
541977
38
4976