From e1bb0d5a41cddf3ac3de72d45c944604ecc5dcc0 Mon Sep 17 00:00:00 2001 From: Javad Khalilian Date: Tue, 14 May 2024 14:52:11 +0200 Subject: [PATCH 1/5] feat(client): type guards for IPactDecimal and IPactInt --- packages/libs/client/src/index.ts | 2 + .../src/utils/tests/type-guards.test.ts | 43 +++++++++++++++++++ packages/libs/client/src/utils/type-guards.ts | 15 +++++++ 3 files changed, 60 insertions(+) create mode 100644 packages/libs/client/src/utils/tests/type-guards.test.ts create mode 100644 packages/libs/client/src/utils/type-guards.ts diff --git a/packages/libs/client/src/index.ts b/packages/libs/client/src/index.ts index 35c17501ab..0f31cbb809 100644 --- a/packages/libs/client/src/index.ts +++ b/packages/libs/client/src/index.ts @@ -14,6 +14,8 @@ export { Pact } from './pact'; export type * from './interfaces/IPactCommand'; export type * from './interfaces/ISigningRequest'; +export { isIPactDecimal, isIPactInt } from './utils/type-guards'; + export { ClientRequestInit, ICommandResult, diff --git a/packages/libs/client/src/utils/tests/type-guards.test.ts b/packages/libs/client/src/utils/tests/type-guards.test.ts new file mode 100644 index 0000000000..71e3c80a5d --- /dev/null +++ b/packages/libs/client/src/utils/tests/type-guards.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from 'vitest'; +import { hasField, isIPactDecimal, isIPactInt, isObject } from '../type-guards'; + +describe('isObject', () => { + it('returns true for object', () => { + expect(isObject({})).toBe(true); + }); + it('returns false for null', () => { + expect(isObject(null)).toBe(false); + }); + it('returns false for non-object types', () => { + [1, 'hello', true, undefined, Symbol('test')].forEach((item) => + expect(isObject(item)).toBe(false), + ); + }); +}); + +describe('hasField', () => { + it('returns true if object has the filed', () => { + expect(hasField({ test: '1' }, 'test')).toBe(true); + }); + it('returns false if object does not has the filed', () => { + expect(hasField({ one: '1' }, 'two')).toBe(false); + }); +}); + +describe('isIPactDecimal', () => { + it('returns true for IPactDecimal', () => { + expect(isIPactDecimal({ decimal: '1' })).toBe(true); + }); + it('returns false for non-IPactDecimal', () => { + expect(isIPactDecimal({ int: '1' })).toBe(false); + }); +}); + +describe('isIPactInt', () => { + it('returns true for IPactInt', () => { + expect(isIPactInt({ int: '1' })).toBe(true); + }); + it('returns false for non-IPactInt', () => { + expect(isIPactInt({ decimal: '1' })).toBe(false); + }); +}); diff --git a/packages/libs/client/src/utils/type-guards.ts b/packages/libs/client/src/utils/type-guards.ts new file mode 100644 index 0000000000..a485eb2a05 --- /dev/null +++ b/packages/libs/client/src/utils/type-guards.ts @@ -0,0 +1,15 @@ +import { IPactDecimal, IPactInt } from "@kadena/types"; + +export const isObject = (obj: unknown): obj is Record => { + return typeof obj === "object" && obj !== null; +} + +export const hasField = (obj: Record, field: T): obj is Record => { + return field in obj; +} + +export const isIPactDecimal = (arg: unknown): arg is IPactDecimal => + isObject(arg) && hasField(arg, "decimal") && typeof arg.decimal === "string"; + +export const isIPactInt = (arg: unknown): arg is IPactInt => + isObject(arg) && hasField(arg, "int") && typeof arg.int === "string"; From c6f7c31a2c37bba1b037de20b4d7c34eb4b15ef8 Mon Sep 17 00:00:00 2001 From: Javad Khalilian Date: Tue, 14 May 2024 15:02:26 +0200 Subject: [PATCH 2/5] chore(client): update api doc --- packages/libs/client/etc/client.api.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/libs/client/etc/client.api.md b/packages/libs/client/etc/client.api.md index 218d421f9b..fb003c8866 100644 --- a/packages/libs/client/etc/client.api.md +++ b/packages/libs/client/etc/client.api.md @@ -14,6 +14,8 @@ import type { IExecPayload } from '@kadena/types'; import { IKeyPair } from '@kadena/types'; import type { ILocalCommandResult } from '@kadena/chainweb-node-client'; import type { ILocalOptions } from '@kadena/chainweb-node-client'; +import { IPactDecimal } from '@kadena/types'; +import { IPactInt } from '@kadena/types'; import { IPollResponse } from '@kadena/chainweb-node-client'; import { IPreflightResult } from '@kadena/chainweb-node-client'; import type { ISigningCap } from '@kadena/types'; @@ -439,6 +441,12 @@ export interface ISingleSignFunction { (transaction: IUnsignedCommand): Promise; } +// @public (undocumented) +export const isIPactDecimal: (arg: unknown) => arg is IPactDecimal; + +// @public (undocumented) +export const isIPactInt: (arg: unknown) => arg is IPactInt; + // @public export function isSignedTransaction(command: IUnsignedCommand | ICommand): command is ICommand; From 76994afb4f3bd936006f0690be291b29a9e97fe2 Mon Sep 17 00:00:00 2001 From: Javad Khalilian Date: Mon, 10 Feb 2025 14:26:19 +0100 Subject: [PATCH 3/5] chore(client): api --- packages/libs/client/etc/client.api.md | 4 +-- packages/libs/client/src/utils/type-guards.ts | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/libs/client/etc/client.api.md b/packages/libs/client/etc/client.api.md index fb003c8866..8376415158 100644 --- a/packages/libs/client/etc/client.api.md +++ b/packages/libs/client/etc/client.api.md @@ -14,8 +14,8 @@ import type { IExecPayload } from '@kadena/types'; import { IKeyPair } from '@kadena/types'; import type { ILocalCommandResult } from '@kadena/chainweb-node-client'; import type { ILocalOptions } from '@kadena/chainweb-node-client'; -import { IPactDecimal } from '@kadena/types'; -import { IPactInt } from '@kadena/types'; +import type { IPactDecimal } from '@kadena/types'; +import type { IPactInt } from '@kadena/types'; import { IPollResponse } from '@kadena/chainweb-node-client'; import { IPreflightResult } from '@kadena/chainweb-node-client'; import type { ISigningCap } from '@kadena/types'; diff --git a/packages/libs/client/src/utils/type-guards.ts b/packages/libs/client/src/utils/type-guards.ts index a485eb2a05..e23ae820b1 100644 --- a/packages/libs/client/src/utils/type-guards.ts +++ b/packages/libs/client/src/utils/type-guards.ts @@ -1,15 +1,30 @@ -import { IPactDecimal, IPactInt } from "@kadena/types"; +import type { IPactDecimal, IPactInt } from '@kadena/types'; export const isObject = (obj: unknown): obj is Record => { - return typeof obj === "object" && obj !== null; -} + return typeof obj === 'object' && obj !== null; +}; -export const hasField = (obj: Record, field: T): obj is Record => { +export const hasField = ( + obj: Record, + field: T, +): obj is Record => { return field in obj; -} +}; +/** + * + * @param arg + * @returns boolean + * @public + */ export const isIPactDecimal = (arg: unknown): arg is IPactDecimal => - isObject(arg) && hasField(arg, "decimal") && typeof arg.decimal === "string"; + isObject(arg) && hasField(arg, 'decimal') && typeof arg.decimal === 'string'; +/** + * + * @param arg + * @returns boolean + * @public + */ export const isIPactInt = (arg: unknown): arg is IPactInt => - isObject(arg) && hasField(arg, "int") && typeof arg.int === "string"; + isObject(arg) && hasField(arg, 'int') && typeof arg.int === 'string'; From 988723916d365088eecdbea142aea31657f11438 Mon Sep 17 00:00:00 2001 From: Javad Khalilian Date: Mon, 10 Feb 2025 14:37:59 +0100 Subject: [PATCH 4/5] chore(client): api extractor --- packages/libs/client/src/utils/type-guards.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/libs/client/src/utils/type-guards.ts b/packages/libs/client/src/utils/type-guards.ts index e23ae820b1..7c111cab6a 100644 --- a/packages/libs/client/src/utils/type-guards.ts +++ b/packages/libs/client/src/utils/type-guards.ts @@ -13,7 +13,7 @@ export const hasField = ( /** * - * @param arg + * @param arg - the argument to check * @returns boolean * @public */ @@ -22,7 +22,7 @@ export const isIPactDecimal = (arg: unknown): arg is IPactDecimal => /** * - * @param arg + * @param arg - the argument to check * @returns boolean * @public */ From 67fffdd07f92ef53fd2638e388337c89c224cd3e Mon Sep 17 00:00:00 2001 From: Javad Khalilian Date: Tue, 11 Feb 2025 11:16:26 +0100 Subject: [PATCH 5/5] chore: changeset --- .changeset/three-cows-jog.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/three-cows-jog.md diff --git a/.changeset/three-cows-jog.md b/.changeset/three-cows-jog.md new file mode 100644 index 0000000000..88e6218cb5 --- /dev/null +++ b/.changeset/three-cows-jog.md @@ -0,0 +1,5 @@ +--- +'@kadena/client': minor +--- + +Exported isIPactDecimal and isIPactInt type guards