@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.
- package/CHANGELOG.md +15 -0
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/client/protocol-version.mjs +12 -0
- package/dist/client/protocol-version.mjs.map +1 -0
- package/dist/cryptography/signature.d.mts +6 -6
- package/dist/graphql/client.d.mts.map +1 -1
- package/dist/graphql/client.mjs +7 -4
- package/dist/graphql/client.mjs.map +1 -1
- package/dist/grpc/client.d.mts.map +1 -1
- package/dist/grpc/client.mjs +2 -2
- package/dist/grpc/client.mjs.map +1 -1
- package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
- package/dist/grpc/transport.d.mts +5 -3
- package/dist/grpc/transport.d.mts.map +1 -1
- package/dist/grpc/transport.mjs +32 -2
- package/dist/grpc/transport.mjs.map +1 -1
- package/dist/transactions/Transaction.d.mts +6 -6
- package/dist/transactions/data/internal.d.mts +133 -133
- package/dist/transactions/data/internal.d.mts.map +1 -1
- package/dist/transactions/data/internal.mjs +24 -2
- package/dist/transactions/data/internal.mjs.map +1 -1
- package/dist/transactions/data/v1.d.mts +239 -239
- package/dist/transactions/data/v1.d.mts.map +1 -1
- package/dist/transactions/data/v2.d.mts +16 -16
- package/dist/transactions/data/v2.d.mts.map +1 -1
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/docs/clients/grpc.md +7 -1
- package/package.json +1 -1
- package/src/client/protocol-version.ts +11 -0
- package/src/graphql/client.ts +13 -4
- package/src/grpc/client.ts +5 -3
- package/src/grpc/transport.ts +40 -1
- package/src/transactions/data/internal.ts +43 -10
- package/src/version.ts +1 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { EnumInputShape } from "@mysten/bcs";
|
|
2
|
-
import * as
|
|
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:
|
|
7
|
-
readonly version:
|
|
8
|
-
readonly sender:
|
|
9
|
-
readonly expiration:
|
|
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:
|
|
34
|
-
readonly budget:
|
|
35
|
-
readonly price:
|
|
36
|
-
readonly owner:
|
|
37
|
-
readonly payment:
|
|
38
|
-
readonly objectId:
|
|
39
|
-
readonly version:
|
|
40
|
-
readonly digest:
|
|
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:
|
|
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:
|
|
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:
|
|
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,
|
|
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
package/dist/version.mjs.map
CHANGED
|
@@ -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.
|
|
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"}
|
package/dist/zklogin/bcs.d.mts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as
|
|
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:
|
|
6
|
-
inputs:
|
|
7
|
-
proofPoints:
|
|
8
|
-
a:
|
|
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:
|
|
11
|
+
b: _mysten_bcs1248.BcsType<string[][], Iterable<Iterable<string> & {
|
|
12
12
|
length: number;
|
|
13
13
|
}> & {
|
|
14
14
|
length: number;
|
|
15
15
|
}, string>;
|
|
16
|
-
c:
|
|
16
|
+
c: _mysten_bcs1248.BcsType<string[], Iterable<string> & {
|
|
17
17
|
length: number;
|
|
18
18
|
}, string>;
|
|
19
19
|
}, string>;
|
|
20
|
-
issBase64Details:
|
|
21
|
-
value:
|
|
22
|
-
indexMod4:
|
|
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:
|
|
25
|
-
addressSeed:
|
|
24
|
+
headerBase64: _mysten_bcs1248.BcsType<string, string, "string">;
|
|
25
|
+
addressSeed: _mysten_bcs1248.BcsType<string, string, "string">;
|
|
26
26
|
}, string>;
|
|
27
|
-
maxEpoch:
|
|
28
|
-
userSignature:
|
|
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'];
|
package/docs/clients/grpc.md
CHANGED
|
@@ -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
|
|
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
|
@@ -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;
|
package/src/graphql/client.ts
CHANGED
|
@@ -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
|
package/src/grpc/client.ts
CHANGED
|
@@ -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
|
-
//
|
|
170
|
-
const transport =
|
|
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);
|
package/src/grpc/transport.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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