@azure/core-client
Advanced tools
Comparing version
@@ -5,4 +5,4 @@ // Copyright (c) Microsoft Corporation. | ||
export { createSpanFunction } from "./createSpan"; | ||
export { ServiceClient } from "./serviceClient"; | ||
export { ServiceClient, createClientPipeline } from "./serviceClient"; | ||
export { deserializationPolicy, deserializationPolicyName } from "./deserializationPolicy"; | ||
//# sourceMappingURL=index.js.map |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { __awaiter } from "tslib"; | ||
import { isTokenCredential } from "@azure/core-auth"; | ||
import { DefaultHttpsClient, createPipelineRequest, createPipelineFromOptions, bearerTokenAuthenticationPolicy } from "@azure/core-https"; | ||
@@ -26,6 +25,10 @@ import { getPathStringFromParameter, isStreamOperation } from "./interfaceHelpers"; | ||
this._httpsClient = options.httpsClient || new DefaultHttpsClient(); | ||
this._stringifyXML = options.stringifyXML; | ||
this._pipeline = | ||
options.pipeline || | ||
createDefaultPipeline({ baseUri: this._baseUri, credential: options.credential }); | ||
this._stringifyXML = options.stringifyXML; | ||
createDefaultPipeline({ | ||
baseUri: this._baseUri, | ||
credential: options.credential, | ||
parseXML: options.parseXML | ||
}); | ||
} | ||
@@ -197,13 +200,26 @@ /** | ||
function createDefaultPipeline(options = {}) { | ||
const pipeline = createPipelineFromOptions({}); | ||
const credential = options.credential; | ||
return createClientPipeline({ | ||
credentialOptions: options, | ||
deserializationOptions: { | ||
parseXML: options.parseXML | ||
} | ||
}); | ||
} | ||
/** | ||
* Creates a new Pipeline for use with a Service Client. | ||
* Adds in deserializationPolicy by default. | ||
* Also adds in bearerTokenAuthenticationPolicy if passed a TokenCredential. | ||
* @param options Options to customize the created pipeline. | ||
*/ | ||
export function createClientPipeline(options = {}) { | ||
var _a, _b; | ||
const pipeline = createPipelineFromOptions(options !== null && options !== void 0 ? options : {}); | ||
const credential = (_a = options.credentialOptions) === null || _a === void 0 ? void 0 : _a.credential; | ||
if (credential) { | ||
if (isTokenCredential(credential)) { | ||
pipeline.addPolicy(bearerTokenAuthenticationPolicy({ credential, scopes: `${options.baseUri || ""}/.default` })); | ||
} | ||
else { | ||
throw new Error("The credential argument must implement the TokenCredential interface"); | ||
} | ||
pipeline.addPolicy(bearerTokenAuthenticationPolicy({ | ||
credential, | ||
scopes: `${((_b = options.credentialOptions) === null || _b === void 0 ? void 0 : _b.baseUri) || ""}/.default` | ||
})); | ||
} | ||
pipeline.addPolicy(deserializationPolicy(), { phase: "Serialize" }); | ||
pipeline.addPolicy(deserializationPolicy(options.deserializationOptions), { phase: "Serialize" }); | ||
return pipeline; | ||
@@ -210,0 +226,0 @@ } |
@@ -25,3 +25,3 @@ // Copyright (c) Microsoft Corporation. | ||
else { | ||
requestUrl = appendPath(requestUrl, operationSpec.path); | ||
requestUrl = appendPath(requestUrl, path); | ||
} | ||
@@ -59,15 +59,17 @@ } | ||
} | ||
function appendPath(url, path) { | ||
let result = url; | ||
let toAppend = path; | ||
if (toAppend) { | ||
if (!result.endsWith("/")) { | ||
result = `${result}/`; | ||
} | ||
if (toAppend.startsWith("/")) { | ||
toAppend = toAppend.substring(1); | ||
} | ||
result = result + toAppend; | ||
function appendPath(url, pathToAppend) { | ||
if (!pathToAppend) { | ||
return url; | ||
} | ||
return result; | ||
const parsedUrl = new URL(url); | ||
let newPath = parsedUrl.pathname; | ||
if (!newPath.endsWith("/")) { | ||
newPath = `${newPath}/`; | ||
} | ||
if (pathToAppend.startsWith("/")) { | ||
pathToAppend = pathToAppend.substring(1); | ||
} | ||
newPath = newPath + pathToAppend; | ||
parsedUrl.pathname = newPath; | ||
return parsedUrl.toString(); | ||
} | ||
@@ -125,12 +127,31 @@ function calculateQueryParameters(operationSpec, operationArguments, fallbackObject) { | ||
} | ||
function simpleParseQueryParams(queryString) { | ||
if (!queryString || queryString[0] !== "?") { | ||
return []; | ||
} | ||
// remove the leading ? | ||
queryString = queryString.slice(1); | ||
const pairs = queryString.split("&"); | ||
return pairs.map((pair) => { | ||
const [name, value] = pair.split("=", 2); | ||
return [name, value]; | ||
}); | ||
} | ||
function appendQueryParams(url, queryParams) { | ||
if (queryParams.size === 0) { | ||
return url; | ||
} | ||
const parsedUrl = new URL(url); | ||
const combinedParams = new Map(queryParams); | ||
for (const [name, value] of parsedUrl.searchParams) { | ||
// QUIRK: parsedUrl.searchParams will have their name/value pairs decoded, which | ||
// can change their meaning to the server, such as in the case of a SAS signature. | ||
// To avoid accidentally un-encoding a query param, we parse the key/values ourselves | ||
const existingParams = simpleParseQueryParams(parsedUrl.search); | ||
const combinedParams = new Map(existingParams); | ||
for (const [name, value] of queryParams) { | ||
const existingValue = combinedParams.get(name); | ||
if (Array.isArray(existingValue)) { | ||
existingValue.push(value); | ||
existingValue.push(...value); | ||
} | ||
else if (existingValue) { | ||
combinedParams.set(name, [existingValue, value]); | ||
combinedParams.set(name, [existingValue, ...value]); | ||
} | ||
@@ -137,0 +158,0 @@ else { |
{ | ||
"name": "@azure/core-client", | ||
"version": "1.0.0-alpha.20201113.2", | ||
"version": "1.0.0-alpha.20201118.1", | ||
"description": "Core library for interfacing with AutoRest generated code", | ||
@@ -5,0 +5,0 @@ "sdk-type": "client", |
import { AbortSignalLike } from '@azure/abort-controller'; | ||
import { HttpMethods } from '@azure/core-https'; | ||
import { HttpsClient } from '@azure/core-https'; | ||
import { InternalPipelineOptions } from '@azure/core-https'; | ||
import { OperationTracingOptions } from '@azure/core-tracing'; | ||
@@ -70,2 +71,20 @@ import { Pipeline } from '@azure/core-https'; | ||
} | ||
/** | ||
* Options for creating a Pipeline to use with ServiceClient. | ||
* Mostly for customizing the auth policy (if using token auth) or | ||
* the deserialization options when using XML. | ||
*/ | ||
export declare interface ClientPipelineOptions extends InternalPipelineOptions { | ||
/** | ||
* Options to customize bearerTokenAuthenticationPolicy. | ||
*/ | ||
credentialOptions?: { | ||
baseUri?: string; | ||
credential?: TokenCredential; | ||
}; | ||
/** | ||
* Options to customize deserializationPolicy. | ||
*/ | ||
deserializationOptions?: DeserializationPolicyOptions; | ||
} | ||
export declare interface CompositeMapper extends BaseMapper { | ||
@@ -85,2 +104,9 @@ type: CompositeMapperType; | ||
/** | ||
* Creates a new Pipeline for use with a Service Client. | ||
* Adds in deserializationPolicy by default. | ||
* Also adds in bearerTokenAuthenticationPolicy if passed a TokenCredential. | ||
* @param options Options to customize the created pipeline. | ||
*/ | ||
export declare function createClientPipeline(options?: ClientPipelineOptions): Pipeline; | ||
/** | ||
* Method that creates and returns a Serializer. | ||
@@ -539,2 +565,8 @@ * @param modelMappers Known models to map | ||
}) => string; | ||
/** | ||
* A method that is able to parse XML. | ||
*/ | ||
parseXML?: (str: string, opts?: { | ||
includeRoot?: boolean; | ||
}) => Promise<any>; | ||
} | ||
@@ -541,0 +573,0 @@ export declare interface SimpleMapperType { |
import { AbortSignalLike } from '@azure/abort-controller'; | ||
import { HttpMethods } from '@azure/core-https'; | ||
import { HttpsClient } from '@azure/core-https'; | ||
import { InternalPipelineOptions } from '@azure/core-https'; | ||
import { OperationTracingOptions } from '@azure/core-tracing'; | ||
@@ -72,2 +73,21 @@ import { Pipeline } from '@azure/core-https'; | ||
/** | ||
* Options for creating a Pipeline to use with ServiceClient. | ||
* Mostly for customizing the auth policy (if using token auth) or | ||
* the deserialization options when using XML. | ||
*/ | ||
export declare interface ClientPipelineOptions extends InternalPipelineOptions { | ||
/** | ||
* Options to customize bearerTokenAuthenticationPolicy. | ||
*/ | ||
credentialOptions?: { | ||
baseUri?: string; | ||
credential?: TokenCredential; | ||
}; | ||
/** | ||
* Options to customize deserializationPolicy. | ||
*/ | ||
deserializationOptions?: DeserializationPolicyOptions; | ||
} | ||
export declare interface CompositeMapper extends BaseMapper { | ||
@@ -89,2 +109,10 @@ type: CompositeMapperType; | ||
/** | ||
* Creates a new Pipeline for use with a Service Client. | ||
* Adds in deserializationPolicy by default. | ||
* Also adds in bearerTokenAuthenticationPolicy if passed a TokenCredential. | ||
* @param options Options to customize the created pipeline. | ||
*/ | ||
export declare function createClientPipeline(options?: ClientPipelineOptions): Pipeline; | ||
/** | ||
* Method that creates and returns a Serializer. | ||
@@ -576,2 +604,8 @@ * @param modelMappers Known models to map | ||
}) => string; | ||
/** | ||
* A method that is able to parse XML. | ||
*/ | ||
parseXML?: (str: string, opts?: { | ||
includeRoot?: boolean; | ||
}) => Promise<any>; | ||
} | ||
@@ -578,0 +612,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
486828
2.38%4590
3.12%