@mysten/sui 2.31.2 → 2.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/bcs/bcs.d.mts +6 -6
  3. package/dist/client/protocol-version.mjs +12 -0
  4. package/dist/client/protocol-version.mjs.map +1 -0
  5. package/dist/cryptography/signature.d.mts +6 -6
  6. package/dist/graphql/client.d.mts.map +1 -1
  7. package/dist/graphql/client.mjs +7 -4
  8. package/dist/graphql/client.mjs.map +1 -1
  9. package/dist/grpc/client.d.mts.map +1 -1
  10. package/dist/grpc/client.mjs +2 -2
  11. package/dist/grpc/client.mjs.map +1 -1
  12. package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
  13. package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
  14. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
  15. package/dist/grpc/transport.d.mts +5 -3
  16. package/dist/grpc/transport.d.mts.map +1 -1
  17. package/dist/grpc/transport.mjs +32 -2
  18. package/dist/grpc/transport.mjs.map +1 -1
  19. package/dist/transactions/Transaction.d.mts +6 -6
  20. package/dist/transactions/data/internal.d.mts +133 -133
  21. package/dist/transactions/data/internal.d.mts.map +1 -1
  22. package/dist/transactions/data/internal.mjs +24 -2
  23. package/dist/transactions/data/internal.mjs.map +1 -1
  24. package/dist/transactions/data/v1.d.mts +239 -239
  25. package/dist/transactions/data/v1.d.mts.map +1 -1
  26. package/dist/transactions/data/v2.d.mts +16 -16
  27. package/dist/transactions/data/v2.d.mts.map +1 -1
  28. package/dist/version.mjs +1 -1
  29. package/dist/version.mjs.map +1 -1
  30. package/dist/zklogin/bcs.d.mts +14 -14
  31. package/docs/clients/grpc.md +7 -1
  32. package/package.json +1 -1
  33. package/src/client/protocol-version.ts +11 -0
  34. package/src/graphql/client.ts +13 -4
  35. package/src/grpc/client.ts +5 -3
  36. package/src/grpc/transport.ts +40 -1
  37. package/src/transactions/data/internal.ts +43 -10
  38. package/src/version.ts +1 -1
@@ -1,12 +1,12 @@
1
1
  import { EnumInputShape } from "@mysten/bcs";
2
- import * as valibot1369 from "valibot";
2
+ import * as valibot0 from "valibot";
3
3
  import { GenericSchema, InferOutput } from "valibot";
4
4
 
5
5
  //#region src/transactions/data/v2.d.ts
