From 7ad07daab1b9a813944d1574de68725d0192738c Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 8 Sep 2024 18:05:57 +0900 Subject: [PATCH] Apply Json type to Broadcast API and Devtools --- packages/sdk/src/client/client.ts | 3 ++- packages/sdk/src/devtools/types.ts | 15 +-------------- packages/sdk/src/document/document.ts | 6 +++--- packages/sdk/src/util/validator.ts | 2 +- packages/sdk/test/integration/client_test.ts | 2 ++ 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/sdk/src/client/client.ts b/packages/sdk/src/client/client.ts index aa406ed40..bae869c59 100644 --- a/packages/sdk/src/client/client.ts +++ b/packages/sdk/src/client/client.ts @@ -43,6 +43,7 @@ import { OpSource } from '@yorkie-js-sdk/src/document/operation/operation'; import { createAuthInterceptor } from '@yorkie-js-sdk/src/client/auth_interceptor'; import { createMetricInterceptor } from '@yorkie-js-sdk/src/client/metric_interceptor'; import { validateSerializable } from '../util/validator'; +import { Json } from '@yorkie-js-sdk/src/document/document'; /** * `SyncMode` defines synchronization modes for the PushPullChanges API. @@ -607,7 +608,7 @@ export class Client { public broadcast( docKey: DocumentKey, topic: string, - payload: any, + payload: Json, ): Promise { if (!this.isActive()) { throw new YorkieError( diff --git a/packages/sdk/src/devtools/types.ts b/packages/sdk/src/devtools/types.ts index 87edebd57..7cf2efd89 100644 --- a/packages/sdk/src/devtools/types.ts +++ b/packages/sdk/src/devtools/types.ts @@ -17,20 +17,7 @@ import type { PrimitiveValue } from '@yorkie-js-sdk/src/document/crdt/primitive'; import type { CRDTTreePosStruct } from '@yorkie-js-sdk/src/document/crdt/tree'; import { CounterValue } from '@yorkie-js-sdk/src/document/crdt/counter'; - -/** - * `Json` represents a JSON value. - * - * TODO(hackerwins): We need to replace `Indexable` with `Json`. - */ -export type Json = - | string - | number - | boolean - // eslint-disable-next-line @typescript-eslint/ban-types - | null - | { [key: string]: Json } - | Array; +import { Json } from '@yorkie-js-sdk/src/document/document'; /** * `Client` represents a client value in devtools. diff --git a/packages/sdk/src/document/document.ts b/packages/sdk/src/document/document.ts index 82ffae048..056b3e4b0 100644 --- a/packages/sdk/src/document/document.ts +++ b/packages/sdk/src/document/document.ts @@ -385,7 +385,7 @@ export interface PresenceChangedEvent

export interface BroadcastEvent extends BaseDocEvent { type: DocEventType.Broadcast; - value: { clientID: ActorID; topic: string; payload: any }; + value: { clientID: ActorID; topic: string; payload: Json }; error?: ErrorFn; } @@ -420,7 +420,7 @@ export type DocEventTopic = keyof DocEventCallbackMap; export type DocEventCallback

= DocEventCallbackMap

[DocEventTopic]; -type Json = JsonScalar | JsonArray | JsonObject; +export type Json = JsonScalar | JsonArray | JsonObject; // eslint-disable-next-line @typescript-eslint/ban-types type JsonScalar = string | number | boolean | null; @@ -2065,7 +2065,7 @@ export class Document { /** * `broadcast` the payload to the given topic. */ - public broadcast(topic: string, payload: any, error?: ErrorFn) { + public broadcast(topic: string, payload: Json, error?: ErrorFn) { const broadcastEvent: LocalBroadcastEvent = { type: DocEventType.LocalBroadcast, value: { topic, payload }, diff --git a/packages/sdk/src/util/validator.ts b/packages/sdk/src/util/validator.ts index 33625d837..0d57912f3 100644 --- a/packages/sdk/src/util/validator.ts +++ b/packages/sdk/src/util/validator.ts @@ -17,7 +17,7 @@ /** * `validateSerializable` returns whether the given value is serializable or not. */ -export const validateSerializable = (value: any): boolean => { +export const validateSerializable = (value: unknown): boolean => { try { const serialized = JSON.stringify(value); diff --git a/packages/sdk/test/integration/client_test.ts b/packages/sdk/test/integration/client_test.ts index 004e0b506..2d485e386 100644 --- a/packages/sdk/test/integration/client_test.ts +++ b/packages/sdk/test/integration/client_test.ts @@ -900,6 +900,8 @@ describe.sequential('Client', function () { eventCollector.add(error.message); }; + // @ts-ignore + // Disable type checking for testing purposes doc.broadcast(broadcastTopic, payload, errorHandler); await eventCollector.waitAndVerifyNthEvent(1, broadcastErrMessage);