@zonastery/bursar 0.0.2 → 1.0.1

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 (505) hide show
  1. package/README.md +118 -333
  2. package/dist/billing/auto-recharge-port.d.ts +27 -0
  3. package/dist/billing/auto-recharge-port.d.ts.map +1 -0
  4. package/dist/billing/auto-recharge-port.js +2 -0
  5. package/dist/billing/auto-recharge-port.js.map +1 -0
  6. package/dist/billing/auto-recharge-service.d.ts +43 -0
  7. package/dist/billing/auto-recharge-service.d.ts.map +1 -0
  8. package/dist/billing/auto-recharge-service.js +218 -0
  9. package/dist/billing/auto-recharge-service.js.map +1 -0
  10. package/dist/billing/billing-service.d.ts +71 -0
  11. package/dist/billing/billing-service.d.ts.map +1 -0
  12. package/dist/billing/billing-service.js +172 -0
  13. package/dist/billing/billing-service.js.map +1 -0
  14. package/dist/billing/billing-store.d.ts +45 -13
  15. package/dist/billing/billing-store.d.ts.map +1 -1
  16. package/dist/billing/billing-store.js.map +1 -1
  17. package/dist/billing/contracts.d.ts +149 -0
  18. package/dist/billing/contracts.d.ts.map +1 -0
  19. package/dist/billing/contracts.js +2 -0
  20. package/dist/billing/contracts.js.map +1 -0
  21. package/dist/billing/event-handlers.d.ts +57 -0
  22. package/dist/billing/event-handlers.d.ts.map +1 -0
  23. package/dist/billing/event-handlers.js +684 -0
  24. package/dist/billing/event-handlers.js.map +1 -0
  25. package/dist/billing/event-processor.d.ts +20 -0
  26. package/dist/billing/event-processor.d.ts.map +1 -0
  27. package/dist/billing/event-processor.js +131 -0
  28. package/dist/billing/event-processor.js.map +1 -0
  29. package/dist/billing/financial-event-handlers.d.ts +26 -0
  30. package/dist/billing/financial-event-handlers.d.ts.map +1 -0
  31. package/dist/billing/financial-event-handlers.js +241 -0
  32. package/dist/billing/financial-event-handlers.js.map +1 -0
  33. package/dist/billing/index.d.ts +9 -5
  34. package/dist/billing/index.d.ts.map +1 -1
  35. package/dist/billing/index.js +4 -4
  36. package/dist/billing/index.js.map +1 -1
  37. package/dist/billing/management.d.ts +59 -0
  38. package/dist/billing/management.d.ts.map +1 -0
  39. package/dist/billing/management.js +113 -0
  40. package/dist/billing/management.js.map +1 -0
  41. package/dist/billing/policy-window.d.ts +24 -0
  42. package/dist/billing/policy-window.d.ts.map +1 -0
  43. package/dist/billing/policy-window.js +88 -0
  44. package/dist/billing/policy-window.js.map +1 -0
  45. package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
  46. package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
  47. package/dist/billing/postgres/repositories/auto-recharge.js +180 -0
  48. package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
  49. package/dist/billing/postgres/repositories/customer.d.ts +12 -0
  50. package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
  51. package/dist/billing/postgres/repositories/customer.js +33 -0
  52. package/dist/billing/postgres/repositories/customer.js.map +1 -0
  53. package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
  54. package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
  55. package/dist/billing/postgres/repositories/dispute.js +18 -0
  56. package/dist/billing/postgres/repositories/dispute.js.map +1 -0
  57. package/dist/billing/postgres/repositories/event.d.ts +21 -0
  58. package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
  59. package/dist/billing/postgres/repositories/event.js +45 -0
  60. package/dist/billing/postgres/repositories/event.js.map +1 -0
  61. package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
  62. package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
  63. package/dist/billing/postgres/repositories/invoice.js +36 -0
  64. package/dist/billing/postgres/repositories/invoice.js.map +1 -0
  65. package/dist/billing/postgres/repositories/offer.d.ts +26 -0
  66. package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
  67. package/dist/billing/postgres/repositories/offer.js +73 -0
  68. package/dist/billing/postgres/repositories/offer.js.map +1 -0
  69. package/dist/billing/postgres/repositories/payment.d.ts +32 -0
  70. package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
  71. package/dist/billing/postgres/repositories/payment.js +78 -0
  72. package/dist/billing/postgres/repositories/payment.js.map +1 -0
  73. package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
  74. package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
  75. package/dist/billing/postgres/repositories/preferences.js +23 -0
  76. package/dist/billing/postgres/repositories/preferences.js.map +1 -0
  77. package/dist/billing/postgres/repositories/refund.d.ts +7 -0
  78. package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
  79. package/dist/billing/postgres/repositories/refund.js +32 -0
  80. package/dist/billing/postgres/repositories/refund.js.map +1 -0
  81. package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
  82. package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
  83. package/dist/billing/postgres/repositories/subscription.js +187 -0
  84. package/dist/billing/postgres/repositories/subscription.js.map +1 -0
  85. package/dist/billing/postgres/repositories/topup.d.ts +27 -0
  86. package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
  87. package/dist/billing/postgres/repositories/topup.js +51 -0
  88. package/dist/billing/postgres/repositories/topup.js.map +1 -0
  89. package/dist/billing/postgres/store.d.ts +89 -0
  90. package/dist/billing/postgres/store.d.ts.map +1 -0
  91. package/dist/billing/postgres/store.js +542 -0
  92. package/dist/billing/postgres/store.js.map +1 -0
  93. package/dist/billing/service-types.d.ts +37 -0
  94. package/dist/billing/service-types.d.ts.map +1 -0
  95. package/dist/billing/service-types.js +2 -0
  96. package/dist/billing/service-types.js.map +1 -0
  97. package/dist/billing/types/auto-recharge.d.ts +56 -0
  98. package/dist/billing/types/auto-recharge.d.ts.map +1 -0
  99. package/dist/billing/types/auto-recharge.js +2 -0
  100. package/dist/billing/types/auto-recharge.js.map +1 -0
  101. package/dist/billing/types/catalog.d.ts +47 -0
  102. package/dist/billing/types/catalog.d.ts.map +1 -0
  103. package/dist/billing/types/catalog.js +2 -0
  104. package/dist/billing/types/catalog.js.map +1 -0
  105. package/dist/billing/types/common.d.ts +16 -0
  106. package/dist/billing/types/common.d.ts.map +1 -0
  107. package/dist/billing/types/common.js +2 -0
  108. package/dist/billing/types/common.js.map +1 -0
  109. package/dist/billing/types/documents.d.ts +39 -0
  110. package/dist/billing/types/documents.d.ts.map +1 -0
  111. package/dist/billing/types/documents.js +2 -0
  112. package/dist/billing/types/documents.js.map +1 -0
  113. package/dist/billing/types/events.d.ts +75 -0
  114. package/dist/billing/types/events.d.ts.map +1 -0
  115. package/dist/billing/types/events.js +38 -0
  116. package/dist/billing/types/events.js.map +1 -0
  117. package/dist/billing/types/index.d.ts +8 -0
  118. package/dist/billing/types/index.d.ts.map +1 -0
  119. package/dist/billing/types/index.js +8 -0
  120. package/dist/billing/types/index.js.map +1 -0
  121. package/dist/billing/types/preferences.d.ts +13 -0
  122. package/dist/billing/types/preferences.d.ts.map +1 -0
  123. package/dist/billing/types/preferences.js +2 -0
  124. package/dist/billing/types/preferences.js.map +1 -0
  125. package/dist/billing/types/subscriptions.d.ts +97 -0
  126. package/dist/billing/types/subscriptions.d.ts.map +1 -0
  127. package/dist/billing/types/subscriptions.js +12 -0
  128. package/dist/billing/types/subscriptions.js.map +1 -0
  129. package/dist/breakdown.d.ts +3 -1
  130. package/dist/breakdown.d.ts.map +1 -1
  131. package/dist/breakdown.js +4 -1
  132. package/dist/breakdown.js.map +1 -1
  133. package/dist/browser.d.ts +8 -0
  134. package/dist/browser.d.ts.map +1 -0
  135. package/dist/browser.js +7 -0
  136. package/dist/browser.js.map +1 -0
  137. package/dist/bursar.d.ts +84 -0
  138. package/dist/bursar.d.ts.map +1 -0
  139. package/dist/bursar.js +137 -0
  140. package/dist/bursar.js.map +1 -0
  141. package/dist/catalog.d.ts +62 -0
  142. package/dist/catalog.d.ts.map +1 -0
  143. package/dist/catalog.js +96 -0
  144. package/dist/catalog.js.map +1 -0
  145. package/dist/commerce/errors.d.ts +68 -0
  146. package/dist/commerce/errors.d.ts.map +1 -0
  147. package/dist/commerce/errors.js +105 -0
  148. package/dist/commerce/errors.js.map +1 -0
  149. package/dist/commerce/index.d.ts +6 -0
  150. package/dist/commerce/index.d.ts.map +1 -0
  151. package/dist/commerce/index.js +6 -0
  152. package/dist/commerce/index.js.map +1 -0
  153. package/dist/commerce/plan-change.d.ts +10 -0
  154. package/dist/commerce/plan-change.d.ts.map +1 -0
  155. package/dist/commerce/plan-change.js +42 -0
  156. package/dist/commerce/plan-change.js.map +1 -0
  157. package/dist/commerce/provider-registry.d.ts +18 -0
  158. package/dist/commerce/provider-registry.d.ts.map +1 -0
  159. package/dist/commerce/provider-registry.js +70 -0
  160. package/dist/commerce/provider-registry.js.map +1 -0
  161. package/dist/commerce/service.d.ts +73 -0
  162. package/dist/commerce/service.d.ts.map +1 -0
  163. package/dist/commerce/service.js +1016 -0
  164. package/dist/commerce/service.js.map +1 -0
  165. package/dist/commerce/types.d.ts +225 -0
  166. package/dist/commerce/types.d.ts.map +1 -0
  167. package/dist/commerce/types.js.map +1 -0
  168. package/dist/config/parse-access.d.ts +6 -0
  169. package/dist/config/parse-access.d.ts.map +1 -0
  170. package/dist/config/parse-access.js +72 -0
  171. package/dist/config/parse-access.js.map +1 -0
  172. package/dist/config/parse-commerce.d.ts +3 -0
  173. package/dist/config/parse-commerce.d.ts.map +1 -0
  174. package/dist/config/parse-commerce.js +236 -0
  175. package/dist/config/parse-commerce.js.map +1 -0
  176. package/dist/config/parse-credits.d.ts +3 -0
  177. package/dist/config/parse-credits.d.ts.map +1 -0
  178. package/dist/config/parse-credits.js +99 -0
  179. package/dist/config/parse-credits.js.map +1 -0
  180. package/dist/config/parse-plans.d.ts +5 -0
  181. package/dist/config/parse-plans.d.ts.map +1 -0
  182. package/dist/config/parse-plans.js +142 -0
  183. package/dist/config/parse-plans.js.map +1 -0
  184. package/dist/config/parse-pricing.d.ts +4 -0
  185. package/dist/config/parse-pricing.d.ts.map +1 -0
  186. package/dist/config/parse-pricing.js +252 -0
  187. package/dist/config/parse-pricing.js.map +1 -0
  188. package/dist/config/parse-utils.d.ts +18 -0
  189. package/dist/config/parse-utils.d.ts.map +1 -0
  190. package/dist/config/parse-utils.js +118 -0
  191. package/dist/config/parse-utils.js.map +1 -0
  192. package/dist/config/types.d.ts +340 -0
  193. package/dist/config/types.d.ts.map +1 -0
  194. package/dist/config/types.js +2 -0
  195. package/dist/config/types.js.map +1 -0
  196. package/dist/config.d.ts +12 -23
  197. package/dist/config.d.ts.map +1 -1
  198. package/dist/config.js +141 -377
  199. package/dist/config.js.map +1 -1
  200. package/dist/{stores → credits}/events.d.ts +6 -6
  201. package/dist/credits/events.d.ts.map +1 -0
  202. package/dist/{stores → credits}/events.js +5 -5
  203. package/dist/credits/events.js.map +1 -0
  204. package/dist/credits/lease-workflow.d.ts +90 -0
  205. package/dist/credits/lease-workflow.d.ts.map +1 -0
  206. package/dist/credits/lease-workflow.js +378 -0
  207. package/dist/credits/lease-workflow.js.map +1 -0
  208. package/dist/credits/low-balance-monitor.d.ts +26 -0
  209. package/dist/credits/low-balance-monitor.d.ts.map +1 -0
  210. package/dist/credits/low-balance-monitor.js +110 -0
  211. package/dist/credits/low-balance-monitor.js.map +1 -0
  212. package/dist/credits/postgres/repositories/analytics.d.ts +81 -0
  213. package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
  214. package/dist/credits/postgres/repositories/analytics.js +153 -0
  215. package/dist/credits/postgres/repositories/analytics.js.map +1 -0
  216. package/dist/credits/postgres/repositories/balance.d.ts +66 -0
  217. package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
  218. package/dist/credits/postgres/repositories/balance.js +139 -0
  219. package/dist/credits/postgres/repositories/balance.js.map +1 -0
  220. package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
  221. package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
  222. package/dist/credits/postgres/repositories/bucket.js +52 -0
  223. package/dist/credits/postgres/repositories/bucket.js.map +1 -0
  224. package/dist/credits/postgres/repositories/deduction.d.ts +64 -0
  225. package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
  226. package/dist/credits/postgres/repositories/deduction.js +154 -0
  227. package/dist/credits/postgres/repositories/deduction.js.map +1 -0
  228. package/dist/credits/postgres/repositories/lease.d.ts +69 -0
  229. package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
  230. package/dist/credits/postgres/repositories/lease.js +148 -0
  231. package/dist/credits/postgres/repositories/lease.js.map +1 -0
  232. package/dist/credits/postgres/repositories/plan.d.ts +110 -0
  233. package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
  234. package/dist/credits/postgres/repositories/plan.js +194 -0
  235. package/dist/credits/postgres/repositories/plan.js.map +1 -0
  236. package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
  237. package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
  238. package/dist/credits/postgres/repositories/pricing.js +76 -0
  239. package/dist/credits/postgres/repositories/pricing.js.map +1 -0
  240. package/dist/credits/postgres/repositories/team.d.ts +63 -0
  241. package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
  242. package/dist/credits/postgres/repositories/team.js +120 -0
  243. package/dist/credits/postgres/repositories/team.js.map +1 -0
  244. package/dist/credits/postgres/store.d.ts +89 -0
  245. package/dist/credits/postgres/store.d.ts.map +1 -0
  246. package/dist/credits/postgres/store.js +838 -0
  247. package/dist/credits/postgres/store.js.map +1 -0
  248. package/dist/credits/postgres/value-mappers.d.ts +17 -0
  249. package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
  250. package/dist/credits/postgres/value-mappers.js +102 -0
  251. package/dist/credits/postgres/value-mappers.js.map +1 -0
  252. package/dist/credits/pricing-runtime.d.ts +35 -0
  253. package/dist/credits/pricing-runtime.d.ts.map +1 -0
  254. package/dist/credits/pricing-runtime.js +141 -0
  255. package/dist/credits/pricing-runtime.js.map +1 -0
  256. package/dist/credits/queries.d.ts +33 -0
  257. package/dist/credits/queries.d.ts.map +1 -0
  258. package/dist/credits/queries.js +71 -0
  259. package/dist/credits/queries.js.map +1 -0
  260. package/dist/credits/service-errors.d.ts +4 -0
  261. package/dist/credits/service-errors.d.ts.map +1 -0
  262. package/dist/credits/service-errors.js +57 -0
  263. package/dist/credits/service-errors.js.map +1 -0
  264. package/dist/credits/service-types.d.ts +122 -0
  265. package/dist/credits/service-types.d.ts.map +1 -0
  266. package/dist/credits/service-types.js +2 -0
  267. package/dist/credits/service-types.js.map +1 -0
  268. package/dist/credits/service.d.ts +220 -0
  269. package/dist/credits/service.d.ts.map +1 -0
  270. package/dist/credits/service.js +640 -0
  271. package/dist/credits/service.js.map +1 -0
  272. package/dist/credits/store.d.ts +172 -0
  273. package/dist/credits/store.d.ts.map +1 -0
  274. package/dist/{stores/credit-store.js → credits/store.js} +27 -43
  275. package/dist/credits/store.js.map +1 -0
  276. package/dist/credits/types/account.d.ts +82 -0
  277. package/dist/credits/types/account.d.ts.map +1 -0
  278. package/dist/credits/types/account.js +2 -0
  279. package/dist/credits/types/account.js.map +1 -0
  280. package/dist/credits/types/admission.d.ts +39 -0
  281. package/dist/credits/types/admission.d.ts.map +1 -0
  282. package/dist/credits/types/admission.js +2 -0
  283. package/dist/credits/types/admission.js.map +1 -0
  284. package/dist/credits/types/analytics.d.ts +52 -0
  285. package/dist/credits/types/analytics.d.ts.map +1 -0
  286. package/dist/credits/types/analytics.js +2 -0
  287. package/dist/credits/types/analytics.js.map +1 -0
  288. package/dist/credits/types/catalog.d.ts +71 -0
  289. package/dist/credits/types/catalog.d.ts.map +1 -0
  290. package/dist/credits/types/catalog.js +2 -0
  291. package/dist/credits/types/catalog.js.map +1 -0
  292. package/dist/credits/types/grants.d.ts +24 -0
  293. package/dist/credits/types/grants.d.ts.map +1 -0
  294. package/dist/credits/types/grants.js +2 -0
  295. package/dist/credits/types/grants.js.map +1 -0
  296. package/dist/credits/types/index.d.ts +9 -0
  297. package/dist/credits/types/index.d.ts.map +1 -0
  298. package/dist/credits/types/index.js +2 -0
  299. package/dist/credits/types/index.js.map +1 -0
  300. package/dist/credits/types/ledger.d.ts +87 -0
  301. package/dist/credits/types/ledger.d.ts.map +1 -0
  302. package/dist/credits/types/ledger.js +2 -0
  303. package/dist/credits/types/ledger.js.map +1 -0
  304. package/dist/credits/types/quotas.d.ts +40 -0
  305. package/dist/credits/types/quotas.d.ts.map +1 -0
  306. package/dist/credits/types/quotas.js +2 -0
  307. package/dist/credits/types/quotas.js.map +1 -0
  308. package/dist/credits/types/teams.d.ts +44 -0
  309. package/dist/credits/types/teams.d.ts.map +1 -0
  310. package/dist/credits/types/teams.js +2 -0
  311. package/dist/credits/types/teams.js.map +1 -0
  312. package/dist/engine.d.ts +18 -39
  313. package/dist/engine.d.ts.map +1 -1
  314. package/dist/engine.js +169 -145
  315. package/dist/engine.js.map +1 -1
  316. package/dist/errors.d.ts +133 -25
  317. package/dist/errors.d.ts.map +1 -1
  318. package/dist/errors.js +173 -24
  319. package/dist/errors.js.map +1 -1
  320. package/dist/expr/ast.d.ts +46 -0
  321. package/dist/expr/ast.d.ts.map +1 -0
  322. package/dist/expr/ast.js +18 -0
  323. package/dist/expr/ast.js.map +1 -0
  324. package/dist/expr/evaluator.d.ts +4 -0
  325. package/dist/expr/evaluator.d.ts.map +1 -0
  326. package/dist/expr/evaluator.js +144 -0
  327. package/dist/expr/evaluator.js.map +1 -0
  328. package/dist/expr/language.d.ts +5 -0
  329. package/dist/expr/language.d.ts.map +1 -0
  330. package/dist/expr/language.js +73 -0
  331. package/dist/expr/language.js.map +1 -0
  332. package/dist/expr/parser.d.ts +24 -0
  333. package/dist/expr/parser.d.ts.map +1 -0
  334. package/dist/expr/parser.js +175 -0
  335. package/dist/expr/parser.js.map +1 -0
  336. package/dist/expr/tokenizer.d.ts +7 -0
  337. package/dist/expr/tokenizer.d.ts.map +1 -0
  338. package/dist/expr/tokenizer.js +85 -0
  339. package/dist/expr/tokenizer.js.map +1 -0
  340. package/dist/expr.d.ts +7 -58
  341. package/dist/expr.d.ts.map +1 -1
  342. package/dist/expr.js +41 -640
  343. package/dist/expr.js.map +1 -1
  344. package/dist/generated/pricing-config.schema.json +2792 -0
  345. package/dist/generated/pricing-config.types.d.ts +685 -0
  346. package/dist/generated/pricing-config.types.d.ts.map +1 -0
  347. package/dist/generated/pricing-config.types.js +8 -0
  348. package/dist/generated/pricing-config.types.js.map +1 -0
  349. package/dist/index.d.ts +23 -17
  350. package/dist/index.d.ts.map +1 -1
  351. package/dist/index.js +12 -10
  352. package/dist/index.js.map +1 -1
  353. package/dist/load-pricing-file.d.ts.map +1 -1
  354. package/dist/load-pricing-file.js +8 -4
  355. package/dist/load-pricing-file.js.map +1 -1
  356. package/dist/metrics.d.ts +6 -16
  357. package/dist/metrics.d.ts.map +1 -1
  358. package/dist/metrics.js.map +1 -1
  359. package/dist/node.d.ts +9 -2
  360. package/dist/node.d.ts.map +1 -1
  361. package/dist/node.js +6 -2
  362. package/dist/node.js.map +1 -1
  363. package/dist/providers/_shared.d.ts +9 -0
  364. package/dist/providers/_shared.d.ts.map +1 -0
  365. package/dist/providers/_shared.js +52 -0
  366. package/dist/providers/_shared.js.map +1 -0
  367. package/dist/providers/dodo/client-contract.d.ts +106 -0
  368. package/dist/providers/dodo/client-contract.d.ts.map +1 -0
  369. package/dist/providers/dodo/client-contract.js +2 -0
  370. package/dist/providers/dodo/client-contract.js.map +1 -0
  371. package/dist/providers/dodo/event-mapper.d.ts +5 -3
  372. package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
  373. package/dist/providers/dodo/event-mapper.js +213 -38
  374. package/dist/providers/dodo/event-mapper.js.map +1 -1
  375. package/dist/providers/dodo/index.d.ts +1 -0
  376. package/dist/providers/dodo/index.d.ts.map +1 -1
  377. package/dist/providers/dodo/index.js.map +1 -1
  378. package/dist/providers/dodo/provider.d.ts +24 -14
  379. package/dist/providers/dodo/provider.d.ts.map +1 -1
  380. package/dist/providers/dodo/provider.js +249 -31
  381. package/dist/providers/dodo/provider.js.map +1 -1
  382. package/dist/providers/index.d.ts +3 -1
  383. package/dist/providers/index.d.ts.map +1 -1
  384. package/dist/providers/index.js +1 -0
  385. package/dist/providers/index.js.map +1 -1
  386. package/dist/providers/mock/index.js.map +1 -1
  387. package/dist/providers/mock/provider.d.ts +14 -10
  388. package/dist/providers/mock/provider.d.ts.map +1 -1
  389. package/dist/providers/mock/provider.js +78 -10
  390. package/dist/providers/mock/provider.js.map +1 -1
  391. package/dist/providers/stripe/event-mapper.d.ts +3 -3
  392. package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
  393. package/dist/providers/stripe/event-mapper.js +139 -30
  394. package/dist/providers/stripe/event-mapper.js.map +1 -1
  395. package/dist/providers/stripe/index.js.map +1 -1
  396. package/dist/providers/stripe/provider.d.ts +21 -12
  397. package/dist/providers/stripe/provider.d.ts.map +1 -1
  398. package/dist/providers/stripe/provider.js +175 -17
  399. package/dist/providers/stripe/provider.js.map +1 -1
  400. package/dist/providers/types.d.ts +128 -32
  401. package/dist/providers/types.d.ts.map +1 -1
  402. package/dist/providers/types.js +17 -1
  403. package/dist/providers/types.js.map +1 -1
  404. package/dist/retry.d.ts +35 -0
  405. package/dist/retry.d.ts.map +1 -0
  406. package/dist/retry.js +70 -0
  407. package/dist/retry.js.map +1 -0
  408. package/dist/shared/diagnostics.d.ts +7 -0
  409. package/dist/shared/diagnostics.d.ts.map +1 -0
  410. package/dist/shared/diagnostics.js +18 -0
  411. package/dist/shared/diagnostics.js.map +1 -0
  412. package/dist/shared/logger.d.ts +10 -0
  413. package/dist/shared/logger.d.ts.map +1 -0
  414. package/dist/shared/logger.js +15 -0
  415. package/dist/shared/logger.js.map +1 -0
  416. package/dist/shared/postgres-client.d.ts +84 -0
  417. package/dist/shared/postgres-client.d.ts.map +1 -0
  418. package/dist/shared/postgres-client.js +337 -0
  419. package/dist/shared/postgres-client.js.map +1 -0
  420. package/dist/shared/postgres-errors.d.ts +11 -0
  421. package/dist/shared/postgres-errors.d.ts.map +1 -0
  422. package/dist/shared/postgres-errors.js +105 -0
  423. package/dist/shared/postgres-errors.js.map +1 -0
  424. package/dist/shared/postgres-types.d.ts +7 -0
  425. package/dist/shared/postgres-types.d.ts.map +1 -0
  426. package/dist/shared/postgres-types.js +2 -0
  427. package/dist/shared/postgres-types.js.map +1 -0
  428. package/dist/shared/postgres-validation.d.ts +8 -0
  429. package/dist/shared/postgres-validation.d.ts.map +1 -0
  430. package/dist/shared/postgres-validation.js +40 -0
  431. package/dist/shared/postgres-validation.js.map +1 -0
  432. package/dist/storage/adapters/clickhouse.d.ts +61 -0
  433. package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
  434. package/dist/storage/adapters/clickhouse.js +347 -0
  435. package/dist/storage/adapters/clickhouse.js.map +1 -0
  436. package/dist/storage/adapters/s3.d.ts +35 -0
  437. package/dist/storage/adapters/s3.d.ts.map +1 -0
  438. package/dist/storage/adapters/s3.js +100 -0
  439. package/dist/storage/adapters/s3.js.map +1 -0
  440. package/dist/storage/outbox-worker.d.ts +41 -0
  441. package/dist/storage/outbox-worker.d.ts.map +1 -0
  442. package/dist/storage/outbox-worker.js +146 -0
  443. package/dist/storage/outbox-worker.js.map +1 -0
  444. package/dist/storage/ports.d.ts +78 -0
  445. package/dist/storage/ports.d.ts.map +1 -0
  446. package/dist/storage/ports.js +2 -0
  447. package/dist/storage/ports.js.map +1 -0
  448. package/dist/storage/postgres-repository.d.ts +14 -0
  449. package/dist/storage/postgres-repository.d.ts.map +1 -0
  450. package/dist/storage/postgres-repository.js +140 -0
  451. package/dist/storage/postgres-repository.js.map +1 -0
  452. package/dist/storage/runtime.d.ts +77 -0
  453. package/dist/storage/runtime.d.ts.map +1 -0
  454. package/dist/storage/runtime.js +369 -0
  455. package/dist/storage/runtime.js.map +1 -0
  456. package/package.json +43 -21
  457. package/dist/allowance.d.ts +0 -48
  458. package/dist/allowance.d.ts.map +0 -1
  459. package/dist/allowance.js +0 -140
  460. package/dist/allowance.js.map +0 -1
  461. package/dist/billing/billing-manager.d.ts +0 -66
  462. package/dist/billing/billing-manager.d.ts.map +0 -1
  463. package/dist/billing/billing-manager.js +0 -560
  464. package/dist/billing/billing-manager.js.map +0 -1
  465. package/dist/billing/billing-types.d.ts +0 -203
  466. package/dist/billing/billing-types.d.ts.map +0 -1
  467. package/dist/billing/billing-types.js +0 -6
  468. package/dist/billing/billing-types.js.map +0 -1
  469. package/dist/billing/memory-billing-store.d.ts +0 -37
  470. package/dist/billing/memory-billing-store.d.ts.map +0 -1
  471. package/dist/billing/memory-billing-store.js +0 -197
  472. package/dist/billing/memory-billing-store.js.map +0 -1
  473. package/dist/billing/postgres-billing-store.d.ts +0 -35
  474. package/dist/billing/postgres-billing-store.d.ts.map +0 -1
  475. package/dist/billing/postgres-billing-store.js +0 -274
  476. package/dist/billing/postgres-billing-store.js.map +0 -1
  477. package/dist/billing/supabase-billing-store.d.ts +0 -29
  478. package/dist/billing/supabase-billing-store.d.ts.map +0 -1
  479. package/dist/billing/supabase-billing-store.js +0 -250
  480. package/dist/billing/supabase-billing-store.js.map +0 -1
  481. package/dist/manager.d.ts +0 -444
  482. package/dist/manager.d.ts.map +0 -1
  483. package/dist/manager.js +0 -1153
  484. package/dist/manager.js.map +0 -1
  485. package/dist/stores/credit-store.d.ts +0 -198
  486. package/dist/stores/credit-store.d.ts.map +0 -1
  487. package/dist/stores/credit-store.js.map +0 -1
  488. package/dist/stores/events.d.ts.map +0 -1
  489. package/dist/stores/events.js.map +0 -1
  490. package/dist/stores/memory-store.d.ts +0 -187
  491. package/dist/stores/memory-store.d.ts.map +0 -1
  492. package/dist/stores/memory-store.js +0 -1890
  493. package/dist/stores/memory-store.js.map +0 -1
  494. package/dist/stores/postgres-store.d.ts +0 -100
  495. package/dist/stores/postgres-store.d.ts.map +0 -1
  496. package/dist/stores/postgres-store.js +0 -891
  497. package/dist/stores/postgres-store.js.map +0 -1
  498. package/dist/stores/supabase-store.d.ts +0 -87
  499. package/dist/stores/supabase-store.d.ts.map +0 -1
  500. package/dist/stores/supabase-store.js +0 -948
  501. package/dist/stores/supabase-store.js.map +0 -1
  502. package/dist/types.d.ts +0 -463
  503. package/dist/types.d.ts.map +0 -1
  504. package/dist/types.js.map +0 -1
  505. /package/dist/{types.js → commerce/types.js} +0 -0
