@usestatemachines/sdk 0.7.0 → 0.8.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/README.md +86 -33
- package/agents/SKILL.md +71 -48
- package/agents/index.d.ts +2 -0
- package/agents/index.js +9 -7
- package/agents/llms.txt +23 -15
- package/agents/prompt.md +5 -25
- package/esm/extensions/client.d.ts +11 -7
- package/esm/extensions/client.js +32 -49
- package/esm/extensions/credentials.d.ts +25 -8
- package/esm/extensions/credentials.js +75 -40
- package/esm/extensions/environment.d.ts +1 -1
- package/esm/extensions/errors.js +1 -1
- package/esm/extensions/index.d.ts +2 -2
- package/esm/extensions/yaml.js +14 -12
- package/esm/funcs/environments-creators.d.ts +11 -0
- package/esm/funcs/environments-creators.js +90 -0
- package/esm/funcs/environments-list.js +2 -0
- package/esm/funcs/requests-list.js +5 -1
- package/esm/funcs/requests-stats.js +5 -1
- package/esm/lib/config.d.ts +2 -2
- package/esm/lib/config.js +2 -2
- package/esm/models/app-entry.d.ts +8 -0
- package/esm/models/app-entry.js +11 -0
- package/esm/models/create-environment-app.d.ts +25 -0
- package/esm/models/create-environment-app.js +14 -0
- package/esm/models/create-environment.d.ts +3 -26
- package/esm/models/create-environment.js +2 -19
- package/esm/models/credential.d.ts +50 -0
- package/esm/models/credential.js +60 -0
- package/esm/models/credentials.d.ts +26 -5
- package/esm/models/credentials.js +20 -3
- package/esm/models/environment-failure.d.ts +3 -2
- package/esm/models/environment-failure.js +3 -2
- package/esm/models/environment.d.ts +4 -1
- package/esm/models/environment.js +2 -1
- package/esm/models/error.d.ts +1 -0
- package/esm/models/error.js +1 -0
- package/esm/models/index.d.ts +6 -1
- package/esm/models/index.js +6 -1
- package/esm/models/mcp-server.d.ts +11 -0
- package/esm/models/mcp-server.js +16 -0
- package/esm/models/mcp-tool.d.ts +12 -0
- package/esm/models/mcp-tool.js +16 -0
- package/esm/models/me.d.ts +2 -0
- package/esm/models/me.js +2 -0
- package/esm/models/operations/create-credentials.d.ts +2 -0
- package/esm/models/operations/create-credentials.js +1 -0
- package/esm/models/operations/get-request-stats.d.ts +10 -2
- package/esm/models/operations/get-request-stats.js +5 -1
- package/esm/models/operations/index.d.ts +1 -0
- package/esm/models/operations/index.js +1 -0
- package/esm/models/operations/list-environment-creators.d.ts +23 -0
- package/esm/models/operations/list-environment-creators.js +20 -0
- package/esm/models/operations/list-environments.d.ts +5 -1
- package/esm/models/operations/list-environments.js +2 -0
- package/esm/models/operations/list-requests.d.ts +10 -2
- package/esm/models/operations/list-requests.js +5 -1
- package/esm/models/request-detail.d.ts +8 -1
- package/esm/models/request-detail.js +5 -1
- package/esm/models/request-stats.d.ts +8 -0
- package/esm/models/request-stats.js +9 -0
- package/esm/models/request.d.ts +8 -1
- package/esm/models/request.js +5 -1
- package/esm/models/service.d.ts +25 -0
- package/esm/models/service.js +28 -0
- package/esm/models/snapshot.d.ts +7 -0
- package/esm/models/snapshot.js +3 -0
- package/esm/models/version.d.ts +2 -2
- package/esm/models/version.js +2 -2
- package/esm/sdk/environments.d.ts +1 -0
- package/esm/sdk/environments.js +4 -0
- package/esm/types/discriminated-union.d.ts +24 -0
- package/esm/types/discriminated-union.js +60 -0
- package/esm/types/smart-union.js +2 -1
- package/openapi.json +596 -116
- package/package.json +6 -1
- package/esm/models/api.d.ts +0 -17
- package/esm/models/api.js +0 -21
|
@@ -14,13 +14,15 @@ export const RequestDetailRespondedBy = {
|
|
|
14
14
|
export const RequestDetailRespondedBy$inboundSchema = openEnums.inboundSchema(RequestDetailRespondedBy);
|
|
15
15
|
/** @internal */
|
|
16
16
|
export const RequestDetail$inboundSchema = z.object({
|
|
17
|
-
|
|
17
|
+
appName: types.string(),
|
|
18
18
|
completedAt: types.date(),
|
|
19
19
|
durationMs: types.number(),
|
|
20
20
|
environmentId: types.string(),
|
|
21
21
|
errorCode: types.nullable(types.string()),
|
|
22
22
|
id: types.string(),
|
|
23
|
+
mcpServer: types.nullable(types.string()),
|
|
23
24
|
method: types.string(),
|
|
25
|
+
operation: types.nullable(types.string()),
|
|
24
26
|
path: types.string(),
|
|
25
27
|
query: types.string(),
|
|
26
28
|
receivedAt: types.date(),
|
|
@@ -28,6 +30,8 @@ export const RequestDetail$inboundSchema = z.object({
|
|
|
28
30
|
respondedBy: RequestDetailRespondedBy$inboundSchema,
|
|
29
31
|
responseBytes: types.nullable(types.number()),
|
|
30
32
|
responseStatus: types.number(),
|
|
33
|
+
service: types.nullable(types.string()),
|
|
34
|
+
tool: types.nullable(types.string()),
|
|
31
35
|
requestBody: BodyPreview$inboundSchema,
|
|
32
36
|
requestHeaders: z.record(z.string(), types.string()),
|
|
33
37
|
responseBody: BodyPreview$inboundSchema,
|
|
@@ -7,6 +7,10 @@ export type Bucket = {
|
|
|
7
7
|
serverError: number;
|
|
8
8
|
success: number;
|
|
9
9
|
};
|
|
10
|
+
export type RequestStatsService = {
|
|
11
|
+
count: number;
|
|
12
|
+
id: string;
|
|
13
|
+
};
|
|
10
14
|
export type RequestStats = {
|
|
11
15
|
buckets: Array<Bucket>;
|
|
12
16
|
clientErrorCount: number;
|
|
@@ -15,6 +19,7 @@ export type RequestStats = {
|
|
|
15
19
|
p50Ms: number | null;
|
|
16
20
|
p95Ms: number | null;
|
|
17
21
|
serverErrorCount: number;
|
|
22
|
+
services: Array<RequestStatsService>;
|
|
18
23
|
successCount: number;
|
|
19
24
|
to: Date;
|
|
20
25
|
};
|
|
@@ -22,5 +27,8 @@ export type RequestStats = {
|
|
|
22
27
|
export declare const Bucket$inboundSchema: z.ZodMiniType<Bucket, unknown>;
|
|
23
28
|
export declare function bucketFromJSON(jsonString: string): SafeParseResult<Bucket, SDKValidationError>;
|
|
24
29
|
/** @internal */
|
|
30
|
+
export declare const RequestStatsService$inboundSchema: z.ZodMiniType<RequestStatsService, unknown>;
|
|
31
|
+
export declare function requestStatsServiceFromJSON(jsonString: string): SafeParseResult<RequestStatsService, SDKValidationError>;
|
|
32
|
+
/** @internal */
|
|
25
33
|
export declare const RequestStats$inboundSchema: z.ZodMiniType<RequestStats, unknown>;
|
|
26
34
|
export declare function requestStatsFromJSON(jsonString: string): SafeParseResult<RequestStats, SDKValidationError>;
|
|
@@ -15,6 +15,14 @@ export function bucketFromJSON(jsonString) {
|
|
|
15
15
|
return safeParse(jsonString, (x) => Bucket$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'Bucket' from JSON`);
|
|
16
16
|
}
|
|
17
17
|
/** @internal */
|
|
18
|
+
export const RequestStatsService$inboundSchema = z.object({
|
|
19
|
+
count: types.number(),
|
|
20
|
+
id: types.string(),
|
|
21
|
+
});
|
|
22
|
+
export function requestStatsServiceFromJSON(jsonString) {
|
|
23
|
+
return safeParse(jsonString, (x) => RequestStatsService$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'RequestStatsService' from JSON`);
|
|
24
|
+
}
|
|
25
|
+
/** @internal */
|
|
18
26
|
export const RequestStats$inboundSchema = z.object({
|
|
19
27
|
buckets: z.array(z.lazy(() => Bucket$inboundSchema)),
|
|
20
28
|
clientErrorCount: types.number(),
|
|
@@ -23,6 +31,7 @@ export const RequestStats$inboundSchema = z.object({
|
|
|
23
31
|
p50Ms: types.nullable(types.number()),
|
|
24
32
|
p95Ms: types.nullable(types.number()),
|
|
25
33
|
serverErrorCount: types.number(),
|
|
34
|
+
services: z.array(z.lazy(() => RequestStatsService$inboundSchema)),
|
|
26
35
|
successCount: types.number(),
|
|
27
36
|
to: types.date(),
|
|
28
37
|
});
|
package/esm/models/request.d.ts
CHANGED
|
@@ -8,13 +8,18 @@ export declare const RequestRespondedBy: {
|
|
|
8
8
|
};
|
|
9
9
|
export type RequestRespondedBy = OpenEnum<typeof RequestRespondedBy>;
|
|
10
10
|
export type RequestT = {
|
|
11
|
-
|
|
11
|
+
appName: string;
|
|
12
12
|
completedAt: Date;
|
|
13
13
|
durationMs: number;
|
|
14
14
|
environmentId: string;
|
|
15
15
|
errorCode: string | null;
|
|
16
16
|
id: string;
|
|
17
|
+
/**
|
|
18
|
+
* The MCP server that the request called, as the app named it. `null` when the gateway answered, when State Machines refused the request before the app read it, or when the request called no MCP server that the environment serves.
|
|
19
|
+
*/
|
|
20
|
+
mcpServer: string | null;
|
|
17
21
|
method: string;
|
|
22
|
+
operation: string | null;
|
|
18
23
|
path: string;
|
|
19
24
|
query: string;
|
|
20
25
|
receivedAt: Date;
|
|
@@ -22,6 +27,8 @@ export type RequestT = {
|
|
|
22
27
|
respondedBy: RequestRespondedBy;
|
|
23
28
|
responseBytes: number | null;
|
|
24
29
|
responseStatus: number;
|
|
30
|
+
service: string | null;
|
|
31
|
+
tool: string | null;
|
|
25
32
|
};
|
|
26
33
|
/** @internal */
|
|
27
34
|
export declare const RequestRespondedBy$inboundSchema: z.ZodMiniType<RequestRespondedBy, unknown>;
|
package/esm/models/request.js
CHANGED
|
@@ -14,13 +14,15 @@ export const RequestRespondedBy$inboundSchema = openEnums.inboundSchema(RequestR
|
|
|
14
14
|
/** @internal */
|
|
15
15
|
export const RequestT$inboundSchema = z
|
|
16
16
|
.object({
|
|
17
|
-
|
|
17
|
+
appName: types.string(),
|
|
18
18
|
completedAt: types.date(),
|
|
19
19
|
durationMs: types.number(),
|
|
20
20
|
environmentId: types.string(),
|
|
21
21
|
errorCode: types.nullable(types.string()),
|
|
22
22
|
id: types.string(),
|
|
23
|
+
mcpServer: types.nullable(types.string()),
|
|
23
24
|
method: types.string(),
|
|
25
|
+
operation: types.nullable(types.string()),
|
|
24
26
|
path: types.string(),
|
|
25
27
|
query: types.string(),
|
|
26
28
|
receivedAt: types.date(),
|
|
@@ -28,6 +30,8 @@ export const RequestT$inboundSchema = z
|
|
|
28
30
|
respondedBy: RequestRespondedBy$inboundSchema,
|
|
29
31
|
responseBytes: types.nullable(types.number()),
|
|
30
32
|
responseStatus: types.number(),
|
|
33
|
+
service: types.nullable(types.string()),
|
|
34
|
+
tool: types.nullable(types.string()),
|
|
31
35
|
});
|
|
32
36
|
export function requestFromJSON(jsonString) {
|
|
33
37
|
return safeParse(jsonString, (x) => RequestT$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'RequestT' from JSON`);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
import { OpenEnum } from "../types/enums.js";
|
|
3
|
+
import { Result as SafeParseResult } from "../types/fp.js";
|
|
4
|
+
import { SDKValidationError } from "./errors/sdk-validation-error.js";
|
|
5
|
+
import { McpServer } from "./mcp-server.js";
|
|
6
|
+
export declare const Visibility: {
|
|
7
|
+
readonly Public: "public";
|
|
8
|
+
readonly Preview: "preview";
|
|
9
|
+
};
|
|
10
|
+
export type Visibility = OpenEnum<typeof Visibility>;
|
|
11
|
+
export type Service = {
|
|
12
|
+
description: string;
|
|
13
|
+
id: string;
|
|
14
|
+
limitations: Array<string>;
|
|
15
|
+
name: string;
|
|
16
|
+
path: string;
|
|
17
|
+
prompt: Array<string>;
|
|
18
|
+
servers?: Array<McpServer> | undefined;
|
|
19
|
+
visibility: Visibility;
|
|
20
|
+
};
|
|
21
|
+
/** @internal */
|
|
22
|
+
export declare const Visibility$inboundSchema: z.ZodMiniType<Visibility, unknown>;
|
|
23
|
+
/** @internal */
|
|
24
|
+
export declare const Service$inboundSchema: z.ZodMiniType<Service, unknown>;
|
|
25
|
+
export declare function serviceFromJSON(jsonString: string): SafeParseResult<Service, SDKValidationError>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
|
+
*/
|
|
4
|
+
import * as z from "zod/v4-mini";
|
|
5
|
+
import { safeParse } from "../lib/schemas.js";
|
|
6
|
+
import * as openEnums from "../types/enums.js";
|
|
7
|
+
import * as types from "../types/primitives.js";
|
|
8
|
+
import { McpServer$inboundSchema } from "./mcp-server.js";
|
|
9
|
+
export const Visibility = {
|
|
10
|
+
Public: "public",
|
|
11
|
+
Preview: "preview",
|
|
12
|
+
};
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const Visibility$inboundSchema = openEnums.inboundSchema(Visibility);
|
|
15
|
+
/** @internal */
|
|
16
|
+
export const Service$inboundSchema = z.object({
|
|
17
|
+
description: types.string(),
|
|
18
|
+
id: types.string(),
|
|
19
|
+
limitations: z.array(types.string()),
|
|
20
|
+
name: types.string(),
|
|
21
|
+
path: types.string(),
|
|
22
|
+
prompt: z.array(types.string()),
|
|
23
|
+
servers: types.optional(z.array(McpServer$inboundSchema)),
|
|
24
|
+
visibility: Visibility$inboundSchema,
|
|
25
|
+
});
|
|
26
|
+
export function serviceFromJSON(jsonString) {
|
|
27
|
+
return safeParse(jsonString, (x) => Service$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'Service' from JSON`);
|
|
28
|
+
}
|
package/esm/models/snapshot.d.ts
CHANGED
|
@@ -7,6 +7,12 @@ import { SnapshotStatus } from "./snapshot-status.js";
|
|
|
7
7
|
export type SnapshotApp = {
|
|
8
8
|
appId: string;
|
|
9
9
|
name: string;
|
|
10
|
+
services: {
|
|
11
|
+
[k: string]: any;
|
|
12
|
+
};
|
|
13
|
+
setup: {
|
|
14
|
+
[k: string]: any;
|
|
15
|
+
};
|
|
10
16
|
sizeBytes: number | null;
|
|
11
17
|
version: string;
|
|
12
18
|
versionId: string;
|
|
@@ -26,6 +32,7 @@ export type Snapshot = {
|
|
|
26
32
|
createdBy: Principal;
|
|
27
33
|
deletedAt: Date | null;
|
|
28
34
|
environmentId: string;
|
|
35
|
+
environmentName: string | null;
|
|
29
36
|
failure: Failure | null;
|
|
30
37
|
id: string;
|
|
31
38
|
name: string | null;
|
package/esm/models/snapshot.js
CHANGED
|
@@ -16,6 +16,8 @@ export const SnapshotApp$inboundSchema = z
|
|
|
16
16
|
.object({
|
|
17
17
|
appId: types.string(),
|
|
18
18
|
name: types.string(),
|
|
19
|
+
services: z.record(z.string(), z.any()),
|
|
20
|
+
setup: z.record(z.string(), z.any()),
|
|
19
21
|
sizeBytes: types.nullable(types.number()),
|
|
20
22
|
version: types.string(),
|
|
21
23
|
versionId: types.string(),
|
|
@@ -41,6 +43,7 @@ export const Snapshot$inboundSchema = z
|
|
|
41
43
|
createdBy: Principal$inboundSchema,
|
|
42
44
|
deletedAt: types.nullable(types.date()),
|
|
43
45
|
environmentId: types.string(),
|
|
46
|
+
environmentName: types.nullable(types.string()),
|
|
44
47
|
failure: types.nullable(z.lazy(() => Failure$inboundSchema)),
|
|
45
48
|
id: types.string(),
|
|
46
49
|
name: types.nullable(types.string()),
|
package/esm/models/version.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as z from "zod/v4-mini";
|
|
|
2
2
|
import { OpenEnum } from "../types/enums.js";
|
|
3
3
|
import { Result as SafeParseResult } from "../types/fp.js";
|
|
4
4
|
import { Actor } from "./actor.js";
|
|
5
|
-
import { Api } from "./api.js";
|
|
6
5
|
import { SDKValidationError } from "./errors/sdk-validation-error.js";
|
|
6
|
+
import { Service } from "./service.js";
|
|
7
7
|
export declare const Status: {
|
|
8
8
|
readonly Published: "published";
|
|
9
9
|
readonly Retired: "retired";
|
|
@@ -11,12 +11,12 @@ export declare const Status: {
|
|
|
11
11
|
export type Status = OpenEnum<typeof Status>;
|
|
12
12
|
export type Version = {
|
|
13
13
|
actors: Array<Actor>;
|
|
14
|
-
apis: Array<Api>;
|
|
15
14
|
appId: string;
|
|
16
15
|
id: string;
|
|
17
16
|
isDefault: boolean;
|
|
18
17
|
publishedAt: Date;
|
|
19
18
|
retiredAt: Date | null;
|
|
19
|
+
services: Array<Service>;
|
|
20
20
|
status: Status;
|
|
21
21
|
version: string;
|
|
22
22
|
};
|
package/esm/models/version.js
CHANGED
|
@@ -6,7 +6,7 @@ import { safeParse } from "../lib/schemas.js";
|
|
|
6
6
|
import * as openEnums from "../types/enums.js";
|
|
7
7
|
import * as types from "../types/primitives.js";
|
|
8
8
|
import { Actor$inboundSchema } from "./actor.js";
|
|
9
|
-
import {
|
|
9
|
+
import { Service$inboundSchema } from "./service.js";
|
|
10
10
|
export const Status = {
|
|
11
11
|
Published: "published",
|
|
12
12
|
Retired: "retired",
|
|
@@ -17,12 +17,12 @@ export const Status$inboundSchema = openEnums
|
|
|
17
17
|
/** @internal */
|
|
18
18
|
export const Version$inboundSchema = z.object({
|
|
19
19
|
actors: z.array(Actor$inboundSchema),
|
|
20
|
-
apis: z.array(Api$inboundSchema),
|
|
21
20
|
appId: types.string(),
|
|
22
21
|
id: types.string(),
|
|
23
22
|
isDefault: types.boolean(),
|
|
24
23
|
publishedAt: types.date(),
|
|
25
24
|
retiredAt: types.nullable(types.date()),
|
|
25
|
+
services: z.array(Service$inboundSchema),
|
|
26
26
|
status: Status$inboundSchema,
|
|
27
27
|
version: types.string(),
|
|
28
28
|
});
|
|
@@ -4,6 +4,7 @@ import * as operations from "../models/operations/index.js";
|
|
|
4
4
|
export declare class Environments extends ClientSDK {
|
|
5
5
|
list(request?: operations.ListEnvironmentsRequest | undefined, options?: RequestOptions): Promise<operations.ListEnvironmentsResponse>;
|
|
6
6
|
create(request: operations.CreateEnvironmentRequest, options?: RequestOptions): Promise<models.Environment>;
|
|
7
|
+
creators(request?: operations.ListEnvironmentCreatorsRequest | undefined, options?: RequestOptions): Promise<operations.ListEnvironmentCreatorsResponse>;
|
|
7
8
|
delete(request: operations.DeleteEnvironmentRequest, options?: RequestOptions): Promise<models.Environment>;
|
|
8
9
|
get(request: operations.GetEnvironmentRequest, options?: RequestOptions): Promise<models.Environment>;
|
|
9
10
|
update(request: operations.UpdateEnvironmentRequest, options?: RequestOptions): Promise<models.Environment>;
|
package/esm/sdk/environments.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { environmentsConnect } from "../funcs/environments-connect.js";
|
|
5
5
|
import { environmentsCreate } from "../funcs/environments-create.js";
|
|
6
|
+
import { environmentsCreators } from "../funcs/environments-creators.js";
|
|
6
7
|
import { environmentsDelete } from "../funcs/environments-delete.js";
|
|
7
8
|
import { environmentsGet } from "../funcs/environments-get.js";
|
|
8
9
|
import { environmentsList } from "../funcs/environments-list.js";
|
|
@@ -16,6 +17,9 @@ export class Environments extends ClientSDK {
|
|
|
16
17
|
async create(request, options) {
|
|
17
18
|
return unwrapAsync(environmentsCreate(this, request, options));
|
|
18
19
|
}
|
|
20
|
+
async creators(request, options) {
|
|
21
|
+
return unwrapAsync(environmentsCreators(this, request, options));
|
|
22
|
+
}
|
|
19
23
|
async delete(request, options) {
|
|
20
24
|
return unwrapAsync(environmentsDelete(this, request, options));
|
|
21
25
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
export type Unknown<Discriminator extends string, UnknownValue = "UNKNOWN"> = {
|
|
3
|
+
[K in Discriminator]: UnknownValue;
|
|
4
|
+
} & {
|
|
5
|
+
raw: unknown;
|
|
6
|
+
isUnknown: true;
|
|
7
|
+
};
|
|
8
|
+
export declare function isUnknown<Discriminator extends string>(value: unknown): value is Unknown<Discriminator>;
|
|
9
|
+
/**
|
|
10
|
+
* Forward-compatible discriminated union parser.
|
|
11
|
+
*
|
|
12
|
+
* If the input does not match one of the predefined options, it will be
|
|
13
|
+
* captured and available as `{ raw: <original input>, [discriminator]: "UNKNOWN", isUnknown: true }`.
|
|
14
|
+
*
|
|
15
|
+
* @param inputPropertyName - The discriminator property name in the input payload
|
|
16
|
+
* @param options - Map of discriminator values to their corresponding schemas
|
|
17
|
+
* @param opts - Optional configuration object
|
|
18
|
+
* @param opts.unknownValue - The value to use for the discriminator when the input is unknown (default: "UNKNOWN")
|
|
19
|
+
* @param opts.outputPropertyName - Output property name if the sanitized (camelCase) property name differs from inputPropertyName
|
|
20
|
+
*/
|
|
21
|
+
export declare function discriminatedUnion<InputDiscriminator extends string, TOptions extends Readonly<Record<string, z.ZodMiniType>>, UnknownValue extends string = "UNKNOWN", OutputDiscriminator extends string = InputDiscriminator>(inputPropertyName: InputDiscriminator, options: TOptions, opts?: {
|
|
22
|
+
unknownValue?: UnknownValue;
|
|
23
|
+
outputPropertyName?: OutputDiscriminator;
|
|
24
|
+
}): z.ZodMiniType<z.output<TOptions[keyof TOptions]> | Unknown<OutputDiscriminator, UnknownValue>, unknown>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
|
+
*/
|
|
4
|
+
import * as z from "zod/v4-mini";
|
|
5
|
+
import { startCountingDefaultToZeroValue } from "./default-to-zero-value.js";
|
|
6
|
+
import { startCountingUnrecognized } from "./unrecognized.js";
|
|
7
|
+
const UNKNOWN = Symbol("UNKNOWN");
|
|
8
|
+
export function isUnknown(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && UNKNOWN in value;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Forward-compatible discriminated union parser.
|
|
13
|
+
*
|
|
14
|
+
* If the input does not match one of the predefined options, it will be
|
|
15
|
+
* captured and available as `{ raw: <original input>, [discriminator]: "UNKNOWN", isUnknown: true }`.
|
|
16
|
+
*
|
|
17
|
+
* @param inputPropertyName - The discriminator property name in the input payload
|
|
18
|
+
* @param options - Map of discriminator values to their corresponding schemas
|
|
19
|
+
* @param opts - Optional configuration object
|
|
20
|
+
* @param opts.unknownValue - The value to use for the discriminator when the input is unknown (default: "UNKNOWN")
|
|
21
|
+
* @param opts.outputPropertyName - Output property name if the sanitized (camelCase) property name differs from inputPropertyName
|
|
22
|
+
*/
|
|
23
|
+
export function discriminatedUnion(inputPropertyName, options, opts = {}) {
|
|
24
|
+
const { unknownValue = "UNKNOWN", outputPropertyName } = opts;
|
|
25
|
+
return z.pipe(z.unknown(), z.transform((input) => {
|
|
26
|
+
const fallback = Object.defineProperties({
|
|
27
|
+
raw: input,
|
|
28
|
+
[outputPropertyName ?? inputPropertyName]: unknownValue,
|
|
29
|
+
isUnknown: true,
|
|
30
|
+
}, { [UNKNOWN]: { value: true, enumerable: false, configurable: false } });
|
|
31
|
+
const isObject = typeof input === "object" && input !== null;
|
|
32
|
+
if (!isObject)
|
|
33
|
+
return fallback;
|
|
34
|
+
const discriminator = input[inputPropertyName];
|
|
35
|
+
if (typeof discriminator !== "string")
|
|
36
|
+
return fallback;
|
|
37
|
+
if (!(discriminator in options))
|
|
38
|
+
return fallback;
|
|
39
|
+
const schema = options[discriminator];
|
|
40
|
+
if (!schema)
|
|
41
|
+
return fallback;
|
|
42
|
+
// Start counters before parsing to track nested unrecognized/zeroDefault values
|
|
43
|
+
const unrecognizedCtr = startCountingUnrecognized();
|
|
44
|
+
const zeroDefaultCtr = startCountingDefaultToZeroValue();
|
|
45
|
+
const result = z.safeParse(schema, input);
|
|
46
|
+
if (!result.success) {
|
|
47
|
+
// Parse failed - don't propagate any counts from the failed attempt
|
|
48
|
+
unrecognizedCtr.end(0);
|
|
49
|
+
zeroDefaultCtr.end(0);
|
|
50
|
+
return fallback;
|
|
51
|
+
}
|
|
52
|
+
// Parse succeeded - propagate the actual counts
|
|
53
|
+
unrecognizedCtr.end();
|
|
54
|
+
zeroDefaultCtr.end();
|
|
55
|
+
if (outputPropertyName) {
|
|
56
|
+
result.data[outputPropertyName] = discriminator;
|
|
57
|
+
}
|
|
58
|
+
return result.data;
|
|
59
|
+
}));
|
|
60
|
+
}
|
package/esm/types/smart-union.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// Not needed if lax mode
|
|
5
5
|
import * as z from "zod/v4-mini";
|
|
6
6
|
import { startCountingDefaultToZeroValue } from "./default-to-zero-value.js";
|
|
7
|
+
import { isUnknown as isDiscriminatedUnionUnknown } from "./discriminated-union.js";
|
|
7
8
|
import { RFCDate } from "./rfcdate.js";
|
|
8
9
|
import { startCountingUnrecognized } from "./unrecognized.js";
|
|
9
10
|
/**
|
|
@@ -87,7 +88,7 @@ function countFieldsRecursive(parsed) {
|
|
|
87
88
|
let index = 0;
|
|
88
89
|
while (index < queue.length) {
|
|
89
90
|
const value = queue[index++];
|
|
90
|
-
if (value === undefined) {
|
|
91
|
+
if (value === undefined || isDiscriminatedUnionUnknown(value)) {
|
|
91
92
|
continue;
|
|
92
93
|
}
|
|
93
94
|
// Check if it's a primitive value
|