|
1 | 1 | import { JSONSchema7 } from "json-schema";
|
2 | 2 |
|
3 | 3 | export type SchemaType = {
|
4 |
| - schema: JSONSchema7; |
5 |
| - deserialize: (value: any) => any; |
| 4 | + schema: JSONSchema7; |
| 5 | + deserialize: (value: any) => any; |
6 | 6 | };
|
7 | 7 |
|
8 | 8 | export const SchemaTypes: Record<string, SchemaType> = {
|
9 |
| - date: { |
10 |
| - schema: { |
11 |
| - type: "string", |
12 |
| - format: "date-time", |
| 9 | + date: { |
| 10 | + schema: { |
| 11 | + type: "string", |
| 12 | + format: "date-time", |
| 13 | + }, |
| 14 | + deserialize: (value: string) => new Date(value), |
13 | 15 | },
|
14 |
| - deserialize: (value: string) => new Date(value), |
15 |
| - }, |
16 |
| - number: { |
17 |
| - schema: { |
18 |
| - type: "number", |
| 16 | + number: { |
| 17 | + schema: { |
| 18 | + type: "number", |
| 19 | + }, |
| 20 | + |
| 21 | + deserialize: (value: number) => value, |
19 | 22 | },
|
20 |
| - deserialize: (value: number) => value, |
21 |
| - }, |
22 |
| - string: { |
23 |
| - schema: { |
24 |
| - type: "string", |
| 23 | + string: { |
| 24 | + schema: { |
| 25 | + type: "string", |
| 26 | + }, |
| 27 | + deserialize: (value: string) => value, |
25 | 28 | },
|
26 |
| - deserialize: (value: string) => value, |
27 |
| - }, |
28 |
| - boolean: { |
29 |
| - schema: { |
30 |
| - type: "boolean", |
| 29 | + boolean: { |
| 30 | + schema: { |
| 31 | + type: "boolean", |
| 32 | + }, |
| 33 | + deserialize: (value: boolean) => value, |
31 | 34 | },
|
32 |
| - deserialize: (value: boolean) => value, |
33 |
| - }, |
34 |
| - array: { |
35 |
| - schema: { |
36 |
| - type: "array", |
| 35 | + array: { |
| 36 | + schema: { |
| 37 | + type: "array", |
| 38 | + }, |
| 39 | + deserialize: (value: any[]) => value, |
37 | 40 | },
|
38 |
| - deserialize: (value: any[]) => value, |
39 |
| - }, |
40 |
| - object: { |
41 |
| - schema: { |
42 |
| - type: "object", |
| 41 | + object: { |
| 42 | + schema: { |
| 43 | + type: "object", |
| 44 | + }, |
| 45 | + deserialize: (value: Record<string, any>) => value, |
43 | 46 | },
|
44 |
| - deserialize: (value: Record<string, any>) => value, |
45 |
| - }, |
46 | 47 | };
|
47 | 48 |
|
48 | 49 | export function getSchemaType(value: any): SchemaType {
|
49 |
| - if (value instanceof Date) return SchemaTypes.date; |
50 |
| - if (Array.isArray(value)) return SchemaTypes.array; |
51 |
| - if (typeof value === "object" && value !== null) return SchemaTypes.object; |
52 |
| - if (typeof value === "number") return SchemaTypes.number; |
53 |
| - if (typeof value === "boolean") return SchemaTypes.boolean; |
54 |
| - return SchemaTypes.string; |
| 50 | + if (value instanceof Date) return SchemaTypes.date; |
| 51 | + if (Array.isArray(value)) return SchemaTypes.array; |
| 52 | + if (typeof value === "object" && value !== null) return SchemaTypes.object; |
| 53 | + if (typeof value === "number") return SchemaTypes.number; |
| 54 | + if (typeof value === "boolean") return SchemaTypes.boolean; |
| 55 | + return SchemaTypes.string; |
55 | 56 | }
|
56 | 57 |
|
57 | 58 | export function serializeValue(value: any): { value: any; type: string } {
|
58 |
| - const type = getSchemaType(value); |
59 |
| - let serializedValue = value; |
| 59 | + const type = getSchemaType(value); |
| 60 | + let serializedValue = value; |
60 | 61 |
|
61 |
| - if (type === SchemaTypes.date) { |
62 |
| - serializedValue = value.toISOString(); |
63 |
| - } else if (type === SchemaTypes.object) { |
64 |
| - serializedValue = JSON.stringify(value); |
65 |
| - } |
| 62 | + if (type === SchemaTypes.date) { |
| 63 | + serializedValue = value.toISOString(); |
| 64 | + } else if (type === SchemaTypes.object) { |
| 65 | + serializedValue = JSON.stringify(value); |
| 66 | + } |
66 | 67 |
|
67 |
| - return { |
68 |
| - value: serializedValue, |
69 |
| - type: |
70 |
| - Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) || |
71 |
| - "string", |
72 |
| - }; |
| 68 | + return { |
| 69 | + value: serializedValue, |
| 70 | + type: |
| 71 | + Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) || |
| 72 | + "string", |
| 73 | + }; |
73 | 74 | }
|
74 | 75 |
|
75 | 76 | export function deserializeValue(value: any, type: string): any {
|
76 |
| - const schemaType = SchemaTypes[type]; |
77 |
| - if (!schemaType) return value; |
| 77 | + const schemaType = SchemaTypes[type]; |
| 78 | + if (!schemaType) return value; |
78 | 79 |
|
79 |
| - if (type === "object") { |
80 |
| - return JSON.parse(value); |
81 |
| - } |
| 80 | + if (type === "object") { |
| 81 | + return JSON.parse(value); |
| 82 | + } |
82 | 83 |
|
83 |
| - return schemaType.deserialize(value); |
| 84 | + return schemaType.deserialize(value); |
84 | 85 | }
|
0 commit comments