Skip to content

Commit

Permalink
Move common type guards into core-util (Azure#22872)
Browse files Browse the repository at this point in the history
A few type guards had duplicate definitions in multiple packages. This change moves the common functions to `core-util`.

Fixes Azure#21734
  • Loading branch information
dgetu authored Aug 12, 2022
1 parent fdb65d1 commit f6c54e1
Show file tree
Hide file tree
Showing 55 changed files with 123 additions and 289 deletions.
2 changes: 1 addition & 1 deletion sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 0 additions & 47 deletions sdk/appconfiguration/app-configuration/src/internal/typeguards.ts

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/core/core-amqp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/auth/tokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/requestResponseLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 1 addition & 47 deletions sdk/core/core-amqp/src/util/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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, PropertyName extends string>(
thing: Thing,
properties: PropertyName[]
): thing is Thing & Record<PropertyName, unknown> {
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, PropertyName extends string>(
thing: Thing,
property: PropertyName
): thing is Thing & Record<PropertyName, unknown> {
return typeof thing === "object" && property in (thing as Record<string, unknown>);
}
import { isObjectWithProperties } from "@azure/core-util";

/**
* Typeguard that checks if the input is a SasTokenProvider.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-amqp/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"sideEffects": false,
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-util": "^1.0.1",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-auth/src/azureNamedKeyCredential.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-auth/src/azureSASCredential.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
49 changes: 0 additions & 49 deletions sdk/core/core-auth/src/typeguards.ts

This file was deleted.

1 change: 1 addition & 0 deletions sdk/core/core-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-http/src/util/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down
11 changes: 0 additions & 11 deletions sdk/core/core-http/src/util/typeguards.ts

This file was deleted.

2 changes: 2 additions & 0 deletions sdk/core/core-util/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- Add helper type guards `isDefined`, `isObjectWithProperties`, `objectHasProperty`.

### Breaking Changes

### Bugs Fixed
Expand Down
9 changes: 9 additions & 0 deletions sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export function getErrorMessage(e: unknown): string;
// @public
export function getRandomIntegerInclusive(min: number, max: number): number;

// @public
export function isDefined<T>(thing: T | undefined | null): thing is T;

// @public
export function isError(e: unknown): e is Error;

Expand All @@ -28,6 +31,12 @@ export const isNode: boolean;
// @public
export function isObject(input: unknown): input is UnknownObject;

// @public
export function isObjectWithProperties<Thing, PropertyName extends string>(thing: Thing, properties: PropertyName[]): thing is Thing & Record<PropertyName, unknown>;

// @public
export function objectHasProperty<Thing, PropertyName extends string>(thing: Thing, property: PropertyName): thing is Thing & Record<PropertyName, unknown>;

// @public
export type UnknownObject = {
[s: string]: unknown;
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* Helper TypeGuard that checks if something is defined or not.
* @param thing - Anything
* @internal
*/
export function isDefined<T>(thing: T | undefined | null): thing is T {
return typeof thing !== "undefined" && thing !== null;
Expand All @@ -14,7 +13,6 @@ export function isDefined<T>(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, PropertyName extends string>(
thing: Thing,
Expand All @@ -37,11 +35,12 @@ export function isObjectWithProperties<Thing, PropertyName extends string>(
* 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, PropertyName extends string>(
export function objectHasProperty<Thing, PropertyName extends string>(
thing: Thing,
property: PropertyName
): thing is Thing & Record<PropertyName, unknown> {
return typeof thing === "object" && property in (thing as Record<string, unknown>);
return (
isDefined(thing) && typeof thing === "object" && property in (thing as Record<string, unknown>)
);
}
Loading

0 comments on commit f6c54e1

Please sign in to comment.