From 7e41a99d68b0bc0fb25ef11f1ad3cf68e52c80ac Mon Sep 17 00:00:00 2001 From: Lupascu Rares Date: Sat, 17 Jul 2021 21:28:27 +0300 Subject: [PATCH] fix encode / decode by type to support branded ids --- src/utils.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index ce80e75..78b8674 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -28,6 +28,19 @@ export function encodeByType(type: string, value: any): string | null { return (value as Date).getTime().toString(); } + /** + * Support for branded id's having the following structure + * + * interface ConversationId extends String { + * _conversationBrand: string; + * } + * + * the above interface will support toString() method + */ + if (typeof value.toString === 'function') { + return value.toString(); + } + break; } default: break; @@ -38,7 +51,21 @@ export function encodeByType(type: string, value: any): string | null { export function decodeByType(type: string, value: string): string | number | Date { switch (type) { - case 'object': + case 'object': { + /** + * Support for branded id's having the following structure + * + * interface ConversationId extends String { + * _conversationBrand: string; + * } + * + * the above interface will support toString() method + */ + if (typeof value.toString === 'function') { + return value.toString(); + } + } + case 'date': { const timestamp = parseInt(value, 10);