Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions sdk/core/core-http/lib/coreHttp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export { WebResource, HttpRequestBody, RequestPrepareOptions, HttpMethods, ParameterValue, RequestOptionsBase, TransferProgressEvent } from "./webResource";
export {
WebResource,
HttpRequestBody,
RequestPrepareOptions,
HttpMethods,
ParameterValue,
RequestOptionsBase,
TransferProgressEvent
} from "./webResource";
export { DefaultHttpClient } from "./defaultHttpClient";
export { HttpClient } from "./httpClient";
export { HttpHeaders } from "./httpHeaders";
Expand All @@ -10,15 +18,27 @@ export { HttpPipelineLogger } from "./httpPipelineLogger";
export { HttpPipelineLogLevel } from "./httpPipelineLogLevel";
export { RestError } from "./restError";
export { OperationArguments } from "./operationArguments";
export { OperationParameter, OperationQueryParameter, OperationURLParameter } from "./operationParameter";
export {
OperationParameter,
OperationQueryParameter,
OperationURLParameter
} from "./operationParameter";
export { OperationResponse } from "./operationResponse";
export { OperationSpec } from "./operationSpec";
export { ServiceClient, ServiceClientOptions, flattenResponse } from "./serviceClient";
export { QueryCollectionFormat } from "./queryCollectionFormat";
export { Constants } from "./util/constants";
export { BearerTokenAuthenticationPolicy, bearerTokenAuthenticationPolicy } from "./policies/bearerTokenAuthenticationPolicy";
export {
BearerTokenAuthenticationPolicy,
bearerTokenAuthenticationPolicy
} from "./policies/bearerTokenAuthenticationPolicy";
export { logPolicy } from "./policies/logPolicy";
export { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./policies/requestPolicy";
export {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions
} from "./policies/requestPolicy";
export { generateClientRequestIdPolicy } from "./policies/generateClientRequestIdPolicy";
export { exponentialRetryPolicy } from "./policies/exponentialRetryPolicy";
export { systemErrorRetryPolicy } from "./policies/systemErrorRetryPolicy";
Expand All @@ -29,22 +49,50 @@ export { signingPolicy } from "./policies/signingPolicy";
export { userAgentPolicy, getDefaultUserAgentValue } from "./policies/userAgentPolicy";
export { deserializationPolicy, deserializeResponseBody } from "./policies/deserializationPolicy";
export {
MapperType, SimpleMapperType, CompositeMapperType, DictionaryMapperType, SequenceMapperType, EnumMapperType,
Mapper, BaseMapper, CompositeMapper, SequenceMapper, DictionaryMapper, EnumMapper,
MapperConstraints, PolymorphicDiscriminator,
Serializer, UrlParameterValue, serializeObject
MapperType,
SimpleMapperType,
CompositeMapperType,
DictionaryMapperType,
SequenceMapperType,
EnumMapperType,
Mapper,
BaseMapper,
CompositeMapper,
SequenceMapper,
DictionaryMapper,
EnumMapper,
MapperConstraints,
PolymorphicDiscriminator,
Serializer,
UrlParameterValue,
serializeObject
} from "./serializer";
export {
stripRequest, stripResponse, delay,
executePromisesSequentially, generateUuid, encodeUri, ServiceCallback,
promiseToCallback, promiseToServiceCallback, isValidUuid,
applyMixins, isNode, isDuration
stripRequest,
stripResponse,
delay,
executePromisesSequentially,
generateUuid,
encodeUri,
ServiceCallback,
promiseToCallback,
promiseToServiceCallback,
isValidUuid,
applyMixins,
isNode,
isDuration
} from "./util/utils";
export { URLBuilder, URLQuery } from "./url";
export { AbortSignalLike } from "@azure/abort-controller";

// Credentials
export { TokenCredential, GetTokenOptions, AccessToken, isTokenCredential, SimpleTokenCredential } from "@azure/core-auth";
export {
TokenCredential,
GetTokenOptions,
AccessToken,
isTokenCredential,
SimpleTokenCredential
} from "@azure/core-auth";
export { AccessTokenCache, ExpiringAccessTokenCache } from "./credentials/accessTokenCache";
export { BasicAuthenticationCredentials } from "./credentials/basicAuthenticationCredentials";
export { ApiKeyCredentials, ApiKeyCredentialOptions } from "./credentials/apiKeyCredentials";
Expand Down
65 changes: 45 additions & 20 deletions sdk/core/core-http/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { Constants } from "./constants";
/**
* A constant that indicates whether the environment is node.js or browser based.
*/
export const isNode = (typeof process !== "undefined") && !!process.version && !!process.versions && !!process.versions.node;
export const isNode =
typeof process !== "undefined" &&
!!process.version &&
!!process.versions &&
!!process.versions.node;

/**
* Checks if a parsed URL is HTTPS
Expand Down Expand Up @@ -77,7 +81,10 @@ export function stripRequest(request: WebResource): WebResource {
* @return {boolean} True if the uuid is valid; false otherwise.
*/
export function isValidUuid(uuid: string): boolean {
const validUuidRegex = new RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", "ig");
const validUuidRegex = new RegExp(
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
"ig"
);
return validUuidRegex.test(uuid);
}

Expand All @@ -89,7 +96,7 @@ export function isValidUuid(uuid: string): boolean {
*
* @return {any[]} An array of values of the given object.
*/
export function objectValues(obj: { [key: string]: any; }): any[] {
export function objectValues(obj: { [key: string]: any }): any[] {
const result: any[] = [];
if (obj && obj instanceof Object) {
for (const key in obj) {
Expand All @@ -98,8 +105,11 @@ export function objectValues(obj: { [key: string]: any; }): any[] {
}
}
} else {
throw new Error(`The provided object ${JSON.stringify(obj, undefined, 2)} is not a valid object that can be ` +
`enumerated to provide its values as an array.`);
const stringifiedObj = JSON.stringify(obj, undefined, 2);
throw new Error(
`The provided object ${stringifiedObj} is not a valid object that can be ` +
`enumerated to provide its values as an array.`
);
}
return result;
}
Expand Down Expand Up @@ -140,7 +150,7 @@ export function executePromisesSequentially(promiseFactories: Array<any>, kickst
*
* @returns {object} Returns the merged target object.
*/
export function mergeObjects(source: { [key: string]: any; }, target: { [key: string]: any; }) {
export function mergeObjects(source: { [key: string]: any }, target: { [key: string]: any }) {
Object.keys(source).forEach((key) => {
target[key] = source[key];
});
Expand Down Expand Up @@ -168,7 +178,12 @@ export interface ServiceCallback<TResult> {
* @param {WebResource} [request] The raw/actual request sent to the server if an error did not occur.
* @param {HttpOperationResponse} [response] The raw/actual response from the server if an error did not occur.
*/
(err: Error | RestError | null, result?: TResult, request?: WebResource, response?: HttpOperationResponse): void;
(
err: Error | RestError | null,
result?: TResult,
request?: WebResource,
response?: HttpOperationResponse
): void;
}

/**
Expand All @@ -182,11 +197,14 @@ export function promiseToCallback(promise: Promise<any>): Function {
throw new Error("The provided input is not a Promise.");
}
return (cb: Function): void => {
promise.then((data: any) => {
cb(undefined, data);
}, (err: Error) => {
cb(err);
});
promise.then(
(data: any) => {
cb(undefined, data);
},
(err: Error) => {
cb(err);
}
);
};
}

Expand All @@ -200,11 +218,14 @@ export function promiseToServiceCallback<T>(promise: Promise<HttpOperationRespon
throw new Error("The provided input is not a Promise.");
}
return (cb: ServiceCallback<T>): void => {
promise.then((data: HttpOperationResponse) => {
process.nextTick(cb, undefined, data.parsedBody as T, data.request, data);
}, (err: Error) => {
process.nextTick(cb, err);
});
promise.then(
(data: HttpOperationResponse) => {
process.nextTick(cb, undefined, data.parsedBody as T, data.request, data);
},
(err: Error) => {
process.nextTick(cb, err);
}
);
};
}

Expand All @@ -221,8 +242,8 @@ export function prepareXMLRootList(obj: any, elementName: string) {
* @param {Array<object>} sourceCtors An array of source objects from which the properties need to be taken.
*/
export function applyMixins(targetCtor: any, sourceCtors: any[]): void {
sourceCtors.forEach(sourceCtors => {
Object.getOwnPropertyNames(sourceCtors.prototype).forEach(name => {
sourceCtors.forEach((sourceCtors) => {
Object.getOwnPropertyNames(sourceCtors.prototype).forEach((name) => {
targetCtor.prototype[name] = sourceCtors.prototype[name];
});
});
Expand All @@ -246,7 +267,11 @@ export function isDuration(value: string): boolean {
* @param {string} replaceValue The value to replace searchValue with in the value argument.
* @returns {string | undefined} The value where each instance of searchValue was replaced with replacedValue.
*/
export function replaceAll(value: string | undefined, searchValue: string, replaceValue: string): string | undefined {
export function replaceAll(
value: string | undefined,
searchValue: string,
replaceValue: string
): string | undefined {
return !value || !searchValue ? value : value.split(searchValue).join(replaceValue || "");
}

Expand Down
24 changes: 15 additions & 9 deletions sdk/core/core-http/lib/util/xml.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function parseXML(str: string): Promise<any> {

let errorNS = "";
try {
errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0].namespaceURI!;
errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI!;
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
}
Expand Down Expand Up @@ -48,7 +49,12 @@ function domToObject(node: Node): any {
const childNodeCount: number = node.childNodes.length;

const firstChildNode: Node = node.childNodes[0];
const onlyChildTextValue: string | undefined = (firstChildNode && childNodeCount === 1 && firstChildNode.nodeType === Node.TEXT_NODE && firstChildNode.nodeValue) || undefined;
const onlyChildTextValue: string | undefined =
(firstChildNode &&
childNodeCount === 1 &&
firstChildNode.nodeType === Node.TEXT_NODE &&
firstChildNode.nodeValue) ||
undefined;

const elementWithAttributes: Element | undefined = asElementWithAttributes(node);
if (elementWithAttributes) {
Expand Down Expand Up @@ -93,12 +99,14 @@ const doc = document.implementation.createDocument(null, null, null);
const serializer = new XMLSerializer();

export function stringifyXML(obj: any, opts?: { rootName?: string }) {
const rootName = opts && opts.rootName || "root";
const rootName = (opts && opts.rootName) || "root";
const dom = buildNode(obj, rootName)[0];
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + serializer.serializeToString(dom);
return (
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + serializer.serializeToString(dom)
);
}

function buildAttributes(attrs: { [key: string]: { toString(): string; } }): Attr[] {
function buildAttributes(attrs: { [key: string]: { toString(): string } }): Attr[] {
const result = [];
for (const key of Object.keys(attrs)) {
const attr = doc.createAttribute(key);
Expand All @@ -113,8 +121,7 @@ function buildNode(obj: any, elementName: string): Node[] {
const elem = doc.createElement(elementName);
elem.textContent = obj.toString();
return [elem];
}
else if (Array.isArray(obj)) {
} else if (Array.isArray(obj)) {
const result = [];
for (const arrayElem of obj) {
for (const child of buildNode(arrayElem, elementName)) {
Expand All @@ -136,8 +143,7 @@ function buildNode(obj: any, elementName: string): Node[] {
}
}
return [elem];
}
else {
} else {
throw new Error(`Illegal value passed to buildObject: ${obj}`);
}
}
Loading