Skip to content

Commit a9ec190

Browse files
authored
Remove JSON types (#188)
1 parent 3edec39 commit a9ec190

File tree

10 files changed

+62
-76
lines changed

10 files changed

+62
-76
lines changed

.changeset/shiny-suits-kneel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@thirdweb-dev/sdk": patch
3+
"@thirdweb-dev/solana": patch
4+
---
5+
6+
Update JSON schema in SDKs

packages/sdk/src/core/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,3 @@ export interface GaslessTransaction {
108108
functionArgs: any[];
109109
callOverrides: CallOverrides;
110110
}
111-
112-
type JsonLiteralOutput = boolean | null | number | string;
113-
type JsonLiteralInput = JsonLiteralOutput | BigNumber | bigint;
114-
115-
export type JsonOutput = JsonLiteralOutput | JsonObjectOutput | JsonOutput[];
116-
export type JsonObjectOutput = { [key: string]: JsonOutput };
117-
118-
export type JsonInput = JsonLiteralInput | JsonObjectInput | JsonInput[];
119-
export type JsonObjectInput = { [key: string]: JsonInput };

packages/sdk/src/schema/contracts/common/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
AddressSchema,
33
BasisPointsSchema,
44
FileOrBufferOrStringSchema,
5-
JsonSchema,
65
} from "../../shared";
76
import { constants } from "ethers";
87
import { z } from "zod";
@@ -24,7 +23,7 @@ export type CommonContractSchemaInput = z.input<typeof CommonContractSchema>;
2423
*/
2524
export const CommonContractOutputSchema = CommonContractSchema.extend({
2625
image: z.string().optional(),
27-
}).catchall(z.lazy(() => JsonSchema));
26+
}).catchall(z.unknown());
2827

2928
/**
3029
* @internal

packages/sdk/src/schema/contracts/custom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { toSemver } from "../../common/index";
22
import {
33
AddressSchema,
44
BigNumberishSchema,
5+
BigNumberTransformSchema,
56
FileOrBufferOrStringSchema,
6-
JsonSchema,
77
} from "../shared";
88
import {
99
CommonContractOutputSchema,
@@ -22,7 +22,7 @@ import { z } from "zod";
2222
* @internal
2323
*/
2424
export const BYOCContractMetadataSchema = CommonContractSchema.catchall(
25-
z.lazy(() => JsonSchema),
25+
z.union([BigNumberTransformSchema, z.unknown()]),
2626
);
2727

2828
/**

packages/sdk/src/schema/shared.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
import { JsonOutput, JsonInput } from "../core/types";
21
import { BigNumber, CallOverrides, utils } from "ethers";
3-
import { z, ZodTypeDef } from "zod";
2+
import { z } from "zod";
3+
4+
const isBrowser = () => typeof window !== "undefined";
5+
const FileOrBufferUnionSchema = isBrowser()
6+
? (z.instanceof(File) as z.ZodType<InstanceType<typeof File>>)
7+
: (z.instanceof(Buffer) as z.ZodTypeAny); // @fixme, this is a hack to make browser happy for now
8+
9+
/**
10+
* @internal
11+
*/
12+
export const FileOrBufferSchema = z.union([
13+
FileOrBufferUnionSchema,
14+
z.object({
15+
data: z.union([FileOrBufferUnionSchema, z.string()]),
16+
name: z.string(),
17+
}),
18+
]);
19+
20+
/**
21+
* @internal
22+
*/
23+
export const FileOrBufferOrStringSchema = z.union([
24+
FileOrBufferSchema,
25+
z.string(),
26+
]);
427

528
export const MAX_BPS = 10000;
629

@@ -21,6 +44,17 @@ export const BigNumberishSchema = BigNumberSchema.transform((arg) =>
2144
arg.toString(),
2245
);
2346

47+
export const BigNumberTransformSchema = z
48+
.union([
49+
z.bigint(),
50+
z.custom<BigNumber>((data) => {
51+
return BigNumber.isBigNumber(data);
52+
}),
53+
])
54+
.transform((arg) => {
55+
return BigNumber.from(arg).toString();
56+
});
57+
2458
export const BasisPointsSchema = z
2559
.number()
2660
.max(MAX_BPS, "Cannot exeed 100%")
@@ -48,18 +82,6 @@ export const AddressSchema = z.string().refine(
4882
},
4983
);
5084

