Repository navigation
Expand file tree
/
Copy pathserializable.ts
More file actions
79 lines (75 loc) · 2.61 KB
/
Copy pathserializable.ts
File metadata and controls
79 lines (75 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
createSafeExecutor,
type Fn,
type SafeResult,
} from "@imhonglu/toolkit";
/**
* A decorator that makes a class serializable by implementing toString() and toJSON() methods.
* The target class must implement static `parse`, `safeParse`, and `stringify` methods.
*
* @typeParam T - The type of the class being decorated. Must extend Newable and include `parse`, `safeParse`, and `stringify` methods
* @param targetClass - The class to make serializable
* @returns A proxy of the decorated class with `toString()`, `toJSON()` instance methods and `safeParse` static method
*
* @remarks
* The decorated class must implement:
* - static parse(value: string): Instance - Converts a string to class instance
* - static safeParse(value: string): SafeResult<Instance> - Converts a string to class instance with error handling
* - static stringify(value: Instance): string - Converts class instance to string
*
* ** Note: **
* The `safeParse` method is automatically created by the decorator.
*
* The decorator automatically:
* - Adds toString() and toJSON() instance methods that use the stringify implementation
* - Creates a safeParse static method that wraps the parse method with error handling
*
* @example
* ```ts
* @Serializable
* class Example {
* constructor(public text: string) {}
*
* static parse(value: string): Example {
* return new Example(value);
* }
*
* static stringify(value: Example): string {
* return value.text;
* }
* }
*
* const example = Example.parse("example");
* example.toString(); // "example"
* JSON.stringify(example); // "example"
*
* const result = Example.safeParse("example"); // { success: true, data: Example }
* if (result.success) {
* result.data.toString(); // "example"
* } else {
* result.error; // Error
* }
* ```
*
* @see {@link https://imhonglu.github.io/toolkit/docs/utils/create-safe-executor/create-safe-executor.html | createSafeExecutor}
*/
export function Serializable<
T extends Fn.Newable & {
parse: Fn.Callable<{ return: InstanceType<T> }>;
safeParse: Fn.Callable<{ return: SafeResult<InstanceType<T>> }>;
stringify: Fn.Callable<{ args: [InstanceType<T>]; return: string }>;
},
>(targetClass: T) {
const serialize = function (this: InstanceType<T>) {
return targetClass.stringify(this);
};
Object.defineProperties(targetClass.prototype, {
toString: { value: serialize },
toJSON: { value: serialize },
});
const safeParse = createSafeExecutor(targetClass.parse);
return new Proxy(targetClass, {
get: (target, prop) =>
prop === "safeParse" ? safeParse : target[prop as keyof T],
});
}