6
- declare const SerializedTransactionDataV2Schema: valibot1369.ObjectSchema<{
7
- readonly version: valibot1369.LiteralSchema<2, undefined>;
8
- readonly sender: valibot1369.NullishSchema<valibot1369.SchemaWithPipe<readonly [valibot1369.StringSchema<undefined>, valibot1369.TransformAction<string, string>, valibot1369.CheckAction<string, undefined>]>, undefined>;
9
- readonly expiration: valibot1369.NullishSchema<GenericSchema<EnumInputShape<{
6
+ declare const SerializedTransactionDataV2Schema: valibot0.ObjectSchema<{
7
+ readonly version: valibot0.LiteralSchema<2, undefined>;
8
+ readonly sender: valibot0.NullishSchema<valibot0.SchemaWithPipe<readonly [valibot0.StringSchema<undefined>, valibot0.TransformAction<string, string>, valibot0.CheckAction<string, undefined>]>, undefined>;
9
+ readonly expiration: valibot0.NullishSchema<GenericSchema<EnumInputShape<{
10
10
  None: true;
11
11
  Epoch: string | number;
12
12
  ValidDuring: {
@@ -30,17 +30,17 @@ declare const SerializedTransactionDataV2Schema: valibot1369.ObjectSchema<{
30
30
  nonce: number;
31
31
  };
32
32
  }>>, undefined>;
33
- readonly gasData: valibot1369.ObjectSchema<{
34
- readonly budget: valibot1369.NullableSchema<valibot1369.SchemaWithPipe<readonly [valibot1369.UnionSchema<[valibot1369.StringSchema<undefined>, valibot1369.SchemaWithPipe<readonly [valibot1369.NumberSchema<undefined>, valibot1369.IntegerAction<number, undefined>]>], undefined>, valibot1369.CheckAction<string | number, "Invalid u64">]>, undefined>;
35
- readonly price: valibot1369.NullableSchema<valibot1369.SchemaWithPipe<readonly [valibot1369.UnionSchema<[valibot1369.StringSchema<undefined>, valibot1369.SchemaWithPipe<readonly [valibot1369.NumberSchema<undefined>, valibot1369.IntegerAction<number, undefined>]>], undefined>, valibot1369.CheckAction<string | number, "Invalid u64">]>, undefined>;
36
- readonly owner: valibot1369.NullableSchema<valibot1369.SchemaWithPipe<readonly [valibot1369.StringSchema<undefined>, valibot1369.TransformAction<string, string>, valibot1369.CheckAction<string, undefined>]>, undefined>;
37
- readonly payment: valibot1369.NullableSchema<valibot1369.ArraySchema<valibot1369.ObjectSchema<{
38
- readonly objectId: valibot1369.SchemaWithPipe<readonly [valibot1369.StringSchema<undefined>, valibot1369.TransformAction<string, string>, valibot1369.CheckAction<string, undefined>]>;
39
- readonly version: valibot1369.SchemaWithPipe<readonly [valibot1369.UnionSchema<[valibot1369.StringSchema<undefined>, valibot1369.SchemaWithPipe<readonly [valibot1369.NumberSchema<undefined>, valibot1369.IntegerAction<number, undefined>]>], undefined>, valibot1369.CheckAction<string | number, "Invalid u64">]>;
40
- readonly digest: valibot1369.StringSchema<undefined>;
33
+ readonly gasData: valibot0.ObjectSchema<{
34
+ readonly budget: valibot0.NullableSchema<valibot0.SchemaWithPipe<readonly [valibot0.UnionSchema<[valibot0.StringSchema<undefined>, valibot0.SchemaWithPipe<readonly [valibot0.NumberSchema<undefined>, valibot0.IntegerAction<number, undefined>]>], undefined>, valibot0.CheckAction<string | number, "Invalid u64">]>, undefined>;
35
+ readonly price: valibot0.NullableSchema<valibot0.SchemaWithPipe<readonly [valibot0.UnionSchema<[valibot0.StringSchema<undefined>, valibot0.SchemaWithPipe<readonly [valibot0.NumberSchema<undefined>, valibot0.IntegerAction<number, undefined>]>], undefined>, valibot0.CheckAction<string | number, "Invalid u64">]>, undefined>;
36
+ readonly owner: valibot0.NullableSchema<valibot0.SchemaWithPipe<readonly [valibot0.StringSchema<undefined>, valibot0.TransformAction<string, string>, valibot0.CheckAction<string, undefined>]>, undefined>;
37
+ readonly payment: valibot0.NullableSchema<valibot0.ArraySchema<valibot0.ObjectSchema<{
38
+ readonly objectId: valibot0.SchemaWithPipe<readonly [valibot0.StringSchema<undefined>, valibot0.TransformAction<string, string>, valibot0.CheckAction<string, undefined>]>;
39
+ readonly version: valibot0.SchemaWithPipe<readonly [valibot0.UnionSchema<[valibot0.StringSchema<undefined>, valibot0.SchemaWithPipe<readonly [valibot0.NumberSchema<undefined>, valibot0.IntegerAction<number, undefined>]>], undefined>, valibot0.CheckAction<string | number, "Invalid u64">]>;
40
+ readonly digest: valibot0.StringSchema<undefined>;
41
41
  }, undefined>, undefined>, undefined>;
42
42
  }, undefined>;
43
- readonly inputs: valibot1369.ArraySchema<GenericSchema<EnumInputShape<{
43
+ readonly inputs: valibot0.ArraySchema<GenericSchema<EnumInputShape<{
44
44
  Object: EnumInputShape<{
45
45
  ImmOrOwnedObject: {
46
46
  objectId: string;
@@ -90,7 +90,7 @@ declare const SerializedTransactionDataV2Schema: valibot1369.ObjectSchema<{
90
90
  };
91
91
  };
92
92
  }>>, undefined>;
93
- readonly commands: valibot1369.ArraySchema<GenericSchema<EnumInputShape<{
93
+ readonly commands: valibot0.ArraySchema<GenericSchema<EnumInputShape<{
94
94
  MoveCall: {
95
95
  package: string;
96
96
  module: string;
@@ -189,7 +189,7 @@ declare const SerializedTransactionDataV2Schema: valibot1369.ObjectSchema<{
189
189
  };
190
190
  };
191
191
  }>>, undefined>;
192
- readonly digest: valibot1369.OptionalSchema<valibot1369.NullableSchema<valibot1369.StringSchema<undefined>, undefined>, undefined>;
192
+ readonly digest: valibot0.OptionalSchema<valibot0.NullableSchema<valibot0.StringSchema<undefined>, undefined>, undefined>;
193
193
  }, undefined>;
194
194
  type SerializedTransactionDataV2 = InferOutput<typeof SerializedTransactionDataV2Schema>;
195
195
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"v2.d.mts","names":[],"sources":["../../../src/transactions/data/v2.ts"],"mappings":";;;;;cAqJa,iCAAA,cAAiC,YAAA;EAAA,kBAQ5C,WAAA,CAAA,aAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAEU,2BAAA,GAA8B,WAAA,QAAmB,iCAAA"}
1
+ {"version":3,"file":"v2.d.mts","names":[],"sources":["../../../src/transactions/data/v2.ts"],"mappings":";;;;;cAqJa,iCAAA,WAAiC,YAAA;EAAA,kBAQ5C,QAAA,CAAA,aAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAEU,2BAAA,GAA8B,WAAA,QAAmB,iCAAA"}
package/dist/version.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/version.ts
2
- const PACKAGE_VERSION = "2.31.2";
2
+ const PACKAGE_VERSION = "2.32.0";
3
3
 
4
4
  //#endregion
5
5
  export { PACKAGE_VERSION };
@@ -1 +1 @@
1
- {"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.31.2';\n"],"mappings":";AAKA,MAAa,kBAAkB"}
1
+ {"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.32.0';\n"],"mappings":";AAKA,MAAa,kBAAkB"}
@@ -1,31 +1,31 @@
1
- import * as _mysten_bcs1384 from "@mysten/bcs";
1
+ import * as _mysten_bcs1248 from "@mysten/bcs";
2
2
  import { InferBcsInput } from "@mysten/bcs";
3
3
 
4
4
  //#region src/zklogin/bcs.d.ts
5
- declare const zkLoginSignature: _mysten_bcs1384.BcsStruct<{
6
- inputs: _mysten_bcs1384.BcsStruct<{
7
- proofPoints: _mysten_bcs1384.BcsStruct<{
8
- a: _mysten_bcs1384.BcsType<string[], Iterable<string> & {
5
+ declare const zkLoginSignature: _mysten_bcs1248.BcsStruct<{
6
+ inputs: _mysten_bcs1248.BcsStruct<{
7
+ proofPoints: _mysten_bcs1248.BcsStruct<{
8
+ a: _mysten_bcs1248.BcsType<string[], Iterable<string> & {
9
9
  length: number;
10
10
  }, string>;
11
- b: _mysten_bcs1384.BcsType<string[][], Iterable<Iterable<string> & {
11
+ b: _mysten_bcs1248.BcsType<string[][], Iterable<Iterable<string> & {
12
12
  length: number;
13
13
  }> & {
14
14
  length: number;
15
15
  }, string>;
16
- c: _mysten_bcs1384.BcsType<string[], Iterable<string> & {
16
+ c: _mysten_bcs1248.BcsType<string[], Iterable<string> & {
17
17
  length: number;
18
18
  }, string>;
19
19
  }, string>;
20
- issBase64Details: _mysten_bcs1384.BcsStruct<{
21
- value: _mysten_bcs1384.BcsType<string, string, "string">;
22
- indexMod4: _mysten_bcs1384.BcsType<number, number, "u8">;
20
+ issBase64Details: _mysten_bcs1248.BcsStruct<{
21
+ value: _mysten_bcs1248.BcsType<string, string, "string">;
22
+ indexMod4: _mysten_bcs1248.BcsType<number, number, "u8">;
23
23
  }, string>;
24
- headerBase64: _mysten_bcs1384.BcsType<string, string, "string">;
25
- addressSeed: _mysten_bcs1384.BcsType<string, string, "string">;
24
+ headerBase64: _mysten_bcs1248.BcsType<string, string, "string">;
25
+ addressSeed: _mysten_bcs1248.BcsType<string, string, "string">;
26
26
  }, string>;
27
- maxEpoch: _mysten_bcs1384.BcsType<string, string | number | bigint, "u64">;
28
- userSignature: _mysten_bcs1384.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
27
+ maxEpoch: _mysten_bcs1248.BcsType<string, string | number | bigint, "u64">;
28
+ userSignature: _mysten_bcs1248.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
29
29
  }, string>;
30
30
  type ZkLoginSignature = InferBcsInput<typeof zkLoginSignature>;
31
31
  type ZkLoginSignatureInputs = ZkLoginSignature['inputs'];
@@ -69,7 +69,13 @@ That class extends the transport of the same name from
69
69
  `AbortSignal.timeout`, the status on a reason carrying one, and `CANCELLED` otherwise. Upstream
70
70
  reports all but a standard `AbortError` cancellation as `INTERNAL`.
71
71
 
72
- Build custom transports from this class. `SuiGrpcClient` uses a transport you pass it as given.
72
+ Build custom gRPC-web transports from this class to retain these error-handling fixes.
73
+
74
+ `SuiGrpcClient` adds an `x-sui-client-protocol-version` request header with the highest Sui protocol
75
+ version whose types the SDK can decode. This applies to all transports, including native gRPC, and
76
+ to unary and streaming requests. Explicit values in the transport's `meta` options or an individual
77
+ service call's `meta` override the default. The client does not mutate a supplied transport or
78
+ change its error handling.
73
79
 
74
80
  ### gRPC-web transport (default)
75
81
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Mysten Labs <build@mystenlabs.com>",
4
4
  "description": "Sui TypeScript API",
5
5
  "homepage": "https://sdk.mystenlabs.com",
6
- "version": "2.31.2",
6
+ "version": "2.32.0",
7
7
  "license": "Apache-2.0",
8
8
  "sideEffects": false,
9
9
  "files": [
@@ -0,0 +1,11 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /** Request header carrying the highest protocol version whose types the client can decode. */
5
+ export const CLIENT_PROTOCOL_VERSION_HEADER = 'x-sui-client-protocol-version';
6
+
7
+ /**
8
+ * Highest Sui protocol version whose on-chain types this SDK can decode. Bump this when the SDK
9
+ * gains support for types introduced in a newer protocol version.
10
+ */
11
+ export const MAX_PROTOCOL_VERSION = 138;
@@ -5,6 +5,10 @@ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
5
5
  import type { TadaDocumentNode } from 'gql.tada';
6
6
  import type { DocumentNode } from 'graphql';
7
7
  import { print } from 'graphql';
8
+ import {
9
+ CLIENT_PROTOCOL_VERSION_HEADER,
10
+ MAX_PROTOCOL_VERSION,
11
+ } from '../client/protocol-version.js';
8
12
  import { BaseClient } from '../client/index.js';
9
13
  import type { SuiClientTypes } from '../client/index.js';
10
14
  import { GraphQLCoreClient } from './core.js';
@@ -137,12 +141,17 @@ export class SuiGraphQLClient<Queries extends Record<string, GraphQLDocument> =
137
141
  async query<Result = Record<string, unknown>, Variables = Record<string, unknown>>(
138
142
  options: GraphQLQueryOptions<Result, Variables>,
139
143
  ): Promise<GraphQLQueryResult<Result>> {
144
+ const headers = new Headers({
145
+ 'Content-Type': 'application/json',
146
+ ...this.#headers,
147
+ });
148
+ if (!headers.has(CLIENT_PROTOCOL_VERSION_HEADER)) {
149
+ headers.set(CLIENT_PROTOCOL_VERSION_HEADER, String(MAX_PROTOCOL_VERSION));
150
+ }
151
+
140
152
  const res = await this.#fetch(this.#url, {
141
153
  method: 'POST',
142
- headers: {
143
- 'Content-Type': 'application/json',
144
- ...this.#headers,
145
- },
154
+ headers,
146
155
  body: JSON.stringify({
147
156
  query:
148
157
  typeof options.query === 'string' || options.query instanceof String
@@ -18,7 +18,7 @@ import { fromBase64, toBase64 } from '@mysten/utils';
18
18
  import { NameServiceClient } from './proto/sui/rpc/v2/name_service.client.js';
19
19
  import { ForkingServiceClient } from './proto/sui/forking/v1alpha/forking_service.client.js';
20
20
  import type { TransactionPlugin } from '../transactions/index.js';
21
- import { GrpcWebFetchTransport } from './transport.js';
21
+ import { GrpcWebFetchTransport, withClientProtocolVersion } from './transport.js';
22
22
 
23
23
  interface SuiGrpcTransportOptions extends GrpcWebOptions {
24
24
  transport?: never;
@@ -166,8 +166,10 @@ export class SuiGrpcClient extends BaseClient implements SuiClientTypes.Transpor
166
166
  ...transportOptions
167
167
  } = options as SuiGrpcClientOptions & SuiGrpcTransportOptions & { transport?: RpcTransport };
168
168
 
169
- // A caller-supplied transport is used as given. See ./transport.ts for the default.
170
- const transport = providedTransport ?? new GrpcWebFetchTransport(transportOptions);
169
+ // Add the protocol-version default for every transport, including native gRPC.
170
+ const transport = withClientProtocolVersion(
171
+ providedTransport ?? new GrpcWebFetchTransport(transportOptions),
172
+ );
171
173
  this.transactionExecutionService = new TransactionExecutionServiceClient(transport);
172
174
  this.ledgerService = new LedgerServiceClient(transport);
173
175
  this.stateService = new StateServiceClient(transport);
@@ -1,16 +1,23 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import type { GrpcWebOptions } from '@protobuf-ts/grpcweb-transport';
4
5
  import { GrpcStatusCode } from '@protobuf-ts/grpcweb-transport';
5
6
  import { GrpcWebFetchTransport as UpstreamGrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport';
6
7
  import type {
7
8
  MethodInfo,
8
9
  RpcOptions,
10
+ RpcTransport,
9
11
  ServerStreamingCall,
10
12
  UnaryCall,
11
13
  } from '@protobuf-ts/runtime-rpc';
12
14
  import { RpcError } from '@protobuf-ts/runtime-rpc';
13
15
 
16
+ import {
17
+ CLIENT_PROTOCOL_VERSION_HEADER,
18
+ MAX_PROTOCOL_VERSION,
19
+ } from '../client/protocol-version.js';
20
+
14
21
  // A failed call rejects four promises with the same error, so it is only decoded once. Registered
15
22
  // globally so another installed copy of this package sees the same marker.
16
23
  const MESSAGE_DECODED = Symbol.for('@mysten/sui/grpc/decoded-status-message');
@@ -126,12 +133,24 @@ function normalizeRejections(promises: Promise<unknown>[], signal: AbortSignal |
126
133
 
127
134
  /**
128
135
  * `GrpcWebFetchTransport` from `@protobuf-ts/grpcweb-transport`, subclassed to decode status
129
- * messages and to code an aborted call from its reason rather than as `INTERNAL`.
136
+ * messages, to code an aborted call from its reason rather than as `INTERNAL`, and to send the
137
+ * `x-sui-client-protocol-version` header.
130
138
  *
131
139
  * `SuiGrpcClient` builds one by default. A transport imported from `@protobuf-ts/grpcweb-transport`
132
140
  * keeps that package's behaviour.
133
141
  */
134
142
  export class GrpcWebFetchTransport extends UpstreamGrpcWebFetchTransport {
143
+ constructor(options: GrpcWebOptions) {
144
+ // Lets the server avoid sending types this SDK cannot decode. Caller-supplied `meta` wins.
145
+ super({
146
+ ...options,
147
+ meta: {
148
+ [CLIENT_PROTOCOL_VERSION_HEADER]: String(MAX_PROTOCOL_VERSION),
149
+ ...options.meta,
150
+ },
151
+ });
152
+ }
153
+
135
154
  override unary<I extends object, O extends object>(
136
155
  method: MethodInfo<I, O>,
137
156
  input: I,
@@ -158,3 +177,23 @@ export class GrpcWebFetchTransport extends UpstreamGrpcWebFetchTransport {
158
177
  return call;
159
178
  }
160
179
  }
180
+
181
+ /** Adds SDK metadata without mutating the caller's transport or its default options. */
182
+ export function withClientProtocolVersion(transport: RpcTransport): RpcTransport {
183
+ return {
184
+ mergeOptions(options) {
185
+ const merged = transport.mergeOptions(options);
186
+ return {
187
+ ...merged,
188
+ meta: {
189
+ [CLIENT_PROTOCOL_VERSION_HEADER]: String(MAX_PROTOCOL_VERSION),
190
+ ...merged.meta,
191
+ },
192
+ };
193
+ },
194
+ unary: transport.unary.bind(transport),
195
+ serverStreaming: transport.serverStreaming.bind(transport),
196
+ clientStreaming: transport.clientStreaming.bind(transport),
197
+ duplex: transport.duplex.bind(transport),
198
+ };
199
+ }
@@ -22,6 +22,7 @@ import {
22
22
  tuple,
23
23
  union,
24
24
  unknown,
25
+ variant,
25
26
  } from 'valibot';
26
27
 
27
28
  import { isValidSuiAddress, normalizeSuiAddress } from '../../utils/sui-types.js';
@@ -46,17 +47,49 @@ type EnumSchema<T extends Record<string, GenericSchema<any>>> = GenericSchema<
46
47
  >;
47
48
 
48
49
  export function safeEnum<T extends Record<string, GenericSchema<any>>>(options: T): EnumSchema<T> {
49
- return union(
50
- Object.keys(options).map(
51
- (key) =>
52
- withKind(
53
- key,
54
- object({
55
- [key]: options[key],
56
- }),
57
- ) as GenericSchema<EnumOutputShape<T>>,
50
+ const schemas = Object.keys(options).map((key) =>
51
+ object({
52
+ [key]: options[key],
53
+ $kind: literal(key),
54
+ }),
55
+ );
56
+
57
+ return pipe(
58
+ unknown(),
59
+ check(
60
+ (input) => unsupportedEnumVariantMessage(input, options) === undefined,
61
+ ({ input, message }) => unsupportedEnumVariantMessage(input, options) ?? message,
58
62
  ),
59
- ) as EnumSchema<T>;
63
+ transform(addMissingEnumKind),
64
+ variant('$kind', schemas),
65
+ ) as unknown as EnumSchema<T>;
66
+ }
67
+
68
+ function getEnumKind(input: unknown) {
69
+ if (typeof input !== 'object' || input === null) return null;
70
+ if ('$kind' in input && typeof input.$kind === 'string') return input.$kind;
71
+
72
+ return Object.keys(input).find((key) => key !== '$kind') ?? null;
73
+ }
74
+
75
+ function addMissingEnumKind(input: unknown) {
76
+ if (typeof input !== 'object' || input === null || '$kind' in input) return input;
77
+
78
+ const kind = getEnumKind(input);
79
+ return kind === null ? input : { ...input, $kind: kind };
80
+ }
81
+
82
+ function unsupportedEnumVariantMessage(
83
+ input: unknown,
84
+ options: Record<string, GenericSchema<any>>,
85
+ ) {
86
+ const kind = getEnumKind(input);
87
+ if (kind === null || Object.hasOwn(options, kind)) return undefined;
88
+
89
+ const expected = Object.keys(options)
90
+ .map((key) => `"${key}"`)
91
+ .join(', ');
92
+ return `Unsupported enum variant "${kind}". Expected one of: ${expected}.`;
60
93
  }
61
94
 
62
95
  function withKind<K extends string, TEntries extends ObjectEntries>(
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  // This file is generated by genversion.mjs. Do not edit it directly.
5
5
 
6
- export const PACKAGE_VERSION = '2.31.2';
6
+ export const PACKAGE_VERSION = '2.32.0';