Skip to content

Commit 46a4814

Browse files
committed
remove String extension in LazyJsonString
1 parent b52b4e8 commit 46a4814

File tree

3 files changed

+38
-62
lines changed

3 files changed

+38
-62
lines changed

.changeset/warm-dragons-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": major
3+
---
4+
5+
remove String extension in LazyJsonString

packages/smithy-client/src/lazy-json.spec.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,26 @@ import { describe, expect, test as it } from "vitest";
22

33
import { LazyJsonString } from "./lazy-json";
44
describe("LazyJsonString", () => {
5-
it("should has string methods", () => {
6-
const jsonValue = new LazyJsonString('"foo"');
7-
expect(jsonValue.length).toBe(5);
8-
expect(jsonValue.toString()).toBe('"foo"');
9-
});
10-
11-
it("should deserialize json properly", () => {
12-
const jsonValue = new LazyJsonString('"foo"');
13-
expect(jsonValue.deserializeJSON()).toBe("foo");
14-
const wrongJsonValue = new LazyJsonString("foo");
15-
expect(() => wrongJsonValue.deserializeJSON()).toThrow();
16-
});
17-
18-
it("should get JSON string properly", () => {
19-
const jsonValue = new LazyJsonString('{"foo", "bar"}');
20-
expect(jsonValue.toJSON()).toBe('{"foo", "bar"}');
5+
it("returns identical values for toString(), valueOf(), and toJSON()", () => {
6+
const jsonValue = LazyJsonString.from({ foo: "bar" });
7+
expect(jsonValue.valueOf()).toBe(JSON.stringify({ foo: "bar" }));
8+
expect(jsonValue.toString()).toBe(JSON.stringify({ foo: "bar" }));
9+
expect(jsonValue.toJSON()).toBe(JSON.stringify({ foo: "bar" }));
2110
});
2211

2312
it("can instantiate from LazyJsonString class", () => {
24-
const original = new LazyJsonString('"foo"');
25-
const newOne = LazyJsonString.fromObject(original);
13+
const original = LazyJsonString.from('"foo"');
14+
const newOne = LazyJsonString.from(original);
2615
expect(newOne.toString()).toBe('"foo"');
2716
});
2817

2918
it("can instantiate from String class", () => {
30-
const jsonValue = LazyJsonString.fromObject(new String('"foo"'));
19+
const jsonValue = LazyJsonString.from('"foo"');
3120
expect(jsonValue.toString()).toBe('"foo"');
3221
});
3322

3423
it("can instantiate from object", () => {
35-
const jsonValue = LazyJsonString.fromObject({ foo: "bar" });
24+
const jsonValue = LazyJsonString.from({ foo: "bar" });
3625
expect(jsonValue.toString()).toBe('{"foo":"bar"}');
3726
});
3827
});
Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
11
/**
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-
*
152
* @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+
*
168
*/
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) {}
3611

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;
4314
}
4415

45-
toJSON(): string {
46-
return super.toString();
16+
public valueOf(): string {
17+
return this.value;
4718
}
4819

49-
static fromObject(object: any): LazyJsonString {
20+
public toJSON(): string {
21+
return this.value;
22+
}
23+
24+
public static from(object: any): LazyJsonString {
5025
if (object instanceof LazyJsonString) {
5126
return object;
52-
} else if (object instanceof String || typeof object === "string") {
27+
} else if (typeof object === "string") {
5328
return new LazyJsonString(object);
5429
}
5530
return new LazyJsonString(JSON.stringify(object));
5631
}
32+
33+
/**
34+
* @deprecated call from() instead.
35+
*/
36+
public static fromObject(object: any): LazyJsonString {
37+
return LazyJsonString.from(object);
38+
}
5739
}

0 commit comments

Comments
 (0)