diff --git a/src/server/trpc/routers/feed/integration.ts b/src/server/trpc/routers/feed/integration.ts index 56f6a72..5cb6eb5 100644 --- a/src/server/trpc/routers/feed/integration.ts +++ b/src/server/trpc/routers/feed/integration.ts @@ -5,6 +5,7 @@ import _ from 'lodash'; import { subscribeEventBus } from '../../../ws/shared'; import { OpenApiMeta } from 'trpc-openapi'; import { OPENAPI_TAG } from '../../../utils/const'; +import { serializeJSON } from '../../../utils/json'; export const feedIntegrationRouter = router({ github: publicProcedure @@ -62,7 +63,11 @@ export const feedIntegrationRouter = router({ url, }, }); - subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event); + subscribeEventBus.emit( + 'onReceiveFeedEvent', + workspaceId, + serializeJSON(event) + ); return 'ok'; } else if (eventType === 'star') { @@ -84,7 +89,11 @@ export const feedIntegrationRouter = router({ url, }, }); - subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event); + subscribeEventBus.emit( + 'onReceiveFeedEvent', + workspaceId, + serializeJSON(event) + ); return 'ok'; } else if (eventType === 'issues') { @@ -120,7 +129,11 @@ export const feedIntegrationRouter = router({ url, }, }); - subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event); + subscribeEventBus.emit( + 'onReceiveFeedEvent', + workspaceId, + serializeJSON(event) + ); return 'ok'; } diff --git a/src/server/utils/json.ts b/src/server/utils/json.ts new file mode 100644 index 0000000..bcb9f26 --- /dev/null +++ b/src/server/utils/json.ts @@ -0,0 +1,50 @@ +/** + * serialize object + * make data easy to transfer + */ +export function serializeJSON(input: T) { + return serializeDate(input); +} + +type ConvertDateToString = T extends Date + ? string + : T extends Date | null + ? string | null + : T extends Date | undefined + ? string | undefined + : T extends Date | null | undefined + ? string | null | undefined + : T; + +export type DateToString = { + [P in keyof T]: T[P] extends Date | null | undefined + ? ConvertDateToString + : DateToString; +}; + +function serializeDate(obj: T): DateToString { + function stripDates(_obj: unknown): any { + if (!_obj || typeof _obj !== 'object') { + return _obj; + } + + if (_obj instanceof Date) { + return _obj.toISOString(); + } + + if (Array.isArray(_obj)) { + return _obj.map(stripDates); + } + + return Object.keys(_obj).reduce( + (acc, key) => { + const value = (_obj as any)[key]; + acc[key] = stripDates(value); // Recursive to handle nested objects + return acc; + }, + {} as { [key: string]: any } + ); + } + + return stripDates(obj); +}