@@ -0,0 +1,236 @@
1
+ import { asDecimal, asInteger, asObject, asString, parseAvailability, parseBillingInterval, parseDuration, parseExpiry, parseWindow, semanticError, validateIdentifiers, } from "./parse-utils.js";
2
+ function parseProviderReference(value) {
3
+ const raw = asObject(value);
4
+ if (raw.type === "stripe_price") {
5
+ return { type: "stripe_price", priceId: asString(raw.price_id) };
6
+ }
7
+ if (raw.type === "dodo_product") {
8
+ return { type: "dodo_product", productId: asString(raw.product_id) };
9
+ }
10
+ return {
11
+ type: "custom_object",
12
+ objectKind: raw.object_kind,
13
+ externalId: asString(raw.external_id),
14
+ };
15
+ }
16
+ function providerExternalId(reference) {
17
+ switch (reference.type) {
18
+ case "stripe_price":
19
+ return reference.priceId;
20
+ case "dodo_product":
21
+ return reference.productId;
22
+ case "custom_object":
23
+ return reference.externalId;
24
+ }
25
+ }
26
+ function providerReferenceIsCompatible(provider, reference) {
27
+ return ((provider.type === "stripe" && reference.type === "stripe_price") ||
28
+ (provider.type === "dodo" && reference.type === "dodo_product") ||
29
+ (provider.type === "custom" && reference.type === "custom_object"));
30
+ }
31
+ function validateCurrency(value, path) {
32
+ if (!/^[A-Z]{3}$/.test(value)) {
33
+ semanticError(`${path} must be an uppercase ISO-4217 currency code`);
34
+ }
35
+ return value;
36
+ }
37
+ function validateIntegerRange(value, path) {
38
+ if (value.minimum > value.maximum ||
39
+ value.default < value.minimum ||
40
+ value.default > value.maximum) {
41
+ semanticError(`${path} requires minimum <= default <= maximum`);
42
+ }
43
+ }
44
+ export function parseCommerce(value, credits) {
45
+ const raw = asObject(value ?? {});
46
+ const providersRaw = asObject(raw.providers ?? {});
47
+ const offersRaw = asObject(raw.offers ?? {});
48
+ validateIdentifiers(providersRaw, "commerce.providers");
49
+ validateIdentifiers(offersRaw, "commerce.offers");
50
+ const providers = Object.fromEntries(Object.entries(providersRaw).map(([key, input]) => {
51
+ const provider = asObject(input);
52
+ return [
53
+ key,
54
+ provider.type === "custom"
55
+ ? { type: "custom", adapter: asString(provider.adapter) }
56
+ : { type: provider.type },
57
+ ];
58
+ }));
59
+ const offers = {};
60
+ const seenProviderObjects = new Set();
61
+ for (const [offerKey, input] of Object.entries(offersRaw)) {
62
+ const offer = asObject(input);
63
+ const references = Object.fromEntries(Object.entries(asObject(offer.providers)).map(([providerKey, inputReference]) => {
64
+ const provider = providers[providerKey];
65
+ if (!provider) {
66
+ semanticError(`commerce.offers.${offerKey} references unknown provider`);
67
+ }
68
+ const reference = parseProviderReference(inputReference);
69
+ if (!providerReferenceIsCompatible(provider, reference)) {
70
+ semanticError(`commerce.offers.${offerKey} has incompatible provider reference`);
71
+ }
72
+ const uniqueKey = `${providerKey}/${providerExternalId(reference)}`;
73
+ if (seenProviderObjects.has(uniqueKey)) {
74
+ semanticError(`duplicate provider object reference ${uniqueKey}`);
75
+ }
76
+ seenProviderObjects.add(uniqueKey);
77
+ return [providerKey, reference];
78
+ }));
79
+ const common = {
80
+ displayName: asString(offer.display_name),
81
+ ...(offer.description == null ? {} : { description: asString(offer.description) }),
82
+ sortOrder: asInteger(offer.sort_order ?? 0),
83
+ ...(offer.availability == null
84
+ ? {}
85
+ : { availability: parseAvailability(offer.availability) }),
86
+ price: {
87
+ amountMinor: asInteger(asObject(offer.price).amount_minor),
88
+ currency: validateCurrency(asString(asObject(offer.price).currency), `commerce.offers.${offerKey}.price.currency`),
89
+ taxBehavior: (asObject(offer.price).tax_behavior ??
90
+ "unspecified"),
91
+ },
92
+ providers: references,
93
+ };
94
+ if (offer.type === "subscription") {
95
+ const cycleRaw = offer.cycle_grant == null ? undefined : asObject(offer.cycle_grant);
96
+ const cycleGrant = cycleRaw == null
97
+ ? undefined
98
+ : {
99
+ amount: asDecimal(cycleRaw.amount),
100
+ bucket: asString(cycleRaw.bucket),
101
+ renewal: cycleRaw.renewal,
102
+ expiry: parseExpiry(cycleRaw.expiry ?? { type: "subscription_end" }, `commerce.offers.${offerKey}.cycle_grant.expiry`),
103
+ };
104
+ if (cycleGrant != null && !credits.buckets[cycleGrant.bucket]) {
105
+ semanticError(`commerce.offers.${offerKey}.cycle_grant references unknown bucket`);
106
+ }
107
+ offers[offerKey] = {
108
+ ...common,
109
+ type: "subscription",
110
+ plan: asString(offer.plan),
111
+ billingInterval: parseBillingInterval(offer.billing_interval),
112
+ ...(offer.trial == null ? {} : { trial: parseBillingInterval(offer.trial) }),
113
+ ...(cycleGrant == null ? {} : { cycleGrant }),
114
+ };
115
+ continue;
116
+ }
117
+ const bucket = asString(offer.bucket);
118
+ if (!credits.buckets[bucket]) {
119
+ semanticError(`commerce.offers.${offerKey}.bucket references unknown bucket`);
120
+ }
121
+ const quantityRaw = asObject(offer.quantity ?? {});
122
+ const parsedQuantity = {
123
+ minimum: asInteger(quantityRaw.minimum ?? 1),
124
+ maximum: asInteger(quantityRaw.maximum ?? 1),
125
+ default: asInteger(quantityRaw.default ?? 1),
126
+ };
127
+ validateIntegerRange(parsedQuantity, `commerce.offers.${offerKey}.quantity`);
128
+ const parsedExpiry = offer.expiry == null
129
+ ? undefined
130
+ : parseExpiry(offer.expiry, `commerce.offers.${offerKey}.expiry`);
131
+ if (parsedExpiry?.type === "subscription_end") {
132
+ semanticError(`commerce.offers.${offerKey} top-up cannot use subscription_end expiry`);
133
+ }
134
+ offers[offerKey] = {
135
+ ...common,
136
+ type: "topup",
137
+ creditsPerUnit: asDecimal(offer.credits_per_unit),
138
+ quantity: parsedQuantity,
139
+ bucket,
140
+ ...(parsedExpiry == null ? {} : { expiry: parsedExpiry }),
141
+ lotBehavior: (offer.lot_behavior ?? "separate_lots"),
142
+ };
143
+ }
144
+ const subscriptionChanges = parseSubscriptionChanges(raw.subscription_changes);
145
+ const autoRecharge = raw.auto_recharge == null ? undefined : parseAutoRecharge(raw.auto_recharge, offers);
146
+ return {
147
+ providers,
148
+ offers,
149
+ ...(subscriptionChanges == null ? {} : { subscriptionChanges }),
150
+ ...(autoRecharge == null ? {} : { autoRecharge }),
151
+ };
152
+ }
153
+ const SUBSCRIPTION_CHANGE_CLASSIFICATIONS = [
154
+ "upgrade",
155
+ "downgrade",
156
+ "lateral",
157
+ "cadence_change",
158
+ ];
159
+ function parseSubscriptionChanges(value) {
160
+ if (value == null)
161
+ return undefined;
162
+ const raw = asObject(value);
163
+ const result = {};
164
+ for (const classification of SUBSCRIPTION_CHANGE_CLASSIFICATIONS) {
165
+ if (raw[classification] == null)
166
+ continue;
167
+ const policy = asObject(raw[classification]);
168
+ result[classification] = {
169
+ effective: asString(policy.effective),
170
+ proration: asString(policy.proration),
171
+ paymentFailure: asString(policy.payment_failure ?? "prevent_change"),
172
+ };
173
+ }
174
+ return result;
175
+ }
176
+ function parseAutoRecharge(value, offers) {
177
+ const auto = asObject(value);
178
+ const threshold = asObject(auto.balance_below);
179
+ const quantity = asObject(auto.quantity);
180
+ const limits = asObject(auto.limits);
181
+ const eligibleTopups = auto.eligible_topups;
182
+ const currencies = new Set(eligibleTopups.map((key) => {
183
+ const offer = offers[key];
184
+ if (offer?.type !== "topup") {
185
+ semanticError(`commerce.auto_recharge references non-top-up offer '${key}'`);
186
+ }
187
+ return offer.price.currency;
188
+ }));
189
+ if (currencies.size !== 1) {
190
+ semanticError("commerce.auto_recharge eligible top-ups must use one currency");
191
+ }
192
+ const parsedQuantity = {
193
+ minimum: asInteger(quantity.minimum),
194
+ maximum: asInteger(quantity.maximum),
195
+ default: asInteger(quantity.default),
196
+ };
197
+ validateIntegerRange(parsedQuantity, "commerce.auto_recharge.quantity");
198
+ const parsedMinimum = asDecimal(threshold.minimum);
199
+ const parsedMaximum = asDecimal(threshold.maximum);
200
+ const parsedDefault = asDecimal(threshold.default);
201
+ if (parsedMinimum.gt(parsedMaximum) ||
202
+ parsedDefault.lt(parsedMinimum) ||
203
+ parsedDefault.gt(parsedMaximum)) {
204
+ semanticError("commerce.auto_recharge.balance_below requires minimum <= default <= maximum");
205
+ }
206
+ for (const key of eligibleTopups) {
207
+ const offer = offers[key];
208
+ if (parsedQuantity.minimum < offer.quantity.minimum ||
209
+ parsedQuantity.maximum > offer.quantity.maximum) {
210
+ semanticError(`commerce.auto_recharge.quantity must fit commerce.offers.${key}.quantity`);
211
+ }
212
+ }
213
+ const parsedRearmAbove = asDecimal(auto.rearm_above);
214
+ if (parsedRearmAbove.lte(parsedMaximum)) {
215
+ semanticError("commerce.auto_recharge.rearm_above must exceed balance_below.maximum");
216
+ }
217
+ return {
218
+ eligibleTopups,
219
+ balanceBelow: {
220
+ minimum: parsedMinimum,
221
+ maximum: parsedMaximum,
222
+ default: parsedDefault,
223
+ },
224
+ rearmAbove: parsedRearmAbove,
225
+ quantity: parsedQuantity,
226
+ limits: {
227
+ maxPurchases: asInteger(limits.max_purchases),
228
+ window: parseWindow(limits.window, "commerce.auto_recharge.limits.window"),
229
+ maxChargeMinor: asInteger(limits.max_charge_minor),
230
+ cooldown: parseDuration(limits.cooldown),
231
+ maxConsecutiveFailures: asInteger(limits.max_consecutive_failures ?? 3),
232
+ failureAction: "pause",
233
+ },
234
+ };
235
+ }
236
+ //# sourceMappingURL=parse-commerce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-commerce.js","sourceRoot":"","sources":["../../src/config/parse-commerce.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAgB1B,SAAS,sBAAsB,CAAC,KAAc;IAC5C,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC5B,IAAI,GAAG,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;QAChC,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,CAAC,GAAG,CAAC,Q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,GAAG,CAAC,aAAa,CAAC,EAAE,CAAC;QACxC,aAAa,CAAC,sEAAsE,CAAC,CAAC;IACxF,CAAC;IACD,OAAO;QACL,cAAc;QACd,YAAY,EAAE;YACZ,OAAO,EAAE,aAAa;YACtB,OAAO,EAAE,aAAa;YACtB,OAAO,EAAE,aAAa;SACvB;QACD,UAAU,EAAE,gBAAgB;QAC5B,QAAQ,EAAE,cAAc;QACxB,MAAM,EAAE;YACN,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC;YAC7C,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,sCAAsC,CAGxE;YACD,cAAc,EAAE,SAAS,CAAC,MAAM,CAAC,gBAAgB,CAAC;YAClD,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC;YACxC,sBAAsB,EAAE,SAAS,CAAC,MAAM,CAAC,wBAAwB,IAAI,CAAC,CAAC;YACvE,aAAa,EAAE,OAAO;SACvB;KACF,CAAC;AACJ,CAAC","sourcesContent":["import {\n asDecimal,\n asInteger,\n asObject,\n asString,\n parseAvailability,\n parseBillingInterval,\n parseDuration,\n parseExpiry,\n parseWindow,\n semanticError,\n validateIdentifiers,\n} from \"./parse-utils.js\";\nimport type {\n AutoRechargeGuardrails,\n CommerceConfig,\n CommerceOffer,\n CreditsConfig,\n OfferCommon,\n OfferPrice,\n ProviderDefinition,\n ProviderReference,\n SubscriptionChangeClassification,\n SubscriptionChangePolicy,\n TopupOffer,\n Window,\n} from \"./types.js\";\n\nfunction parseProviderReference(value: unknown): ProviderReference {\n const raw = asObject(value);\n if (raw.type === \"stripe_price\") {\n return { type: \"stripe_price\", priceId: asString(raw.price_id) };\n }\n if (raw.type === \"dodo_product\") {\n return { type: \"dodo_product\", productId: asString(raw.product_id) };\n }\n return {\n type: \"custom_object\",\n objectKind: raw.object_kind as \"subscription\" | \"one_time\",\n externalId: asString(raw.external_id),\n };\n}\n\nfunction providerExternalId(reference: ProviderReference): string {\n switch (reference.type) {\n case \"stripe_price\":\n return reference.priceId;\n case \"dodo_product\":\n return reference.productId;\n case \"custom_object\":\n return reference.externalId;\n }\n}\n\nfunction providerReferenceIsCompatible(\n provider: ProviderDefinition,\n reference: ProviderReference,\n): boolean {\n return (\n (provider.type === \"stripe\" && reference.type === \"stripe_price\") ||\n (provider.type === \"dodo\" && reference.type === \"dodo_product\") ||\n (provider.type === \"custom\" && reference.type === \"custom_object\")\n );\n}\n\nfunction validateCurrency(value: string, path: string): string {\n if (!/^[A-Z]{3}$/.test(value)) {\n semanticError(`${path} must be an uppercase ISO-4217 currency code`);\n }\n return value;\n}\n\nfunction validateIntegerRange(\n value: { minimum: number; maximum: number; default: number },\n path: string,\n): void {\n if (\n value.minimum > value.maximum ||\n value.default < value.minimum ||\n value.default > value.maximum\n ) {\n semanticError(`${path} requires minimum <= default <= maximum`);\n }\n}\n\nexport function parseCommerce(value: unknown, credits: CreditsConfig): CommerceConfig {\n const raw = asObject(value ?? {});\n const providersRaw = asObject(raw.providers ?? {});\n const offersRaw = asObject(raw.offers ?? {});\n validateIdentifiers(providersRaw, \"commerce.providers\");\n validateIdentifiers(offersRaw, \"commerce.offers\");\n\n const providers = Object.fromEntries(\n Object.entries(providersRaw).map(([key, input]) => {\n const provider = asObject(input);\n return [\n key,\n provider.type === \"custom\"\n ? ({ type: \"custom\", adapter: asString(provider.adapter) } as const)\n : ({ type: provider.type } as ProviderDefinition),\n ];\n }),\n );\n\n const offers: Record<string, CommerceOffer> = {};\n const seenProviderObjects = new Set<string>();\n for (const [offerKey, input] of Object.entries(offersRaw)) {\n const offer = asObject(input);\n const references = Object.fromEntries(\n Object.entries(asObject(offer.providers)).map(([providerKey, inputReference]) => {\n const provider = providers[providerKey];\n if (!provider) {\n semanticError(`commerce.offers.${offerKey} references unknown provider`);\n }\n const reference = parseProviderReference(inputReference);\n if (!providerReferenceIsCompatible(provider, reference)) {\n semanticError(`commerce.offers.${offerKey} has incompatible provider reference`);\n }\n const uniqueKey = `${providerKey}/${providerExternalId(reference)}`;\n if (seenProviderObjects.has(uniqueKey)) {\n semanticError(`duplicate provider object reference ${uniqueKey}`);\n }\n seenProviderObjects.add(uniqueKey);\n return [providerKey, reference];\n }),\n );\n\n const common: OfferCommon = {\n displayName: asString(offer.display_name),\n ...(offer.description == null ? {} : { description: asString(offer.description) }),\n sortOrder: asInteger(offer.sort_order ?? 0),\n ...(offer.availability == null\n ? {}\n : { availability: parseAvailability(offer.availability) }),\n price: {\n amountMinor: asInteger(asObject(offer.price).amount_minor),\n currency: validateCurrency(\n asString(asObject(offer.price).currency),\n `commerce.offers.${offerKey}.price.currency`,\n ),\n taxBehavior: (asObject(offer.price).tax_behavior ??\n \"unspecified\") as OfferPrice[\"taxBehavior\"],\n },\n providers: references,\n };\n\n if (offer.type === \"subscription\") {\n const cycleRaw = offer.cycle_grant == null ? undefined : asObject(offer.cycle_grant);\n const cycleGrant =\n cycleRaw == null\n ? undefined\n : {\n amount: asDecimal(cycleRaw.amount),\n bucket: asString(cycleRaw.bucket),\n renewal: cycleRaw.renewal as \"replace_previous\" | \"accumulate\",\n expiry: parseExpiry(\n cycleRaw.expiry ?? { type: \"subscription_end\" },\n `commerce.offers.${offerKey}.cycle_grant.expiry`,\n ),\n };\n if (cycleGrant != null && !credits.buckets[cycleGrant.bucket]) {\n semanticError(`commerce.offers.${offerKey}.cycle_grant references unknown bucket`);\n }\n offers[offerKey] = {\n ...common,\n type: \"subscription\",\n plan: asString(offer.plan),\n billingInterval: parseBillingInterval(offer.billing_interval),\n ...(offer.trial == null ? {} : { trial: parseBillingInterval(offer.trial) }),\n ...(cycleGrant == null ? {} : { cycleGrant }),\n };\n continue;\n }\n\n const bucket = asString(offer.bucket);\n if (!credits.buckets[bucket]) {\n semanticError(`commerce.offers.${offerKey}.bucket references unknown bucket`);\n }\n const quantityRaw = asObject(offer.quantity ?? {});\n const parsedQuantity = {\n minimum: asInteger(quantityRaw.minimum ?? 1),\n maximum: asInteger(quantityRaw.maximum ?? 1),\n default: asInteger(quantityRaw.default ?? 1),\n };\n validateIntegerRange(parsedQuantity, `commerce.offers.${offerKey}.quantity`);\n const parsedExpiry =\n offer.expiry == null\n ? undefined\n : parseExpiry(offer.expiry, `commerce.offers.${offerKey}.expiry`);\n if (parsedExpiry?.type === \"subscription_end\") {\n semanticError(`commerce.offers.${offerKey} top-up cannot use subscription_end expiry`);\n }\n offers[offerKey] = {\n ...common,\n type: \"topup\",\n creditsPerUnit: asDecimal(offer.credits_per_unit),\n quantity: parsedQuantity,\n bucket,\n ...(parsedExpiry == null ? {} : { expiry: parsedExpiry }),\n lotBehavior: (offer.lot_behavior ?? \"separate_lots\") as TopupOffer[\"lotBehavior\"],\n };\n }\n\n const subscriptionChanges = parseSubscriptionChanges(raw.subscription_changes);\n const autoRecharge =\n raw.auto_recharge == null ? undefined : parseAutoRecharge(raw.auto_recharge, offers);\n return {\n providers,\n offers,\n ...(subscriptionChanges == null ? {} : { subscriptionChanges }),\n ...(autoRecharge == null ? {} : { autoRecharge }),\n };\n}\n\nconst SUBSCRIPTION_CHANGE_CLASSIFICATIONS = [\n \"upgrade\",\n \"downgrade\",\n \"lateral\",\n \"cadence_change\",\n] as const satisfies readonly SubscriptionChangeClassification[];\n\nfunction parseSubscriptionChanges(\n value: unknown,\n): CommerceConfig[\"subscriptionChanges\"] | undefined {\n if (value == null) return undefined;\n const raw = asObject(value);\n const result: Partial<Record<SubscriptionChangeClassification, SubscriptionChangePolicy>> = {};\n for (const classification of SUBSCRIPTION_CHANGE_CLASSIFICATIONS) {\n if (raw[classification] == null) continue;\n const policy = asObject(raw[classification]);\n result[classification] = {\n effective: asString(policy.effective) as SubscriptionChangePolicy[\"effective\"],\n proration: asString(policy.proration) as SubscriptionChangePolicy[\"proration\"],\n paymentFailure: asString(\n policy.payment_failure ?? \"prevent_change\",\n ) as SubscriptionChangePolicy[\"paymentFailure\"],\n };\n }\n return result;\n}\n\nfunction parseAutoRecharge(\n value: unknown,\n offers: Record<string, CommerceOffer>,\n): AutoRechargeGuardrails {\n const auto = asObject(value);\n const threshold = asObject(auto.balance_below);\n const quantity = asObject(auto.quantity);\n const limits = asObject(auto.limits);\n const eligibleTopups = auto.eligible_topups as string[];\n const currencies = new Set(\n eligibleTopups.map((key) => {\n const offer = offers[key];\n if (offer?.type !== \"topup\") {\n semanticError(`commerce.auto_recharge references non-top-up offer '${key}'`);\n }\n return offer.price.currency;\n }),\n );\n if (currencies.size !== 1) {\n semanticError(\"commerce.auto_recharge eligible top-ups must use one currency\");\n }\n const parsedQuantity = {\n minimum: asInteger(quantity.minimum),\n maximum: asInteger(quantity.maximum),\n default: asInteger(quantity.default),\n };\n validateIntegerRange(parsedQuantity, \"commerce.auto_recharge.quantity\");\n const parsedMinimum = asDecimal(threshold.minimum);\n const parsedMaximum = asDecimal(threshold.maximum);\n const parsedDefault = asDecimal(threshold.default);\n if (\n parsedMinimum.gt(parsedMaximum) ||\n parsedDefault.lt(parsedMinimum) ||\n parsedDefault.gt(parsedMaximum)\n ) {\n semanticError(\"commerce.auto_recharge.balance_below requires minimum <= default <= maximum\");\n }\n for (const key of eligibleTopups) {\n const offer = offers[key] as TopupOffer;\n if (\n parsedQuantity.minimum < offer.quantity.minimum ||\n parsedQuantity.maximum > offer.quantity.maximum\n ) {\n semanticError(`commerce.auto_recharge.quantity must fit commerce.offers.${key}.quantity`);\n }\n }\n const parsedRearmAbove = asDecimal(auto.rearm_above);\n if (parsedRearmAbove.lte(parsedMaximum)) {\n semanticError(\"commerce.auto_recharge.rearm_above must exceed balance_below.maximum\");\n }\n return {\n eligibleTopups,\n balanceBelow: {\n minimum: parsedMinimum,\n maximum: parsedMaximum,\n default: parsedDefault,\n },\n rearmAbove: parsedRearmAbove,\n quantity: parsedQuantity,\n limits: {\n maxPurchases: asInteger(limits.max_purchases),\n window: parseWindow(limits.window, \"commerce.auto_recharge.limits.window\") as Extract<\n Window,\n { type: \"calendar\" | \"rolling\" }\n >,\n maxChargeMinor: asInteger(limits.max_charge_minor),\n cooldown: parseDuration(limits.cooldown),\n maxConsecutiveFailures: asInteger(limits.max_consecutive_failures ?? 3),\n failureAction: \"pause\",\n },\n };\n}\n"]}
@@ -0,0 +1,3 @@
1
+ import type { CreditsConfig } from "./types.js";
2
+ export declare function parseCredits(value: unknown): CreditsConfig;
3
+ //# sourceMappingURL=parse-credits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-credits.d.ts","sourceRoot":"","sources":["../../src/config/parse-credits.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAgB,aAAa,EAAgB,MAAM,YAAY,CAAC;AAE5E,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CA8G1D"}
@@ -0,0 +1,99 @@
1
+ import { asDecimal, asInteger, asObject, asString, parseAvailability, parseExpiry, semanticError, validateIdentifiers, validateRegions, } from "./parse-utils.js";
2
+ export function parseCredits(value) {
3
+ const raw = asObject(value);
4
+ const accountingRaw = asObject(raw.accounting ?? { unit: "credit", scale: 6, rounding: "half_up" });
5
+ const bucketsRaw = asObject(raw.buckets ?? {});
6
+ const policiesRaw = asObject(raw.policies ?? {});
7
+ const programsRaw = asObject(raw.grant_programs ?? {});
8
+ const displayRaw = raw.display == null ? null : asObject(raw.display);
9
+ validateIdentifiers(bucketsRaw, "credits.buckets");
10
+ validateIdentifiers(policiesRaw, "credits.policies");
11
+ validateIdentifiers(programsRaw, "credits.grant_programs");
12
+ const buckets = Object.fromEntries(Object.entries(bucketsRaw).map(([key, input]) => {
13
+ const bucket = asObject(input);
14
+ const parsedExpiry = parseExpiry(bucket.expiry ?? { type: "never" }, `credits.buckets.${key}.expiry`);
15
+ if (parsedExpiry.type === "subscription_end") {
16
+ semanticError(`credits.buckets.${key}.expiry cannot be subscription_end`);
17
+ }
18
+ return [key, { priority: asInteger(bucket.priority), expiry: parsedExpiry }];
19
+ }));
20
+ const priorities = Object.values(buckets).map((bucket) => bucket.priority);
21
+ if (new Set(priorities).size !== priorities.length) {
22
+ semanticError("credits bucket priorities must be unique");
23
+ }
24
+ const defaultBucket = raw.default_bucket == null ? undefined : asString(raw.default_bucket);
25
+ if (defaultBucket != null && !buckets[defaultBucket]) {
26
+ semanticError("credits.default_bucket references an unknown bucket");
27
+ }
28
+ const policies = {};
29
+ for (const [key, input] of Object.entries(policiesRaw)) {
30
+ const policy = asObject(input);
31
+ policies[key] =
32
+ policy.type === "credit_line"
33
+ ? { type: "credit_line", limit: asDecimal(policy.limit) }
34
+ : { type: "prepaid" };
35
+ }
36
+ const grantPrograms = {};
37
+ for (const [key, input] of Object.entries(programsRaw)) {
38
+ const program = asObject(input);
39
+ const awards = program.awards.map((awardInput) => {
40
+ const award = asObject(awardInput);
41
+ const bucket = asString(award.bucket);
42
+ if (!buckets[bucket]) {
43
+ semanticError(`grant program '${key}' references unknown bucket '${bucket}'`);
44
+ }
45
+ const parsedExpiry = award.expiry == null
46
+ ? undefined
47
+ : parseExpiry(award.expiry, `credits.grant_programs.${key}`);
48
+ if (parsedExpiry?.type === "subscription_end") {
49
+ semanticError(`grant program '${key}' cannot use subscription_end expiry`);
50
+ }
51
+ return {
52
+ recipient: (award.recipient ?? "subject"),
53
+ amount: asDecimal(award.amount),
54
+ bucket,
55
+ ...(parsedExpiry ? { expiry: parsedExpiry } : {}),
56
+ };
57
+ });
58
+ const eligibilityRaw = asObject(program.eligibility ?? {});
59
+ const eligibilityRegions = (eligibilityRaw.regions ?? []);
60
+ validateRegions(eligibilityRegions, `credits.grant_programs.${key}.eligibility.regions`);
61
+ if (program.trigger !== "referral_completed" &&
62
+ awards.some((award) => award.recipient === "referrer")) {
63
+ semanticError(`grant program '${key}' referrer awards require referral_completed`);
64
+ }
65
+ grantPrograms[key] = {
66
+ trigger: program.trigger,
67
+ awards,
68
+ ...(program.availability == null
69
+ ? {}
70
+ : { availability: parseAvailability(program.availability) }),
71
+ eligibility: {
72
+ plans: (eligibilityRaw.plans ?? []),
73
+ regions: eligibilityRegions,
74
+ },
75
+ maxAwardsPerSubject: asInteger(program.max_awards_per_subject ?? 1),
76
+ idempotencyScope: (program.idempotency_scope ?? "subject"),
77
+ };
78
+ }
79
+ return {
80
+ accounting: {
81
+ unit: accountingRaw.unit,
82
+ scale: asInteger(accountingRaw.scale),
83
+ rounding: accountingRaw.rounding,
84
+ },
85
+ buckets,
86
+ ...(defaultBucket == null ? {} : { defaultBucket }),
87
+ policies,
88
+ grantPrograms,
89
+ ...(displayRaw
90
+ ? {
91
+ display: {
92
+ currency: asString(displayRaw.currency).toUpperCase(),
93
+ unitsPerMajor: asDecimal(displayRaw.units_per_major),
94
+ },
95
+ }
96
+ : {}),
97
+ };
98
+ }
99
+ //# sourceMappingURL=parse-credits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-credits.js","sourceRoot":"","sources":["../../src/config/parse-credits.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,iBAAiB,EACjB,WAAW,EACX,aAAa,EACb,mBAAmB,EACnB,eAAe,GAChB,MAAM,kBAAkB,CAAC;AAG1B,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC5B,MAAM,aAAa,GAAG,QAAQ,CAC5B,GAAG,CAAC,UAAU,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,CACpE,CAAC;IACF,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IAC/C,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IACjD,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,cAAc,IAAI,EAAE,CAAC,CAAC;IACvD,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,IAAI,IAAI,CAAC,CAAC,CA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CAAa;gBAC/C,OAAO,EAAE,kBAAkB;aAC5B;YACD,mBAAmB,EAAE,SAAS,CAAC,OAAO,CAAC,sBAAsB,IAAI,CAAC,CAAC;YACnE,gBAAgB,EAAE,CAAC,OAAO,CAAC,iBAAiB,IAAI,SAAS,CAAwB;SAClF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,UAAU,EAAE;YACV,IAAI,EAAE,aAAa,CAAC,IAAgB;YACpC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,KAAK,CAAM;YAC1C,QAAQ,EAAE,aAAa,CAAC,QAAqB;SAC9C;QACD,OAAO;QACP,GAAG,CAAC,aAAa,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC;QACnD,QAAQ;QACR,aAAa;QACb,GAAG,CAAC,UAAU;YACZ,CAAC,CAAC;gBACE,OAAO,EAAE;oBACP,QAAQ,EAAE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,WAAW,EAAE;oBACrD,aAAa,EAAE,SAAS,CAAC,UAAU,CAAC,eAAe,CAAC;iBACrD;aACF;YACH,CAAC,CAAC,EAAE,CAAC;KACR,CAAC;AACJ,CAAC","sourcesContent":["import {\n asDecimal,\n asInteger,\n asObject,\n asString,\n parseAvailability,\n parseExpiry,\n semanticError,\n validateIdentifiers,\n validateRegions,\n} from \"./parse-utils.js\";\nimport type { CreditPolicy, CreditsConfig, GrantProgram } from \"./types.js\";\n\nexport function parseCredits(value: unknown): CreditsConfig {\n const raw = asObject(value);\n const accountingRaw = asObject(\n raw.accounting ?? { unit: \"credit\", scale: 6, rounding: \"half_up\" },\n );\n const bucketsRaw = asObject(raw.buckets ?? {});\n const policiesRaw = asObject(raw.policies ?? {});\n const programsRaw = asObject(raw.grant_programs ?? {});\n const displayRaw = raw.display == null ? null : asObject(raw.display);\n validateIdentifiers(bucketsRaw, \"credits.buckets\");\n validateIdentifiers(policiesRaw, \"credits.policies\");\n validateIdentifiers(programsRaw, \"credits.grant_programs\");\n\n const buckets = Object.fromEntries(\n Object.entries(bucketsRaw).map(([key, input]) => {\n const bucket = asObject(input);\n const parsedExpiry = parseExpiry(\n bucket.expiry ?? { type: \"never\" },\n `credits.buckets.${key}.expiry`,\n );\n if (parsedExpiry.type === \"subscription_end\") {\n semanticError(`credits.buckets.${key}.expiry cannot be subscription_end`);\n }\n return [key, { priority: asInteger(bucket.priority), expiry: parsedExpiry }];\n }),\n );\n const priorities = Object.values(buckets).map((bucket) => bucket.priority);\n if (new Set(priorities).size !== priorities.length) {\n semanticError(\"credits bucket priorities must be unique\");\n }\n const defaultBucket = raw.default_bucket == null ? undefined : asString(raw.default_bucket);\n if (defaultBucket != null && !buckets[defaultBucket]) {\n semanticError(\"credits.default_bucket references an unknown bucket\");\n }\n\n const policies: Record<string, CreditPolicy> = {};\n for (const [key, input] of Object.entries(policiesRaw)) {\n const policy = asObject(input);\n policies[key] =\n policy.type === \"credit_line\"\n ? { type: \"credit_line\", limit: asDecimal(policy.limit) }\n : { type: \"prepaid\" };\n }\n\n const grantPrograms: Record<string, GrantProgram> = {};\n for (const [key, input] of Object.entries(programsRaw)) {\n const program = asObject(input);\n const awards = (program.awards as unknown[]).map((awardInput) => {\n const award = asObject(awardInput);\n const bucket = asString(award.bucket);\n if (!buckets[bucket]) {\n semanticError(`grant program '${key}' references unknown bucket '${bucket}'`);\n }\n const parsedExpiry =\n award.expiry == null\n ? undefined\n : parseExpiry(award.expiry, `credits.grant_programs.${key}`);\n if (parsedExpiry?.type === \"subscription_end\") {\n semanticError(`grant program '${key}' cannot use subscription_end expiry`);\n }\n return {\n recipient: (award.recipient ?? \"subject\") as \"subject\" | \"referrer\",\n amount: asDecimal(award.amount),\n bucket,\n ...(parsedExpiry ? { expiry: parsedExpiry } : {}),\n };\n });\n const eligibilityRaw = asObject(program.eligibility ?? {});\n const eligibilityRegions = (eligibilityRaw.regions ?? []) as string[];\n validateRegions(eligibilityRegions, `credits.grant_programs.${key}.eligibility.regions`);\n if (\n program.trigger !== \"referral_completed\" &&\n awards.some((award) => award.recipient === \"referrer\")\n ) {\n semanticError(`grant program '${key}' referrer awards require referral_completed`);\n }\n grantPrograms[key] = {\n trigger: program.trigger as GrantProgram[\"trigger\"],\n awards,\n ...(program.availability == null\n ? {}\n : { availability: parseAvailability(program.availability) }),\n eligibility: {\n plans: (eligibilityRaw.plans ?? []) as string[],\n regions: eligibilityRegions,\n },\n maxAwardsPerSubject: asInteger(program.max_awards_per_subject ?? 1),\n idempotencyScope: (program.idempotency_scope ?? \"subject\") as \"subject\" | \"event\",\n };\n }\n\n return {\n accounting: {\n unit: accountingRaw.unit as \"credit\",\n scale: asInteger(accountingRaw.scale) as 6,\n rounding: accountingRaw.rounding as \"half_up\",\n },\n buckets,\n ...(defaultBucket == null ? {} : { defaultBucket }),\n policies,\n grantPrograms,\n ...(displayRaw\n ? {\n display: {\n currency: asString(displayRaw.currency).toUpperCase(),\n unitsPerMajor: asDecimal(displayRaw.units_per_major),\n },\n }\n : {}),\n };\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import type { AdmissionPolicy, CreditsConfig, EntitlementsConfig, PlanDefinition, PricingConfig } from "./types.js";
2
+ export declare function parsePlans(value: unknown, pricing: PricingConfig | undefined, credits: CreditsConfig, entitlements: EntitlementsConfig, admission: {
3
+ policies: Record<string, AdmissionPolicy>;
4
+ }, subscriptionPlans: Set<string>): Record<string, PlanDefinition>;
5
+ //# sourceMappingURL=parse-plans.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-plans.d.ts","sourceRoot":"","sources":["../../src/config/parse-plans.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,kBAAkB,EAElB,cAAc,EAEd,aAAa,EAEd,MAAM,YAAY,CAAC;AAkDpB,wBAAgB,UAAU,CACxB,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,aAAa,GAAG,SAAS,EAClC,OAAO,EAAE,aAAa,EACtB,YAAY,EAAE,kBAAkB,EAChC,SAAS,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;CAAE,EACxD,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,GAC7B,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAiIhC"}
@@ -0,0 +1,142 @@
1
+ import { resolvesOperation } from "./parse-pricing.js";
2
+ import { asDecimal, asInteger, asObject, asString, identifier, parseWindow, semanticError, validateIdentifiers, } from "./parse-utils.js";
3
+ function validateFeatureValue(planKey, featureKey, featureValue, entitlements) {
4
+ const definition = entitlements.features[featureKey];
5
+ if (!definition) {
6
+ semanticError(`plans.${planKey} references unknown feature '${featureKey}'`);
7
+ }
8
+ if (definition.type === "boolean" && typeof featureValue !== "boolean") {
9
+ semanticError(`plans.${planKey}.features.${featureKey} must be boolean`);
10
+ }
11
+ if (definition.type === "integer" && !Number.isInteger(featureValue)) {
12
+ semanticError(`plans.${planKey}.features.${featureKey} must be integer`);
13
+ }
14
+ if (definition.type === "integer" &&
15
+ definition.minimum != null &&
16
+ Number(featureValue) < definition.minimum) {
17
+ semanticError(`plans.${planKey}.features.${featureKey} is below the feature minimum`);
18
+ }
19
+ if (definition.type === "integer" &&
20
+ definition.maximum != null &&
21
+ Number(featureValue) > definition.maximum) {
22
+ semanticError(`plans.${planKey}.features.${featureKey} exceeds the feature maximum`);
23
+ }
24
+ if (definition.type === "enum" &&
25
+ (typeof featureValue !== "string" || !definition.values.includes(featureValue))) {
26
+ semanticError(`plans.${planKey}.features.${featureKey} has an invalid enum value`);
27
+ }
28
+ if (definition.type === "string" && typeof featureValue !== "string") {
29
+ semanticError(`plans.${planKey}.features.${featureKey} must be string`);
30
+ }
31
+ if (definition.type === "string" &&
32
+ definition.pattern != null &&
33
+ !new RegExp(definition.pattern).test(String(featureValue))) {
34
+ semanticError(`plans.${planKey}.features.${featureKey} does not match the feature pattern`);
35
+ }
36
+ }
37
+ export function parsePlans(value, pricing, credits, entitlements, admission, subscriptionPlans) {
38
+ const raw = asObject(value ?? {});
39
+ validateIdentifiers(raw, "plans");
40
+ const plans = {};
41
+ for (const [planKey, input] of Object.entries(raw)) {
42
+ const plan = asObject(input);
43
+ const evolution = plan.evolution == null ? undefined : asObject(plan.evolution);
44
+ const defaultRollout = (evolution?.default_rollout ??
45
+ (subscriptionPlans.has(planKey) ? "next_renewal" : "immediate"));
46
+ if (!["immediate", "next_renewal", "new_assignments_only"].includes(defaultRollout)) {
47
+ semanticError(`plans.${planKey}.evolution.default_rollout is invalid`);
48
+ }
49
+ if (defaultRollout === "next_renewal" && !subscriptionPlans.has(planKey)) {
50
+ semanticError(`plans.${planKey}.evolution.default_rollout=next_renewal requires a subscription offer`);
51
+ }
52
+ const rateCard = plan.rate_card == null ? undefined : asString(plan.rate_card);
53
+ const allowedOperations = (plan.allowed_operations ?? []);
54
+ if (new Set(allowedOperations).size !== allowedOperations.length) {
55
+ semanticError(`plans.${planKey}.allowed_operations must not contain duplicates`);
56
+ }
57
+ if (rateCard != null && !pricing?.rateCards[rateCard]) {
58
+ semanticError(`plans.${planKey}.rate_card references unknown rate card '${rateCard}'`);
59
+ }
60
+ for (const operation of allowedOperations) {
61
+ identifier(operation, `plans.${planKey}.allowed_operations`);
62
+ if (!pricing?.operations[operation]) {
63
+ semanticError(`plans.${planKey} references unknown operation '${operation}'`);
64
+ }
65
+ if (rateCard == null || !resolvesOperation(pricing, rateCard, operation)) {
66
+ semanticError(`plans.${planKey} enables '${operation}' without pricing`);
67
+ }
68
+ }
69
+ const features = asObject(plan.features ?? {});
70
+ validateIdentifiers(features, `plans.${planKey}.features`);
71
+ for (const [featureKey, featureValue] of Object.entries(features)) {
72
+ validateFeatureValue(planKey, featureKey, featureValue, entitlements);
73
+ }
74
+ const quotasRaw = asObject(plan.quotas ?? {});
75
+ validateIdentifiers(quotasRaw, `plans.${planKey}.quotas`);
76
+ const quotas = {};
77
+ for (const [quotaKey, quotaInput] of Object.entries(quotasRaw)) {
78
+ const quota = asObject(quotaInput);
79
+ const operation = asString(quota.operation);
80
+ const measure = asString(quota.measure);
81
+ const emitAtPercent = (quota.emit_at_percent ?? [100]);
82
+ if (emitAtPercent.some((threshold) => threshold < 1 || threshold > 100) ||
83
+ emitAtPercent.some((threshold, index) => index > 0 && threshold <= emitAtPercent[index - 1])) {
84
+ semanticError(`plans.${planKey}.quotas.${quotaKey}.emit_at_percent must be unique, increasing, and between 1 and 100`);
85
+ }
86
+ if (!pricing?.operations[operation]?.measures[measure]) {
87
+ semanticError(`plans.${planKey}.quotas.${quotaKey} references an unknown operation measure`);
88
+ }
89
+ quotas[quotaKey] = {
90
+ operation,
91
+ measure,
92
+ limit: asDecimal(quota.limit),
93
+ window: parseWindow(quota.window, `plans.${planKey}.quotas.${quotaKey}.window`),
94
+ enforcement: quota.enforcement,
95
+ emitAtPercent,
96
+ };
97
+ }
98
+ const creditPolicy = plan.credit_policy == null ? undefined : asString(plan.credit_policy);
99
+ if (creditPolicy != null && !credits.policies[creditPolicy]) {
100
+ semanticError(`plans.${planKey}.credit_policy references unknown policy`);
101
+ }
102
+ const admissionPolicy = plan.admission_policy == null ? undefined : asString(plan.admission_policy);
103
+ if (admissionPolicy != null && !admission.policies[admissionPolicy]) {
104
+ semanticError(`plans.${planKey}.admission_policy references unknown policy`);
105
+ }
106
+ const allowanceInput = plan.credit_allowance == null ? undefined : asObject(plan.credit_allowance);
107
+ const allowancePriority = allowanceInput?.priority == null ? undefined : asInteger(allowanceInput.priority);
108
+ if (allowancePriority != null && allowancePriority < 0) {
109
+ semanticError(`plans.${planKey}.credit_allowance.priority must be non-negative`);
110
+ }
111
+ if (allowancePriority != null &&
112
+ Object.values(credits.buckets).some((bucket) => bucket.priority === allowancePriority)) {
113
+ semanticError(`plans.${planKey}.credit_allowance.priority conflicts with credit bucket priority ${allowancePriority}`);
114
+ }
115
+ plans[planKey] = {
116
+ displayName: asString(plan.display_name),
117
+ rank: asInteger(plan.rank ?? 0),
118
+ ...(plan.description == null ? {} : { description: asString(plan.description) }),
119
+ ...(rateCard == null ? {} : { rateCard }),
120
+ allowedOperations,
121
+ features,
122
+ ...(allowanceInput == null
123
+ ? {}
124
+ : {
125
+ creditAllowance: {
126
+ amount: asDecimal(allowanceInput.amount),
127
+ ...(allowancePriority == null ? {} : { priority: allowancePriority }),
128
+ window: parseWindow(allowanceInput.window, `plans.${planKey}.credit_allowance.window`),
129
+ },
130
+ }),
131
+ quotas,
132
+ ...(creditPolicy == null ? {} : { creditPolicy }),
133
+ ...(admissionPolicy == null ? {} : { admissionPolicy }),
134
+ evolution: { defaultRollout },
135
+ };
136
+ if (plans[planKey].creditAllowance != null && credits.defaultBucket == null) {
137
+ semanticError(`plans.${planKey}.credit_allowance requires credits.default_bucket`);
138
+ }
139
+ }
140
+ return plans;
141
+ }
142
+ //# sourceMappingURL=parse-plans.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-plans.js","sourceRoot":"","sources":["../../src/config/parse-plans.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EACL,SAAS,EACT,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,UAAU,EACV,WAAW,EACX,aAAa,EACb,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAY1B,SAAS,oBAAoB,CAC3B,OAAe,EACf,UAAkB,EAClB,YAA0B,EAC1B,YAAgC;IAEhC,MAAM,UAAU,GAAG,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IACrD,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,aAAa,CAAC,SAAS,OAAO,gCAAgC,UAAU,GAAG,CAAC,CAAC;IAC/E,CAAC;IACD,IAAI,UAAU,CAAC,IAAI,KAAK,SAAS,IAAI,OAAO,YAAY,KAAK,SAAS,EAAE,CAAC;QACvE,aAAa,CAAC,SAAS,OAAO,aAAa,UAAU,kBAAkB,CAAC,CAAC;IAC3E,CAAC;I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ACF,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,eAAe,IAAI,IAAI,IAAI,OAAO,CAAC,aAAa,IAAI,IAAI,EAAE,CAAC;YAC5E,aAAa,CAAC,SAAS,OAAO,mDAAmD,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { resolvesOperation } from \"./parse-pricing.js\";\nimport {\n asDecimal,\n asInteger,\n asObject,\n asString,\n identifier,\n parseWindow,\n semanticError,\n validateIdentifiers,\n} from \"./parse-utils.js\";\nimport type {\n AdmissionPolicy,\n CreditsConfig,\n EntitlementsConfig,\n FeatureValue,\n PlanDefinition,\n PlanRolloutStrategy,\n PricingConfig,\n QuotaDefinition,\n} from \"./types.js\";\n\nfunction validateFeatureValue(\n planKey: string,\n featureKey: string,\n featureValue: FeatureValue,\n entitlements: EntitlementsConfig,\n): void {\n const definition = entitlements.features[featureKey];\n if (!definition) {\n semanticError(`plans.${planKey} references unknown feature '${featureKey}'`);\n }\n if (definition.type === \"boolean\" && typeof featureValue !== \"boolean\") {\n semanticError(`plans.${planKey}.features.${featureKey} must be boolean`);\n }\n if (definition.type === \"integer\" && !Number.isInteger(featureValue)) {\n semanticError(`plans.${planKey}.features.${featureKey} must be integer`);\n }\n if (\n definition.type === \"integer\" &&\n definition.minimum != null &&\n Number(featureValue) < definition.minimum\n ) {\n semanticError(`plans.${planKey}.features.${featureKey} is below the feature minimum`);\n }\n if (\n definition.type === \"integer\" &&\n definition.maximum != null &&\n Number(featureValue) > definition.maximum\n ) {\n semanticError(`plans.${planKey}.features.${featureKey} exceeds the feature maximum`);\n }\n if (\n definition.type === \"enum\" &&\n (typeof featureValue !== \"string\" || !definition.values.includes(featureValue))\n ) {\n semanticError(`plans.${planKey}.features.${featureKey} has an invalid enum value`);\n }\n if (definition.type === \"string\" && typeof featureValue !== \"string\") {\n semanticError(`plans.${planKey}.features.${featureKey} must be string`);\n }\n if (\n definition.type === \"string\" &&\n definition.pattern != null &&\n !new RegExp(definition.pattern).test(String(featureValue))\n ) {\n semanticError(`plans.${planKey}.features.${featureKey} does not match the feature pattern`);\n }\n}\n\nexport function parsePlans(\n value: unknown,\n pricing: PricingConfig | undefined,\n credits: CreditsConfig,\n entitlements: EntitlementsConfig,\n admission: { policies: Record<string, AdmissionPolicy> },\n subscriptionPlans: Set<string>,\n): Record<string, PlanDefinition> {\n const raw = asObject(value ?? {});\n validateIdentifiers(raw, \"plans\");\n const plans: Record<string, PlanDefinition> = {};\n\n for (const [planKey, input] of Object.entries(raw)) {\n const plan = asObject(input);\n const evolution = plan.evolution == null ? undefined : asObject(plan.evolution);\n const defaultRollout = (evolution?.default_rollout ??\n (subscriptionPlans.has(planKey) ? \"next_renewal\" : \"immediate\")) as PlanRolloutStrategy;\n if (\n !([\"immediate\", \"next_renewal\", \"new_assignments_only\"] as const).includes(defaultRollout)\n ) {\n semanticError(`plans.${planKey}.evolution.default_rollout is invalid`);\n }\n if (defaultRollout === \"next_renewal\" && !subscriptionPlans.has(planKey)) {\n semanticError(\n `plans.${planKey}.evolution.default_rollout=next_renewal requires a subscription offer`,\n );\n }\n const rateCard = plan.rate_card == null ? undefined : asString(plan.rate_card);\n const allowedOperations = (plan.allowed_operations ?? []) as string[];\n if (new Set(allowedOperations).size !== allowedOperations.length) {\n semanticError(`plans.${planKey}.allowed_operations must not contain duplicates`);\n }\n if (rateCard != null && !pricing?.rateCards[rateCard]) {\n semanticError(`plans.${planKey}.rate_card references unknown rate card '${rateCard}'`);\n }\n for (const operation of allowedOperations) {\n identifier(operation, `plans.${planKey}.allowed_operations`);\n if (!pricing?.operations[operation]) {\n semanticError(`plans.${planKey} references unknown operation '${operation}'`);\n }\n if (rateCard == null || !resolvesOperation(pricing, rateCard, operation)) {\n semanticError(`plans.${planKey} enables '${operation}' without pricing`);\n }\n }\n\n const features = asObject(plan.features ?? {}) as Record<string, FeatureValue>;\n validateIdentifiers(features, `plans.${planKey}.features`);\n for (const [featureKey, featureValue] of Object.entries(features)) {\n validateFeatureValue(planKey, featureKey, featureValue, entitlements);\n }\n\n const quotasRaw = asObject(plan.quotas ?? {});\n validateIdentifiers(quotasRaw, `plans.${planKey}.quotas`);\n const quotas: Record<string, QuotaDefinition> = {};\n for (const [quotaKey, quotaInput] of Object.entries(quotasRaw)) {\n const quota = asObject(quotaInput);\n const operation = asString(quota.operation);\n const measure = asString(quota.measure);\n const emitAtPercent = (quota.emit_at_percent ?? [100]) as number[];\n if (\n emitAtPercent.some((threshold) => threshold < 1 || threshold > 100) ||\n emitAtPercent.some((threshold, index) => index > 0 && threshold <= emitAtPercent[index - 1])\n ) {\n semanticError(\n `plans.${planKey}.quotas.${quotaKey}.emit_at_percent must be unique, increasing, and between 1 and 100`,\n );\n }\n if (!pricing?.operations[operation]?.measures[measure]) {\n semanticError(\n `plans.${planKey}.quotas.${quotaKey} references an unknown operation measure`,\n );\n }\n quotas[quotaKey] = {\n operation,\n measure,\n limit: asDecimal(quota.limit),\n window: parseWindow(quota.window, `plans.${planKey}.quotas.${quotaKey}.window`),\n enforcement: quota.enforcement as \"block\" | \"allow\",\n emitAtPercent,\n };\n }\n\n const creditPolicy = plan.credit_policy == null ? undefined : asString(plan.credit_policy);\n if (creditPolicy != null && !credits.policies[creditPolicy]) {\n semanticError(`plans.${planKey}.credit_policy references unknown policy`);\n }\n const admissionPolicy =\n plan.admission_policy == null ? undefined : asString(plan.admission_policy);\n if (admissionPolicy != null && !admission.policies[admissionPolicy]) {\n semanticError(`plans.${planKey}.admission_policy references unknown policy`);\n }\n const allowanceInput =\n plan.credit_allowance == null ? undefined : asObject(plan.credit_allowance);\n const allowancePriority =\n allowanceInput?.priority == null ? undefined : asInteger(allowanceInput.priority);\n if (allowancePriority != null && allowancePriority < 0) {\n semanticError(`plans.${planKey}.credit_allowance.priority must be non-negative`);\n }\n if (\n allowancePriority != null &&\n Object.values(credits.buckets).some((bucket) => bucket.priority === allowancePriority)\n ) {\n semanticError(\n `plans.${planKey}.credit_allowance.priority conflicts with credit bucket priority ${allowancePriority}`,\n );\n }\n\n plans[planKey] = {\n displayName: asString(plan.display_name),\n rank: asInteger(plan.rank ?? 0),\n ...(plan.description == null ? {} : { description: asString(plan.description) }),\n ...(rateCard == null ? {} : { rateCard }),\n allowedOperations,\n features,\n ...(allowanceInput == null\n ? {}\n : {\n creditAllowance: {\n amount: asDecimal(allowanceInput.amount),\n ...(allowancePriority == null ? {} : { priority: allowancePriority }),\n window: parseWindow(\n allowanceInput.window,\n `plans.${planKey}.credit_allowance.window`,\n ),\n },\n }),\n quotas,\n ...(creditPolicy == null ? {} : { creditPolicy }),\n ...(admissionPolicy == null ? {} : { admissionPolicy }),\n evolution: { defaultRollout },\n };\n if (plans[planKey].creditAllowance != null && credits.defaultBucket == null) {\n semanticError(`plans.${planKey}.credit_allowance requires credits.default_bucket`);\n }\n }\n return plans;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { PricingConfig } from "./types.js";
2
+ export declare function parsePricing(value: unknown): PricingConfig;
3
+ export declare function resolvesOperation(pricing: PricingConfig, cardKey: string, operation: string): boolean;
4
+ //# sourceMappingURL=parse-pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-pricing.d.ts","sourceRoot":"","sources":["../../src/config/parse-pricing.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAQV,aAAa,EAEd,MAAM,YAAY,CAAC;AA2KpB,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CA2G1D;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAChB,OAAO,CAMT"}