51-
export const JsonLiteral = z.union([
52-
z.string(),
53-
z.number(),
54-
z.boolean(),
55-
z.null(),
56-
BigNumberishSchema,
57-
]);
58-
59-
export const JsonSchema: z.ZodSchema<JsonOutput, ZodTypeDef, JsonInput> =
60-
z.lazy(() => z.union([JsonLiteral, JsonObjectSchema, z.array(JsonSchema)]));
61-
export const JsonObjectSchema = z.record(JsonSchema);
62-
6385
export const AmountSchema = z
6486
.union([
6587
z.string().regex(/^([0-9]+\.?[0-9]*|\.[0-9]+)$/, "Invalid amount"),
@@ -96,27 +118,3 @@ export const CallOverrideSchema: z.ZodType<CallOverrides> = z
96118
type: z.number().optional(),
97119
})
98120
.strict();
99-
100-
const isBrowser = () => typeof window !== "undefined";
101-
const FileOrBufferUnionSchema = isBrowser()
102-
? (z.instanceof(File) as z.ZodType<InstanceType<typeof File>>)
103-
: (z.instanceof(Buffer) as z.ZodTypeAny); // @fixme, this is a hack to make browser happy for now
104-
105-
/**
106-
* @internal
107-
*/
108-
export const FileOrBufferSchema = z.union([
109-
FileOrBufferUnionSchema,
110-
z.object({
111-
data: z.union([FileOrBufferUnionSchema, z.string()]),
112-
name: z.string(),
113-
}),
114-
]);
115-
116-
/**
117-
* @internal
118-
*/
119-
export const FileOrBufferOrStringSchema = z.union([
120-
FileOrBufferSchema,
121-
z.string(),
122-
]);

packages/sdk/src/schema/tokens/common/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
BigNumberSchema,
3+
BigNumberTransformSchema,
34
FileOrBufferOrStringSchema,
45
HexColor,
5-
JsonSchema,
66
} from "../../shared";
77
import { OptionalPropertiesInput } from "./properties";
88
import { z } from "zod";
@@ -17,7 +17,7 @@ export const CommonTokenInput = z
1717
image: FileOrBufferOrStringSchema.nullable().optional(),
1818
external_url: FileOrBufferOrStringSchema.nullable().optional(),
1919
})
20-
.catchall(z.lazy(() => JsonSchema));
20+
.catchall(z.union([BigNumberTransformSchema, z.unknown()]));
2121

2222
/**
2323
* @internal
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { JsonObjectSchema } from "../../shared";
1+
import { BigNumberTransformSchema } from "../../shared";
22
import { z } from "zod";
33

4+
const PropertiesInput = z
5+
.object({})
6+
.catchall(z.union([BigNumberTransformSchema, z.unknown()]));
7+
48
/**
59
* @internal
610
*/
711
export const OptionalPropertiesInput = z
8-
.union([z.array(JsonObjectSchema), JsonObjectSchema])
12+
.union([z.array(PropertiesInput), PropertiesInput])
913
.optional();

packages/solana/src/types/common.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ export const JsonLiteral = z.union([
3232
z.null(),
3333
]);
3434

35-
/**
36-
* @internal
37-
*/
38-
export const JsonSchema: z.ZodType<Json> = z.lazy(() =>
39-
z.union([JsonLiteral, JsonObjectSchema, z.array(JsonSchema)]),
40-
);
41-
42-
/**
43-
* @internal
44-
*/
45-
export const JsonObjectSchema = z.record(z.string(), JsonSchema);
46-
4735
/**
4836
* @internal
4937
*/
@@ -70,11 +58,16 @@ export const HexColor = z.union([
7058
z.string().length(0),
7159
]);
7260

61+
/**
62+
* @internal
63+
*/
64+
const PropertiesInput = z.object({}).catchall(z.unknown());
65+
7366
/**
7467
* @internal
7568
*/
7669
export const OptionalPropertiesInput = z
77-
.union([z.array(JsonObjectSchema), JsonObjectSchema])
70+
.union([z.array(PropertiesInput), PropertiesInput])
7871
.optional();
7972

8073
/**

packages/solana/src/types/contracts/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
AmountSchema,
3-
FileOrBufferOrStringSchema,
4-
JsonSchema,
5-
} from "../common";
1+
import { AmountSchema, FileOrBufferOrStringSchema } from "../common";
62
import { z } from "zod";
73

84
/**
@@ -21,7 +17,7 @@ export const CommonContractSchema = z.object({
2117
*/
2218
export const CommonContractOutputSchema = CommonContractSchema.extend({
2319
image: z.string().optional(),
24-
}).catchall(z.lazy(() => JsonSchema));
20+
}).catchall(z.unknown());
2521

2622
/// NFT ///
2723

packages/solana/src/types/nft.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
CurrencyValueSchema,
33
FileOrBufferOrStringSchema,
44
HexColor,
5-
JsonSchema,
65
OptionalPropertiesInput,
76
} from "./common";
87
import { z } from "zod";
@@ -18,7 +17,7 @@ export const CommonTokenInput = z
1817
image: FileOrBufferOrStringSchema.nullable().optional(),
1918
external_url: FileOrBufferOrStringSchema.nullable().optional(),
2019
})
21-
.catchall(z.lazy(() => JsonSchema));
20+
.catchall(z.unknown());
2221

2322
/**
2423
* @internal

0 commit comments

Comments
 (0)