Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/large-mayflies-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@thirdweb-dev/sdk": patch
"@thirdweb-dev/solana": patch
"@thirdweb-dev/storage": patch
---

Use independent JSON types by package
18 changes: 17 additions & 1 deletion packages/sdk/src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { CONTRACTS_MAP, PREBUILT_CONTRACTS_MAP } from "../contracts";
import type { SmartContract } from "../contracts/smart-contract";
import { BigNumber, BytesLike, CallOverrides, Signer, providers } from "ethers";
import {
BigNumber,
BytesLike,
CallOverrides,
Signer,
providers,
BigNumberish,
} from "ethers";

// --- utility types extracted from from ts-toolbelt --- //

Expand Down Expand Up @@ -108,3 +115,12 @@ export interface GaslessTransaction {
functionArgs: any[];
callOverrides: CallOverrides;
}

type JsonLiteralOutput = boolean | null | number | string;
type JsonLiteralInput = JsonLiteralOutput | BigNumber | bigint;

export type JsonOutput = JsonLiteralOutput | JsonObjectOutput | JsonOutput[];
export type JsonObjectOutput = { [key: string]: JsonOutput };

export type JsonInput = JsonLiteralInput | JsonObjectInput | JsonInput[];
export type JsonObjectInput = { [key: string]: JsonInput };
8 changes: 6 additions & 2 deletions packages/sdk/src/schema/contracts/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { AddressSchema, BasisPointsSchema } from "../../shared";
import { FileOrBufferOrStringSchema, JsonSchema } from "@thirdweb-dev/storage";
import {
AddressSchema,
BasisPointsSchema,
FileOrBufferOrStringSchema,
JsonSchema,
} from "../../shared";
import { constants } from "ethers";
import { z } from "zod";

Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/schema/contracts/custom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { toSemver } from "../../common/index";
import { AddressSchema, BigNumberishSchema } from "../shared";
import {
AddressSchema,
BigNumberishSchema,
FileOrBufferOrStringSchema,
JsonSchema,
} from "../shared";
import {
CommonContractOutputSchema,
CommonContractSchema,
Expand All @@ -10,7 +15,6 @@ import {
CommonTrustedForwarderSchema,
MerkleSchema,
} from "./common";
import { FileOrBufferOrStringSchema, JsonSchema } from "@thirdweb-dev/storage";
import { BigNumberish } from "ethers";
import { z } from "zod";

Expand Down
40 changes: 38 additions & 2 deletions packages/sdk/src/schema/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Json } from "@thirdweb-dev/storage";
import { JsonOutput, JsonInput } from "../core/types";
import { BigNumber, CallOverrides, utils } from "ethers";
import { z } from "zod";
import { z, ZodTypeDef } from "zod";

export const MAX_BPS = 10000;

Expand Down Expand Up @@ -48,6 +48,18 @@ export const AddressSchema = z.string().refine(
},
);

export const JsonLiteral = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
BigNumberishSchema,
]);

export const JsonSchema: z.ZodSchema<JsonOutput, ZodTypeDef, JsonInput> =
z.lazy(() => z.union([JsonLiteral, JsonObjectSchema, z.array(JsonSchema)]));
export const JsonObjectSchema = z.record(JsonSchema);

export const AmountSchema = z
.union([
z.string().regex(/^([0-9]+\.?[0-9]*|\.[0-9]+)$/, "Invalid amount"),
Expand Down Expand Up @@ -84,3 +96,27 @@ export const CallOverrideSchema: z.ZodType<CallOverrides> = z
type: z.number().optional(),
})
.strict();

const isBrowser = () => typeof window !== "undefined";
const FileOrBufferUnionSchema = isBrowser()
? (z.instanceof(File) as z.ZodType<InstanceType<typeof File>>)
: (z.instanceof(Buffer) as z.ZodTypeAny); // @fixme, this is a hack to make browser happy for now

/**
* @internal
*/
export const FileOrBufferSchema = z.union([
FileOrBufferUnionSchema,
z.object({
data: z.union([FileOrBufferUnionSchema, z.string()]),
name: z.string(),
}),
]);

/**
* @internal
*/
export const FileOrBufferOrStringSchema = z.union([
FileOrBufferSchema,
z.string(),
]);
8 changes: 6 additions & 2 deletions packages/sdk/src/schema/tokens/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { BigNumberSchema, HexColor } from "../../shared";
import {
BigNumberSchema,
FileOrBufferOrStringSchema,
HexColor,
JsonSchema,
} from "../../shared";
import { OptionalPropertiesInput } from "./properties";
import { FileOrBufferOrStringSchema, JsonSchema } from "@thirdweb-dev/storage";
import { z } from "zod";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/schema/tokens/common/properties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonObjectSchema } from "@thirdweb-dev/storage";
import { JsonObjectSchema } from "../../shared";
import { z } from "zod";

/**
Expand Down
52 changes: 51 additions & 1 deletion packages/solana/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Signer, WalletAdapter } from "@metaplex-foundation/js";
import { JsonObjectSchema } from "@thirdweb-dev/storage";
import { z } from "zod";

/**
Expand Down Expand Up @@ -33,6 +32,33 @@ export const JsonLiteral = z.union([
z.null(),
]);

/**
* @internal
*/
export const JsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([JsonLiteral, JsonObjectSchema, z.array(JsonSchema)]),
);

/**
* @internal
*/
export const JsonObjectSchema = z.record(z.string(), JsonSchema);

/**
* @internal
*/
export type JsonLiteral = number | string | null | boolean;

/**
* @internal
*/
export type Json = JsonLiteral | JsonObject | Json[];

/**
* @internal
*/
export type JsonObject = { [key: string]: Json };

/**
* @internal
*/
Expand Down Expand Up @@ -104,3 +130,27 @@ export type WalletAccount = {
address: string;
name: string;
};

const isBrowser = () => typeof window !== "undefined";
const FileOrBufferUnionSchema = isBrowser()
? (z.instanceof(File) as z.ZodType<InstanceType<typeof File>>)
: (z.instanceof(Buffer) as z.ZodTypeAny); // @fixme, this is a hack to make browser happy for now

/**
* @internal
*/
export const FileOrBufferSchema = z.union([
FileOrBufferUnionSchema,
z.object({
data: z.union([FileOrBufferUnionSchema, z.string()]),
name: z.string(),
}),
]);

/**
* @internal
*/
export const FileOrBufferOrStringSchema = z.union([
FileOrBufferSchema,
z.string(),
]);
7 changes: 5 additions & 2 deletions packages/solana/src/types/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { AmountSchema } from "../common";
import { FileOrBufferOrStringSchema, JsonSchema } from "@thirdweb-dev/storage";
import {
AmountSchema,
FileOrBufferOrStringSchema,
JsonSchema,
} from "../common";
import { z } from "zod";

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/solana/src/types/nft.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
CurrencyValueSchema,
FileOrBufferOrStringSchema,
HexColor,
JsonSchema,
OptionalPropertiesInput,
} from "./common";
import { FileOrBufferOrStringSchema, JsonSchema } from "@thirdweb-dev/storage";
import { z } from "zod";

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"dependencies": {
"cross-fetch": "^3.1.5",
"form-data": "^4.0.0",
"uuid": "^9.0.0",
"zod": "^3.11.6"
"uuid": "^9.0.0"
}
}
Loading