|
1 | 1 | /** |
2 | | - * Lazy String holder for JSON typed contents. |
3 | | - */ |
4 | | - |
5 | | -interface StringWrapper { |
6 | | - new (arg: any): String; |
7 | | -} |
8 | | - |
9 | | -/** |
10 | | - * Because of https://github.com/microsoft/tslib/issues/95, |
11 | | - * TS 'extends' shim doesn't support extending native types like String. |
12 | | - * So here we create StringWrapper that duplicate everything from String |
13 | | - * class including its prototype chain. So we can extend from here. |
14 | | - * |
15 | 2 | * @internal |
| 3 | + * |
| 4 | + * This class allows the usage of data objects in fields that expect |
| 5 | + * JSON strings. It serializes the data object into JSON |
| 6 | + * if needed during the request serialization step. |
| 7 | + * |
16 | 8 | */ |
17 | | -// @ts-ignore StringWrapper implementation is not a simple constructor |
18 | | -export const StringWrapper: StringWrapper = function () { |
19 | | - //@ts-ignore 'this' cannot be assigned to any, but Object.getPrototypeOf accepts any |
20 | | - const Class = Object.getPrototypeOf(this).constructor; |
21 | | - const Constructor = Function.bind.apply(String, [null as any, ...arguments]); |
22 | | - //@ts-ignore Call wrapped String constructor directly, don't bother typing it. |
23 | | - const instance = new Constructor(); |
24 | | - Object.setPrototypeOf(instance, Class.prototype); |
25 | | - return instance as String; |
26 | | -}; |
27 | | -StringWrapper.prototype = Object.create(String.prototype, { |
28 | | - constructor: { |
29 | | - value: StringWrapper, |
30 | | - enumerable: false, |
31 | | - writable: true, |
32 | | - configurable: true, |
33 | | - }, |
34 | | -}); |
35 | | -Object.setPrototypeOf(StringWrapper, String); |
| 9 | +export class LazyJsonString { |
| 10 | + private constructor(private value: string) {} |
36 | 11 |
|
37 | | -/** |
38 | | - * @internal |
39 | | - */ |
40 | | -export class LazyJsonString extends StringWrapper { |
41 | | - deserializeJSON(): any { |
42 | | - return JSON.parse(super.toString()); |
| 12 | + public toString(): string { |
| 13 | + return this.value; |
43 | 14 | } |
44 | 15 |
|
45 | | - toJSON(): string { |
46 | | - return super.toString(); |
| 16 | + public valueOf(): string { |
| 17 | + return this.value; |
47 | 18 | } |
48 | 19 |
|
49 | | - static fromObject(object: any): LazyJsonString { |
| 20 | + public toJSON(): string { |
| 21 | + return this.value; |
| 22 | + } |
| 23 | + |
| 24 | + public static from(object: any): LazyJsonString { |
50 | 25 | if (object instanceof LazyJsonString) { |
51 | 26 | return object; |
52 | | - } else if (object instanceof String || typeof object === "string") { |
| 27 | + } else if (typeof object === "string") { |
53 | 28 | return new LazyJsonString(object); |
54 | 29 | } |
55 | 30 | return new LazyJsonString(JSON.stringify(object)); |
56 | 31 | } |
| 32 | + |
| 33 | + /** |
| 34 | + * @deprecated call from() instead. |
| 35 | + */ |
| 36 | + public static fromObject(object: any): LazyJsonString { |
| 37 | + return LazyJsonString.from(object); |
| 38 | + } |
57 | 39 | } |
0 commit comments