-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaffa2a
commit 0ccdd75
Showing
7 changed files
with
75 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,30 @@ | ||
import { expect } from '@jest/globals'; | ||
|
||
import { deserialize, serialize } from './transformer'; | ||
|
||
const dateString = '_serializedDate_1970-01-01T00:00:00.000Z'; | ||
const undefinedString = '_undefined_'; | ||
|
||
describe('Transformer', () => { | ||
it('should serialize and deserialize', async () => { | ||
const values = [ | ||
{ from: 1, to: 1 }, | ||
{ from: 'a', to: 'a' }, | ||
{ from: new Date(0), to: dateString }, | ||
{ from: undefined, to: undefinedString }, | ||
{ from: [], to: [] }, | ||
{ from: ['a', 1, new Date(0), [new Date(0)]], to: ['a', 1, dateString, [dateString]] }, | ||
{ from: ['a', [new Date(0)]], to: ['a', [dateString]] }, | ||
{ from: [undefined, [[undefined]]], to: [undefinedString, [[undefinedString]]] }, | ||
{ from: {}, to: {} }, | ||
{ from: { a: { b: new Date(0), c: {} } }, to: { a: { b: dateString, c: {} } } }, | ||
{ from: { a: { b: null } }, to: { a: { b: null } } }, | ||
]; | ||
for (const { from, to } of values) { | ||
const serialized = serialize(from); | ||
expect(serialized).toStrictEqual(to); | ||
const deserialized = deserialize(to); | ||
expect(deserialized).toStrictEqual(from); | ||
} | ||
}); | ||
}); |
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,36 @@ | ||
import { CombinedDataTransformer } from '@trpc/server'; | ||
|
||
const datePrefix = '_serializedDate_'; | ||
const undefinedPlaceholder = '_undefined_'; | ||
|
||
export const serialize = (data: any): any => { | ||
if (typeof data === 'function') throw new Error('Cannot serialize function'); | ||
if (typeof data === 'symbol') throw new Error('Cannot serialize symbol'); | ||
if (typeof data === 'bigint') throw new Error('Cannot serialize bigint'); | ||
if (typeof data === 'undefined') return undefinedPlaceholder; | ||
if (typeof data === 'object') { | ||
if (data === null) return data; | ||
if (data instanceof Date) return `${datePrefix}${data.toISOString()}`; | ||
if (Array.isArray(data)) return data.map(serialize); | ||
return Object.fromEntries(Object.entries(data).map(([k, v]) => [k, serialize(v)])); | ||
} | ||
return data; | ||
}; | ||
|
||
export const deserialize = (data: any): any => { | ||
if (typeof data === 'string') { | ||
if (data.startsWith(datePrefix)) return new Date(data.slice(datePrefix.length)); | ||
if (data === undefinedPlaceholder) return undefined; | ||
} | ||
if (typeof data === 'object') { | ||
if (data === null) return data; | ||
if (Array.isArray(data)) return data.map(deserialize); | ||
return Object.fromEntries(Object.entries(data).map(([k, v]) => [k, deserialize(v)])); | ||
} | ||
return data; | ||
}; | ||
|
||
export const transformer: CombinedDataTransformer = { | ||
input: { serialize, deserialize }, | ||
output: { serialize, deserialize }, | ||
}; |
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
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