wellcrafted 0.29.1 → 0.31.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 +26 -2
- package/dist/brand.d.ts +64 -5
- package/dist/brand.d.ts.map +1 -1
- package/dist/error/index.d.ts +72 -241
- package/dist/error/index.d.ts.map +1 -1
- package/dist/error/index.js +38 -91
- package/dist/error/index.js.map +1 -1
- package/dist/query/index.js +2 -2
- package/dist/result/index.js +2 -2
- package/dist/{result-DfuKgZo9.js → result-0QjbC3Hw.js} +2 -2
- package/dist/{result-DfuKgZo9.js.map → result-0QjbC3Hw.js.map} +1 -1
- package/dist/{result-B1iWFqM9.js → result-DnOm5ds5.js} +1 -3
- package/dist/result-DnOm5ds5.js.map +1 -0
- package/dist/result-DolxQXIZ.d.ts.map +1 -1
- package/dist/standard-schema/index.d.ts +371 -0
- package/dist/standard-schema/index.d.ts.map +1 -0
- package/dist/standard-schema/index.js +344 -0
- package/dist/standard-schema/index.js.map +1 -0
- package/package.json +13 -5
- package/dist/result-B1iWFqM9.js.map +0 -1
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
//#region src/standard-schema/failures.ts
|
|
2
|
+
const FAILURES = {
|
|
3
|
+
EXPECTED_OBJECT: { issues: [{ message: "Expected object" }] },
|
|
4
|
+
EXPECTED_DATA_ERROR_PROPS: { issues: [{ message: "Expected object with 'data' and 'error' properties" }] },
|
|
5
|
+
EXPECTED_ERROR_NULL: { issues: [{
|
|
6
|
+
message: "Expected 'error' to be null for Ok variant",
|
|
7
|
+
path: ["error"]
|
|
8
|
+
}] },
|
|
9
|
+
EXPECTED_ERROR_NOT_NULL: { issues: [{
|
|
10
|
+
message: "Expected 'error' to be non-null for Err variant",
|
|
11
|
+
path: ["error"]
|
|
12
|
+
}] }
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/standard-schema/types.ts
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a schema has validation capability.
|
|
19
|
+
*/
|
|
20
|
+
function hasValidate(schema) {
|
|
21
|
+
return "validate" in schema["~standard"] && typeof schema["~standard"].validate === "function";
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a schema has JSON Schema generation capability.
|
|
25
|
+
*/
|
|
26
|
+
function hasJsonSchema(schema) {
|
|
27
|
+
return "jsonSchema" in schema["~standard"] && typeof schema["~standard"].jsonSchema === "object" && schema["~standard"].jsonSchema !== null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/standard-schema/err.ts
|
|
32
|
+
function createErrValidate(innerSchema) {
|
|
33
|
+
return (value) => {
|
|
34
|
+
if (typeof value !== "object" || value === null) return FAILURES.EXPECTED_OBJECT;
|
|
35
|
+
if (!("data" in value) || !("error" in value)) return FAILURES.EXPECTED_DATA_ERROR_PROPS;
|
|
36
|
+
const obj = value;
|
|
37
|
+
if (obj.error === null) return FAILURES.EXPECTED_ERROR_NOT_NULL;
|
|
38
|
+
const innerResult = innerSchema["~standard"].validate(obj.error);
|
|
39
|
+
if (innerResult instanceof Promise) return innerResult.then((r) => {
|
|
40
|
+
if (r.issues) return { issues: r.issues.map((issue) => ({
|
|
41
|
+
...issue,
|
|
42
|
+
path: ["error", ...issue.path || []]
|
|
43
|
+
})) };
|
|
44
|
+
return { value: {
|
|
45
|
+
data: null,
|
|
46
|
+
error: r.value
|
|
47
|
+
} };
|
|
48
|
+
});
|
|
49
|
+
if (innerResult.issues) return { issues: innerResult.issues.map((issue) => ({
|
|
50
|
+
...issue,
|
|
51
|
+
path: ["error", ...issue.path || []]
|
|
52
|
+
})) };
|
|
53
|
+
return { value: {
|
|
54
|
+
data: null,
|
|
55
|
+
error: innerResult.value
|
|
56
|
+
} };
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function createErrJsonSchema(innerSchema) {
|
|
60
|
+
return {
|
|
61
|
+
input(options) {
|
|
62
|
+
return {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties: {
|
|
65
|
+
data: { type: "null" },
|
|
66
|
+
error: innerSchema["~standard"].jsonSchema.input(options)
|
|
67
|
+
},
|
|
68
|
+
required: ["data", "error"],
|
|
69
|
+
additionalProperties: false
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
output(options) {
|
|
73
|
+
return {
|
|
74
|
+
type: "object",
|
|
75
|
+
properties: {
|
|
76
|
+
data: { type: "null" },
|
|
77
|
+
error: innerSchema["~standard"].jsonSchema.output(options)
|
|
78
|
+
},
|
|
79
|
+
required: ["data", "error"],
|
|
80
|
+
additionalProperties: false
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Wraps a Standard Schema into an Err variant schema.
|
|
87
|
+
*
|
|
88
|
+
* Takes a schema for type E and returns a schema for `{ data: null, error: E }`.
|
|
89
|
+
* Preserves the capabilities of the input schema (validate, jsonSchema, or both).
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { z } from "zod";
|
|
94
|
+
* import { ErrSchema } from "wellcrafted/standard-schema";
|
|
95
|
+
*
|
|
96
|
+
* const errorSchema = z.object({ code: z.string(), message: z.string() });
|
|
97
|
+
* const errResultSchema = ErrSchema(errorSchema);
|
|
98
|
+
*
|
|
99
|
+
* // Validates: { data: null, error: { code: "NOT_FOUND", message: "User not found" } }
|
|
100
|
+
* const result = errResultSchema["~standard"].validate({
|
|
101
|
+
* data: null,
|
|
102
|
+
* error: { code: "NOT_FOUND", message: "User not found" },
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function ErrSchema(innerSchema) {
|
|
107
|
+
const base = { "~standard": {
|
|
108
|
+
version: 1,
|
|
109
|
+
vendor: "wellcrafted",
|
|
110
|
+
types: {
|
|
111
|
+
input: void 0,
|
|
112
|
+
output: void 0
|
|
113
|
+
}
|
|
114
|
+
} };
|
|
115
|
+
if (hasValidate(innerSchema)) base["~standard"].validate = createErrValidate(innerSchema);
|
|
116
|
+
if (hasJsonSchema(innerSchema)) base["~standard"].jsonSchema = createErrJsonSchema(innerSchema);
|
|
117
|
+
return base;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/standard-schema/ok.ts
|
|
122
|
+
function createOkValidate(innerSchema) {
|
|
123
|
+
return (value) => {
|
|
124
|
+
if (typeof value !== "object" || value === null) return FAILURES.EXPECTED_OBJECT;
|
|
125
|
+
if (!("data" in value) || !("error" in value)) return FAILURES.EXPECTED_DATA_ERROR_PROPS;
|
|
126
|
+
const obj = value;
|
|
127
|
+
if (obj.error !== null) return FAILURES.EXPECTED_ERROR_NULL;
|
|
128
|
+
const innerResult = innerSchema["~standard"].validate(obj.data);
|
|
129
|
+
if (innerResult instanceof Promise) return innerResult.then((r) => {
|
|
130
|
+
if (r.issues) return { issues: r.issues.map((issue) => ({
|
|
131
|
+
...issue,
|
|
132
|
+
path: ["data", ...issue.path || []]
|
|
133
|
+
})) };
|
|
134
|
+
return { value: {
|
|
135
|
+
data: r.value,
|
|
136
|
+
error: null
|
|
137
|
+
} };
|
|
138
|
+
});
|
|
139
|
+
if (innerResult.issues) return { issues: innerResult.issues.map((issue) => ({
|
|
140
|
+
...issue,
|
|
141
|
+
path: ["data", ...issue.path || []]
|
|
142
|
+
})) };
|
|
143
|
+
return { value: {
|
|
144
|
+
data: innerResult.value,
|
|
145
|
+
error: null
|
|
146
|
+
} };
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function createOkJsonSchema(innerSchema) {
|
|
150
|
+
return {
|
|
151
|
+
input(options) {
|
|
152
|
+
return {
|
|
153
|
+
type: "object",
|
|
154
|
+
properties: {
|
|
155
|
+
data: innerSchema["~standard"].jsonSchema.input(options),
|
|
156
|
+
error: { type: "null" }
|
|
157
|
+
},
|
|
158
|
+
required: ["data", "error"],
|
|
159
|
+
additionalProperties: false
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
output(options) {
|
|
163
|
+
return {
|
|
164
|
+
type: "object",
|
|
165
|
+
properties: {
|
|
166
|
+
data: innerSchema["~standard"].jsonSchema.output(options),
|
|
167
|
+
error: { type: "null" }
|
|
168
|
+
},
|
|
169
|
+
required: ["data", "error"],
|
|
170
|
+
additionalProperties: false
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Wraps a Standard Schema into an Ok variant schema.
|
|
177
|
+
*
|
|
178
|
+
* Takes a schema for type T and returns a schema for `{ data: T, error: null }`.
|
|
179
|
+
* Preserves the capabilities of the input schema (validate, jsonSchema, or both).
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* import { z } from "zod";
|
|
184
|
+
* import { OkSchema } from "wellcrafted/standard-schema";
|
|
185
|
+
*
|
|
186
|
+
* const userSchema = z.object({ name: z.string() });
|
|
187
|
+
* const okUserSchema = OkSchema(userSchema);
|
|
188
|
+
*
|
|
189
|
+
* // Validates: { data: { name: "Alice" }, error: null }
|
|
190
|
+
* const result = okUserSchema["~standard"].validate({
|
|
191
|
+
* data: { name: "Alice" },
|
|
192
|
+
* error: null,
|
|
193
|
+
* });
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
function OkSchema(innerSchema) {
|
|
197
|
+
const base = { "~standard": {
|
|
198
|
+
version: 1,
|
|
199
|
+
vendor: "wellcrafted",
|
|
200
|
+
types: {
|
|
201
|
+
input: void 0,
|
|
202
|
+
output: void 0
|
|
203
|
+
}
|
|
204
|
+
} };
|
|
205
|
+
if (hasValidate(innerSchema)) base["~standard"].validate = createOkValidate(innerSchema);
|
|
206
|
+
if (hasJsonSchema(innerSchema)) base["~standard"].jsonSchema = createOkJsonSchema(innerSchema);
|
|
207
|
+
return base;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/standard-schema/result.ts
|
|
212
|
+
function createResultValidate(dataSchema, errorSchema) {
|
|
213
|
+
return (value) => {
|
|
214
|
+
if (typeof value !== "object" || value === null) return FAILURES.EXPECTED_OBJECT;
|
|
215
|
+
if (!("data" in value) || !("error" in value)) return FAILURES.EXPECTED_DATA_ERROR_PROPS;
|
|
216
|
+
const obj = value;
|
|
217
|
+
const isOk = obj.error === null;
|
|
218
|
+
if (isOk) {
|
|
219
|
+
const innerResult$1 = dataSchema["~standard"].validate(obj.data);
|
|
220
|
+
if (innerResult$1 instanceof Promise) return innerResult$1.then((r) => {
|
|
221
|
+
if (r.issues) return { issues: r.issues.map((issue) => ({
|
|
222
|
+
...issue,
|
|
223
|
+
path: ["data", ...issue.path || []]
|
|
224
|
+
})) };
|
|
225
|
+
return { value: {
|
|
226
|
+
data: r.value,
|
|
227
|
+
error: null
|
|
228
|
+
} };
|
|
229
|
+
});
|
|
230
|
+
if (innerResult$1.issues) return { issues: innerResult$1.issues.map((issue) => ({
|
|
231
|
+
...issue,
|
|
232
|
+
path: ["data", ...issue.path || []]
|
|
233
|
+
})) };
|
|
234
|
+
return { value: {
|
|
235
|
+
data: innerResult$1.value,
|
|
236
|
+
error: null
|
|
237
|
+
} };
|
|
238
|
+
}
|
|
239
|
+
const innerResult = errorSchema["~standard"].validate(obj.error);
|
|
240
|
+
if (innerResult instanceof Promise) return innerResult.then((r) => {
|
|
241
|
+
if (r.issues) return { issues: r.issues.map((issue) => ({
|
|
242
|
+
...issue,
|
|
243
|
+
path: ["error", ...issue.path || []]
|
|
244
|
+
})) };
|
|
245
|
+
return { value: {
|
|
246
|
+
data: null,
|
|
247
|
+
error: r.value
|
|
248
|
+
} };
|
|
249
|
+
});
|
|
250
|
+
if (innerResult.issues) return { issues: innerResult.issues.map((issue) => ({
|
|
251
|
+
...issue,
|
|
252
|
+
path: ["error", ...issue.path || []]
|
|
253
|
+
})) };
|
|
254
|
+
return { value: {
|
|
255
|
+
data: null,
|
|
256
|
+
error: innerResult.value
|
|
257
|
+
} };
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function createResultJsonSchema(dataSchema, errorSchema) {
|
|
261
|
+
return {
|
|
262
|
+
input(options) {
|
|
263
|
+
return { oneOf: [{
|
|
264
|
+
type: "object",
|
|
265
|
+
properties: {
|
|
266
|
+
data: dataSchema["~standard"].jsonSchema.input(options),
|
|
267
|
+
error: { type: "null" }
|
|
268
|
+
},
|
|
269
|
+
required: ["data", "error"],
|
|
270
|
+
additionalProperties: false
|
|
271
|
+
}, {
|
|
272
|
+
type: "object",
|
|
273
|
+
properties: {
|
|
274
|
+
data: { type: "null" },
|
|
275
|
+
error: errorSchema["~standard"].jsonSchema.input(options)
|
|
276
|
+
},
|
|
277
|
+
required: ["data", "error"],
|
|
278
|
+
additionalProperties: false
|
|
279
|
+
}] };
|
|
280
|
+
},
|
|
281
|
+
output(options) {
|
|
282
|
+
return { oneOf: [{
|
|
283
|
+
type: "object",
|
|
284
|
+
properties: {
|
|
285
|
+
data: dataSchema["~standard"].jsonSchema.output(options),
|
|
286
|
+
error: { type: "null" }
|
|
287
|
+
},
|
|
288
|
+
required: ["data", "error"],
|
|
289
|
+
additionalProperties: false
|
|
290
|
+
}, {
|
|
291
|
+
type: "object",
|
|
292
|
+
properties: {
|
|
293
|
+
data: { type: "null" },
|
|
294
|
+
error: errorSchema["~standard"].jsonSchema.output(options)
|
|
295
|
+
},
|
|
296
|
+
required: ["data", "error"],
|
|
297
|
+
additionalProperties: false
|
|
298
|
+
}] };
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Combines two Standard Schemas into a Result discriminated union schema.
|
|
304
|
+
*
|
|
305
|
+
* Takes a data schema for type T and an error schema for type E, returning a schema
|
|
306
|
+
* for `{ data: T, error: null } | { data: null, error: E }`.
|
|
307
|
+
*
|
|
308
|
+
* Preserves the capabilities of the input schemas - if both have validate, output
|
|
309
|
+
* has validate; if both have jsonSchema, output has jsonSchema.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* import { z } from "zod";
|
|
314
|
+
* import { ResultSchema } from "wellcrafted/standard-schema";
|
|
315
|
+
*
|
|
316
|
+
* const userSchema = z.object({ id: z.string(), name: z.string() });
|
|
317
|
+
* const errorSchema = z.object({ code: z.string(), message: z.string() });
|
|
318
|
+
* const resultSchema = ResultSchema(userSchema, errorSchema);
|
|
319
|
+
*
|
|
320
|
+
* // Validates Ok variant: { data: { id: "1", name: "Alice" }, error: null }
|
|
321
|
+
* // Validates Err variant: { data: null, error: { code: "NOT_FOUND", message: "..." } }
|
|
322
|
+
* const result = resultSchema["~standard"].validate({
|
|
323
|
+
* data: { id: "1", name: "Alice" },
|
|
324
|
+
* error: null,
|
|
325
|
+
* });
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
function ResultSchema(dataSchema, errorSchema) {
|
|
329
|
+
const base = { "~standard": {
|
|
330
|
+
version: 1,
|
|
331
|
+
vendor: "wellcrafted",
|
|
332
|
+
types: {
|
|
333
|
+
input: void 0,
|
|
334
|
+
output: void 0
|
|
335
|
+
}
|
|
336
|
+
} };
|
|
337
|
+
if (hasValidate(dataSchema) && hasValidate(errorSchema)) base["~standard"].validate = createResultValidate(dataSchema, errorSchema);
|
|
338
|
+
if (hasJsonSchema(dataSchema) && hasJsonSchema(errorSchema)) base["~standard"].jsonSchema = createResultJsonSchema(dataSchema, errorSchema);
|
|
339
|
+
return base;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
//#endregion
|
|
343
|
+
export { ErrSchema, FAILURES, OkSchema, ResultSchema, hasJsonSchema, hasValidate };
|
|
344
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["schema: T","innerSchema: TSchema","value: unknown","issue: StandardSchemaV1.Issue","options: StandardJSONSchemaV1.Options","innerSchema: TSchema","value: unknown","issue: StandardSchemaV1.Issue","options: StandardJSONSchemaV1.Options","dataSchema: TDataSchema","errorSchema: TErrorSchema","value: unknown","innerResult","issue: StandardSchemaV1.Issue","options: StandardJSONSchemaV1.Options"],"sources":["../../src/standard-schema/failures.ts","../../src/standard-schema/types.ts","../../src/standard-schema/err.ts","../../src/standard-schema/ok.ts","../../src/standard-schema/result.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"./types.js\";\n\nexport const FAILURES = {\n\tEXPECTED_OBJECT: { issues: [{ message: \"Expected object\" }] },\n\tEXPECTED_DATA_ERROR_PROPS: {\n\t\tissues: [{ message: \"Expected object with 'data' and 'error' properties\" }],\n\t},\n\tEXPECTED_ERROR_NULL: {\n\t\tissues: [\n\t\t\t{\n\t\t\t\tmessage: \"Expected 'error' to be null for Ok variant\",\n\t\t\t\tpath: [\"error\"],\n\t\t\t},\n\t\t],\n\t},\n\tEXPECTED_ERROR_NOT_NULL: {\n\t\tissues: [\n\t\t\t{\n\t\t\t\tmessage: \"Expected 'error' to be non-null for Err variant\",\n\t\t\t\tpath: [\"error\"],\n\t\t\t},\n\t\t],\n\t},\n} as const satisfies Record<string, StandardSchemaV1.FailureResult>;\n","/**\n * Standard Schema type definitions.\n *\n * These interfaces are copied from the Standard Schema specification\n * (https://standardschema.dev) to avoid external dependencies.\n *\n * @see https://github.com/standard-schema/standard-schema\n */\n\n// #########################\n// ### Standard Typed ###\n// #########################\n\n/**\n * The Standard Typed interface. This is a base type extended by other specs.\n */\nexport type StandardTypedV1<Input = unknown, Output = Input> = {\n\t/** The Standard properties. */\n\treadonly \"~standard\": StandardTypedV1.Props<Input, Output>;\n};\n\nexport declare namespace StandardTypedV1 {\n\t/** The Standard Typed properties interface. */\n\ttype Props<Input = unknown, Output = Input> = {\n\t\t/** The version number of the standard. */\n\t\treadonly version: 1;\n\t\t/** The vendor name of the schema library. */\n\t\treadonly vendor: string;\n\t\t/** Inferred types associated with the schema. */\n\t\treadonly types?: Types<Input, Output> | undefined;\n\t};\n\n\t/** The Standard Typed types interface. */\n\ttype Types<Input = unknown, Output = Input> = {\n\t\t/** The input type of the schema. */\n\t\treadonly input: Input;\n\t\t/** The output type of the schema. */\n\t\treadonly output: Output;\n\t};\n\n\t/** Infers the input type of a Standard Typed. */\n\ttype InferInput<Schema extends StandardTypedV1> = NonNullable<\n\t\tSchema[\"~standard\"][\"types\"]\n\t>[\"input\"];\n\n\t/** Infers the output type of a Standard Typed. */\n\ttype InferOutput<Schema extends StandardTypedV1> = NonNullable<\n\t\tSchema[\"~standard\"][\"types\"]\n\t>[\"output\"];\n}\n\n// ##########################\n// ### Standard Schema ###\n// ##########################\n\n/**\n * The Standard Schema interface.\n *\n * Extends StandardTypedV1 with a validate function for runtime validation.\n */\nexport type StandardSchemaV1<Input = unknown, Output = Input> = {\n\t/** The Standard Schema properties. */\n\treadonly \"~standard\": StandardSchemaV1.Props<Input, Output>;\n};\n\nexport declare namespace StandardSchemaV1 {\n\t/** The Standard Schema properties interface. */\n\ttype Props<Input = unknown, Output = Input> = StandardTypedV1.Props<\n\t\tInput,\n\t\tOutput\n\t> & {\n\t\t/** Validates unknown input values. */\n\t\treadonly validate: (\n\t\t\tvalue: unknown,\n\t\t\toptions?: StandardSchemaV1.Options | undefined,\n\t\t) => Result<Output> | Promise<Result<Output>>;\n\t};\n\n\t/** The result interface of the validate function. */\n\ttype Result<Output> = SuccessResult<Output> | FailureResult;\n\n\t/** The result interface if validation succeeds. */\n\ttype SuccessResult<Output> = {\n\t\t/** The typed output value. */\n\t\treadonly value: Output;\n\t\t/** A falsy value for `issues` indicates success. */\n\t\treadonly issues?: undefined;\n\t};\n\n\t/** Options for the validate function. */\n\ttype Options = {\n\t\t/** Explicit support for additional vendor-specific parameters, if needed. */\n\t\treadonly libraryOptions?: Record<string, unknown> | undefined;\n\t};\n\n\t/** The result interface if validation fails. */\n\ttype FailureResult = {\n\t\t/** The issues of failed validation. */\n\t\treadonly issues: ReadonlyArray<Issue>;\n\t};\n\n\t/** The issue interface of the failure output. */\n\ttype Issue = {\n\t\t/** The error message of the issue. */\n\t\treadonly message: string;\n\t\t/** The path of the issue, if any. */\n\t\treadonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;\n\t};\n\n\t/** The path segment interface of the issue. */\n\ttype PathSegment = {\n\t\t/** The key representing a path segment. */\n\t\treadonly key: PropertyKey;\n\t};\n\n\t/** Infers the input type of a Standard Schema. */\n\ttype InferInput<Schema extends StandardTypedV1> =\n\t\tStandardTypedV1.InferInput<Schema>;\n\n\t/** Infers the output type of a Standard Schema. */\n\ttype InferOutput<Schema extends StandardTypedV1> =\n\t\tStandardTypedV1.InferOutput<Schema>;\n}\n\n// ###############################\n// ### Standard JSON Schema ###\n// ###############################\n\n/**\n * The Standard JSON Schema interface.\n *\n * Extends StandardTypedV1 with methods for generating JSON Schema.\n */\nexport type StandardJSONSchemaV1<Input = unknown, Output = Input> = {\n\t/** The Standard JSON Schema properties. */\n\treadonly \"~standard\": StandardJSONSchemaV1.Props<Input, Output>;\n};\n\nexport declare namespace StandardJSONSchemaV1 {\n\t/** The Standard JSON Schema properties interface. */\n\ttype Props<Input = unknown, Output = Input> = StandardTypedV1.Props<\n\t\tInput,\n\t\tOutput\n\t> & {\n\t\t/** Methods for generating the input/output JSON Schema. */\n\t\treadonly jsonSchema: StandardJSONSchemaV1.Converter;\n\t};\n\n\t/** The Standard JSON Schema converter interface. */\n\ttype Converter = {\n\t\t/** Converts the input type to JSON Schema. May throw if conversion is not supported. */\n\t\treadonly input: (\n\t\t\toptions: StandardJSONSchemaV1.Options,\n\t\t) => Record<string, unknown>;\n\t\t/** Converts the output type to JSON Schema. May throw if conversion is not supported. */\n\t\treadonly output: (\n\t\t\toptions: StandardJSONSchemaV1.Options,\n\t\t) => Record<string, unknown>;\n\t};\n\n\t/**\n\t * The target version of the generated JSON Schema.\n\t *\n\t * It is *strongly recommended* that implementers support `\"draft-2020-12\"` and `\"draft-07\"`,\n\t * as they are both in wide use. All other targets can be implemented on a best-effort basis.\n\t * Libraries should throw if they don't support a specified target.\n\t *\n\t * The `\"openapi-3.0\"` target is intended as a standardized specifier for OpenAPI 3.0\n\t * which is a superset of JSON Schema `\"draft-04\"`.\n\t */\n\ttype Target =\n\t\t| \"draft-2020-12\"\n\t\t| \"draft-07\"\n\t\t| \"openapi-3.0\"\n\t\t// Accepts any string for future targets while preserving autocomplete\n\t\t| (string & {});\n\n\t/** The options for the input/output methods. */\n\ttype Options = {\n\t\t/** Specifies the target version of the generated JSON Schema. */\n\t\treadonly target: Target;\n\t\t/** Explicit support for additional vendor-specific parameters, if needed. */\n\t\treadonly libraryOptions?: Record<string, unknown> | undefined;\n\t};\n\n\t/** Infers the input type of a Standard JSON Schema. */\n\ttype InferInput<Schema extends StandardTypedV1> =\n\t\tStandardTypedV1.InferInput<Schema>;\n\n\t/** Infers the output type of a Standard JSON Schema. */\n\ttype InferOutput<Schema extends StandardTypedV1> =\n\t\tStandardTypedV1.InferOutput<Schema>;\n}\n\n// ###############################\n// ### Utility Types ###\n// ###############################\n\n/**\n * A schema that implements both StandardSchemaV1 and StandardJSONSchemaV1.\n */\nexport type StandardFullSchemaV1<Input = unknown, Output = Input> = {\n\treadonly \"~standard\": StandardSchemaV1.Props<Input, Output> &\n\t\tStandardJSONSchemaV1.Props<Input, Output>;\n};\n\n/**\n * Checks if a schema has validation capability.\n */\nexport function hasValidate<T extends StandardTypedV1>(\n\tschema: T,\n): schema is T & StandardSchemaV1 {\n\treturn (\n\t\t\"validate\" in schema[\"~standard\"] &&\n\t\ttypeof schema[\"~standard\"].validate === \"function\"\n\t);\n}\n\n/**\n * Checks if a schema has JSON Schema generation capability.\n */\nexport function hasJsonSchema<T extends StandardTypedV1>(\n\tschema: T,\n): schema is T & StandardJSONSchemaV1 {\n\treturn (\n\t\t\"jsonSchema\" in schema[\"~standard\"] &&\n\t\ttypeof schema[\"~standard\"].jsonSchema === \"object\" &&\n\t\tschema[\"~standard\"].jsonSchema !== null\n\t);\n}\n","import { FAILURES } from \"./failures.js\";\nimport {\n\thasJsonSchema,\n\thasValidate,\n\ttype StandardJSONSchemaV1,\n\ttype StandardSchemaV1,\n\ttype StandardTypedV1,\n} from \"./types.js\";\n\n/**\n * Output type for ErrSchema - wraps inner schema's types with Err structure.\n *\n * Preserves the capabilities of the input schema:\n * - If input has validate, output has validate\n * - If input has jsonSchema, output has jsonSchema\n */\nexport type Err<TSchema extends StandardTypedV1> = {\n\treadonly \"~standard\": {\n\t\treadonly version: 1;\n\t\treadonly vendor: \"wellcrafted\";\n\t\treadonly types: {\n\t\t\treadonly input: {\n\t\t\t\tdata: null;\n\t\t\t\terror: StandardTypedV1.InferInput<TSchema>;\n\t\t\t};\n\t\t\treadonly output: {\n\t\t\t\tdata: null;\n\t\t\t\terror: StandardTypedV1.InferOutput<TSchema>;\n\t\t\t};\n\t\t};\n\t} & (TSchema extends StandardSchemaV1\n\t\t? {\n\t\t\t\treadonly validate: StandardSchemaV1.Props<\n\t\t\t\t\t{ data: null; error: StandardTypedV1.InferInput<TSchema> },\n\t\t\t\t\t{ data: null; error: StandardTypedV1.InferOutput<TSchema> }\n\t\t\t\t>[\"validate\"];\n\t\t\t}\n\t\t: Record<string, never>) &\n\t\t(TSchema extends StandardJSONSchemaV1\n\t\t\t? { readonly jsonSchema: StandardJSONSchemaV1.Converter }\n\t\t\t: Record<string, never>);\n};\n\nfunction createErrValidate<TSchema extends StandardSchemaV1>(\n\tinnerSchema: TSchema,\n): StandardSchemaV1.Props<\n\t{ data: null; error: StandardTypedV1.InferInput<TSchema> },\n\t{ data: null; error: StandardTypedV1.InferOutput<TSchema> }\n>[\"validate\"] {\n\treturn (value: unknown) => {\n\t\tif (typeof value !== \"object\" || value === null) {\n\t\t\treturn FAILURES.EXPECTED_OBJECT;\n\t\t}\n\n\t\tif (!(\"data\" in value) || !(\"error\" in value)) {\n\t\t\treturn FAILURES.EXPECTED_DATA_ERROR_PROPS;\n\t\t}\n\n\t\tconst obj = value as { data: unknown; error: unknown };\n\n\t\tif (obj.error === null) {\n\t\t\treturn FAILURES.EXPECTED_ERROR_NOT_NULL;\n\t\t}\n\n\t\tconst innerResult = innerSchema[\"~standard\"].validate(obj.error);\n\n\t\tif (innerResult instanceof Promise) {\n\t\t\treturn innerResult.then((r) => {\n\t\t\t\tif (r.issues) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tissues: r.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\tpath: [\"error\", ...(issue.path || [])],\n\t\t\t\t\t\t})),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn { value: { data: null as null, error: r.value } };\n\t\t\t});\n\t\t}\n\n\t\tif (innerResult.issues) {\n\t\t\treturn {\n\t\t\t\tissues: innerResult.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t...issue,\n\t\t\t\t\tpath: [\"error\", ...(issue.path || [])],\n\t\t\t\t})),\n\t\t\t};\n\t\t}\n\n\t\treturn { value: { data: null as null, error: innerResult.value } };\n\t};\n}\n\nfunction createErrJsonSchema<TSchema extends StandardJSONSchemaV1>(\n\tinnerSchema: TSchema,\n): StandardJSONSchemaV1.Converter {\n\treturn {\n\t\tinput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\ttype: \"object\",\n\t\t\t\tproperties: {\n\t\t\t\t\tdata: { type: \"null\" },\n\t\t\t\t\terror: innerSchema[\"~standard\"].jsonSchema.input(options),\n\t\t\t\t},\n\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\tadditionalProperties: false,\n\t\t\t};\n\t\t},\n\t\toutput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\ttype: \"object\",\n\t\t\t\tproperties: {\n\t\t\t\t\tdata: { type: \"null\" },\n\t\t\t\t\terror: innerSchema[\"~standard\"].jsonSchema.output(options),\n\t\t\t\t},\n\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\tadditionalProperties: false,\n\t\t\t};\n\t\t},\n\t};\n}\n\n/**\n * Wraps a Standard Schema into an Err variant schema.\n *\n * Takes a schema for type E and returns a schema for `{ data: null, error: E }`.\n * Preserves the capabilities of the input schema (validate, jsonSchema, or both).\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n * import { ErrSchema } from \"wellcrafted/standard-schema\";\n *\n * const errorSchema = z.object({ code: z.string(), message: z.string() });\n * const errResultSchema = ErrSchema(errorSchema);\n *\n * // Validates: { data: null, error: { code: \"NOT_FOUND\", message: \"User not found\" } }\n * const result = errResultSchema[\"~standard\"].validate({\n * data: null,\n * error: { code: \"NOT_FOUND\", message: \"User not found\" },\n * });\n * ```\n */\nexport function ErrSchema<TSchema extends StandardTypedV1>(\n\tinnerSchema: TSchema,\n): Err<TSchema> {\n\tconst base = {\n\t\t\"~standard\": {\n\t\t\tversion: 1 as const,\n\t\t\tvendor: \"wellcrafted\",\n\t\t\ttypes: {\n\t\t\t\tinput: undefined as unknown as {\n\t\t\t\t\tdata: null;\n\t\t\t\t\terror: StandardTypedV1.InferInput<TSchema>;\n\t\t\t\t},\n\t\t\t\toutput: undefined as unknown as {\n\t\t\t\t\tdata: null;\n\t\t\t\t\terror: StandardTypedV1.InferOutput<TSchema>;\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t};\n\n\tif (hasValidate(innerSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).validate =\n\t\t\tcreateErrValidate(innerSchema);\n\t}\n\n\tif (hasJsonSchema(innerSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).jsonSchema =\n\t\t\tcreateErrJsonSchema(innerSchema);\n\t}\n\n\treturn base as Err<TSchema>;\n}\n","import { FAILURES } from \"./failures.js\";\nimport {\n\thasJsonSchema,\n\thasValidate,\n\ttype StandardJSONSchemaV1,\n\ttype StandardSchemaV1,\n\ttype StandardTypedV1,\n} from \"./types.js\";\n\n/**\n * Output type for OkSchema - wraps inner schema's types with Ok structure.\n *\n * Preserves the capabilities of the input schema:\n * - If input has validate, output has validate\n * - If input has jsonSchema, output has jsonSchema\n */\nexport type Ok<TSchema extends StandardTypedV1> = {\n\treadonly \"~standard\": {\n\t\treadonly version: 1;\n\t\treadonly vendor: \"wellcrafted\";\n\t\treadonly types: {\n\t\t\treadonly input: {\n\t\t\t\tdata: StandardTypedV1.InferInput<TSchema>;\n\t\t\t\terror: null;\n\t\t\t};\n\t\t\treadonly output: {\n\t\t\t\tdata: StandardTypedV1.InferOutput<TSchema>;\n\t\t\t\terror: null;\n\t\t\t};\n\t\t};\n\t} & (TSchema extends StandardSchemaV1\n\t\t? {\n\t\t\t\treadonly validate: StandardSchemaV1.Props<\n\t\t\t\t\t{ data: StandardTypedV1.InferInput<TSchema>; error: null },\n\t\t\t\t\t{ data: StandardTypedV1.InferOutput<TSchema>; error: null }\n\t\t\t\t>[\"validate\"];\n\t\t\t}\n\t\t: Record<string, never>) &\n\t\t(TSchema extends StandardJSONSchemaV1\n\t\t\t? { readonly jsonSchema: StandardJSONSchemaV1.Converter }\n\t\t\t: Record<string, never>);\n};\n\nfunction createOkValidate<TSchema extends StandardSchemaV1>(\n\tinnerSchema: TSchema,\n): StandardSchemaV1.Props<\n\t{ data: StandardTypedV1.InferInput<TSchema>; error: null },\n\t{ data: StandardTypedV1.InferOutput<TSchema>; error: null }\n>[\"validate\"] {\n\treturn (value: unknown) => {\n\t\tif (typeof value !== \"object\" || value === null) {\n\t\t\treturn FAILURES.EXPECTED_OBJECT;\n\t\t}\n\n\t\tif (!(\"data\" in value) || !(\"error\" in value)) {\n\t\t\treturn FAILURES.EXPECTED_DATA_ERROR_PROPS;\n\t\t}\n\n\t\tconst obj = value as { data: unknown; error: unknown };\n\n\t\tif (obj.error !== null) {\n\t\t\treturn FAILURES.EXPECTED_ERROR_NULL;\n\t\t}\n\n\t\tconst innerResult = innerSchema[\"~standard\"].validate(obj.data);\n\n\t\tif (innerResult instanceof Promise) {\n\t\t\treturn innerResult.then((r) => {\n\t\t\t\tif (r.issues) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tissues: r.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\tpath: [\"data\", ...(issue.path || [])],\n\t\t\t\t\t\t})),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn { value: { data: r.value, error: null as null } };\n\t\t\t});\n\t\t}\n\n\t\tif (innerResult.issues) {\n\t\t\treturn {\n\t\t\t\tissues: innerResult.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t...issue,\n\t\t\t\t\tpath: [\"data\", ...(issue.path || [])],\n\t\t\t\t})),\n\t\t\t};\n\t\t}\n\n\t\treturn { value: { data: innerResult.value, error: null as null } };\n\t};\n}\n\nfunction createOkJsonSchema<TSchema extends StandardJSONSchemaV1>(\n\tinnerSchema: TSchema,\n): StandardJSONSchemaV1.Converter {\n\treturn {\n\t\tinput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\ttype: \"object\",\n\t\t\t\tproperties: {\n\t\t\t\t\tdata: innerSchema[\"~standard\"].jsonSchema.input(options),\n\t\t\t\t\terror: { type: \"null\" },\n\t\t\t\t},\n\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\tadditionalProperties: false,\n\t\t\t};\n\t\t},\n\t\toutput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\ttype: \"object\",\n\t\t\t\tproperties: {\n\t\t\t\t\tdata: innerSchema[\"~standard\"].jsonSchema.output(options),\n\t\t\t\t\terror: { type: \"null\" },\n\t\t\t\t},\n\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\tadditionalProperties: false,\n\t\t\t};\n\t\t},\n\t};\n}\n\n/**\n * Wraps a Standard Schema into an Ok variant schema.\n *\n * Takes a schema for type T and returns a schema for `{ data: T, error: null }`.\n * Preserves the capabilities of the input schema (validate, jsonSchema, or both).\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n * import { OkSchema } from \"wellcrafted/standard-schema\";\n *\n * const userSchema = z.object({ name: z.string() });\n * const okUserSchema = OkSchema(userSchema);\n *\n * // Validates: { data: { name: \"Alice\" }, error: null }\n * const result = okUserSchema[\"~standard\"].validate({\n * data: { name: \"Alice\" },\n * error: null,\n * });\n * ```\n */\nexport function OkSchema<TSchema extends StandardTypedV1>(\n\tinnerSchema: TSchema,\n): Ok<TSchema> {\n\tconst base = {\n\t\t\"~standard\": {\n\t\t\tversion: 1 as const,\n\t\t\tvendor: \"wellcrafted\",\n\t\t\ttypes: {\n\t\t\t\tinput: undefined as unknown as {\n\t\t\t\t\tdata: StandardTypedV1.InferInput<TSchema>;\n\t\t\t\t\terror: null;\n\t\t\t\t},\n\t\t\t\toutput: undefined as unknown as {\n\t\t\t\t\tdata: StandardTypedV1.InferOutput<TSchema>;\n\t\t\t\t\terror: null;\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t};\n\n\tif (hasValidate(innerSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).validate =\n\t\t\tcreateOkValidate(innerSchema);\n\t}\n\n\tif (hasJsonSchema(innerSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).jsonSchema =\n\t\t\tcreateOkJsonSchema(innerSchema);\n\t}\n\n\treturn base as Ok<TSchema>;\n}\n","import { FAILURES } from \"./failures.js\";\nimport {\n\thasJsonSchema,\n\thasValidate,\n\ttype StandardJSONSchemaV1,\n\ttype StandardSchemaV1,\n\ttype StandardTypedV1,\n} from \"./types.js\";\n\n/**\n * Output type for ResultSchema - creates a discriminated union of Ok and Err.\n *\n * Preserves the capabilities of the input schemas:\n * - If both inputs have validate, output has validate\n * - If both inputs have jsonSchema, output has jsonSchema\n */\nexport type Result<\n\tTDataSchema extends StandardTypedV1,\n\tTErrorSchema extends StandardTypedV1,\n> = {\n\treadonly \"~standard\": {\n\t\treadonly version: 1;\n\t\treadonly vendor: \"wellcrafted\";\n\t\treadonly types: {\n\t\t\treadonly input:\n\t\t\t\t| { data: StandardTypedV1.InferInput<TDataSchema>; error: null }\n\t\t\t\t| { data: null; error: StandardTypedV1.InferInput<TErrorSchema> };\n\t\t\treadonly output:\n\t\t\t\t| { data: StandardTypedV1.InferOutput<TDataSchema>; error: null }\n\t\t\t\t| { data: null; error: StandardTypedV1.InferOutput<TErrorSchema> };\n\t\t};\n\t} & (TDataSchema extends StandardSchemaV1\n\t\t? TErrorSchema extends StandardSchemaV1\n\t\t\t? {\n\t\t\t\t\treadonly validate: StandardSchemaV1.Props<\n\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\tdata: StandardTypedV1.InferInput<TDataSchema>;\n\t\t\t\t\t\t\t\terror: null;\n\t\t\t\t\t\t }\n\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\tdata: null;\n\t\t\t\t\t\t\t\terror: StandardTypedV1.InferInput<TErrorSchema>;\n\t\t\t\t\t\t },\n\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\tdata: StandardTypedV1.InferOutput<TDataSchema>;\n\t\t\t\t\t\t\t\terror: null;\n\t\t\t\t\t\t }\n\t\t\t\t\t\t| {\n\t\t\t\t\t\t\t\tdata: null;\n\t\t\t\t\t\t\t\terror: StandardTypedV1.InferOutput<TErrorSchema>;\n\t\t\t\t\t\t }\n\t\t\t\t\t>[\"validate\"];\n\t\t\t\t}\n\t\t\t: Record<string, never>\n\t\t: Record<string, never>) &\n\t\t(TDataSchema extends StandardJSONSchemaV1\n\t\t\t? TErrorSchema extends StandardJSONSchemaV1\n\t\t\t\t? { readonly jsonSchema: StandardJSONSchemaV1.Converter }\n\t\t\t\t: Record<string, never>\n\t\t\t: Record<string, never>);\n};\n\nfunction createResultValidate<\n\tTDataSchema extends StandardSchemaV1,\n\tTErrorSchema extends StandardSchemaV1,\n>(\n\tdataSchema: TDataSchema,\n\terrorSchema: TErrorSchema,\n): StandardSchemaV1.Props<\n\t| { data: StandardTypedV1.InferInput<TDataSchema>; error: null }\n\t| { data: null; error: StandardTypedV1.InferInput<TErrorSchema> },\n\t| { data: StandardTypedV1.InferOutput<TDataSchema>; error: null }\n\t| { data: null; error: StandardTypedV1.InferOutput<TErrorSchema> }\n>[\"validate\"] {\n\treturn (value: unknown) => {\n\t\tif (typeof value !== \"object\" || value === null) {\n\t\t\treturn FAILURES.EXPECTED_OBJECT;\n\t\t}\n\n\t\tif (!(\"data\" in value) || !(\"error\" in value)) {\n\t\t\treturn FAILURES.EXPECTED_DATA_ERROR_PROPS;\n\t\t}\n\n\t\tconst obj = value as { data: unknown; error: unknown };\n\n\t\tconst isOk = obj.error === null;\n\n\t\tif (isOk) {\n\t\t\tconst innerResult = dataSchema[\"~standard\"].validate(obj.data);\n\n\t\t\tif (innerResult instanceof Promise) {\n\t\t\t\treturn innerResult.then((r) => {\n\t\t\t\t\tif (r.issues) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tissues: r.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [\"data\", ...(issue.path || [])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\treturn { value: { data: r.value, error: null as null } };\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (innerResult.issues) {\n\t\t\t\treturn {\n\t\t\t\t\tissues: innerResult.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t\t...issue,\n\t\t\t\t\t\tpath: [\"data\", ...(issue.path || [])],\n\t\t\t\t\t})),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn { value: { data: innerResult.value, error: null as null } };\n\t\t}\n\n\t\tconst innerResult = errorSchema[\"~standard\"].validate(obj.error);\n\n\t\tif (innerResult instanceof Promise) {\n\t\t\treturn innerResult.then((r) => {\n\t\t\t\tif (r.issues) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tissues: r.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\tpath: [\"error\", ...(issue.path || [])],\n\t\t\t\t\t\t})),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn { value: { data: null as null, error: r.value } };\n\t\t\t});\n\t\t}\n\n\t\tif (innerResult.issues) {\n\t\t\treturn {\n\t\t\t\tissues: innerResult.issues.map((issue: StandardSchemaV1.Issue) => ({\n\t\t\t\t\t...issue,\n\t\t\t\t\tpath: [\"error\", ...(issue.path || [])],\n\t\t\t\t})),\n\t\t\t};\n\t\t}\n\n\t\treturn { value: { data: null as null, error: innerResult.value } };\n\t};\n}\n\nfunction createResultJsonSchema<\n\tTDataSchema extends StandardJSONSchemaV1,\n\tTErrorSchema extends StandardJSONSchemaV1,\n>(\n\tdataSchema: TDataSchema,\n\terrorSchema: TErrorSchema,\n): StandardJSONSchemaV1.Converter {\n\treturn {\n\t\tinput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\toneOf: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tdata: dataSchema[\"~standard\"].jsonSchema.input(options),\n\t\t\t\t\t\t\terror: { type: \"null\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\t\t\tadditionalProperties: false,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tdata: { type: \"null\" },\n\t\t\t\t\t\t\terror: errorSchema[\"~standard\"].jsonSchema.input(options),\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\t\t\tadditionalProperties: false,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t};\n\t\t},\n\t\toutput(options: StandardJSONSchemaV1.Options) {\n\t\t\treturn {\n\t\t\t\toneOf: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tdata: dataSchema[\"~standard\"].jsonSchema.output(options),\n\t\t\t\t\t\t\terror: { type: \"null\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\t\t\tadditionalProperties: false,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tdata: { type: \"null\" },\n\t\t\t\t\t\t\terror: errorSchema[\"~standard\"].jsonSchema.output(options),\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"data\", \"error\"],\n\t\t\t\t\t\tadditionalProperties: false,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t};\n\t\t},\n\t};\n}\n\n/**\n * Combines two Standard Schemas into a Result discriminated union schema.\n *\n * Takes a data schema for type T and an error schema for type E, returning a schema\n * for `{ data: T, error: null } | { data: null, error: E }`.\n *\n * Preserves the capabilities of the input schemas - if both have validate, output\n * has validate; if both have jsonSchema, output has jsonSchema.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n * import { ResultSchema } from \"wellcrafted/standard-schema\";\n *\n * const userSchema = z.object({ id: z.string(), name: z.string() });\n * const errorSchema = z.object({ code: z.string(), message: z.string() });\n * const resultSchema = ResultSchema(userSchema, errorSchema);\n *\n * // Validates Ok variant: { data: { id: \"1\", name: \"Alice\" }, error: null }\n * // Validates Err variant: { data: null, error: { code: \"NOT_FOUND\", message: \"...\" } }\n * const result = resultSchema[\"~standard\"].validate({\n * data: { id: \"1\", name: \"Alice\" },\n * error: null,\n * });\n * ```\n */\nexport function ResultSchema<\n\tTDataSchema extends StandardTypedV1,\n\tTErrorSchema extends StandardTypedV1,\n>(\n\tdataSchema: TDataSchema,\n\terrorSchema: TErrorSchema,\n): Result<TDataSchema, TErrorSchema> {\n\tconst base = {\n\t\t\"~standard\": {\n\t\t\tversion: 1 as const,\n\t\t\tvendor: \"wellcrafted\",\n\t\t\ttypes: {\n\t\t\t\tinput: undefined as unknown as\n\t\t\t\t\t| { data: StandardTypedV1.InferInput<TDataSchema>; error: null }\n\t\t\t\t\t| { data: null; error: StandardTypedV1.InferInput<TErrorSchema> },\n\t\t\t\toutput: undefined as unknown as\n\t\t\t\t\t| { data: StandardTypedV1.InferOutput<TDataSchema>; error: null }\n\t\t\t\t\t| { data: null; error: StandardTypedV1.InferOutput<TErrorSchema> },\n\t\t\t},\n\t\t},\n\t};\n\n\tif (hasValidate(dataSchema) && hasValidate(errorSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).validate =\n\t\t\tcreateResultValidate(dataSchema, errorSchema);\n\t}\n\n\tif (hasJsonSchema(dataSchema) && hasJsonSchema(errorSchema)) {\n\t\t(base[\"~standard\"] as Record<string, unknown>).jsonSchema =\n\t\t\tcreateResultJsonSchema(dataSchema, errorSchema);\n\t}\n\n\treturn base as Result<TDataSchema, TErrorSchema>;\n}\n"],"mappings":";AAEA,MAAa,WAAW;CACvB,iBAAiB,EAAE,QAAQ,CAAC,EAAE,SAAS,kBAAmB,CAAC,EAAE;CAC7D,2BAA2B,EAC1B,QAAQ,CAAC,EAAE,SAAS,qDAAsD,CAAC,EAC3E;CACD,qBAAqB,EACpB,QAAQ,CACP;EACC,SAAS;EACT,MAAM,CAAC,OAAQ;CACf,CACD,EACD;CACD,yBAAyB,EACxB,QAAQ,CACP;EACC,SAAS;EACT,MAAM,CAAC,OAAQ;CACf,CACD,EACD;AACD;;;;;;;AC0LD,SAAgB,YACfA,QACiC;AACjC,QACC,cAAc,OAAO,uBACd,OAAO,aAAa,aAAa;AAEzC;;;;AAKD,SAAgB,cACfA,QACqC;AACrC,QACC,gBAAgB,OAAO,uBAChB,OAAO,aAAa,eAAe,YAC1C,OAAO,aAAa,eAAe;AAEpC;;;;AC1LD,SAAS,kBACRC,aAIa;AACb,QAAO,CAACC,UAAmB;AAC1B,aAAW,UAAU,YAAY,UAAU,KAC1C,QAAO,SAAS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B,sBAAsB;GACtB,GACD;IACC,MAAM;IACN,YAAY;KACX,MAAM,EAAE,MAAM,OAAQ;KACtB,OAAO,YAAY,aAAa,WAAW,OAAO,QAAQ;IAC1D;IACD,UAAU,CAAC,QAAQ,OAAQ;IAC3B,sBAAsB;GACtB,CACD,EACD;EACD;CACD;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BD,SAAgB,aAIfL,YACAC,aACoC;CACpC,MAAM,OAAO,EACZ,aAAa;EACZ,SAAS;EACT,QAAQ;EACR,OAAO;GACN;GAGA;EAGA;CACD,EACD;AAED,KAAI,YAAY,WAAW,IAAI,YAAY,YAAY,CACtD,CAAC,KAAK,aAAyC,WAC9C,qBAAqB,YAAY,YAAY;AAG/C,KAAI,cAAc,WAAW,IAAI,cAAc,YAAY,CAC1D,CAAC,KAAK,aAAyC,aAC9C,uBAAuB,YAAY,YAAY;AAGjD,QAAO;AACP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wellcrafted",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Delightful TypeScript patterns for elegant, type-safe applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -24,15 +24,20 @@
|
|
|
24
24
|
"./query": {
|
|
25
25
|
"types": "./dist/query/index.d.ts",
|
|
26
26
|
"import": "./dist/query/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./standard-schema": {
|
|
29
|
+
"types": "./dist/standard-schema/index.d.ts",
|
|
30
|
+
"import": "./dist/standard-schema/index.js"
|
|
27
31
|
}
|
|
28
32
|
},
|
|
29
33
|
"scripts": {
|
|
30
34
|
"build": "tsdown",
|
|
31
35
|
"format": "biome format --write .",
|
|
32
36
|
"lint": "biome lint --write .",
|
|
33
|
-
"test": "
|
|
34
|
-
"test:watch": "
|
|
35
|
-
"
|
|
37
|
+
"test": "bun test",
|
|
38
|
+
"test:watch": "bun test --watch",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"release": "bun run build && changeset version && changeset publish"
|
|
36
41
|
},
|
|
37
42
|
"keywords": [
|
|
38
43
|
"typescript",
|
|
@@ -56,8 +61,11 @@
|
|
|
56
61
|
"@biomejs/biome": "^2.3.3",
|
|
57
62
|
"@changesets/cli": "^2.27.10",
|
|
58
63
|
"@tanstack/query-core": "^5.82.0",
|
|
64
|
+
"@types/bun": "^1.3.5",
|
|
65
|
+
"arktype": "^2.1.29",
|
|
59
66
|
"tsdown": "^0.12.5",
|
|
60
67
|
"typescript": "^5.8.3",
|
|
61
|
-
"
|
|
68
|
+
"valibot": "^1.2.0",
|
|
69
|
+
"zod": "^4.3.3"
|
|
62
70
|
}
|
|
63
71
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result-B1iWFqM9.js","names":["data: T","error: E","value: unknown","result: Result<T, E>","value: T | Result<T, E>"],"sources":["../src/result/result.ts"],"sourcesContent":["/**\n * Represents the successful outcome of an operation, encapsulating the success value.\n *\n * This is the 'Ok' variant of the `Result` type. It holds a `data` property\n * of type `T` (the success value) and an `error` property explicitly set to `null`,\n * signifying no error occurred.\n *\n * Use this type in conjunction with `Err<E>` and `Result<T, E>`.\n *\n * @template T - The type of the success value contained within.\n */\nexport type Ok<T> = { data: T; error: null };\n\n/**\n * Represents the failure outcome of an operation, encapsulating the error value.\n *\n * This is the 'Err' variant of the `Result` type. It holds an `error` property\n * of type `E` (the error value) and a `data` property explicitly set to `null`,\n * signifying that no success value is present due to the failure.\n *\n * Use this type in conjunction with `Ok<T>` and `Result<T, E>`.\n *\n * @template E - The type of the error value contained within.\n */\nexport type Err<E> = { error: E; data: null };\n\n/**\n * A type that represents the outcome of an operation that can either succeed or fail.\n *\n * `Result<T, E>` is a discriminated union type with two possible variants:\n * - `Ok<T>`: Represents a successful outcome, containing a `data` field with the success value of type `T`.\n * In this case, the `error` field is `null`.\n * - `Err<E>`: Represents a failure outcome, containing an `error` field with the error value of type `E`.\n * In this case, the `data` field is `null`.\n *\n * This type promotes explicit error handling by requiring developers to check\n * the variant of the `Result` before accessing its potential value or error.\n * It helps avoid runtime errors often associated with implicit error handling (e.g., relying on `try-catch` for all errors).\n *\n * @template T - The type of the success value if the operation is successful (held in `Ok<T>`).\n * @template E - The type of the error value if the operation fails (held in `Err<E>`).\n * @example\n * ```ts\n * function divide(numerator: number, denominator: number): Result<number, string> {\n * if (denominator === 0) {\n * return Err(\"Cannot divide by zero\");\n * }\n * return Ok(numerator / denominator);\n * }\n *\n * const result1 = divide(10, 2);\n * if (isOk(result1)) {\n * console.log(\"Success:\", result1.data); // Output: Success: 5\n * }\n *\n * const result2 = divide(10, 0);\n * if (isErr(result2)) {\n * console.error(\"Failure:\", result2.error); // Output: Failure: Cannot divide by zero\n * }\n * ```\n */\nexport type Result<T, E> = Ok<T> | Err<E>;\n\n/**\n * Constructs an `Ok<T>` variant, representing a successful outcome.\n *\n * This factory function creates the success variant of a `Result`.\n * It wraps the provided `data` (the success value) and ensures the `error` property is `null`.\n *\n * @template T - The type of the success value.\n * @param data - The success value to be wrapped in the `Ok` variant.\n * @returns An `Ok<T>` object with the provided data and `error` set to `null`.\n * @example\n * ```ts\n * const successfulResult = Ok(\"Operation completed successfully\");\n * // successfulResult is { data: \"Operation completed successfully\", error: null }\n * ```\n */\nexport const Ok = <T>(data: T): Ok<T> => ({ data, error: null });\n\n/**\n * Constructs an `Err<E>` variant, representing a failure outcome.\n *\n * This factory function creates the error variant of a `Result`.\n * It wraps the provided `error` (the error value) and ensures the `data` property is `null`.\n *\n * @template E - The type of the error value.\n * @param error - The error value to be wrapped in the `Err` variant. This value represents the specific error that occurred.\n * @returns An `Err<E>` object with the provided error and `data` set to `null`.\n * @example\n * ```ts\n * const failedResult = Err(new TypeError(\"Invalid input\"));\n * // failedResult is { error: TypeError(\"Invalid input\"), data: null }\n * ```\n */\nexport const Err = <E>(error: E): Err<E> => ({ error, data: null });\n\n/**\n * Utility type to extract the `Ok<T>` variant from a `Result<T, E>` union type.\n *\n * If `R` is a `Result` type (e.g., `Result<string, Error>`), this type will resolve\n * to `Ok<string>`. This can be useful in generic contexts or for type narrowing.\n *\n * @template R - The `Result<T, E>` union type from which to extract the `Ok<T>` variant.\n * Must extend `Result<unknown, unknown>`.\n */\nexport type ExtractOkFromResult<R extends Result<unknown, unknown>> = Extract<\n\tR,\n\t{ error: null }\n>;\n\n/**\n * Utility type to extract the `Err<E>` variant from a `Result<T, E>` union type.\n *\n * If `R` is a `Result` type (e.g., `Result<string, Error>`), this type will resolve\n * to `Err<Error>`. This can be useful in generic contexts or for type narrowing.\n *\n * @template R - The `Result<T, E>` union type from which to extract the `Err<E>` variant.\n * Must extend `Result<unknown, unknown>`.\n */\nexport type ExtractErrFromResult<R extends Result<unknown, unknown>> = Extract<\n\tR,\n\t{ data: null }\n>;\n\n/**\n * Utility type to extract the success value's type `T` from a `Result<T, E>` type.\n *\n * If `R` is an `Ok<T>` variant (or a `Result<T, E>` that could be an `Ok<T>`),\n * this type resolves to `T`. If `R` can only be an `Err<E>` variant, it resolves to `never`.\n * This is useful for obtaining the type of the `data` field when you know you have a success.\n *\n * @template R - The `Result<T, E>` type from which to extract the success value's type.\n * Must extend `Result<unknown, unknown>`.\n * @example\n * ```ts\n * type MyResult = Result<number, string>;\n * type SuccessValueType = UnwrapOk<MyResult>; // SuccessValueType is number\n *\n * type MyErrorResult = Err<string>;\n * type ErrorValueType = UnwrapOk<MyErrorResult>; // ErrorValueType is never\n * ```\n */\nexport type UnwrapOk<R extends Result<unknown, unknown>> = R extends Ok<infer U>\n\t? U\n\t: never;\n\n/**\n * Utility type to extract the error value's type `E` from a `Result<T, E>` type.\n *\n * If `R` is an `Err<E>` variant (or a `Result<T, E>` that could be an `Err<E>`),\n * this type resolves to `E`. If `R` can only be an `Ok<T>` variant, it resolves to `never`.\n * This is useful for obtaining the type of the `error` field when you know you have a failure.\n *\n * @template R - The `Result<T, E>` type from which to extract the error value's type.\n * Must extend `Result<unknown, unknown>`.\n * @example\n * ```ts\n * type MyResult = Result<number, string>;\n * type ErrorValueType = UnwrapErr<MyResult>; // ErrorValueType is string\n *\n * type MySuccessResult = Ok<number>;\n * type SuccessValueType = UnwrapErr<MySuccessResult>; // SuccessValueType is never\n * ```\n */\nexport type UnwrapErr<R extends Result<unknown, unknown>> = R extends Err<\n\tinfer E\n>\n\t? E\n\t: never;\n\n/**\n * Type guard to runtime check if an unknown value is a valid `Result<T, E>`.\n *\n * A value is considered a valid `Result` if:\n * 1. It is a non-null object.\n * 2. It has both `data` and `error` properties.\n * 3. At least one of the `data` or `error` channels is `null`. Both being `null` represents `Ok(null)`.\n *\n * This function does not validate the types of `data` or `error` beyond `null` checks.\n *\n * @template T - The expected type of the success value if the value is an `Ok` variant (defaults to `unknown`).\n * @template E - The expected type of the error value if the value is an `Err` variant (defaults to `unknown`).\n * @param value - The value to check.\n * @returns `true` if the value conforms to the `Result` structure, `false` otherwise.\n * If `true`, TypeScript's type system will narrow `value` to `Result<T, E>`.\n * @example\n * ```ts\n * declare const someValue: unknown;\n *\n * if (isResult<string, Error>(someValue)) {\n * // someValue is now typed as Result<string, Error>\n * if (isOk(someValue)) {\n * console.log(someValue.data); // string\n * } else {\n * console.error(someValue.error); // Error\n * }\n * }\n * ```\n */\nexport function isResult<T = unknown, E = unknown>(\n\tvalue: unknown,\n): value is Result<T, E> {\n\tconst isNonNullObject = typeof value === \"object\" && value !== null;\n\tif (!isNonNullObject) return false;\n\n\tconst hasDataProperty = \"data\" in value;\n\tconst hasErrorProperty = \"error\" in value;\n\tif (!hasDataProperty || !hasErrorProperty) return false;\n\n\tconst isNeitherNull = value.data !== null && value.error !== null;\n\tif (isNeitherNull) return false;\n\n\t// At least one channel is null (valid Result)\n\treturn true;\n}\n\n/**\n * Type guard to runtime check if a `Result<T, E>` is an `Ok<T>` variant.\n *\n * This function narrows the type of a `Result` to `Ok<T>` if it represents a successful outcome.\n * An `Ok<T>` variant is identified by its `error` property being `null`.\n *\n * @template T - The success value type.\n * @template E - The error value type.\n * @param result - The `Result<T, E>` to check.\n * @returns `true` if the `result` is an `Ok<T>` variant, `false` otherwise.\n * If `true`, TypeScript's type system will narrow `result` to `Ok<T>`.\n * @example\n * ```ts\n * declare const myResult: Result<number, string>;\n *\n * if (isOk(myResult)) {\n * // myResult is now typed as Ok<number>\n * console.log(\"Success value:\", myResult.data); // myResult.data is number\n * }\n * ```\n */\nexport function isOk<T, E>(result: Result<T, E>): result is Ok<T> {\n\treturn result.error === null;\n}\n\n/**\n * Type guard to runtime check if a `Result<T, E>` is an `Err<E>` variant.\n *\n * This function narrows the type of a `Result` to `Err<E>` if it represents a failure outcome.\n * An `Err<E>` variant is identified by its `error` property being non-`null` (and thus `data` being `null`).\n *\n * @template T - The success value type.\n * @template E - The error value type.\n * @param result - The `Result<T, E>` to check.\n * @returns `true` if the `result` is an `Err<E>` variant, `false` otherwise.\n * If `true`, TypeScript's type system will narrow `result` to `Err<E>`.\n * @example\n * ```ts\n * declare const myResult: Result<number, string>;\n *\n * if (isErr(myResult)) {\n * // myResult is now typed as Err<string>\n * console.error(\"Error value:\", myResult.error); // myResult.error is string\n * }\n * ```\n */\nexport function isErr<T, E>(result: Result<T, E>): result is Err<E> {\n\treturn result.error !== null; // Equivalent to result.data === null\n}\n\n/**\n * Executes a synchronous operation and wraps its outcome in a Result type.\n *\n * This function attempts to execute the `try` operation:\n * - If the `try` operation completes successfully, its return value is wrapped in an `Ok<T>` variant.\n * - If the `try` operation throws an exception, the caught exception (of type `unknown`) is passed to\n * the `catch` function, which transforms it into either an `Ok<T>` (recovery) or `Err<E>` (propagation).\n *\n * The return type is automatically narrowed based on what your catch function returns:\n * - If catch always returns `Ok<T>`, the function returns `Ok<T>` (guaranteed success)\n * - If catch always returns `Err<E>`, the function returns `Result<T, E>` (may succeed or fail)\n * - If catch can return either `Ok<T>` or `Err<E>`, the function returns `Result<T, E>` (conditional recovery)\n *\n * @template T - The success value type\n * @template E - The error value type (when catch can return errors)\n * @param options - Configuration object\n * @param options.try - The operation to execute\n * @param options.catch - Error handler that transforms caught exceptions into either `Ok<T>` (recovery) or `Err<E>` (propagation)\n * @returns `Ok<T>` if catch always returns Ok (recovery), otherwise `Result<T, E>` (propagation or conditional recovery)\n *\n * @example\n * ```ts\n * // Returns Ok<string> - guaranteed success since catch always returns Ok\n * const alwaysOk = trySync({\n * try: () => JSON.parse(input),\n * catch: () => Ok(\"fallback\") // Always Ok<T>\n * });\n *\n * // Returns Result<object, string> - may fail since catch always returns Err\n * const mayFail = trySync({\n * try: () => JSON.parse(input),\n * catch: (err) => Err(\"Parse failed\") // Returns Err<E>\n * });\n *\n * // Returns Result<void, MyError> - conditional recovery based on error type\n * const conditional = trySync({\n * try: () => riskyOperation(),\n * catch: (err) => {\n * if (isRecoverable(err)) return Ok(undefined);\n * return MyErr({ message: \"Unrecoverable\" });\n * }\n * });\n * ```\n */\nexport function trySync<T>(options: {\n\ttry: () => T;\n\tcatch: (error: unknown) => Ok<T>;\n}): Ok<T>;\n\nexport function trySync<T, E>(options: {\n\ttry: () => T;\n\tcatch: (error: unknown) => Err<E>;\n}): Result<T, E>;\n\nexport function trySync<T, E>(options: {\n\ttry: () => T;\n\tcatch: (error: unknown) => Ok<T> | Err<E>;\n}): Result<T, E>;\n\nexport function trySync<T, E>({\n\ttry: operation,\n\tcatch: catchFn,\n}: {\n\ttry: () => T;\n\tcatch: (error: unknown) => Ok<T> | Err<E>;\n}): Ok<T> | Result<T, E> {\n\ttry {\n\t\tconst data = operation();\n\t\treturn Ok(data);\n\t} catch (error) {\n\t\treturn catchFn(error);\n\t}\n}\n\n/**\n * Executes an asynchronous operation and wraps its outcome in a Promise<Result>.\n *\n * This function attempts to execute the `try` operation:\n * - If the `try` operation resolves successfully, its resolved value is wrapped in an `Ok<T>` variant.\n * - If the `try` operation rejects or throws an exception, the caught error (of type `unknown`) is passed to\n * the `catch` function, which transforms it into either an `Ok<T>` (recovery) or `Err<E>` (propagation).\n *\n * The return type is automatically narrowed based on what your catch function returns:\n * - If catch always returns `Ok<T>`, the function returns `Promise<Ok<T>>` (guaranteed success)\n * - If catch always returns `Err<E>`, the function returns `Promise<Result<T, E>>` (may succeed or fail)\n * - If catch can return either `Ok<T>` or `Err<E>`, the function returns `Promise<Result<T, E>>` (conditional recovery)\n *\n * @template T - The success value type\n * @template E - The error value type (when catch can return errors)\n * @param options - Configuration object\n * @param options.try - The async operation to execute\n * @param options.catch - Error handler that transforms caught exceptions/rejections into either `Ok<T>` (recovery) or `Err<E>` (propagation)\n * @returns `Promise<Ok<T>>` if catch always returns Ok (recovery), otherwise `Promise<Result<T, E>>` (propagation or conditional recovery)\n *\n * @example\n * ```ts\n * // Returns Promise<Ok<Response>> - guaranteed success since catch always returns Ok\n * const alwaysOk = tryAsync({\n * try: async () => fetch(url),\n * catch: () => Ok(new Response()) // Always Ok<T>\n * });\n *\n * // Returns Promise<Result<Response, Error>> - may fail since catch always returns Err\n * const mayFail = tryAsync({\n * try: async () => fetch(url),\n * catch: (err) => Err(new Error(\"Fetch failed\")) // Returns Err<E>\n * });\n *\n * // Returns Promise<Result<void, BlobError>> - conditional recovery based on error type\n * const conditional = await tryAsync({\n * try: async () => {\n * await deleteFile(filename);\n * },\n * catch: (err) => {\n * if ((err as { name?: string }).name === 'NotFoundError') {\n * return Ok(undefined); // Already deleted, that's fine\n * }\n * return BlobErr({ message: \"Delete failed\" });\n * }\n * });\n * ```\n */\nexport async function tryAsync<T>(options: {\n\ttry: () => Promise<T>;\n\tcatch: (error: unknown) => Ok<T>;\n}): Promise<Ok<T>>;\n\nexport async function tryAsync<T, E>(options: {\n\ttry: () => Promise<T>;\n\tcatch: (error: unknown) => Err<E>;\n}): Promise<Result<T, E>>;\n\nexport async function tryAsync<T, E>(options: {\n\ttry: () => Promise<T>;\n\tcatch: (error: unknown) => Ok<T> | Err<E>;\n}): Promise<Result<T, E>>;\n\nexport async function tryAsync<T, E>({\n\ttry: operation,\n\tcatch: catchFn,\n}: {\n\ttry: () => Promise<T>;\n\tcatch: (error: unknown) => Ok<T> | Err<E>;\n}): Promise<Ok<T> | Result<T, E>> {\n\ttry {\n\t\tconst data = await operation();\n\t\treturn Ok(data);\n\t} catch (error) {\n\t\treturn catchFn(error);\n\t}\n}\n\n/**\n * Resolves a value that may or may not be wrapped in a `Result`, returning the final value.\n *\n * This function handles the common pattern where a value might be a `Result<T, E>` or a plain `T`:\n * - If `value` is an `Ok<T>` variant, returns the contained success value.\n * - If `value` is an `Err<E>` variant, throws the contained error value.\n * - If `value` is not a `Result` (i.e., it's already a plain value of type `T`),\n * returns it as-is.\n *\n * This is useful when working with APIs that might return either direct values or Results,\n * allowing you to normalize them to the actual value or propagate errors via throwing.\n *\n * Use `resolve` when the input might or might not be a Result.\n * Use `unwrap` when you know the input is definitely a Result.\n *\n * @template T - The type of the success value (if `value` is `Ok<T>`) or the type of the plain value.\n * @template E - The type of the error value (if `value` is `Err<E>`).\n * @param value - The value to resolve. Can be a `Result<T, E>` or a plain value of type `T`.\n * @returns The final value of type `T` if `value` is `Ok<T>` or if `value` is already a plain `T`.\n * @throws The error value `E` if `value` is an `Err<E>` variant.\n *\n * @example\n * ```ts\n * // Example with an Ok variant\n * const okResult = Ok(\"success data\");\n * const resolved = resolve(okResult); // \"success data\"\n *\n * // Example with an Err variant\n * const errResult = Err(new Error(\"failure\"));\n * try {\n * resolve(errResult);\n * } catch (e) {\n * console.error(e.message); // \"failure\"\n * }\n *\n * // Example with a plain value\n * const plainValue = \"plain data\";\n * const resolved = resolve(plainValue); // \"plain data\"\n *\n * // Example with a function that might return Result or plain value\n * declare function mightReturnResult(): string | Result<string, Error>;\n * const outcome = mightReturnResult();\n * try {\n * const finalValue = resolve(outcome); // handles both cases\n * console.log(\"Final value:\", finalValue);\n * } catch (e) {\n * console.error(\"Operation failed:\", e);\n * }\n * ```\n */\n/**\n * Unwraps a `Result<T, E>`, returning the success value or throwing the error.\n *\n * This function extracts the data from a `Result`:\n * - If the `Result` is an `Ok<T>` variant, returns the contained success value of type `T`.\n * - If the `Result` is an `Err<E>` variant, throws the contained error value of type `E`.\n *\n * Unlike `resolve`, this function expects the input to always be a `Result` type,\n * making it more direct for cases where you know you're working with a `Result`.\n *\n * @template T - The type of the success value contained in the `Ok<T>` variant.\n * @template E - The type of the error value contained in the `Err<E>` variant.\n * @param result - The `Result<T, E>` to unwrap.\n * @returns The success value of type `T` if the `Result` is `Ok<T>`.\n * @throws The error value of type `E` if the `Result` is `Err<E>`.\n *\n * @example\n * ```ts\n * // Example with an Ok variant\n * const okResult = Ok(\"success data\");\n * const value = unwrap(okResult); // \"success data\"\n *\n * // Example with an Err variant\n * const errResult = Err(new Error(\"something went wrong\"));\n * try {\n * unwrap(errResult);\n * } catch (error) {\n * console.error(error.message); // \"something went wrong\"\n * }\n *\n * // Usage in a function that returns Result\n * function divide(a: number, b: number): Result<number, string> {\n * if (b === 0) return Err(\"Division by zero\");\n * return Ok(a / b);\n * }\n *\n * try {\n * const result = unwrap(divide(10, 2)); // 5\n * console.log(\"Result:\", result);\n * } catch (error) {\n * console.error(\"Division failed:\", error);\n * }\n * ```\n */\nexport function unwrap<T, E>(result: Result<T, E>): T {\n\tif (isOk(result)) {\n\t\treturn result.data;\n\t}\n\tthrow result.error;\n}\n\nexport function resolve<T, E>(value: T | Result<T, E>): T {\n\tif (isResult<T, E>(value)) {\n\t\tif (isOk(value)) {\n\t\t\treturn value.data;\n\t\t}\n\t\t// If it's a Result and not Ok, it must be Err.\n\t\t// The type guard isResult<T,E>(value) and isOk(value) already refine the type.\n\t\t// So, 'value' here is known to be Err<E>.\n\t\tthrow value.error;\n\t}\n\n\t// If it's not a Result type, return the value as-is.\n\t// 'value' here is known to be of type T.\n\treturn value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA8EA,MAAa,KAAK,CAAIA,UAAoB;CAAE;CAAM,OAAO;AAAM;;;;;;;;;;;;;;;;AAiB/D,MAAa,MAAM,CAAIC,WAAsB;CAAE;CAAO,MA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