@usestatemachines/sdk 0.3.0 → 0.4.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 +67 -17
- package/agents/SKILL.md +10 -7
- package/agents/index.js +6 -6
- package/agents/llms.txt +16 -9
- package/agents/prompt.md +1 -1
- package/esm/extensions/client.d.ts +11 -1
- package/esm/extensions/client.js +12 -2
- package/esm/extensions/running-environment.d.ts +16 -2
- package/esm/extensions/running-environment.js +24 -2
- package/esm/extensions/wait.d.ts +3 -1
- package/esm/extensions/wait.js +16 -1
- package/esm/funcs/environments-resume.d.ts +11 -0
- package/esm/funcs/environments-resume.js +92 -0
- package/esm/funcs/environments-suspend.d.ts +11 -0
- package/esm/funcs/environments-suspend.js +92 -0
- package/esm/funcs/get-environment-exchange.d.ts +11 -0
- package/esm/funcs/get-environment-exchange.js +96 -0
- package/esm/funcs/get-environment-position.d.ts +11 -0
- package/esm/funcs/get-environment-position.js +92 -0
- package/esm/funcs/list-environment-history.d.ts +11 -0
- package/esm/funcs/list-environment-history.js +97 -0
- package/esm/lib/config.d.ts +2 -2
- package/esm/lib/config.js +2 -2
- package/esm/models/environment-app.d.ts +10 -0
- package/esm/models/environment-app.js +12 -0
- package/esm/models/environment-exchange.d.ts +116 -0
- package/esm/models/environment-exchange.js +126 -0
- package/esm/models/environment-position.d.ts +25 -0
- package/esm/models/environment-position.js +32 -0
- package/esm/models/environment-snapshot.d.ts +3 -2
- package/esm/models/environment-snapshot.js +3 -2
- package/esm/models/environment.d.ts +4 -0
- package/esm/models/environment.js +4 -0
- package/esm/models/error-response.d.ts +8 -0
- package/esm/models/error-response.js +8 -0
- package/esm/models/history-body.d.ts +28 -0
- package/esm/models/history-body.js +33 -0
- package/esm/models/history-cause.d.ts +48 -0
- package/esm/models/history-cause.js +63 -0
- package/esm/models/history-position.d.ts +11 -0
- package/esm/models/history-position.js +15 -0
- package/esm/models/history-transaction.d.ts +16 -0
- package/esm/models/history-transaction.js +20 -0
- package/esm/models/index.d.ts +6 -0
- package/esm/models/index.js +6 -0
- package/esm/models/operations/create-environment-definition.d.ts +92 -10
- package/esm/models/operations/create-environment-definition.js +63 -4
- package/esm/models/operations/get-environment-exchange.d.ts +25 -0
- package/esm/models/operations/get-environment-exchange.js +21 -0
- package/esm/models/operations/get-environment-position.d.ts +23 -0
- package/esm/models/operations/get-environment-position.js +20 -0
- package/esm/models/operations/index.d.ts +5 -0
- package/esm/models/operations/index.js +5 -0
- package/esm/models/operations/list-environment-history.d.ts +28 -0
- package/esm/models/operations/list-environment-history.js +24 -0
- package/esm/models/operations/resume-environment.d.ts +23 -0
- package/esm/models/operations/resume-environment.js +20 -0
- package/esm/models/operations/stop-environment.d.ts +1 -1
- package/esm/models/operations/suspend-environment.d.ts +23 -0
- package/esm/models/operations/suspend-environment.js +20 -0
- package/esm/models/release.d.ts +11 -0
- package/esm/models/release.js +10 -0
- package/esm/sdk/environments.d.ts +2 -0
- package/esm/sdk/environments.js +8 -0
- package/esm/sdk/sdk.d.ts +3 -0
- package/esm/sdk/sdk.js +12 -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 +1433 -142
- package/package.json +3 -2
|
@@ -0,0 +1,24 @@
|
|
|
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 types from "../../types/primitives.js";
|
|
7
|
+
import * as models from "../index.js";
|
|
8
|
+
/** @internal */
|
|
9
|
+
export const ListEnvironmentHistoryRequest$outboundSchema = z.object({
|
|
10
|
+
environmentId: z.string(),
|
|
11
|
+
limit: z._default(z.int(), 50),
|
|
12
|
+
cursor: z.optional(z.string()),
|
|
13
|
+
});
|
|
14
|
+
export function listEnvironmentHistoryRequestToJSON(listEnvironmentHistoryRequest) {
|
|
15
|
+
return JSON.stringify(ListEnvironmentHistoryRequest$outboundSchema.parse(listEnvironmentHistoryRequest));
|
|
16
|
+
}
|
|
17
|
+
/** @internal */
|
|
18
|
+
export const ListEnvironmentHistoryResponse$inboundSchema = z.object({
|
|
19
|
+
data: z.array(models.EnvironmentExchange$inboundSchema),
|
|
20
|
+
nextCursor: types.nullable(types.string()),
|
|
21
|
+
});
|
|
22
|
+
export function listEnvironmentHistoryResponseFromJSON(jsonString) {
|
|
23
|
+
return safeParse(jsonString, (x) => ListEnvironmentHistoryResponse$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'ListEnvironmentHistoryResponse' from JSON`);
|
|
24
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
import { Result as SafeParseResult } from "../../types/fp.js";
|
|
3
|
+
import { SDKValidationError } from "../errors/sdk-validation-error.js";
|
|
4
|
+
import * as models from "../index.js";
|
|
5
|
+
export type ResumeEnvironmentRequest = {
|
|
6
|
+
environmentId: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Environment queued to run again on the data it was suspended with
|
|
10
|
+
*/
|
|
11
|
+
export type ResumeEnvironmentResponse = {
|
|
12
|
+
data: models.Environment;
|
|
13
|
+
};
|
|
14
|
+
/** @internal */
|
|
15
|
+
export type ResumeEnvironmentRequest$Outbound = {
|
|
16
|
+
environmentId: string;
|
|
17
|
+
};
|
|
18
|
+
/** @internal */
|
|
19
|
+
export declare const ResumeEnvironmentRequest$outboundSchema: z.ZodMiniType<ResumeEnvironmentRequest$Outbound, ResumeEnvironmentRequest>;
|
|
20
|
+
export declare function resumeEnvironmentRequestToJSON(resumeEnvironmentRequest: ResumeEnvironmentRequest): string;
|
|
21
|
+
/** @internal */
|
|
22
|
+
export declare const ResumeEnvironmentResponse$inboundSchema: z.ZodMiniType<ResumeEnvironmentResponse, unknown>;
|
|
23
|
+
export declare function resumeEnvironmentResponseFromJSON(jsonString: string): SafeParseResult<ResumeEnvironmentResponse, SDKValidationError>;
|
|
@@ -0,0 +1,20 @@
|
|
|
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 models from "../index.js";
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const ResumeEnvironmentRequest$outboundSchema = z.object({
|
|
9
|
+
environmentId: z.string(),
|
|
10
|
+
});
|
|
11
|
+
export function resumeEnvironmentRequestToJSON(resumeEnvironmentRequest) {
|
|
12
|
+
return JSON.stringify(ResumeEnvironmentRequest$outboundSchema.parse(resumeEnvironmentRequest));
|
|
13
|
+
}
|
|
14
|
+
/** @internal */
|
|
15
|
+
export const ResumeEnvironmentResponse$inboundSchema = z.object({
|
|
16
|
+
data: models.Environment$inboundSchema,
|
|
17
|
+
});
|
|
18
|
+
export function resumeEnvironmentResponseFromJSON(jsonString) {
|
|
19
|
+
return safeParse(jsonString, (x) => ResumeEnvironmentResponse$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'ResumeEnvironmentResponse' from JSON`);
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as z from "zod/v4-mini";
|
|
2
|
+
import { Result as SafeParseResult } from "../../types/fp.js";
|
|
3
|
+
import { SDKValidationError } from "../errors/sdk-validation-error.js";
|
|
4
|
+
import * as models from "../index.js";
|
|
5
|
+
export type SuspendEnvironmentRequest = {
|
|
6
|
+
environmentId: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Environment suspending or suspended: its sandboxes go, its data stays for a resume
|
|
10
|
+
*/
|
|
11
|
+
export type SuspendEnvironmentResponse = {
|
|
12
|
+
data: models.Environment;
|
|
13
|
+
};
|
|
14
|
+
/** @internal */
|
|
15
|
+
export type SuspendEnvironmentRequest$Outbound = {
|
|
16
|
+
environmentId: string;
|
|
17
|
+
};
|
|
18
|
+
/** @internal */
|
|
19
|
+
export declare const SuspendEnvironmentRequest$outboundSchema: z.ZodMiniType<SuspendEnvironmentRequest$Outbound, SuspendEnvironmentRequest>;
|
|
20
|
+
export declare function suspendEnvironmentRequestToJSON(suspendEnvironmentRequest: SuspendEnvironmentRequest): string;
|
|
21
|
+
/** @internal */
|
|
22
|
+
export declare const SuspendEnvironmentResponse$inboundSchema: z.ZodMiniType<SuspendEnvironmentResponse, unknown>;
|
|
23
|
+
export declare function suspendEnvironmentResponseFromJSON(jsonString: string): SafeParseResult<SuspendEnvironmentResponse, SDKValidationError>;
|
|
@@ -0,0 +1,20 @@
|
|
|
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 models from "../index.js";
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const SuspendEnvironmentRequest$outboundSchema = z.object({
|
|
9
|
+
environmentId: z.string(),
|
|
10
|
+
});
|
|
11
|
+
export function suspendEnvironmentRequestToJSON(suspendEnvironmentRequest) {
|
|
12
|
+
return JSON.stringify(SuspendEnvironmentRequest$outboundSchema.parse(suspendEnvironmentRequest));
|
|
13
|
+
}
|
|
14
|
+
/** @internal */
|
|
15
|
+
export const SuspendEnvironmentResponse$inboundSchema = z.object({
|
|
16
|
+
data: models.Environment$inboundSchema,
|
|
17
|
+
});
|
|
18
|
+
export function suspendEnvironmentResponseFromJSON(jsonString) {
|
|
19
|
+
return safeParse(jsonString, (x) => SuspendEnvironmentResponse$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'SuspendEnvironmentResponse' from JSON`);
|
|
20
|
+
}
|
package/esm/models/release.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ export declare const Qualification: {
|
|
|
9
9
|
readonly Qualified: "qualified";
|
|
10
10
|
};
|
|
11
11
|
export type Qualification = OpenEnum<typeof Qualification>;
|
|
12
|
+
export declare const ReleaseStatus: {
|
|
13
|
+
readonly Candidate: "candidate";
|
|
14
|
+
readonly Default: "default";
|
|
15
|
+
readonly Available: "available";
|
|
16
|
+
readonly Retired: "retired";
|
|
17
|
+
readonly Gone: "gone";
|
|
18
|
+
};
|
|
19
|
+
export type ReleaseStatus = OpenEnum<typeof ReleaseStatus>;
|
|
12
20
|
export type Release = {
|
|
13
21
|
actors: Array<ReleaseActor>;
|
|
14
22
|
apis: Array<ApiProfile>;
|
|
@@ -17,10 +25,13 @@ export type Release = {
|
|
|
17
25
|
id: string;
|
|
18
26
|
qualification: Qualification;
|
|
19
27
|
retiredAt: Date | null;
|
|
28
|
+
status: ReleaseStatus;
|
|
20
29
|
version: string;
|
|
21
30
|
};
|
|
22
31
|
/** @internal */
|
|
23
32
|
export declare const Qualification$inboundSchema: z.ZodMiniType<Qualification, unknown>;
|
|
24
33
|
/** @internal */
|
|
34
|
+
export declare const ReleaseStatus$inboundSchema: z.ZodMiniType<ReleaseStatus, unknown>;
|
|
35
|
+
/** @internal */
|
|
25
36
|
export declare const Release$inboundSchema: z.ZodMiniType<Release, unknown>;
|
|
26
37
|
export declare function releaseFromJSON(jsonString: string): SafeParseResult<Release, SDKValidationError>;
|
package/esm/models/release.js
CHANGED
|
@@ -11,9 +11,18 @@ export const Qualification = {
|
|
|
11
11
|
Development: "development",
|
|
12
12
|
Qualified: "qualified",
|
|
13
13
|
};
|
|
14
|
+
export const ReleaseStatus = {
|
|
15
|
+
Candidate: "candidate",
|
|
16
|
+
Default: "default",
|
|
17
|
+
Available: "available",
|
|
18
|
+
Retired: "retired",
|
|
19
|
+
Gone: "gone",
|
|
20
|
+
};
|
|
14
21
|
/** @internal */
|
|
15
22
|
export const Qualification$inboundSchema = openEnums.inboundSchema(Qualification);
|
|
16
23
|
/** @internal */
|
|
24
|
+
export const ReleaseStatus$inboundSchema = openEnums.inboundSchema(ReleaseStatus);
|
|
25
|
+
/** @internal */
|
|
17
26
|
export const Release$inboundSchema = z.object({
|
|
18
27
|
actors: z.array(ReleaseActor$inboundSchema),
|
|
19
28
|
apis: z.array(ApiProfile$inboundSchema),
|
|
@@ -22,6 +31,7 @@ export const Release$inboundSchema = z.object({
|
|
|
22
31
|
id: types.string(),
|
|
23
32
|
qualification: Qualification$inboundSchema,
|
|
24
33
|
retiredAt: types.nullable(types.date()),
|
|
34
|
+
status: ReleaseStatus$inboundSchema,
|
|
25
35
|
version: types.string(),
|
|
26
36
|
});
|
|
27
37
|
export function releaseFromJSON(jsonString) {
|
|
@@ -6,7 +6,9 @@ export declare class Environments extends ClientSDK {
|
|
|
6
6
|
getConnection(request: operations.GetConnectionRequest, options?: RequestOptions): Promise<operations.GetConnectionResponse>;
|
|
7
7
|
reveal(request: operations.RevealConnectionRequest, options?: RequestOptions): Promise<operations.RevealConnectionResponse>;
|
|
8
8
|
events(request: operations.ListEnvironmentEventsRequest, options?: RequestOptions): Promise<models.EventPage>;
|
|
9
|
+
resume(request: operations.ResumeEnvironmentRequest, options?: RequestOptions): Promise<operations.ResumeEnvironmentResponse>;
|
|
9
10
|
stop(request: operations.StopEnvironmentRequest, options?: RequestOptions): Promise<operations.StopEnvironmentResponse>;
|
|
11
|
+
suspend(request: operations.SuspendEnvironmentRequest, options?: RequestOptions): Promise<operations.SuspendEnvironmentResponse>;
|
|
10
12
|
list(request: operations.ListEnvironmentsRequest, options?: RequestOptions): Promise<operations.ListEnvironmentsResponse>;
|
|
11
13
|
create(request: operations.CreateEnvironmentsRequest, options?: RequestOptions): Promise<operations.CreateEnvironmentsResponse>;
|
|
12
14
|
}
|
package/esm/sdk/environments.js
CHANGED
|
@@ -6,8 +6,10 @@ import { environmentsEvents } from "../funcs/environments-events.js";
|
|
|
6
6
|
import { environmentsGetConnection } from "../funcs/environments-get-connection.js";
|
|
7
7
|
import { environmentsGet } from "../funcs/environments-get.js";
|
|
8
8
|
import { environmentsList } from "../funcs/environments-list.js";
|
|
9
|
+
import { environmentsResume } from "../funcs/environments-resume.js";
|
|
9
10
|
import { environmentsReveal } from "../funcs/environments-reveal.js";
|
|
10
11
|
import { environmentsStop } from "../funcs/environments-stop.js";
|
|
12
|
+
import { environmentsSuspend } from "../funcs/environments-suspend.js";
|
|
11
13
|
import { ClientSDK } from "../lib/sdks.js";
|
|
12
14
|
import { unwrapAsync } from "../types/fp.js";
|
|
13
15
|
export class Environments extends ClientSDK {
|
|
@@ -23,9 +25,15 @@ export class Environments extends ClientSDK {
|
|
|
23
25
|
async events(request, options) {
|
|
24
26
|
return unwrapAsync(environmentsEvents(this, request, options));
|
|
25
27
|
}
|
|
28
|
+
async resume(request, options) {
|
|
29
|
+
return unwrapAsync(environmentsResume(this, request, options));
|
|
30
|
+
}
|
|
26
31
|
async stop(request, options) {
|
|
27
32
|
return unwrapAsync(environmentsStop(this, request, options));
|
|
28
33
|
}
|
|
34
|
+
async suspend(request, options) {
|
|
35
|
+
return unwrapAsync(environmentsSuspend(this, request, options));
|
|
36
|
+
}
|
|
29
37
|
async list(request, options) {
|
|
30
38
|
return unwrapAsync(environmentsList(this, request, options));
|
|
31
39
|
}
|
package/esm/sdk/sdk.d.ts
CHANGED
|
@@ -16,5 +16,8 @@ export declare class StateMachines extends ClientSDK {
|
|
|
16
16
|
get snapshots(): Snapshots;
|
|
17
17
|
private _workspaces?;
|
|
18
18
|
get workspaces(): Workspaces;
|
|
19
|
+
listEnvironmentHistory(request: operations.ListEnvironmentHistoryRequest, options?: RequestOptions): Promise<operations.ListEnvironmentHistoryResponse>;
|
|
20
|
+
getEnvironmentPosition(request: operations.GetEnvironmentPositionRequest, options?: RequestOptions): Promise<operations.GetEnvironmentPositionResponse>;
|
|
21
|
+
getEnvironmentExchange(request: operations.GetEnvironmentExchangeRequest, options?: RequestOptions): Promise<operations.GetEnvironmentExchangeResponse>;
|
|
19
22
|
createEnvironmentDefinition(request: operations.CreateEnvironmentDefinitionRequest, options?: RequestOptions): Promise<operations.CreateEnvironmentDefinitionResponse>;
|
|
20
23
|
}
|
package/esm/sdk/sdk.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
3
|
*/
|
|
4
4
|
import { createEnvironmentDefinition } from "../funcs/create-environment-definition.js";
|
|
5
|
+
import { getEnvironmentExchange } from "../funcs/get-environment-exchange.js";
|
|
6
|
+
import { getEnvironmentPosition } from "../funcs/get-environment-position.js";
|
|
7
|
+
import { listEnvironmentHistory } from "../funcs/list-environment-history.js";
|
|
5
8
|
import { ClientSDK } from "../lib/sdks.js";
|
|
6
9
|
import { unwrapAsync } from "../types/fp.js";
|
|
7
10
|
import { Catalog } from "./catalog.js";
|
|
@@ -30,6 +33,15 @@ export class StateMachines extends ClientSDK {
|
|
|
30
33
|
get workspaces() {
|
|
31
34
|
return (this._workspaces ?? (this._workspaces = new Workspaces(this._options)));
|
|
32
35
|
}
|
|
36
|
+
async listEnvironmentHistory(request, options) {
|
|
37
|
+
return unwrapAsync(listEnvironmentHistory(this, request, options));
|
|
38
|
+
}
|
|
39
|
+
async getEnvironmentPosition(request, options) {
|
|
40
|
+
return unwrapAsync(getEnvironmentPosition(this, request, options));
|
|
41
|
+
}
|
|
42
|
+
async getEnvironmentExchange(request, options) {
|
|
43
|
+
return unwrapAsync(getEnvironmentExchange(this, request, options));
|
|
44
|
+
}
|
|
33
45
|
async createEnvironmentDefinition(request, options) {
|
|
34
46
|
return unwrapAsync(createEnvironmentDefinition(this, request, options));
|
|
35
47
|
}
|
|
@@ -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
|