@kellanjs/actioncraft
Version:
Fluent, type-safe builder for Next.js server actions.
94 lines • 3 kB
JavaScript
// ============================================================================
// Core Result Type
// ============================================================================
export function ok(valueOrActionId, actionId) {
// Handle overloads: ok() vs ok(value) vs ok(value, actionId)
if (arguments.length === 0) {
// ok() - no arguments
return { type: "ok", value: undefined, __ac_id: "unknown" };
}
else if (arguments.length === 1) {
// ok(value) - single argument treated as value (including strings)
return { type: "ok", value: valueOrActionId, __ac_id: "unknown" };
}
else {
// ok(value, actionId) - two arguments
return {
type: "ok",
value: valueOrActionId,
__ac_id: actionId || "unknown",
};
}
}
export function err(errorOrActionId, actionId) {
// Handle overloads: err() vs err(error) vs err(error, actionId)
if (arguments.length === 0) {
// err() - no arguments
return { type: "err", error: undefined, __ac_id: "unknown" };
}
else if (arguments.length === 1) {
// err(error) - single argument treated as error (including strings)
return { type: "err", error: errorOrActionId, __ac_id: "unknown" };
}
else {
// err(error, actionId) - two arguments
return {
type: "err",
error: errorOrActionId,
__ac_id: actionId || "unknown",
};
}
}
/**
* Tests if a Result is successful.
* @param result The Result to check
* @returns true if result is Ok, false if Err
*/
export function isOk(result) {
return result.type === "ok";
}
/**
* Tests if a Result is failed.
* @param result The Result to check
* @returns true if result is Err, false if Ok
*/
export function isErr(result) {
return result.type === "err";
}
/**
* Tests if an unknown value is a valid Result.
* @param value The value to check
* @returns true if value is a Result (Ok or Err), false otherwise
*/
export function isResult(value) {
return (typeof value === "object" &&
value !== null &&
"type" in value &&
((value.type === "ok" && "value" in value) ||
(value.type === "err" && "error" in value)));
}
/**
* Tests if an unknown value is a valid Ok Result.
* @param value The value to check
* @returns true if value is an Ok Result, false otherwise
*/
export function isResultOk(value) {
return (typeof value === "object" &&
value !== null &&
"type" in value &&
value.type === "ok" &&
"value" in value);
}
/**
* Tests if an unknown value is a valid Err Result.
* @param value The value to check
* @returns true if value is an Err Result, false otherwise
*/
export function isResultErr(value) {
return (typeof value === "object" &&
value !== null &&
"type" in value &&
value.type === "err" &&
"error" in value);
}
//# sourceMappingURL=result.js.map