From f6c54e192be25ea48735a2ce4e1a0ad2222f31b1 Mon Sep 17 00:00:00 2001 From: Daniel Getu Date: Fri, 12 Aug 2022 12:44:32 -0700 Subject: [PATCH] Move common type guards into core-util (#22872) A few type guards had duplicate definitions in multiple packages. This change moves the common functions to `core-util`. Fixes #21734 --- .../app-configuration/package.json | 2 +- .../app-configuration/src/internal/helpers.ts | 2 +- .../src/internal/typeguards.ts | 47 --------------- sdk/core/core-amqp/package.json | 2 +- sdk/core/core-amqp/src/auth/tokenProvider.ts | 2 +- .../src/connectionConfig/connectionConfig.ts | 2 +- sdk/core/core-amqp/src/errors.ts | 2 +- sdk/core/core-amqp/src/log.ts | 2 +- sdk/core/core-amqp/src/requestResponseLink.ts | 2 +- sdk/core/core-amqp/src/util/typeGuards.ts | 48 +-------------- sdk/core/core-amqp/src/util/utils.ts | 2 +- sdk/core/core-auth/package.json | 1 + .../core-auth/src/azureNamedKeyCredential.ts | 2 +- sdk/core/core-auth/src/azureSASCredential.ts | 2 +- sdk/core/core-auth/src/typeguards.ts | 49 --------------- sdk/core/core-http/package.json | 1 + sdk/core/core-http/src/util/delay.ts | 2 +- sdk/core/core-http/src/util/typeguards.ts | 11 ---- sdk/core/core-util/CHANGELOG.md | 2 + sdk/core/core-util/review/core-util.api.md | 9 +++ sdk/core/core-util/src/index.ts | 1 + .../src/typeGuards.ts | 9 ++- .../core-util/test/public/typeGuards.spec.ts | 59 +++++++++++++++++++ sdk/eventhub/event-hubs/package.json | 1 + .../src/batchingPartitionChannel.ts | 2 +- sdk/eventhub/event-hubs/src/eventData.ts | 2 +- sdk/eventhub/event-hubs/src/eventDataBatch.ts | 2 +- .../src/eventHubBufferedProducerClient.ts | 3 +- .../event-hubs/src/eventHubProducerClient.ts | 3 +- sdk/eventhub/event-hubs/src/eventHubSender.ts | 2 +- sdk/eventhub/event-hubs/src/eventPosition.ts | 2 +- .../event-hubs/src/impl/partitionAssigner.ts | 2 +- sdk/eventhub/event-hubs/src/log.ts | 2 +- sdk/eventhub/event-hubs/src/util/error.ts | 2 +- sdk/eventhub/event-hubs/src/util/retries.ts | 2 +- .../event-hubs/src/util/typeGuards.ts | 45 -------------- sdk/servicebus/service-bus/package.json | 2 +- .../service-bus/src/core/managementClient.ts | 2 +- .../service-bus/src/core/messageSender.ts | 2 +- sdk/servicebus/service-bus/src/log.ts | 2 +- .../src/receivers/receiverCommon.ts | 2 +- .../src/serializers/ruleResourceSerializer.ts | 2 +- .../src/serviceBusAtomManagementClient.ts | 2 +- .../service-bus/src/serviceBusClient.ts | 3 +- .../service-bus/src/serviceBusError.ts | 2 +- .../service-bus/src/serviceBusMessage.ts | 2 +- .../service-bus/src/session/messageSession.ts | 2 +- .../service-bus/src/util/atomXmlHelper.ts | 2 +- sdk/servicebus/service-bus/src/util/errors.ts | 2 +- .../service-bus/src/util/typeGuards.ts | 45 -------------- sdk/servicebus/service-bus/src/util/utils.ts | 2 +- sdk/test-utils/perf/package.json | 1 + sdk/test-utils/perf/src/eventPerfTest.ts | 2 +- sdk/test-utils/perf/src/options.ts | 2 +- .../testing-recorder-new/karma.conf.js | 2 +- 55 files changed, 123 insertions(+), 289 deletions(-) delete mode 100644 sdk/appconfiguration/app-configuration/src/internal/typeguards.ts delete mode 100644 sdk/core/core-auth/src/typeguards.ts delete mode 100644 sdk/core/core-http/src/util/typeguards.ts rename sdk/core/{core-client-rest => core-util}/src/typeGuards.ts (86%) create mode 100644 sdk/core/core-util/test/public/typeGuards.spec.ts diff --git a/sdk/appconfiguration/app-configuration/package.json b/sdk/appconfiguration/app-configuration/package.json index e7c50ddb1ca2..9c7797e1f6ca 100644 --- a/sdk/appconfiguration/app-configuration/package.json +++ b/sdk/appconfiguration/app-configuration/package.json @@ -87,7 +87,7 @@ "@azure/core-rest-pipeline": "^1.6.0", "@azure/core-tracing": "^1.0.0", "@azure/core-auth": "^1.3.0", - "@azure/core-util": "^1.0.0", + "@azure/core-util": "^1.0.1", "tslib": "^2.2.0" }, "devDependencies": { diff --git a/sdk/appconfiguration/app-configuration/src/internal/helpers.ts b/sdk/appconfiguration/app-configuration/src/internal/helpers.ts index 4592f49fdec9..c0337250ed1f 100644 --- a/sdk/appconfiguration/app-configuration/src/internal/helpers.ts +++ b/sdk/appconfiguration/app-configuration/src/internal/helpers.ts @@ -19,7 +19,7 @@ import { SecretReferenceValue, secretReferenceContentType, } from "../secretReference"; -import { isDefined } from "./typeguards"; +import { isDefined } from "@azure/core-util"; /** * Formats the etag so it can be used with a If-Match/If-None-Match header diff --git a/sdk/appconfiguration/app-configuration/src/internal/typeguards.ts b/sdk/appconfiguration/app-configuration/src/internal/typeguards.ts deleted file mode 100644 index 0e3b38f17935..000000000000 --- a/sdk/appconfiguration/app-configuration/src/internal/typeguards.ts +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Helper TypeGuard that checks if something is defined or not. - * @param thing - Anything - * @internal - */ -export function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified properties. - * @param thing - Anything. - * @param properties - The name of the properties that should appear in the object. - * @internal - */ -export function isObjectWithProperties( - thing: Thing, - properties: PropertyName[] -): thing is Thing & Record { - if (!isDefined(thing) || typeof thing !== "object") { - return false; - } - - for (const property of properties) { - if (!objectHasProperty(thing, property)) { - return false; - } - } - - return true; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified property. - * @param thing - Any object. - * @param property - The name of the property that should appear in the object. - * @internal - */ -function objectHasProperty( - thing: Thing, - property: PropertyName -): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); -} diff --git a/sdk/core/core-amqp/package.json b/sdk/core/core-amqp/package.json index 47f105df52a6..f042fd978e12 100644 --- a/sdk/core/core-amqp/package.json +++ b/sdk/core/core-amqp/package.json @@ -69,7 +69,7 @@ "dependencies": { "@azure/abort-controller": "^1.0.0", "@azure/core-auth": "^1.3.0", - "@azure/core-util": "^1.0.0", + "@azure/core-util": "^1.0.1", "@azure/logger": "^1.0.0", "buffer": "^6.0.0", "events": "^3.0.0", diff --git a/sdk/core/core-amqp/src/auth/tokenProvider.ts b/sdk/core/core-amqp/src/auth/tokenProvider.ts index e18deadf7672..cc54c7c4adb2 100644 --- a/sdk/core/core-amqp/src/auth/tokenProvider.ts +++ b/sdk/core/core-amqp/src/auth/tokenProvider.ts @@ -8,7 +8,7 @@ import { isNamedKeyCredential, isSASCredential, } from "@azure/core-auth"; -import { isObjectWithProperties } from "../util/typeGuards"; +import { isObjectWithProperties } from "@azure/core-util"; import jssha from "jssha"; /** diff --git a/sdk/core/core-amqp/src/connectionConfig/connectionConfig.ts b/sdk/core/core-amqp/src/connectionConfig/connectionConfig.ts index 377ed008944d..7bcd1ba9acd3 100644 --- a/sdk/core/core-amqp/src/connectionConfig/connectionConfig.ts +++ b/sdk/core/core-amqp/src/connectionConfig/connectionConfig.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { WebSocketImpl } from "rhea-promise"; -import { isDefined } from "../util/typeGuards"; +import { isDefined } from "@azure/core-util"; import { parseConnectionString } from "../util/utils"; /** diff --git a/sdk/core/core-amqp/src/errors.ts b/sdk/core/core-amqp/src/errors.ts index 416f288b6d90..e2f8e31cfd98 100644 --- a/sdk/core/core-amqp/src/errors.ts +++ b/sdk/core/core-amqp/src/errors.ts @@ -3,7 +3,7 @@ /* eslint-disable eqeqeq */ import { AmqpError, AmqpResponseStatusCode, isAmqpError as rheaIsAmqpError } from "rhea-promise"; -import { isDefined, isObjectWithProperties } from "./util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { isNode, isNumber, isString } from "../src/util/utils"; import { isError } from "@azure/core-util"; diff --git a/sdk/core/core-amqp/src/log.ts b/sdk/core/core-amqp/src/log.ts index 113f595f90b7..6d46a6602663 100644 --- a/sdk/core/core-amqp/src/log.ts +++ b/sdk/core/core-amqp/src/log.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { createClientLogger } from "@azure/logger"; -import { isObjectWithProperties } from "./util/typeGuards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * The \@azure/logger configuration for this package. diff --git a/sdk/core/core-amqp/src/requestResponseLink.ts b/sdk/core/core-amqp/src/requestResponseLink.ts index 0427ceadae53..4c021d277c5b 100644 --- a/sdk/core/core-amqp/src/requestResponseLink.ts +++ b/sdk/core/core-amqp/src/requestResponseLink.ts @@ -18,7 +18,7 @@ import { } from "rhea-promise"; import { Constants, StandardAbortMessage } from "./util/constants"; import { logErrorStackTrace, logger } from "./log"; -import { isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; /** * Describes the options that can be specified while sending a request. diff --git a/sdk/core/core-amqp/src/util/typeGuards.ts b/sdk/core/core-amqp/src/util/typeGuards.ts index 853a63a8b83a..f836e83133f1 100644 --- a/sdk/core/core-amqp/src/util/typeGuards.ts +++ b/sdk/core/core-amqp/src/util/typeGuards.ts @@ -2,53 +2,7 @@ // Licensed under the MIT license. import { SasTokenProvider } from "../auth/tokenProvider"; - -/** - * Helper TypeGuard that checks if something is defined or not. - * @param thing - Anything - * @internal - */ -export function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified properties. - * Note: The properties may be inherited. - * @param thing - Anything. - * @param properties - The name of the properties that should appear in the object. - * @internal - */ -export function isObjectWithProperties( - thing: Thing, - properties: PropertyName[] -): thing is Thing & Record { - if (!isDefined(thing) || typeof thing !== "object") { - return false; - } - - for (const property of properties) { - if (!objectHasProperty(thing, property)) { - return false; - } - } - - return true; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified property. - * Note: The property may be inherited. - * @param thing - Any object. - * @param property - The name of the property that should appear in the object. - * @internal - */ -export function objectHasProperty( - thing: Thing, - property: PropertyName -): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); -} +import { isObjectWithProperties } from "@azure/core-util"; /** * Typeguard that checks if the input is a SasTokenProvider. diff --git a/sdk/core/core-amqp/src/util/utils.ts b/sdk/core/core-amqp/src/util/utils.ts index 68333b02b7b8..a8724a3bc964 100644 --- a/sdk/core/core-amqp/src/util/utils.ts +++ b/sdk/core/core-amqp/src/util/utils.ts @@ -5,7 +5,7 @@ import { AbortError, AbortSignalLike } from "@azure/abort-controller"; import { CancellableAsyncLock, CancellableAsyncLockImpl } from "./lock"; import { StandardAbortMessage } from "./constants"; import { WebSocketImpl } from "rhea-promise"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; /** * @internal diff --git a/sdk/core/core-auth/package.json b/sdk/core/core-auth/package.json index 68cbe6a719d2..c0db663c30bd 100644 --- a/sdk/core/core-auth/package.json +++ b/sdk/core/core-auth/package.json @@ -63,6 +63,7 @@ "sideEffects": false, "dependencies": { "@azure/abort-controller": "^1.0.0", + "@azure/core-util": "^1.0.1", "tslib": "^2.2.0" }, "devDependencies": { diff --git a/sdk/core/core-auth/src/azureNamedKeyCredential.ts b/sdk/core/core-auth/src/azureNamedKeyCredential.ts index ce6668667b55..ef8f8be333a1 100644 --- a/sdk/core/core-auth/src/azureNamedKeyCredential.ts +++ b/sdk/core/core-auth/src/azureNamedKeyCredential.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { isObjectWithProperties } from "./typeguards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * Represents a credential defined by a static API name and key. diff --git a/sdk/core/core-auth/src/azureSASCredential.ts b/sdk/core/core-auth/src/azureSASCredential.ts index 736a0ecc971b..937289e8a2f5 100644 --- a/sdk/core/core-auth/src/azureSASCredential.ts +++ b/sdk/core/core-auth/src/azureSASCredential.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { isObjectWithProperties } from "./typeguards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * Represents a credential defined by a static shared access signature. diff --git a/sdk/core/core-auth/src/typeguards.ts b/sdk/core/core-auth/src/typeguards.ts deleted file mode 100644 index 2d221ad8710a..000000000000 --- a/sdk/core/core-auth/src/typeguards.ts +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Helper TypeGuard that checks if something is defined or not. - * @param thing - Anything - * @internal - */ -function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified properties. - * Note: The properties may be inherited. - * @param thing - Anything. - * @param properties - The name of the properties that should appear in the object. - * @internal - */ -export function isObjectWithProperties( - thing: Thing, - properties: PropertyName[] -): thing is Thing & Record { - if (!isDefined(thing) || typeof thing !== "object") { - return false; - } - - for (const property of properties) { - if (!objectHasProperty(thing, property)) { - return false; - } - } - - return true; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified property. - * Note: The property may be inherited. - * @param thing - Any object. - * @param property - The name of the property that should appear in the object. - * @internal - */ -function objectHasProperty( - thing: Thing, - property: PropertyName -): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); -} diff --git a/sdk/core/core-http/package.json b/sdk/core/core-http/package.json index ee45fd09cfb1..e86f3396e378 100644 --- a/sdk/core/core-http/package.json +++ b/sdk/core/core-http/package.json @@ -119,6 +119,7 @@ "@azure/abort-controller": "^1.0.0", "@azure/core-auth": "^1.3.0", "@azure/core-tracing": "1.0.0-preview.13", + "@azure/core-util": "^1.0.1", "@azure/logger": "^1.0.0", "@types/node-fetch": "^2.5.0", "@types/tunnel": "^0.0.3", diff --git a/sdk/core/core-http/src/util/delay.ts b/sdk/core/core-http/src/util/delay.ts index b176d18a9f63..f1330061adc2 100644 --- a/sdk/core/core-http/src/util/delay.ts +++ b/sdk/core/core-http/src/util/delay.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { AbortError, AbortSignalLike } from "@azure/abort-controller"; -import { isDefined } from "./typeguards"; +import { isDefined } from "@azure/core-util"; const StandardAbortMessage = "The operation was aborted."; diff --git a/sdk/core/core-http/src/util/typeguards.ts b/sdk/core/core-http/src/util/typeguards.ts deleted file mode 100644 index 43a1b192c580..000000000000 --- a/sdk/core/core-http/src/util/typeguards.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Helper TypeGuard that checks if the value is not null or undefined. - * @param thing - Anything - * @internal - */ -export function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} diff --git a/sdk/core/core-util/CHANGELOG.md b/sdk/core/core-util/CHANGELOG.md index 91e4882558c6..f9c60076be6c 100644 --- a/sdk/core/core-util/CHANGELOG.md +++ b/sdk/core/core-util/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- Add helper type guards `isDefined`, `isObjectWithProperties`, `objectHasProperty`. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/core/core-util/review/core-util.api.md b/sdk/core/core-util/review/core-util.api.md index dc94908451e1..943276bbd752 100644 --- a/sdk/core/core-util/review/core-util.api.md +++ b/sdk/core/core-util/review/core-util.api.md @@ -19,6 +19,9 @@ export function getErrorMessage(e: unknown): string; // @public export function getRandomIntegerInclusive(min: number, max: number): number; +// @public +export function isDefined(thing: T | undefined | null): thing is T; + // @public export function isError(e: unknown): e is Error; @@ -28,6 +31,12 @@ export const isNode: boolean; // @public export function isObject(input: unknown): input is UnknownObject; +// @public +export function isObjectWithProperties(thing: Thing, properties: PropertyName[]): thing is Thing & Record; + +// @public +export function objectHasProperty(thing: Thing, property: PropertyName): thing is Thing & Record; + // @public export type UnknownObject = { [s: string]: unknown; diff --git a/sdk/core/core-util/src/index.ts b/sdk/core/core-util/src/index.ts index 1bbdb0f7ce34..f91afcf8af6f 100644 --- a/sdk/core/core-util/src/index.ts +++ b/sdk/core/core-util/src/index.ts @@ -7,3 +7,4 @@ export { getRandomIntegerInclusive } from "./random"; export { isObject, UnknownObject } from "./object"; export { isError, getErrorMessage } from "./error"; export { computeSha256Hash, computeSha256Hmac } from "./sha256"; +export { isDefined, isObjectWithProperties, objectHasProperty } from "./typeGuards"; diff --git a/sdk/core/core-client-rest/src/typeGuards.ts b/sdk/core/core-util/src/typeGuards.ts similarity index 86% rename from sdk/core/core-client-rest/src/typeGuards.ts rename to sdk/core/core-util/src/typeGuards.ts index 0e3b38f17935..431e18559734 100644 --- a/sdk/core/core-client-rest/src/typeGuards.ts +++ b/sdk/core/core-util/src/typeGuards.ts @@ -4,7 +4,6 @@ /** * Helper TypeGuard that checks if something is defined or not. * @param thing - Anything - * @internal */ export function isDefined(thing: T | undefined | null): thing is T { return typeof thing !== "undefined" && thing !== null; @@ -14,7 +13,6 @@ export function isDefined(thing: T | undefined | null): thing is T { * Helper TypeGuard that checks if the input is an object with the specified properties. * @param thing - Anything. * @param properties - The name of the properties that should appear in the object. - * @internal */ export function isObjectWithProperties( thing: Thing, @@ -37,11 +35,12 @@ export function isObjectWithProperties( * Helper TypeGuard that checks if the input is an object with the specified property. * @param thing - Any object. * @param property - The name of the property that should appear in the object. - * @internal */ -function objectHasProperty( +export function objectHasProperty( thing: Thing, property: PropertyName ): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); + return ( + isDefined(thing) && typeof thing === "object" && property in (thing as Record) + ); } diff --git a/sdk/core/core-util/test/public/typeGuards.spec.ts b/sdk/core/core-util/test/public/typeGuards.spec.ts new file mode 100644 index 000000000000..3245b2945798 --- /dev/null +++ b/sdk/core/core-util/test/public/typeGuards.spec.ts @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { isDefined, isObjectWithProperties, objectHasProperty } from "../../src/index"; +import { assert } from "chai"; + +describe("Type guards", function () { + describe("isDefined", function () { + it("should return false when the argument is undefined", async function () { + assert.isFalse(isDefined(undefined)); + }); + it("should return false when the argument is null", async function () { + assert.isFalse(isDefined(null)); + }); + it("should return true for other primitive types", async function () { + assert.isTrue(isDefined(true)); + assert.isTrue(isDefined(1)); + assert.isTrue(isDefined(BigInt(1))); + assert.isTrue(isDefined(NaN)); + assert.isTrue(isDefined("azure")); + assert.isTrue(isDefined(Symbol("azure"))); + assert.isTrue(isDefined({ defined: true })); + }); + }); + describe("isObjectWithProperties", function () { + it("should return true when the object contains the listed properties", async function () { + assert.isTrue(isObjectWithProperties({ a: 1, b: 2, c: 3 }, ["a"])); + assert.isTrue( + isObjectWithProperties({ a: 1, b: 2, c: 3 }, ["a", "b"]), + "object contains properties `a` and `b`" + ); + }); + it("should return false when the object does not contain at least one listed property", async function () { + assert.isFalse(isObjectWithProperties({ a: 1, b: 2, c: 3 }, ["d"])); + assert.isFalse( + isObjectWithProperties({ a: 1, b: 2, c: 3 }, ["a", "d"]), + "object does not contain property `d`" + ); + }); + }); + describe("objectHasProperty", function () { + it("should return true when the argument is an object containing the property name", async function () { + assert.isTrue(objectHasProperty({ a: 1, b: 2, c: 3 }, "a")); + }); + it("should return false when the object does not contain the given property", async function () { + assert.isFalse(objectHasProperty({ a: 1, b: 2, c: 3 }, "d")); + }); + it("should return false when the argument is not an object", async function () { + assert.isFalse(objectHasProperty(undefined, "azure")); + assert.isFalse(objectHasProperty(null, "azure")); + assert.isFalse(objectHasProperty(true, "azure")); + assert.isFalse(objectHasProperty(1, "azure")); + assert.isFalse(objectHasProperty(BigInt(1), "azure")); + assert.isFalse(objectHasProperty(NaN, "azure")); + assert.isFalse(objectHasProperty("azure", "azure")); + assert.isFalse(objectHasProperty(Symbol("azure"), "azure")); + }); + }); +}); diff --git a/sdk/eventhub/event-hubs/package.json b/sdk/eventhub/event-hubs/package.json index b42d83404b0e..f7b62bc2df96 100644 --- a/sdk/eventhub/event-hubs/package.json +++ b/sdk/eventhub/event-hubs/package.json @@ -110,6 +110,7 @@ "@azure/core-amqp": "^3.1.0", "@azure/core-auth": "^1.3.0", "@azure/core-tracing": "^1.0.0", + "@azure/core-util": "^1.0.1", "@azure/logger": "^1.0.0", "buffer": "^6.0.0", "is-buffer": "^2.0.3", diff --git a/sdk/eventhub/event-hubs/src/batchingPartitionChannel.ts b/sdk/eventhub/event-hubs/src/batchingPartitionChannel.ts index 7d581ebe1922..1b84b66b6800 100644 --- a/sdk/eventhub/event-hubs/src/batchingPartitionChannel.ts +++ b/sdk/eventhub/event-hubs/src/batchingPartitionChannel.ts @@ -9,7 +9,7 @@ import { EventHubProducerClient, OperationOptions, } from "./index"; -import { isDefined, isObjectWithProperties } from "./util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { AbortSignalLike } from "@azure/abort-controller"; import { AwaitableQueue } from "./impl/awaitableQueue"; import { getPromiseParts } from "./util/getPromiseParts"; diff --git a/sdk/eventhub/event-hubs/src/eventData.ts b/sdk/eventhub/event-hubs/src/eventData.ts index be1c0bd6ae9b..dc0729c3016a 100644 --- a/sdk/eventhub/event-hubs/src/eventData.ts +++ b/sdk/eventhub/event-hubs/src/eventData.ts @@ -9,7 +9,7 @@ import { Message as RheaMessage, types, } from "rhea-promise"; -import { isDefined, isObjectWithProperties, objectHasProperty } from "./util/typeGuards"; +import { isDefined, isObjectWithProperties, objectHasProperty } from "@azure/core-util"; import { idempotentProducerAmqpPropertyNames, PENDING_PUBLISH_SEQ_NUM_SYMBOL, diff --git a/sdk/eventhub/event-hubs/src/eventDataBatch.ts b/sdk/eventhub/event-hubs/src/eventDataBatch.ts index 6ef96c87a489..5907787eadb2 100644 --- a/sdk/eventhub/event-hubs/src/eventDataBatch.ts +++ b/sdk/eventhub/event-hubs/src/eventDataBatch.ts @@ -5,7 +5,7 @@ import { AmqpAnnotatedMessage } from "@azure/core-amqp"; import { EventData, populateIdempotentMessageAnnotations, toRheaMessage } from "./eventData"; import { ConnectionContext } from "./connectionContext"; import { MessageAnnotations, message, Message as RheaMessage } from "rhea-promise"; -import { isDefined, isObjectWithProperties } from "./util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { OperationTracingOptions, TracingContext } from "@azure/core-tracing"; import { instrumentEventData } from "./diagnostics/instrumentEventData"; import { throwTypeErrorIfParameterMissing } from "./util/error"; diff --git a/sdk/eventhub/event-hubs/src/eventHubBufferedProducerClient.ts b/sdk/eventhub/event-hubs/src/eventHubBufferedProducerClient.ts index 7afdfd411147..53d8fa1baa18 100644 --- a/sdk/eventhub/event-hubs/src/eventHubBufferedProducerClient.ts +++ b/sdk/eventhub/event-hubs/src/eventHubBufferedProducerClient.ts @@ -13,7 +13,8 @@ import { } from "./models/public"; import { EventHubProperties, PartitionProperties } from "./managementClient"; import { NamedKeyCredential, SASCredential, TokenCredential } from "@azure/core-auth"; -import { isCredential, isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; +import { isCredential } from "./util/typeGuards"; import { AbortController } from "@azure/abort-controller"; import { AmqpAnnotatedMessage, delay } from "@azure/core-amqp"; import { BatchingPartitionChannel } from "./batchingPartitionChannel"; diff --git a/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts b/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts index e1651e5f92a5..4442f307de56 100644 --- a/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts +++ b/sdk/eventhub/event-hubs/src/eventHubProducerClient.ts @@ -15,7 +15,8 @@ import { EventDataBatch, EventDataBatchImpl, isEventDataBatch } from "./eventDat import { EventHubProperties, PartitionProperties } from "./managementClient"; import { TracingContext, TracingSpanLink } from "@azure/core-tracing"; import { NamedKeyCredential, SASCredential, TokenCredential } from "@azure/core-auth"; -import { isCredential, isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; +import { isCredential } from "./util/typeGuards"; import { logErrorStackTrace, logger } from "./log"; import { idempotentAlreadyPublished, diff --git a/sdk/eventhub/event-hubs/src/eventHubSender.ts b/sdk/eventhub/event-hubs/src/eventHubSender.ts index 095981e592a5..029effe6f947 100644 --- a/sdk/eventhub/event-hubs/src/eventHubSender.ts +++ b/sdk/eventhub/event-hubs/src/eventHubSender.ts @@ -40,7 +40,7 @@ import { idempotentProducerAmqpPropertyNames, PENDING_PUBLISH_SEQ_NUM_SYMBOL, } from "./util/constants"; -import { isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; import { translateError } from "./util/error"; import { v4 as uuid } from "uuid"; diff --git a/sdk/eventhub/event-hubs/src/eventPosition.ts b/sdk/eventhub/event-hubs/src/eventPosition.ts index c514b31296c3..51e1fe46477d 100644 --- a/sdk/eventhub/event-hubs/src/eventPosition.ts +++ b/sdk/eventhub/event-hubs/src/eventPosition.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { Constants, ErrorNameConditionMapper, translate } from "@azure/core-amqp"; -import { isDefined, objectHasProperty } from "./util/typeGuards"; +import { isDefined, objectHasProperty } from "@azure/core-util"; /** * Represents the position of an event in an Event Hub partition, typically used when calling the `subscribe()` diff --git a/sdk/eventhub/event-hubs/src/impl/partitionAssigner.ts b/sdk/eventhub/event-hubs/src/impl/partitionAssigner.ts index ba3a982961df..f2804a69a3b2 100644 --- a/sdk/eventhub/event-hubs/src/impl/partitionAssigner.ts +++ b/sdk/eventhub/event-hubs/src/impl/partitionAssigner.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { isDefined } from "../util/typeGuards"; +import { isDefined } from "@azure/core-util"; import { mapPartitionKeyToId } from "./partitionKeyToIdMapper"; /** diff --git a/sdk/eventhub/event-hubs/src/log.ts b/sdk/eventhub/event-hubs/src/log.ts index 0c4ac10ee758..8514d9ef71a4 100644 --- a/sdk/eventhub/event-hubs/src/log.ts +++ b/sdk/eventhub/event-hubs/src/log.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { createClientLogger } from "@azure/logger"; -import { isObjectWithProperties } from "./util/typeGuards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * The `@azure/logger` configuration for this package. diff --git a/sdk/eventhub/event-hubs/src/util/error.ts b/sdk/eventhub/event-hubs/src/util/error.ts index dc7440bf6809..1fc5878bb1f2 100644 --- a/sdk/eventhub/event-hubs/src/util/error.ts +++ b/sdk/eventhub/event-hubs/src/util/error.ts @@ -3,7 +3,7 @@ import { logErrorStackTrace, logger } from "../log"; import { ConnectionContext } from "../connectionContext"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; import { AmqpError, isAmqpError } from "rhea-promise"; import { isMessagingError, MessagingError, translate } from "@azure/core-amqp"; diff --git a/sdk/eventhub/event-hubs/src/util/retries.ts b/sdk/eventhub/event-hubs/src/util/retries.ts index 17415a8d8a06..b48b450018a4 100644 --- a/sdk/eventhub/event-hubs/src/util/retries.ts +++ b/sdk/eventhub/event-hubs/src/util/retries.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { Constants, RetryOptions } from "@azure/core-amqp"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; /** * @internal diff --git a/sdk/eventhub/event-hubs/src/util/typeGuards.ts b/sdk/eventhub/event-hubs/src/util/typeGuards.ts index 7b338beacf13..d44b91e72700 100644 --- a/sdk/eventhub/event-hubs/src/util/typeGuards.ts +++ b/sdk/eventhub/event-hubs/src/util/typeGuards.ts @@ -10,51 +10,6 @@ import { isTokenCredential, } from "@azure/core-auth"; -/** - * Helper TypeGuard that checks if something is defined or not. - * @param thing - Anything - * @internal - */ -export function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified properties. - * @param thing - Anything. - * @param properties - The name of the properties that should appear in the object. - * @internal - */ -export function isObjectWithProperties( - thing: Thing, - properties: PropertyName[] -): thing is Thing & Record { - if (!isDefined(thing) || typeof thing !== "object") { - return false; - } - - for (const property of properties) { - if (!objectHasProperty(thing, property)) { - return false; - } - } - - return true; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified property. - * @param thing - Any object. - * @param property - The name of the property that should appear in the object. - * @internal - */ -export function objectHasProperty( - thing: Thing, - property: PropertyName -): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); -} - /** * Typeguard that checks if the input is a credential type the clients accept. * @param thing - Any object. diff --git a/sdk/servicebus/service-bus/package.json b/sdk/servicebus/service-bus/package.json index c597fa6b63e4..df1e16e4cae9 100644 --- a/sdk/servicebus/service-bus/package.json +++ b/sdk/servicebus/service-bus/package.json @@ -112,7 +112,7 @@ "@azure/core-rest-pipeline": "^1.1.0", "@azure/core-tracing": "^1.0.0", "@azure/core-paging": "^1.1.1", - "@azure/core-util": "^1.0.0", + "@azure/core-util": "^1.0.1", "@azure/core-xml": "^1.0.0", "@azure/logger": "^1.0.0", "@types/is-buffer": "^2.0.0", diff --git a/sdk/servicebus/service-bus/src/core/managementClient.ts b/sdk/servicebus/service-bus/src/core/managementClient.ts index 1a4fbe8be58b..2e3e12be3077 100644 --- a/sdk/servicebus/service-bus/src/core/managementClient.ts +++ b/sdk/servicebus/service-bus/src/core/managementClient.ts @@ -52,7 +52,7 @@ import { AbortSignalLike } from "@azure/abort-controller"; import { ReceiveMode } from "../models"; import { translateServiceBusError } from "../serviceBusError"; import { defaultDataTransformer, tryToJsonDecode } from "../dataTransformer"; -import { isDefined, isObjectWithProperties } from "../util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { RuleProperties, SqlRuleAction, diff --git a/sdk/servicebus/service-bus/src/core/messageSender.ts b/sdk/servicebus/service-bus/src/core/messageSender.ts index 9fd02df3c5cc..292fdfc615b9 100644 --- a/sdk/servicebus/service-bus/src/core/messageSender.ts +++ b/sdk/servicebus/service-bus/src/core/messageSender.ts @@ -31,7 +31,7 @@ import { CreateMessageBatchOptions } from "../models"; import { OperationOptionsBase } from "../modelsToBeSharedWithEventHubs"; import { AbortSignalLike } from "@azure/abort-controller"; import { ServiceBusError, translateServiceBusError } from "../serviceBusError"; -import { isDefined } from "../util/typeGuards"; +import { isDefined } from "@azure/core-util"; import { defaultDataTransformer } from "../dataTransformer"; /** diff --git a/sdk/servicebus/service-bus/src/log.ts b/sdk/servicebus/service-bus/src/log.ts index 798b737c8649..1eac525b0b3d 100644 --- a/sdk/servicebus/service-bus/src/log.ts +++ b/sdk/servicebus/service-bus/src/log.ts @@ -3,7 +3,7 @@ import { AzureLogger, createClientLogger } from "@azure/logger"; import { AmqpError } from "rhea-promise"; -import { isObjectWithProperties } from "./util/typeGuards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * The `@azure/logger` configuration for this package. diff --git a/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts b/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts index 3288b7e78e94..8d92c75056bd 100644 --- a/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts +++ b/sdk/servicebus/service-bus/src/receivers/receiverCommon.ts @@ -25,7 +25,7 @@ import { RetryOptions, } from "@azure/core-amqp"; import { MessageAlreadySettled } from "../util/errors"; -import { isDefined } from "../util/typeGuards"; +import { isDefined } from "@azure/core-util"; /** * @internal diff --git a/sdk/servicebus/service-bus/src/serializers/ruleResourceSerializer.ts b/sdk/servicebus/service-bus/src/serializers/ruleResourceSerializer.ts index 7a320190da88..3afd044c7bc0 100644 --- a/sdk/servicebus/service-bus/src/serializers/ruleResourceSerializer.ts +++ b/sdk/servicebus/service-bus/src/serializers/ruleResourceSerializer.ts @@ -9,7 +9,7 @@ import { serializeToAtomXmlRequest, } from "../util/atomXmlHelper"; import * as Constants from "../util/constants"; -import { isDefined, isObjectWithProperties } from "../util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { getString, getStringOrUndefined } from "../util/utils"; /** diff --git a/sdk/servicebus/service-bus/src/serviceBusAtomManagementClient.ts b/sdk/servicebus/service-bus/src/serviceBusAtomManagementClient.ts index 1e3322b38bff..8b5d848aaa52 100644 --- a/sdk/servicebus/service-bus/src/serviceBusAtomManagementClient.ts +++ b/sdk/servicebus/service-bus/src/serviceBusAtomManagementClient.ts @@ -76,7 +76,7 @@ import * as Constants from "./util/constants"; import { parseURL } from "./util/parseUrl"; import { SasServiceClientCredentials } from "./util/sasServiceClientCredentials"; import { tracingClient } from "./diagnostics/tracing"; -import { isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; import { formatUserAgentPrefix, getHttpResponseOnly, diff --git a/sdk/servicebus/service-bus/src/serviceBusClient.ts b/sdk/servicebus/service-bus/src/serviceBusClient.ts index dfcfa5e7f4b0..9350bc15b69e 100644 --- a/sdk/servicebus/service-bus/src/serviceBusClient.ts +++ b/sdk/servicebus/service-bus/src/serviceBusClient.ts @@ -24,7 +24,8 @@ import { ServiceBusRuleManager, ServiceBusRuleManagerImpl } from "./serviceBusRu import { ServiceBusSender, ServiceBusSenderImpl } from "./sender"; import { entityPathMisMatchError } from "./util/errors"; import { MessageSession } from "./session/messageSession"; -import { isCredential, isDefined } from "./util/typeGuards"; +import { isDefined } from "@azure/core-util"; +import { isCredential } from "./util/typeGuards"; import { ensureValidIdentifier } from "./util/utils"; /** diff --git a/sdk/servicebus/service-bus/src/serviceBusError.ts b/sdk/servicebus/service-bus/src/serviceBusError.ts index 30af1fd81d5e..745e914ff9b8 100644 --- a/sdk/servicebus/service-bus/src/serviceBusError.ts +++ b/sdk/servicebus/service-bus/src/serviceBusError.ts @@ -3,7 +3,7 @@ import { isMessagingError, MessagingError, translate } from "@azure/core-amqp"; import { AmqpError } from "rhea-promise"; -import { isObjectWithProperties } from "./util/typeGuards"; +import { isObjectWithProperties } from "@azure/core-util"; /** * Service Bus failure codes. diff --git a/sdk/servicebus/service-bus/src/serviceBusMessage.ts b/sdk/servicebus/service-bus/src/serviceBusMessage.ts index 1e89cf8092fe..2551daa2eff1 100644 --- a/sdk/servicebus/service-bus/src/serviceBusMessage.ts +++ b/sdk/servicebus/service-bus/src/serviceBusMessage.ts @@ -14,7 +14,7 @@ import { import { defaultDataTransformer } from "./dataTransformer"; import { messageLogger as logger } from "./log"; import { ReceiveMode } from "./models"; -import { isDefined, isObjectWithProperties } from "./util/typeGuards"; +import { isDefined, isObjectWithProperties } from "@azure/core-util"; import { reorderLockToken } from "./util/utils"; /** diff --git a/sdk/servicebus/service-bus/src/session/messageSession.ts b/sdk/servicebus/service-bus/src/session/messageSession.ts index eadf96bede58..c3ba65ffa3b3 100644 --- a/sdk/servicebus/service-bus/src/session/messageSession.ts +++ b/sdk/servicebus/service-bus/src/session/messageSession.ts @@ -37,7 +37,7 @@ import { import { OperationOptionsBase } from "../modelsToBeSharedWithEventHubs"; import { ServiceBusError, translateServiceBusError } from "../serviceBusError"; import { abandonMessage, completeMessage } from "../receivers/receiverCommon"; -import { isDefined } from "../util/typeGuards"; +import { isDefined } from "@azure/core-util"; /** * Describes the options that need to be provided while creating a message session receiver link. diff --git a/sdk/servicebus/service-bus/src/util/atomXmlHelper.ts b/sdk/servicebus/service-bus/src/util/atomXmlHelper.ts index 76c64bb2e7dd..ab74bef567e6 100644 --- a/sdk/servicebus/service-bus/src/util/atomXmlHelper.ts +++ b/sdk/servicebus/service-bus/src/util/atomXmlHelper.ts @@ -16,7 +16,7 @@ import { Buffer } from "buffer"; import { parseURL } from "./parseUrl"; import { isJSONLikeObject } from "./utils"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; import { OperationTracingOptions } from "@azure/core-tracing"; import { AbortSignalLike } from "@azure/abort-controller"; import { InternalQueueOptions } from "../serializers/queueResourceSerializer"; diff --git a/sdk/servicebus/service-bus/src/util/errors.ts b/sdk/servicebus/service-bus/src/util/errors.ts index 601a527b58ea..0ecc20837b19 100644 --- a/sdk/servicebus/service-bus/src/util/errors.ts +++ b/sdk/servicebus/service-bus/src/util/errors.ts @@ -10,7 +10,7 @@ import { isServiceBusMessage, ServiceBusReceivedMessage, } from "../serviceBusMessage"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; /** * Error message to use when EntityPath in connection string does not match the diff --git a/sdk/servicebus/service-bus/src/util/typeGuards.ts b/sdk/servicebus/service-bus/src/util/typeGuards.ts index dec2e8b4f005..0613335da9cd 100644 --- a/sdk/servicebus/service-bus/src/util/typeGuards.ts +++ b/sdk/servicebus/service-bus/src/util/typeGuards.ts @@ -10,51 +10,6 @@ import { TokenCredential, } from "@azure/core-auth"; -/** - * Helper TypeGuard that checks if something is defined or not. - * @param thing - Anything - * @internal - */ -export function isDefined(thing: T | undefined | null): thing is T { - return typeof thing !== "undefined" && thing !== null; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified properties. - * @param thing - Anything. - * @param properties - The name of the properties that should appear in the object. - * @internal - */ -export function isObjectWithProperties( - thing: Thing, - properties: PropertyName[] -): thing is Thing & Record { - if (!isDefined(thing) || typeof thing !== "object") { - return false; - } - - for (const property of properties) { - if (!objectHasProperty(thing, property)) { - return false; - } - } - - return true; -} - -/** - * Helper TypeGuard that checks if the input is an object with the specified property. - * @param thing - Any object. - * @param property - The name of the property that should appear in the object. - * @internal - */ -function objectHasProperty( - thing: Thing, - property: PropertyName -): thing is Thing & Record { - return typeof thing === "object" && property in (thing as Record); -} - /** * Typeguard that checks if the input is a credential type the clients accept. * @param thing - Any object. diff --git a/sdk/servicebus/service-bus/src/util/utils.ts b/sdk/servicebus/service-bus/src/util/utils.ts index b561e0906228..bee294e2f6d8 100644 --- a/sdk/servicebus/service-bus/src/util/utils.ts +++ b/sdk/servicebus/service-bus/src/util/utils.ts @@ -9,7 +9,7 @@ import { Buffer } from "buffer"; import * as Constants from "../util/constants"; import { AbortError, AbortSignalLike } from "@azure/abort-controller"; import { PipelineResponse } from "@azure/core-rest-pipeline"; -import { isDefined } from "./typeGuards"; +import { isDefined } from "@azure/core-util"; import { HttpResponse, toHttpResponse } from "./compat"; import { StandardAbortMessage } from "@azure/core-amqp"; diff --git a/sdk/test-utils/perf/package.json b/sdk/test-utils/perf/package.json index 44d7956a9eed..70e4d920a855 100644 --- a/sdk/test-utils/perf/package.json +++ b/sdk/test-utils/perf/package.json @@ -62,6 +62,7 @@ "@azure/abort-controller": "^1.0.0", "@azure/core-http": "^2.0.0", "@azure/core-rest-pipeline": "^1.1.0", + "@azure/core-util": "^1.0.1", "tslib": "^2.2.0", "node-fetch": "^2.6.6", "minimist": "~1.2.5", diff --git a/sdk/test-utils/perf/src/eventPerfTest.ts b/sdk/test-utils/perf/src/eventPerfTest.ts index 5194f5ea3a49..f36f98c55eb4 100644 --- a/sdk/test-utils/perf/src/eventPerfTest.ts +++ b/sdk/test-utils/perf/src/eventPerfTest.ts @@ -3,7 +3,7 @@ import { AbortController, AbortSignalLike } from "@azure/abort-controller"; import { PerfTestBase } from "./perfTestBase"; -import { isDefined } from "./utils"; +import { isDefined } from "@azure/core-util"; /** * Extends PerfTestBase, enables writing perf tests for the APIs that receive events as a stream diff --git a/sdk/test-utils/perf/src/options.ts b/sdk/test-utils/perf/src/options.ts index 53b8db60062e..90ad426fe11d 100644 --- a/sdk/test-utils/perf/src/options.ts +++ b/sdk/test-utils/perf/src/options.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { default as minimist, ParsedArgs as MinimistParsedArgs } from "minimist"; -import { isDefined } from "./utils"; +import { isDefined } from "@azure/core-util"; /** * The structure of a Perf option. They represent command line parameters. diff --git a/sdk/test-utils/testing-recorder-new/karma.conf.js b/sdk/test-utils/testing-recorder-new/karma.conf.js index 29e7bfcd4d16..9a042f6552ce 100644 --- a/sdk/test-utils/testing-recorder-new/karma.conf.js +++ b/sdk/test-utils/testing-recorder-new/karma.conf.js @@ -59,7 +59,7 @@ module.exports = function (config) { "AZURE_CLIENT_SECRET", "AZURE_TENANT_ID", "RECORDINGS_RELATIVE_PATH", - "TEST_PROXY_HTTP_PORT" + "TEST_PROXY_HTTP_PORT", ], // test results reporter to use