-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add isGrpcStatusObjectWithCode user-defined type guard
1. User-defined Typescript type-guard function that asserts whether a value or object is a `@grpc/grpc-js` {Partial<StatusObject>} or not. The reason why it checks for {Partial} is because all of the properties of the {StatusObject} are defined as optional for some reason, hence we cannot assume anything about those being present or not by default. 2. Therefore this method will just check if the `code` property is set or not and return `true` or `false` based on that. 3. The above is also the reason why the name of the function is slightly more verbose than your average user-defined type-guard that could be named just "isGrpcStatusObject()" but we wanted to make sure that more specific type-guards can be added later that check for other optional properities or for the presence of all of them together. Link to the status builder within grpc-js: https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/status-builder.ts Signed-off-by: Peter Somogyvari <[email protected]>
- Loading branch information
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/cactus-common/src/main/typescript/grpc/is-grpc-status-object-with-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { StatusObject } from "@grpc/grpc-js"; | ||
|
||
/** | ||
* User-defined Typescript type-guard function that asserts whether a value or | ||
* object is a `@grpc/grpc-js` {Partial<StatusObject>} or not. | ||
* | ||
* The reason why it checks for {Partial} is because all of the properties of | ||
* the {StatusObject} are defined as optional for some reason, hence we cannot | ||
* assume anything about those being present or not by default. | ||
* Therefore this method will just check if the `code` property is set or not | ||
* and return `true` or `false` based on that. | ||
* | ||
* The above is also the reason why the name of the function is slightly more | ||
* verbose than your average user-defined type-guard that could be named just | ||
* "isGrpcStatusObject()" but we wanted to make sure that more specific type-guards | ||
* can be added later that check for other optional properities or for the | ||
* presence of all of them together. | ||
* | ||
* @param x Literally any value or object that will be checked at runtime to have | ||
* the `code` property defined as a number. | ||
* @returns `true` if `x` qualifies, `false` otherwise. | ||
* | ||
* @see {StatusObject} of the @grpc/grpc-js library. | ||
*/ | ||
export function isGrpcStatusObjectWithCode( | ||
x: unknown, | ||
): x is Partial<StatusObject> { | ||
return ( | ||
!!x && | ||
typeof (x as StatusObject).code === "number" && | ||
isFinite((x as StatusObject).code) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/cactus-common/src/test/typescript/unit/grpc/is-grpc-status-object-with-code.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import "jest-extended"; | ||
import { isGrpcStatusObjectWithCode } from "../../../../main/typescript"; | ||
|
||
describe("isGrpcStatusObjectWithCode()", () => { | ||
test("returns true for POJO with correct shape and valid data", () => { | ||
expect(isGrpcStatusObjectWithCode({ code: 1 })).toBeTrue(); | ||
expect(isGrpcStatusObjectWithCode({ code: 0 })).toBeTrue(); | ||
expect(isGrpcStatusObjectWithCode({ code: -1 })).toBeTrue(); | ||
}); | ||
|
||
test("returns false for POJO with correct shape and invalid data", () => { | ||
expect(isGrpcStatusObjectWithCode({ code: NaN })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ code: "" })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ code: "Hello" })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ code: true })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ code: [] })).toBeFalse(); | ||
|
||
expect(isGrpcStatusObjectWithCode({ codeX: NaN })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ codeY: "" })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ codeZ: "Hello" })).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode({ codeK: 1 })).toBeFalse(); | ||
}); | ||
|
||
test("returns false for non-POJO input", () => { | ||
expect(isGrpcStatusObjectWithCode(NaN)).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode(null)).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode(undefined)).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode([])).toBeFalse(); | ||
expect(isGrpcStatusObjectWithCode(Symbol)).toBeFalse(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters