awaitly 1.34.0 → 2.0.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.
Files changed (241) hide show
  1. package/dist/{duration.d.ts → di-BDlT7InM.d.cts} +15 -1
  2. package/dist/{duration.d.cts → di-BbFFfO8y.d.ts} +15 -1
  3. package/dist/errors-DtXvrCiO.d.cts +708 -0
  4. package/dist/errors-DtXvrCiO.d.ts +708 -0
  5. package/dist/index.cjs +4594 -1
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +1970 -141
  8. package/dist/index.d.ts +1970 -141
  9. package/dist/index.js +4398 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/result.cjs +641 -1
  12. package/dist/result.cjs.map +1 -1
  13. package/dist/result.d.cts +35 -5
  14. package/dist/result.d.ts +35 -5
  15. package/dist/result.js +561 -1
  16. package/dist/result.js.map +1 -1
  17. package/dist/testing.cjs +4202 -8
  18. package/dist/testing.cjs.map +1 -1
  19. package/dist/testing.d.cts +2 -6
  20. package/dist/testing.d.ts +2 -6
  21. package/dist/testing.js +4154 -8
  22. package/dist/testing.js.map +1 -1
  23. package/dist/{run-entry-D2MmJFj9.d.cts → types-B8NfNRGX.d.ts} +1152 -1499
  24. package/dist/{run-entry-Dduz-is2.d.ts → types-BZ2f4MRR.d.cts} +1152 -1499
  25. package/dist/workflow.cjs +7096 -6
  26. package/dist/workflow.cjs.map +1 -1
  27. package/dist/workflow.d.cts +3346 -22
  28. package/dist/workflow.d.ts +3346 -22
  29. package/dist/workflow.js +6929 -6
  30. package/dist/workflow.js.map +1 -1
  31. package/package.json +13 -178
  32. package/dist/adapters.cjs +0 -7
  33. package/dist/adapters.cjs.map +0 -1
  34. package/dist/adapters.d.cts +0 -179
  35. package/dist/adapters.d.ts +0 -179
  36. package/dist/adapters.js +0 -7
  37. package/dist/adapters.js.map +0 -1
  38. package/dist/batch.cjs +0 -7
  39. package/dist/batch.cjs.map +0 -1
  40. package/dist/batch.d.cts +0 -200
  41. package/dist/batch.d.ts +0 -200
  42. package/dist/batch.js +0 -7
  43. package/dist/batch.js.map +0 -1
  44. package/dist/bind-deps.cjs +0 -2
  45. package/dist/bind-deps.cjs.map +0 -1
  46. package/dist/bind-deps.d.cts +0 -28
  47. package/dist/bind-deps.d.ts +0 -28
  48. package/dist/bind-deps.js +0 -2
  49. package/dist/bind-deps.js.map +0 -1
  50. package/dist/cache.cjs +0 -2
  51. package/dist/cache.cjs.map +0 -1
  52. package/dist/cache.d.cts +0 -269
  53. package/dist/cache.d.ts +0 -269
  54. package/dist/cache.js +0 -2
  55. package/dist/cache.js.map +0 -1
  56. package/dist/circuit-breaker.cjs +0 -7
  57. package/dist/circuit-breaker.cjs.map +0 -1
  58. package/dist/circuit-breaker.d.cts +0 -211
  59. package/dist/circuit-breaker.d.ts +0 -211
  60. package/dist/circuit-breaker.js +0 -7
  61. package/dist/circuit-breaker.js.map +0 -1
  62. package/dist/conditional.cjs +0 -2
  63. package/dist/conditional.cjs.map +0 -1
  64. package/dist/conditional.d.cts +0 -252
  65. package/dist/conditional.d.ts +0 -252
  66. package/dist/conditional.js +0 -2
  67. package/dist/conditional.js.map +0 -1
  68. package/dist/core.cjs +0 -7
  69. package/dist/core.cjs.map +0 -1
  70. package/dist/core.d.cts +0 -5
  71. package/dist/core.d.ts +0 -5
  72. package/dist/core.js +0 -7
  73. package/dist/core.js.map +0 -1
  74. package/dist/di-COl5oFnR.d.cts +0 -15
  75. package/dist/di-CyDj_JyZ.d.ts +0 -15
  76. package/dist/diagnostics.cjs +0 -8
  77. package/dist/diagnostics.cjs.map +0 -1
  78. package/dist/diagnostics.d.cts +0 -68
  79. package/dist/diagnostics.d.ts +0 -68
  80. package/dist/diagnostics.js +0 -8
  81. package/dist/diagnostics.js.map +0 -1
  82. package/dist/durable.cjs +0 -11
  83. package/dist/durable.cjs.map +0 -1
  84. package/dist/durable.d.cts +0 -9
  85. package/dist/durable.d.ts +0 -9
  86. package/dist/durable.js +0 -11
  87. package/dist/durable.js.map +0 -1
  88. package/dist/duration.cjs +0 -2
  89. package/dist/duration.cjs.map +0 -1
  90. package/dist/duration.js +0 -2
  91. package/dist/duration.js.map +0 -1
  92. package/dist/engine.cjs +0 -11
  93. package/dist/engine.cjs.map +0 -1
  94. package/dist/engine.d.cts +0 -115
  95. package/dist/engine.d.ts +0 -115
  96. package/dist/engine.js +0 -11
  97. package/dist/engine.js.map +0 -1
  98. package/dist/errors.cjs +0 -2
  99. package/dist/errors.cjs.map +0 -1
  100. package/dist/errors.d.cts +0 -361
  101. package/dist/errors.d.ts +0 -361
  102. package/dist/errors.js +0 -2
  103. package/dist/errors.js.map +0 -1
  104. package/dist/fetch.cjs +0 -7
  105. package/dist/fetch.cjs.map +0 -1
  106. package/dist/fetch.d.cts +0 -86
  107. package/dist/fetch.d.ts +0 -86
  108. package/dist/fetch.js +0 -7
  109. package/dist/fetch.js.map +0 -1
  110. package/dist/flow.cjs +0 -7
  111. package/dist/flow.cjs.map +0 -1
  112. package/dist/flow.d.cts +0 -163
  113. package/dist/flow.d.ts +0 -163
  114. package/dist/flow.js +0 -7
  115. package/dist/flow.js.map +0 -1
  116. package/dist/functional.cjs +0 -2
  117. package/dist/functional.cjs.map +0 -1
  118. package/dist/functional.d.cts +0 -444
  119. package/dist/functional.d.ts +0 -444
  120. package/dist/functional.js +0 -2
  121. package/dist/functional.js.map +0 -1
  122. package/dist/guards-BodHXLzX.d.cts +0 -72
  123. package/dist/guards-CeWoQ8fn.d.ts +0 -72
  124. package/dist/hitl-BPE_1UiM.d.cts +0 -468
  125. package/dist/hitl-byp570uC.d.ts +0 -468
  126. package/dist/hitl.cjs +0 -7
  127. package/dist/hitl.cjs.map +0 -1
  128. package/dist/hitl.d.cts +0 -442
  129. package/dist/hitl.d.ts +0 -442
  130. package/dist/hitl.js +0 -7
  131. package/dist/hitl.js.map +0 -1
  132. package/dist/index-BYT3amEz.d.ts +0 -417
  133. package/dist/index-C_ak66jy.d.cts +0 -417
  134. package/dist/match-entry-DjI2bLpD.d.cts +0 -209
  135. package/dist/match-entry-DjI2bLpD.d.ts +0 -209
  136. package/dist/match.cjs +0 -2
  137. package/dist/match.cjs.map +0 -1
  138. package/dist/match.d.cts +0 -1
  139. package/dist/match.d.ts +0 -1
  140. package/dist/match.js +0 -2
  141. package/dist/match.js.map +0 -1
  142. package/dist/otel.cjs +0 -2
  143. package/dist/otel.cjs.map +0 -1
  144. package/dist/otel.d.cts +0 -188
  145. package/dist/otel.d.ts +0 -188
  146. package/dist/otel.js +0 -2
  147. package/dist/otel.js.map +0 -1
  148. package/dist/persistence-entry-DOMx3woy.d.ts +0 -822
  149. package/dist/persistence-entry-ymCA4iDu.d.cts +0 -822
  150. package/dist/persistence.cjs +0 -2
  151. package/dist/persistence.cjs.map +0 -1
  152. package/dist/persistence.d.cts +0 -7
  153. package/dist/persistence.d.ts +0 -7
  154. package/dist/persistence.js +0 -2
  155. package/dist/persistence.js.map +0 -1
  156. package/dist/policies.cjs +0 -2
  157. package/dist/policies.cjs.map +0 -1
  158. package/dist/policies.d.cts +0 -379
  159. package/dist/policies.d.ts +0 -379
  160. package/dist/policies.js +0 -2
  161. package/dist/policies.js.map +0 -1
  162. package/dist/ratelimit.cjs +0 -7
  163. package/dist/ratelimit.cjs.map +0 -1
  164. package/dist/ratelimit.d.cts +0 -458
  165. package/dist/ratelimit.d.ts +0 -458
  166. package/dist/ratelimit.js +0 -7
  167. package/dist/ratelimit.js.map +0 -1
  168. package/dist/reliability.cjs +0 -11
  169. package/dist/reliability.cjs.map +0 -1
  170. package/dist/reliability.d.cts +0 -11
  171. package/dist/reliability.d.ts +0 -11
  172. package/dist/reliability.js +0 -11
  173. package/dist/reliability.js.map +0 -1
  174. package/dist/resolver.cjs +0 -7
  175. package/dist/resolver.cjs.map +0 -1
  176. package/dist/resolver.d.cts +0 -68
  177. package/dist/resolver.d.ts +0 -68
  178. package/dist/resolver.js +0 -7
  179. package/dist/resolver.js.map +0 -1
  180. package/dist/resource.cjs +0 -7
  181. package/dist/resource.cjs.map +0 -1
  182. package/dist/resource.d.cts +0 -174
  183. package/dist/resource.d.ts +0 -174
  184. package/dist/resource.js +0 -7
  185. package/dist/resource.js.map +0 -1
  186. package/dist/result/retry.cjs +0 -2
  187. package/dist/result/retry.cjs.map +0 -1
  188. package/dist/result/retry.d.cts +0 -70
  189. package/dist/result/retry.d.ts +0 -70
  190. package/dist/result/retry.js +0 -2
  191. package/dist/result/retry.js.map +0 -1
  192. package/dist/retry.cjs +0 -2
  193. package/dist/retry.cjs.map +0 -1
  194. package/dist/retry.d.cts +0 -388
  195. package/dist/retry.d.ts +0 -388
  196. package/dist/retry.js +0 -2
  197. package/dist/retry.js.map +0 -1
  198. package/dist/run.cjs +0 -7
  199. package/dist/run.cjs.map +0 -1
  200. package/dist/run.d.cts +0 -4
  201. package/dist/run.d.ts +0 -4
  202. package/dist/run.js +0 -7
  203. package/dist/run.js.map +0 -1
  204. package/dist/saga.cjs +0 -11
  205. package/dist/saga.cjs.map +0 -1
  206. package/dist/saga.d.cts +0 -164
  207. package/dist/saga.d.ts +0 -164
  208. package/dist/saga.js +0 -11
  209. package/dist/saga.js.map +0 -1
  210. package/dist/singleflight.cjs +0 -2
  211. package/dist/singleflight.cjs.map +0 -1
  212. package/dist/singleflight.d.cts +0 -145
  213. package/dist/singleflight.d.ts +0 -145
  214. package/dist/singleflight.js +0 -2
  215. package/dist/singleflight.js.map +0 -1
  216. package/dist/slugs.cjs +0 -2
  217. package/dist/slugs.cjs.map +0 -1
  218. package/dist/slugs.d.cts +0 -67
  219. package/dist/slugs.d.ts +0 -67
  220. package/dist/slugs.js +0 -2
  221. package/dist/slugs.js.map +0 -1
  222. package/dist/streaming.cjs +0 -9
  223. package/dist/streaming.cjs.map +0 -1
  224. package/dist/streaming.d.cts +0 -596
  225. package/dist/streaming.d.ts +0 -596
  226. package/dist/streaming.js +0 -9
  227. package/dist/streaming.js.map +0 -1
  228. package/dist/tagged-error.cjs +0 -2
  229. package/dist/tagged-error.cjs.map +0 -1
  230. package/dist/tagged-error.d.cts +0 -275
  231. package/dist/tagged-error.d.ts +0 -275
  232. package/dist/tagged-error.js +0 -2
  233. package/dist/tagged-error.js.map +0 -1
  234. package/dist/types-DQmzO9f4.d.ts +0 -323
  235. package/dist/types-qBUOYi-4.d.cts +0 -323
  236. package/dist/webhook.cjs +0 -7
  237. package/dist/webhook.cjs.map +0 -1
  238. package/dist/webhook.d.cts +0 -499
  239. package/dist/webhook.d.ts +0 -499
  240. package/dist/webhook.js +0 -7
  241. package/dist/webhook.js.map +0 -1
@@ -1,2 +0,0 @@
1
- "use strict";var d=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var w=(o,n)=>{for(var t in n)d(o,t,{get:n[t],enumerable:!0})},x=(o,n,t,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of f(n))!O.call(o,i)&&i!==t&&d(o,i,{get:()=>n[i],enumerable:!(e=c(n,i))||e.enumerable});return o};var y=o=>x(d({},"__esModule",{value:!0}),o);var k={};w(k,{createConditionalHelpers:()=>u,unless:()=>s,unlessOr:()=>C,when:()=>a,whenOr:()=>r});module.exports=y(k);function l(){return`decision_${Date.now()}_${Math.random().toString(36).slice(2,8)}`}function p(o,n,t){if(!o?.onEvent)return;let e={type:"step_skipped",workflowId:o.workflowId,stepKey:n?.key,name:n?.name,reason:n?.reason,decisionId:t,ts:Date.now()},i=e.context!==void 0||o.context===void 0?e:{...e,context:o.context};o.onEvent(i)}function a(o,n,t,e){if(o)return n();let i=l();p(e,t,i)}function s(o,n,t,e){return a(!o,n,t,e)}function r(o,n,t,e,i){if(o)return n();let T=l();return p(i,e,T),t}function C(o,n,t,e,i){return r(!o,n,t,e,i)}function u(o){return{when:(n,t,e)=>a(n,t,e,o),unless:(n,t,e)=>s(n,t,e,o),whenOr:(n,t,e,i)=>r(n,t,e,i,o),unlessOr:(n,t,e,i)=>C(n,t,e,i,o)}}0&&(module.exports={createConditionalHelpers,unless,unlessOr,when,whenOr});
2
- //# sourceMappingURL=conditional.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/conditional-entry.ts","../src/conditional.ts"],"sourcesContent":["/**\n * awaitly/conditional\n *\n * Conditional execution helpers: when/unless for cleaner branching logic.\n *\n * @example\n * ```typescript\n * import { when, unless } from 'awaitly/conditional';\n *\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n * await when(user.isAdmin, () => step(logAdminAccess(user)));\n * return user;\n * });\n * ```\n */\n\nexport {\n // Types\n type ConditionalOptions,\n type ConditionalContext,\n\n // Functions\n when,\n unless,\n whenOr,\n unlessOr,\n createConditionalHelpers,\n} from \"./conditional\";\n","/**\n * awaitly/conditional\n *\n * Conditional step execution helpers for workflows.\n * These helpers allow you to conditionally execute steps based on runtime conditions,\n * with proper event emission for skipped steps.\n */\n\nimport type { WorkflowEvent } from \"./core\";\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * Options for conditional execution.\n */\nexport type ConditionalOptions = {\n /**\n * Human-readable name for the conditional step.\n * Used in step_skipped events for debugging and visualization.\n */\n name?: string;\n\n /**\n * Stable identity key for the conditional step.\n * Used in step_skipped events for tracking and visualization.\n */\n key?: string;\n\n /**\n * Optional reason explaining why the step was skipped.\n * Included in step_skipped events.\n */\n reason?: string;\n};\n\n/**\n * Context for conditional execution, used to emit events.\n */\nexport type ConditionalContext<C = unknown> = {\n /**\n * The workflow ID for event emission.\n */\n workflowId: string;\n\n /**\n * Event emitter function.\n */\n onEvent?: (event: WorkflowEvent<unknown, C>) => void;\n\n /**\n * Optional context value to include in emitted events.\n * When provided, this context is automatically added to step_skipped events.\n */\n context?: C;\n};\n\n/**\n * Type for operations that can be either sync or async.\n */\ntype MaybeAsync<T> = T | Promise<T>;\n\n/**\n * Type for the operation function passed to conditional helpers.\n */\ntype Operation<T> = () => MaybeAsync<T>;\n\n// =============================================================================\n// Internal Helpers\n// =============================================================================\n\n/**\n * Generate a unique decision ID for tracking conditional decisions.\n * @internal\n */\nfunction generateDecisionId(): string {\n return `decision_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;\n}\n\n/**\n * Emit a step_skipped event.\n * @internal\n */\nfunction emitSkipped<C = unknown>(\n ctx: ConditionalContext<C> | undefined,\n options: ConditionalOptions | undefined,\n decisionId: string\n): void {\n if (!ctx?.onEvent) return;\n\n // Create event with context if provided (similar to emitEvent logic)\n const event: WorkflowEvent<unknown, C> = {\n type: \"step_skipped\",\n workflowId: ctx.workflowId,\n stepKey: options?.key,\n name: options?.name,\n reason: options?.reason,\n decisionId,\n ts: Date.now(),\n };\n\n // Add context to event only if:\n // 1. Event doesn't already have context (preserves replayed events)\n // 2. Context is actually provided (don't add context: undefined property)\n const eventWithContext =\n event.context !== undefined || ctx.context === undefined\n ? event\n : ({ ...event, context: ctx.context } as WorkflowEvent<unknown, C>);\n\n ctx.onEvent(eventWithContext);\n}\n\n// =============================================================================\n// Conditional Helpers\n// =============================================================================\n\n/**\n * Run a step only if condition is true, return undefined if skipped.\n *\n * Use this when you want to conditionally execute a step and handle\n * the undefined case yourself. For a version with a default value,\n * use `whenOr`.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is true)\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is true, undefined otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step('fetchUser', () => fetchUser(id));\n *\n * // Only runs if user is premium\n * const premium = await when(\n * user.isPremium,\n * () => step('fetchPremiumData', () => fetchPremiumData(user.id)),\n * { name: 'check-premium', reason: 'User is not premium' }\n * );\n *\n * return { user, premium };\n * });\n * ```\n */\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | undefined>;\n\n/**\n * Synchronous overload for when the operation returns a non-Promise value.\n */\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: () => T,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | undefined | Promise<T | undefined>;\n\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | undefined> {\n if (condition) {\n return operation();\n }\n\n const decisionId = generateDecisionId();\n emitSkipped(ctx, options, decisionId);\n return undefined;\n}\n\n/**\n * Run a step only if condition is false, return undefined if skipped.\n *\n * Use this when you want to conditionally execute a step when a condition\n * is NOT met. For a version with a default value, use `unlessOr`.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is false)\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is false, undefined otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Only runs if user is NOT verified\n * const verification = await unless(\n * user.isVerified,\n * () => step(() => sendVerificationEmail(user.email), { name: 'send-verification' }),\n * { name: 'check-verification', reason: 'User is already verified' }\n * );\n *\n * return { user, verification };\n * });\n * ```\n */\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | undefined>;\n\n/**\n * Synchronous overload for unless when the operation returns a non-Promise value.\n */\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: () => T,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | undefined | Promise<T | undefined>;\n\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | undefined> {\n return when(!condition, operation, options, ctx);\n}\n\n/**\n * Run a step only if condition is true, return default value if skipped.\n *\n * Use this when you want to conditionally execute a step and provide\n * a fallback value when the condition is not met.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is true)\n * @param defaultValue - Value to return if condition is false\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is true, defaultValue otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Get premium limits or use default for non-premium users\n * const limits = await whenOr(\n * user.isPremium,\n * () => step(() => fetchPremiumLimits(user.id), { name: 'premium-limits' }),\n * { maxRequests: 100, maxStorage: 1000 }, // default for non-premium\n * { name: 'check-premium-limits', reason: 'Using default limits for non-premium user' }\n * );\n *\n * return { user, limits };\n * });\n * ```\n */\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | D>;\n\n/**\n * Synchronous overload for whenOr when the operation returns a non-Promise value.\n */\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: () => T,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | D | Promise<T | D>;\n\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | D> {\n if (condition) {\n return operation();\n }\n\n const decisionId = generateDecisionId();\n emitSkipped(ctx, options, decisionId);\n return defaultValue;\n}\n\n/**\n * Run a step only if condition is false, return default value if skipped.\n *\n * Use this when you want to conditionally execute a step when a condition\n * is NOT met, with a fallback value for when the condition is true.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is false)\n * @param defaultValue - Value to return if condition is true\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is false, defaultValue otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Generate new token if user is NOT authenticated, otherwise use existing\n * const token = await unlessOr(\n * user.isAuthenticated,\n * () => step(() => generateNewToken(user.id), { name: 'generate-token' }),\n * user.existingToken, // use existing token if authenticated\n * { name: 'check-auth-for-token', reason: 'Using existing token for authenticated user' }\n * );\n *\n * return { user, token };\n * });\n * ```\n */\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | D>;\n\n/**\n * Synchronous overload for unlessOr when the operation returns a non-Promise value.\n */\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: () => T,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | D | Promise<T | D>;\n\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | D> {\n return whenOr(!condition, operation, defaultValue, options, ctx);\n}\n\n// =============================================================================\n// Factory Functions for Workflow Integration\n// =============================================================================\n\n/**\n * Create a set of conditional helpers bound to a workflow context.\n *\n * Use this factory when you want to automatically emit step_skipped events\n * to the workflow's event stream without passing context manually.\n *\n * @param ctx - The workflow context containing workflowId, onEvent, and optional context\n * @returns Object with bound when, unless, whenOr, and unlessOr functions\n *\n * @example\n * ```typescript\n * // With run() - context is automatically included in events\n * const result = await run(async ({ step }) => {\n * const ctx = { workflowId, onEvent, context: requestContext };\n * const { when, whenOr } = createConditionalHelpers(ctx);\n *\n * const user = await step(fetchUser(id));\n *\n * const premium = await when(\n * user.isPremium,\n * () => step(() => fetchPremiumData(user.id)),\n * { name: 'premium-data' }\n * );\n *\n * return { user, premium };\n * }, { onEvent, workflowId, context: requestContext });\n * \n * // With createWorkflow - access context from onEvent callback\n * const workflow = createWorkflow({ fetchUser }, {\n * createContext: () => ({ requestId: 'req-123' }),\n * onEvent: (event, ctx) => {\n * // ctx is available here, can be passed to conditional helpers\n * }\n * });\n * ```\n */\nexport function createConditionalHelpers<C = unknown>(ctx: ConditionalContext<C>) {\n return {\n /**\n * Run a step only if condition is true, return undefined if skipped.\n */\n when: <T>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions\n ): MaybeAsync<T | undefined> => when(condition, operation, options, ctx),\n\n /**\n * Run a step only if condition is false, return undefined if skipped.\n */\n unless: <T>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions\n ): MaybeAsync<T | undefined> => unless(condition, operation, options, ctx),\n\n /**\n * Run a step only if condition is true, return default value if skipped.\n */\n whenOr: <T, D>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions\n ): MaybeAsync<T | D> => whenOr(condition, operation, defaultValue, options, ctx),\n\n /**\n * Run a step only if condition is false, return default value if skipped.\n */\n unlessOr: <T, D>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions\n ): MaybeAsync<T | D> => unlessOr(condition, operation, defaultValue, options, ctx),\n };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,8BAAAE,EAAA,WAAAC,EAAA,aAAAC,EAAA,SAAAC,EAAA,WAAAC,IAAA,eAAAC,EAAAP,GC4EA,SAASQ,GAA6B,CACpC,MAAO,YAAY,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,EAAG,CAAC,CAAC,EACzE,CAMA,SAASC,EACPC,EACAC,EACAC,EACM,CACN,GAAI,CAACF,GAAK,QAAS,OAGnB,IAAMG,EAAmC,CACvC,KAAM,eACN,WAAYH,EAAI,WAChB,QAASC,GAAS,IAClB,KAAMA,GAAS,KACf,OAAQA,GAAS,OACjB,WAAAC,EACA,GAAI,KAAK,IAAI,CACf,EAKME,EACJD,EAAM,UAAY,QAAaH,EAAI,UAAY,OAC3CG,EACC,CAAE,GAAGA,EAAO,QAASH,EAAI,OAAQ,EAExCA,EAAI,QAAQI,CAAgB,CAC9B,CAoDO,SAASC,EACdC,EACAC,EACAN,EACAD,EAC2B,CAC3B,GAAIM,EACF,OAAOC,EAAU,EAGnB,IAAML,EAAaJ,EAAmB,EACtCC,EAAYC,EAAKC,EAASC,CAAU,CAEtC,CA+CO,SAASM,EACdF,EACAC,EACAN,EACAD,EAC2B,CAC3B,OAAOK,EAAK,CAACC,EAAWC,EAAWN,EAASD,CAAG,CACjD,CAmDO,SAASS,EACdH,EACAC,EACAG,EACAT,EACAD,EACmB,CACnB,GAAIM,EACF,OAAOC,EAAU,EAGnB,IAAML,EAAaJ,EAAmB,EACtC,OAAAC,EAAYC,EAAKC,EAASC,CAAU,EAC7BQ,CACT,CAmDO,SAASC,EACdL,EACAC,EACAG,EACAT,EACAD,EACmB,CACnB,OAAOS,EAAO,CAACH,EAAWC,EAAWG,EAAcT,EAASD,CAAG,CACjE,CA0CO,SAASY,EAAsCZ,EAA4B,CAChF,MAAO,CAIL,KAAM,CACJM,EACAC,EACAN,IAC8BI,EAAKC,EAAWC,EAAWN,EAASD,CAAG,EAKvE,OAAQ,CACNM,EACAC,EACAN,IAC8BO,EAAOF,EAAWC,EAAWN,EAASD,CAAG,EAKzE,OAAQ,CACNM,EACAC,EACAG,EACAT,IACsBQ,EAAOH,EAAWC,EAAWG,EAAcT,EAASD,CAAG,EAK/E,SAAU,CACRM,EACAC,EACAG,EACAT,IACsBU,EAASL,EAAWC,EAAWG,EAAcT,EAASD,CAAG,CACnF,CACF","names":["conditional_entry_exports","__export","createConditionalHelpers","unless","unlessOr","when","whenOr","__toCommonJS","generateDecisionId","emitSkipped","ctx","options","decisionId","event","eventWithContext","when","condition","operation","unless","whenOr","defaultValue","unlessOr","createConditionalHelpers"]}
@@ -1,252 +0,0 @@
1
- import { ag as WorkflowEvent } from './run-entry-D2MmJFj9.cjs';
2
- import './errors.cjs';
3
- import './tagged-error.cjs';
4
- import './slugs.cjs';
5
-
6
- /**
7
- * awaitly/conditional
8
- *
9
- * Conditional step execution helpers for workflows.
10
- * These helpers allow you to conditionally execute steps based on runtime conditions,
11
- * with proper event emission for skipped steps.
12
- */
13
-
14
- /**
15
- * Options for conditional execution.
16
- */
17
- type ConditionalOptions = {
18
- /**
19
- * Human-readable name for the conditional step.
20
- * Used in step_skipped events for debugging and visualization.
21
- */
22
- name?: string;
23
- /**
24
- * Stable identity key for the conditional step.
25
- * Used in step_skipped events for tracking and visualization.
26
- */
27
- key?: string;
28
- /**
29
- * Optional reason explaining why the step was skipped.
30
- * Included in step_skipped events.
31
- */
32
- reason?: string;
33
- };
34
- /**
35
- * Context for conditional execution, used to emit events.
36
- */
37
- type ConditionalContext<C = unknown> = {
38
- /**
39
- * The workflow ID for event emission.
40
- */
41
- workflowId: string;
42
- /**
43
- * Event emitter function.
44
- */
45
- onEvent?: (event: WorkflowEvent<unknown, C>) => void;
46
- /**
47
- * Optional context value to include in emitted events.
48
- * When provided, this context is automatically added to step_skipped events.
49
- */
50
- context?: C;
51
- };
52
- /**
53
- * Type for operations that can be either sync or async.
54
- */
55
- type MaybeAsync<T> = T | Promise<T>;
56
- /**
57
- * Type for the operation function passed to conditional helpers.
58
- */
59
- type Operation<T> = () => MaybeAsync<T>;
60
- /**
61
- * Run a step only if condition is true, return undefined if skipped.
62
- *
63
- * Use this when you want to conditionally execute a step and handle
64
- * the undefined case yourself. For a version with a default value,
65
- * use `whenOr`.
66
- *
67
- * @param condition - Boolean condition to evaluate
68
- * @param operation - Function that performs the step (only called if condition is true)
69
- * @param options - Optional configuration for the conditional step
70
- * @param ctx - Optional context for event emission
71
- * @returns The result of the operation if condition is true, undefined otherwise
72
- *
73
- * @example
74
- * ```typescript
75
- * const result = await workflow(async ({ step }) => {
76
- * const user = await step('fetchUser', () => fetchUser(id));
77
- *
78
- * // Only runs if user is premium
79
- * const premium = await when(
80
- * user.isPremium,
81
- * () => step('fetchPremiumData', () => fetchPremiumData(user.id)),
82
- * { name: 'check-premium', reason: 'User is not premium' }
83
- * );
84
- *
85
- * return { user, premium };
86
- * });
87
- * ```
88
- */
89
- declare function when<T, C = unknown>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | undefined>;
90
- /**
91
- * Synchronous overload for when the operation returns a non-Promise value.
92
- */
93
- declare function when<T, C = unknown>(condition: boolean, operation: () => T, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | undefined | Promise<T | undefined>;
94
- /**
95
- * Run a step only if condition is false, return undefined if skipped.
96
- *
97
- * Use this when you want to conditionally execute a step when a condition
98
- * is NOT met. For a version with a default value, use `unlessOr`.
99
- *
100
- * @param condition - Boolean condition to evaluate
101
- * @param operation - Function that performs the step (only called if condition is false)
102
- * @param options - Optional configuration for the conditional step
103
- * @param ctx - Optional context for event emission
104
- * @returns The result of the operation if condition is false, undefined otherwise
105
- *
106
- * @example
107
- * ```typescript
108
- * const result = await workflow(async ({ step }) => {
109
- * const user = await step(fetchUser(id));
110
- *
111
- * // Only runs if user is NOT verified
112
- * const verification = await unless(
113
- * user.isVerified,
114
- * () => step(() => sendVerificationEmail(user.email), { name: 'send-verification' }),
115
- * { name: 'check-verification', reason: 'User is already verified' }
116
- * );
117
- *
118
- * return { user, verification };
119
- * });
120
- * ```
121
- */
122
- declare function unless<T, C = unknown>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | undefined>;
123
- /**
124
- * Synchronous overload for unless when the operation returns a non-Promise value.
125
- */
126
- declare function unless<T, C = unknown>(condition: boolean, operation: () => T, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | undefined | Promise<T | undefined>;
127
- /**
128
- * Run a step only if condition is true, return default value if skipped.
129
- *
130
- * Use this when you want to conditionally execute a step and provide
131
- * a fallback value when the condition is not met.
132
- *
133
- * @param condition - Boolean condition to evaluate
134
- * @param operation - Function that performs the step (only called if condition is true)
135
- * @param defaultValue - Value to return if condition is false
136
- * @param options - Optional configuration for the conditional step
137
- * @param ctx - Optional context for event emission
138
- * @returns The result of the operation if condition is true, defaultValue otherwise
139
- *
140
- * @example
141
- * ```typescript
142
- * const result = await workflow(async ({ step }) => {
143
- * const user = await step(fetchUser(id));
144
- *
145
- * // Get premium limits or use default for non-premium users
146
- * const limits = await whenOr(
147
- * user.isPremium,
148
- * () => step(() => fetchPremiumLimits(user.id), { name: 'premium-limits' }),
149
- * { maxRequests: 100, maxStorage: 1000 }, // default for non-premium
150
- * { name: 'check-premium-limits', reason: 'Using default limits for non-premium user' }
151
- * );
152
- *
153
- * return { user, limits };
154
- * });
155
- * ```
156
- */
157
- declare function whenOr<T, D, C = unknown>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | D>;
158
- /**
159
- * Synchronous overload for whenOr when the operation returns a non-Promise value.
160
- */
161
- declare function whenOr<T, D, C = unknown>(condition: boolean, operation: () => T, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | D | Promise<T | D>;
162
- /**
163
- * Run a step only if condition is false, return default value if skipped.
164
- *
165
- * Use this when you want to conditionally execute a step when a condition
166
- * is NOT met, with a fallback value for when the condition is true.
167
- *
168
- * @param condition - Boolean condition to evaluate
169
- * @param operation - Function that performs the step (only called if condition is false)
170
- * @param defaultValue - Value to return if condition is true
171
- * @param options - Optional configuration for the conditional step
172
- * @param ctx - Optional context for event emission
173
- * @returns The result of the operation if condition is false, defaultValue otherwise
174
- *
175
- * @example
176
- * ```typescript
177
- * const result = await workflow(async ({ step }) => {
178
- * const user = await step(fetchUser(id));
179
- *
180
- * // Generate new token if user is NOT authenticated, otherwise use existing
181
- * const token = await unlessOr(
182
- * user.isAuthenticated,
183
- * () => step(() => generateNewToken(user.id), { name: 'generate-token' }),
184
- * user.existingToken, // use existing token if authenticated
185
- * { name: 'check-auth-for-token', reason: 'Using existing token for authenticated user' }
186
- * );
187
- *
188
- * return { user, token };
189
- * });
190
- * ```
191
- */
192
- declare function unlessOr<T, D, C = unknown>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | D>;
193
- /**
194
- * Synchronous overload for unlessOr when the operation returns a non-Promise value.
195
- */
196
- declare function unlessOr<T, D, C = unknown>(condition: boolean, operation: () => T, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | D | Promise<T | D>;
197
- /**
198
- * Create a set of conditional helpers bound to a workflow context.
199
- *
200
- * Use this factory when you want to automatically emit step_skipped events
201
- * to the workflow's event stream without passing context manually.
202
- *
203
- * @param ctx - The workflow context containing workflowId, onEvent, and optional context
204
- * @returns Object with bound when, unless, whenOr, and unlessOr functions
205
- *
206
- * @example
207
- * ```typescript
208
- * // With run() - context is automatically included in events
209
- * const result = await run(async ({ step }) => {
210
- * const ctx = { workflowId, onEvent, context: requestContext };
211
- * const { when, whenOr } = createConditionalHelpers(ctx);
212
- *
213
- * const user = await step(fetchUser(id));
214
- *
215
- * const premium = await when(
216
- * user.isPremium,
217
- * () => step(() => fetchPremiumData(user.id)),
218
- * { name: 'premium-data' }
219
- * );
220
- *
221
- * return { user, premium };
222
- * }, { onEvent, workflowId, context: requestContext });
223
- *
224
- * // With createWorkflow - access context from onEvent callback
225
- * const workflow = createWorkflow({ fetchUser }, {
226
- * createContext: () => ({ requestId: 'req-123' }),
227
- * onEvent: (event, ctx) => {
228
- * // ctx is available here, can be passed to conditional helpers
229
- * }
230
- * });
231
- * ```
232
- */
233
- declare function createConditionalHelpers<C = unknown>(ctx: ConditionalContext<C>): {
234
- /**
235
- * Run a step only if condition is true, return undefined if skipped.
236
- */
237
- when: <T>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions) => MaybeAsync<T | undefined>;
238
- /**
239
- * Run a step only if condition is false, return undefined if skipped.
240
- */
241
- unless: <T>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions) => MaybeAsync<T | undefined>;
242
- /**
243
- * Run a step only if condition is true, return default value if skipped.
244
- */
245
- whenOr: <T, D>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions) => MaybeAsync<T | D>;
246
- /**
247
- * Run a step only if condition is false, return default value if skipped.
248
- */
249
- unlessOr: <T, D>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions) => MaybeAsync<T | D>;
250
- };
251
-
252
- export { type ConditionalContext, type ConditionalOptions, createConditionalHelpers, unless, unlessOr, when, whenOr };
@@ -1,252 +0,0 @@
1
- import { ag as WorkflowEvent } from './run-entry-Dduz-is2.js';
2
- import './errors.js';
3
- import './tagged-error.js';
4
- import './slugs.js';
5
-
6
- /**
7
- * awaitly/conditional
8
- *
9
- * Conditional step execution helpers for workflows.
10
- * These helpers allow you to conditionally execute steps based on runtime conditions,
11
- * with proper event emission for skipped steps.
12
- */
13
-
14
- /**
15
- * Options for conditional execution.
16
- */
17
- type ConditionalOptions = {
18
- /**
19
- * Human-readable name for the conditional step.
20
- * Used in step_skipped events for debugging and visualization.
21
- */
22
- name?: string;
23
- /**
24
- * Stable identity key for the conditional step.
25
- * Used in step_skipped events for tracking and visualization.
26
- */
27
- key?: string;
28
- /**
29
- * Optional reason explaining why the step was skipped.
30
- * Included in step_skipped events.
31
- */
32
- reason?: string;
33
- };
34
- /**
35
- * Context for conditional execution, used to emit events.
36
- */
37
- type ConditionalContext<C = unknown> = {
38
- /**
39
- * The workflow ID for event emission.
40
- */
41
- workflowId: string;
42
- /**
43
- * Event emitter function.
44
- */
45
- onEvent?: (event: WorkflowEvent<unknown, C>) => void;
46
- /**
47
- * Optional context value to include in emitted events.
48
- * When provided, this context is automatically added to step_skipped events.
49
- */
50
- context?: C;
51
- };
52
- /**
53
- * Type for operations that can be either sync or async.
54
- */
55
- type MaybeAsync<T> = T | Promise<T>;
56
- /**
57
- * Type for the operation function passed to conditional helpers.
58
- */
59
- type Operation<T> = () => MaybeAsync<T>;
60
- /**
61
- * Run a step only if condition is true, return undefined if skipped.
62
- *
63
- * Use this when you want to conditionally execute a step and handle
64
- * the undefined case yourself. For a version with a default value,
65
- * use `whenOr`.
66
- *
67
- * @param condition - Boolean condition to evaluate
68
- * @param operation - Function that performs the step (only called if condition is true)
69
- * @param options - Optional configuration for the conditional step
70
- * @param ctx - Optional context for event emission
71
- * @returns The result of the operation if condition is true, undefined otherwise
72
- *
73
- * @example
74
- * ```typescript
75
- * const result = await workflow(async ({ step }) => {
76
- * const user = await step('fetchUser', () => fetchUser(id));
77
- *
78
- * // Only runs if user is premium
79
- * const premium = await when(
80
- * user.isPremium,
81
- * () => step('fetchPremiumData', () => fetchPremiumData(user.id)),
82
- * { name: 'check-premium', reason: 'User is not premium' }
83
- * );
84
- *
85
- * return { user, premium };
86
- * });
87
- * ```
88
- */
89
- declare function when<T, C = unknown>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | undefined>;
90
- /**
91
- * Synchronous overload for when the operation returns a non-Promise value.
92
- */
93
- declare function when<T, C = unknown>(condition: boolean, operation: () => T, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | undefined | Promise<T | undefined>;
94
- /**
95
- * Run a step only if condition is false, return undefined if skipped.
96
- *
97
- * Use this when you want to conditionally execute a step when a condition
98
- * is NOT met. For a version with a default value, use `unlessOr`.
99
- *
100
- * @param condition - Boolean condition to evaluate
101
- * @param operation - Function that performs the step (only called if condition is false)
102
- * @param options - Optional configuration for the conditional step
103
- * @param ctx - Optional context for event emission
104
- * @returns The result of the operation if condition is false, undefined otherwise
105
- *
106
- * @example
107
- * ```typescript
108
- * const result = await workflow(async ({ step }) => {
109
- * const user = await step(fetchUser(id));
110
- *
111
- * // Only runs if user is NOT verified
112
- * const verification = await unless(
113
- * user.isVerified,
114
- * () => step(() => sendVerificationEmail(user.email), { name: 'send-verification' }),
115
- * { name: 'check-verification', reason: 'User is already verified' }
116
- * );
117
- *
118
- * return { user, verification };
119
- * });
120
- * ```
121
- */
122
- declare function unless<T, C = unknown>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | undefined>;
123
- /**
124
- * Synchronous overload for unless when the operation returns a non-Promise value.
125
- */
126
- declare function unless<T, C = unknown>(condition: boolean, operation: () => T, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | undefined | Promise<T | undefined>;
127
- /**
128
- * Run a step only if condition is true, return default value if skipped.
129
- *
130
- * Use this when you want to conditionally execute a step and provide
131
- * a fallback value when the condition is not met.
132
- *
133
- * @param condition - Boolean condition to evaluate
134
- * @param operation - Function that performs the step (only called if condition is true)
135
- * @param defaultValue - Value to return if condition is false
136
- * @param options - Optional configuration for the conditional step
137
- * @param ctx - Optional context for event emission
138
- * @returns The result of the operation if condition is true, defaultValue otherwise
139
- *
140
- * @example
141
- * ```typescript
142
- * const result = await workflow(async ({ step }) => {
143
- * const user = await step(fetchUser(id));
144
- *
145
- * // Get premium limits or use default for non-premium users
146
- * const limits = await whenOr(
147
- * user.isPremium,
148
- * () => step(() => fetchPremiumLimits(user.id), { name: 'premium-limits' }),
149
- * { maxRequests: 100, maxStorage: 1000 }, // default for non-premium
150
- * { name: 'check-premium-limits', reason: 'Using default limits for non-premium user' }
151
- * );
152
- *
153
- * return { user, limits };
154
- * });
155
- * ```
156
- */
157
- declare function whenOr<T, D, C = unknown>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | D>;
158
- /**
159
- * Synchronous overload for whenOr when the operation returns a non-Promise value.
160
- */
161
- declare function whenOr<T, D, C = unknown>(condition: boolean, operation: () => T, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | D | Promise<T | D>;
162
- /**
163
- * Run a step only if condition is false, return default value if skipped.
164
- *
165
- * Use this when you want to conditionally execute a step when a condition
166
- * is NOT met, with a fallback value for when the condition is true.
167
- *
168
- * @param condition - Boolean condition to evaluate
169
- * @param operation - Function that performs the step (only called if condition is false)
170
- * @param defaultValue - Value to return if condition is true
171
- * @param options - Optional configuration for the conditional step
172
- * @param ctx - Optional context for event emission
173
- * @returns The result of the operation if condition is false, defaultValue otherwise
174
- *
175
- * @example
176
- * ```typescript
177
- * const result = await workflow(async ({ step }) => {
178
- * const user = await step(fetchUser(id));
179
- *
180
- * // Generate new token if user is NOT authenticated, otherwise use existing
181
- * const token = await unlessOr(
182
- * user.isAuthenticated,
183
- * () => step(() => generateNewToken(user.id), { name: 'generate-token' }),
184
- * user.existingToken, // use existing token if authenticated
185
- * { name: 'check-auth-for-token', reason: 'Using existing token for authenticated user' }
186
- * );
187
- *
188
- * return { user, token };
189
- * });
190
- * ```
191
- */
192
- declare function unlessOr<T, D, C = unknown>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): Promise<T | D>;
193
- /**
194
- * Synchronous overload for unlessOr when the operation returns a non-Promise value.
195
- */
196
- declare function unlessOr<T, D, C = unknown>(condition: boolean, operation: () => T, defaultValue: D, options?: ConditionalOptions, ctx?: ConditionalContext<C>): T | D | Promise<T | D>;
197
- /**
198
- * Create a set of conditional helpers bound to a workflow context.
199
- *
200
- * Use this factory when you want to automatically emit step_skipped events
201
- * to the workflow's event stream without passing context manually.
202
- *
203
- * @param ctx - The workflow context containing workflowId, onEvent, and optional context
204
- * @returns Object with bound when, unless, whenOr, and unlessOr functions
205
- *
206
- * @example
207
- * ```typescript
208
- * // With run() - context is automatically included in events
209
- * const result = await run(async ({ step }) => {
210
- * const ctx = { workflowId, onEvent, context: requestContext };
211
- * const { when, whenOr } = createConditionalHelpers(ctx);
212
- *
213
- * const user = await step(fetchUser(id));
214
- *
215
- * const premium = await when(
216
- * user.isPremium,
217
- * () => step(() => fetchPremiumData(user.id)),
218
- * { name: 'premium-data' }
219
- * );
220
- *
221
- * return { user, premium };
222
- * }, { onEvent, workflowId, context: requestContext });
223
- *
224
- * // With createWorkflow - access context from onEvent callback
225
- * const workflow = createWorkflow({ fetchUser }, {
226
- * createContext: () => ({ requestId: 'req-123' }),
227
- * onEvent: (event, ctx) => {
228
- * // ctx is available here, can be passed to conditional helpers
229
- * }
230
- * });
231
- * ```
232
- */
233
- declare function createConditionalHelpers<C = unknown>(ctx: ConditionalContext<C>): {
234
- /**
235
- * Run a step only if condition is true, return undefined if skipped.
236
- */
237
- when: <T>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions) => MaybeAsync<T | undefined>;
238
- /**
239
- * Run a step only if condition is false, return undefined if skipped.
240
- */
241
- unless: <T>(condition: boolean, operation: Operation<T>, options?: ConditionalOptions) => MaybeAsync<T | undefined>;
242
- /**
243
- * Run a step only if condition is true, return default value if skipped.
244
- */
245
- whenOr: <T, D>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions) => MaybeAsync<T | D>;
246
- /**
247
- * Run a step only if condition is false, return default value if skipped.
248
- */
249
- unlessOr: <T, D>(condition: boolean, operation: Operation<T>, defaultValue: D, options?: ConditionalOptions) => MaybeAsync<T | D>;
250
- };
251
-
252
- export { type ConditionalContext, type ConditionalOptions, createConditionalHelpers, unless, unlessOr, when, whenOr };
@@ -1,2 +0,0 @@
1
- function d(){return`decision_${Date.now()}_${Math.random().toString(36).slice(2,8)}`}function s(t,n,e){if(!t?.onEvent)return;let o={type:"step_skipped",workflowId:t.workflowId,stepKey:n?.key,name:n?.name,reason:n?.reason,decisionId:e,ts:Date.now()},i=o.context!==void 0||t.context===void 0?o:{...o,context:t.context};t.onEvent(i)}function a(t,n,e,o){if(t)return n();let i=d();s(o,e,i)}function C(t,n,e,o){return a(!t,n,e,o)}function r(t,n,e,o,i){if(t)return n();let p=d();return s(i,o,p),e}function l(t,n,e,o,i){return r(!t,n,e,o,i)}function u(t){return{when:(n,e,o)=>a(n,e,o,t),unless:(n,e,o)=>C(n,e,o,t),whenOr:(n,e,o,i)=>r(n,e,o,i,t),unlessOr:(n,e,o,i)=>l(n,e,o,i,t)}}export{u as createConditionalHelpers,C as unless,l as unlessOr,a as when,r as whenOr};
2
- //# sourceMappingURL=conditional.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/conditional.ts"],"sourcesContent":["/**\n * awaitly/conditional\n *\n * Conditional step execution helpers for workflows.\n * These helpers allow you to conditionally execute steps based on runtime conditions,\n * with proper event emission for skipped steps.\n */\n\nimport type { WorkflowEvent } from \"./core\";\n\n// =============================================================================\n// Types\n// =============================================================================\n\n/**\n * Options for conditional execution.\n */\nexport type ConditionalOptions = {\n /**\n * Human-readable name for the conditional step.\n * Used in step_skipped events for debugging and visualization.\n */\n name?: string;\n\n /**\n * Stable identity key for the conditional step.\n * Used in step_skipped events for tracking and visualization.\n */\n key?: string;\n\n /**\n * Optional reason explaining why the step was skipped.\n * Included in step_skipped events.\n */\n reason?: string;\n};\n\n/**\n * Context for conditional execution, used to emit events.\n */\nexport type ConditionalContext<C = unknown> = {\n /**\n * The workflow ID for event emission.\n */\n workflowId: string;\n\n /**\n * Event emitter function.\n */\n onEvent?: (event: WorkflowEvent<unknown, C>) => void;\n\n /**\n * Optional context value to include in emitted events.\n * When provided, this context is automatically added to step_skipped events.\n */\n context?: C;\n};\n\n/**\n * Type for operations that can be either sync or async.\n */\ntype MaybeAsync<T> = T | Promise<T>;\n\n/**\n * Type for the operation function passed to conditional helpers.\n */\ntype Operation<T> = () => MaybeAsync<T>;\n\n// =============================================================================\n// Internal Helpers\n// =============================================================================\n\n/**\n * Generate a unique decision ID for tracking conditional decisions.\n * @internal\n */\nfunction generateDecisionId(): string {\n return `decision_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;\n}\n\n/**\n * Emit a step_skipped event.\n * @internal\n */\nfunction emitSkipped<C = unknown>(\n ctx: ConditionalContext<C> | undefined,\n options: ConditionalOptions | undefined,\n decisionId: string\n): void {\n if (!ctx?.onEvent) return;\n\n // Create event with context if provided (similar to emitEvent logic)\n const event: WorkflowEvent<unknown, C> = {\n type: \"step_skipped\",\n workflowId: ctx.workflowId,\n stepKey: options?.key,\n name: options?.name,\n reason: options?.reason,\n decisionId,\n ts: Date.now(),\n };\n\n // Add context to event only if:\n // 1. Event doesn't already have context (preserves replayed events)\n // 2. Context is actually provided (don't add context: undefined property)\n const eventWithContext =\n event.context !== undefined || ctx.context === undefined\n ? event\n : ({ ...event, context: ctx.context } as WorkflowEvent<unknown, C>);\n\n ctx.onEvent(eventWithContext);\n}\n\n// =============================================================================\n// Conditional Helpers\n// =============================================================================\n\n/**\n * Run a step only if condition is true, return undefined if skipped.\n *\n * Use this when you want to conditionally execute a step and handle\n * the undefined case yourself. For a version with a default value,\n * use `whenOr`.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is true)\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is true, undefined otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step('fetchUser', () => fetchUser(id));\n *\n * // Only runs if user is premium\n * const premium = await when(\n * user.isPremium,\n * () => step('fetchPremiumData', () => fetchPremiumData(user.id)),\n * { name: 'check-premium', reason: 'User is not premium' }\n * );\n *\n * return { user, premium };\n * });\n * ```\n */\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | undefined>;\n\n/**\n * Synchronous overload for when the operation returns a non-Promise value.\n */\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: () => T,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | undefined | Promise<T | undefined>;\n\nexport function when<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | undefined> {\n if (condition) {\n return operation();\n }\n\n const decisionId = generateDecisionId();\n emitSkipped(ctx, options, decisionId);\n return undefined;\n}\n\n/**\n * Run a step only if condition is false, return undefined if skipped.\n *\n * Use this when you want to conditionally execute a step when a condition\n * is NOT met. For a version with a default value, use `unlessOr`.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is false)\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is false, undefined otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Only runs if user is NOT verified\n * const verification = await unless(\n * user.isVerified,\n * () => step(() => sendVerificationEmail(user.email), { name: 'send-verification' }),\n * { name: 'check-verification', reason: 'User is already verified' }\n * );\n *\n * return { user, verification };\n * });\n * ```\n */\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | undefined>;\n\n/**\n * Synchronous overload for unless when the operation returns a non-Promise value.\n */\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: () => T,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | undefined | Promise<T | undefined>;\n\nexport function unless<T, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | undefined> {\n return when(!condition, operation, options, ctx);\n}\n\n/**\n * Run a step only if condition is true, return default value if skipped.\n *\n * Use this when you want to conditionally execute a step and provide\n * a fallback value when the condition is not met.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is true)\n * @param defaultValue - Value to return if condition is false\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is true, defaultValue otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Get premium limits or use default for non-premium users\n * const limits = await whenOr(\n * user.isPremium,\n * () => step(() => fetchPremiumLimits(user.id), { name: 'premium-limits' }),\n * { maxRequests: 100, maxStorage: 1000 }, // default for non-premium\n * { name: 'check-premium-limits', reason: 'Using default limits for non-premium user' }\n * );\n *\n * return { user, limits };\n * });\n * ```\n */\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | D>;\n\n/**\n * Synchronous overload for whenOr when the operation returns a non-Promise value.\n */\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: () => T,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | D | Promise<T | D>;\n\nexport function whenOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | D> {\n if (condition) {\n return operation();\n }\n\n const decisionId = generateDecisionId();\n emitSkipped(ctx, options, decisionId);\n return defaultValue;\n}\n\n/**\n * Run a step only if condition is false, return default value if skipped.\n *\n * Use this when you want to conditionally execute a step when a condition\n * is NOT met, with a fallback value for when the condition is true.\n *\n * @param condition - Boolean condition to evaluate\n * @param operation - Function that performs the step (only called if condition is false)\n * @param defaultValue - Value to return if condition is true\n * @param options - Optional configuration for the conditional step\n * @param ctx - Optional context for event emission\n * @returns The result of the operation if condition is false, defaultValue otherwise\n *\n * @example\n * ```typescript\n * const result = await workflow(async ({ step }) => {\n * const user = await step(fetchUser(id));\n *\n * // Generate new token if user is NOT authenticated, otherwise use existing\n * const token = await unlessOr(\n * user.isAuthenticated,\n * () => step(() => generateNewToken(user.id), { name: 'generate-token' }),\n * user.existingToken, // use existing token if authenticated\n * { name: 'check-auth-for-token', reason: 'Using existing token for authenticated user' }\n * );\n *\n * return { user, token };\n * });\n * ```\n */\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): Promise<T | D>;\n\n/**\n * Synchronous overload for unlessOr when the operation returns a non-Promise value.\n */\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: () => T,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): T | D | Promise<T | D>;\n\nexport function unlessOr<T, D, C = unknown>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions,\n ctx?: ConditionalContext<C>\n): MaybeAsync<T | D> {\n return whenOr(!condition, operation, defaultValue, options, ctx);\n}\n\n// =============================================================================\n// Factory Functions for Workflow Integration\n// =============================================================================\n\n/**\n * Create a set of conditional helpers bound to a workflow context.\n *\n * Use this factory when you want to automatically emit step_skipped events\n * to the workflow's event stream without passing context manually.\n *\n * @param ctx - The workflow context containing workflowId, onEvent, and optional context\n * @returns Object with bound when, unless, whenOr, and unlessOr functions\n *\n * @example\n * ```typescript\n * // With run() - context is automatically included in events\n * const result = await run(async ({ step }) => {\n * const ctx = { workflowId, onEvent, context: requestContext };\n * const { when, whenOr } = createConditionalHelpers(ctx);\n *\n * const user = await step(fetchUser(id));\n *\n * const premium = await when(\n * user.isPremium,\n * () => step(() => fetchPremiumData(user.id)),\n * { name: 'premium-data' }\n * );\n *\n * return { user, premium };\n * }, { onEvent, workflowId, context: requestContext });\n * \n * // With createWorkflow - access context from onEvent callback\n * const workflow = createWorkflow({ fetchUser }, {\n * createContext: () => ({ requestId: 'req-123' }),\n * onEvent: (event, ctx) => {\n * // ctx is available here, can be passed to conditional helpers\n * }\n * });\n * ```\n */\nexport function createConditionalHelpers<C = unknown>(ctx: ConditionalContext<C>) {\n return {\n /**\n * Run a step only if condition is true, return undefined if skipped.\n */\n when: <T>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions\n ): MaybeAsync<T | undefined> => when(condition, operation, options, ctx),\n\n /**\n * Run a step only if condition is false, return undefined if skipped.\n */\n unless: <T>(\n condition: boolean,\n operation: Operation<T>,\n options?: ConditionalOptions\n ): MaybeAsync<T | undefined> => unless(condition, operation, options, ctx),\n\n /**\n * Run a step only if condition is true, return default value if skipped.\n */\n whenOr: <T, D>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions\n ): MaybeAsync<T | D> => whenOr(condition, operation, defaultValue, options, ctx),\n\n /**\n * Run a step only if condition is false, return default value if skipped.\n */\n unlessOr: <T, D>(\n condition: boolean,\n operation: Operation<T>,\n defaultValue: D,\n options?: ConditionalOptions\n ): MaybeAsync<T | D> => unlessOr(condition, operation, defaultValue, options, ctx),\n };\n}\n"],"mappings":"AA4EA,SAASA,GAA6B,CACpC,MAAO,YAAY,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,EAAG,CAAC,CAAC,EACzE,CAMA,SAASC,EACPC,EACAC,EACAC,EACM,CACN,GAAI,CAACF,GAAK,QAAS,OAGnB,IAAMG,EAAmC,CACvC,KAAM,eACN,WAAYH,EAAI,WAChB,QAASC,GAAS,IAClB,KAAMA,GAAS,KACf,OAAQA,GAAS,OACjB,WAAAC,EACA,GAAI,KAAK,IAAI,CACf,EAKME,EACJD,EAAM,UAAY,QAAaH,EAAI,UAAY,OAC3CG,EACC,CAAE,GAAGA,EAAO,QAASH,EAAI,OAAQ,EAExCA,EAAI,QAAQI,CAAgB,CAC9B,CAoDO,SAASC,EACdC,EACAC,EACAN,EACAD,EAC2B,CAC3B,GAAIM,EACF,OAAOC,EAAU,EAGnB,IAAML,EAAaJ,EAAmB,EACtCC,EAAYC,EAAKC,EAASC,CAAU,CAEtC,CA+CO,SAASM,EACdF,EACAC,EACAN,EACAD,EAC2B,CAC3B,OAAOK,EAAK,CAACC,EAAWC,EAAWN,EAASD,CAAG,CACjD,CAmDO,SAASS,EACdH,EACAC,EACAG,EACAT,EACAD,EACmB,CACnB,GAAIM,EACF,OAAOC,EAAU,EAGnB,IAAML,EAAaJ,EAAmB,EACtC,OAAAC,EAAYC,EAAKC,EAASC,CAAU,EAC7BQ,CACT,CAmDO,SAASC,EACdL,EACAC,EACAG,EACAT,EACAD,EACmB,CACnB,OAAOS,EAAO,CAACH,EAAWC,EAAWG,EAAcT,EAASD,CAAG,CACjE,CA0CO,SAASY,EAAsCZ,EAA4B,CAChF,MAAO,CAIL,KAAM,CACJM,EACAC,EACAN,IAC8BI,EAAKC,EAAWC,EAAWN,EAASD,CAAG,EAKvE,OAAQ,CACNM,EACAC,EACAN,IAC8BO,EAAOF,EAAWC,EAAWN,EAASD,CAAG,EAKzE,OAAQ,CACNM,EACAC,EACAG,EACAT,IACsBQ,EAAOH,EAAWC,EAAWG,EAAcT,EAASD,CAAG,EAK/E,SAAU,CACRM,EACAC,EACAG,EACAT,IACsBU,EAASL,EAAWC,EAAWG,EAAcT,EAASD,CAAG,CACnF,CACF","names":["generateDecisionId","emitSkipped","ctx","options","decisionId","event","eventWithContext","when","condition","operation","unless","whenOr","defaultValue","unlessOr","createConditionalHelpers"]}