(autumnUrl: string)
| 87 | } |
| 88 | |
| 89 | export const makeAutumnSurface = (autumnUrl: string): AutumnSurface => { |
| 90 | const usageEvents = (query: UsageQuery) => |
| 91 | Effect.gen(function* () { |
| 92 | // The emulator's ledger endpoint returns every recorded track event; |
| 93 | // filter to this customer + feature. (autumn-js drives /v1/* RPC-style.) |
| 94 | const response = yield* Effect.promise(() => |
| 95 | fetch(`${autumnUrl}/v1/events.list`, { |
| 96 | method: "POST", |
| 97 | headers: { "content-type": "application/json" }, |
| 98 | body: "{}", |
| 99 | }), |
| 100 | ); |
| 101 | if (!response.ok) { |
| 102 | return yield* Effect.fail( |
| 103 | `autumn events.list responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 104 | ); |
| 105 | } |
| 106 | const body = (yield* Effect.promise(() => response.json())) as { |
| 107 | readonly list?: ReadonlyArray<{ |
| 108 | readonly customer_id: string; |
| 109 | readonly feature_id: string; |
| 110 | readonly value: number; |
| 111 | }>; |
| 112 | }; |
| 113 | return (body.list ?? []) |
| 114 | .filter( |
| 115 | (event) => event.customer_id === query.customerId && event.feature_id === query.featureId, |
| 116 | ) |
| 117 | .map((event) => ({ |
| 118 | customerId: event.customer_id, |
| 119 | featureId: event.feature_id, |
| 120 | value: event.value, |
| 121 | })); |
| 122 | }); |
| 123 | |
| 124 | const settleCheckout = (sessionId: string) => |
| 125 | Effect.gen(function* () { |
| 126 | const response = yield* Effect.promise(() => |
| 127 | fetch(`${autumnUrl}/checkout/${encodeURIComponent(sessionId)}/settle`, { method: "POST" }), |
| 128 | ); |
| 129 | if (!response.ok) { |
| 130 | return yield* Effect.fail( |
| 131 | `autumn checkout settle responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 132 | ); |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | // Track exactly the plan's included allotment in one event, driving |
| 137 | // `remaining` (included - usage) to zero so the next check reports blocked. |
| 138 | const exhaustExecutions = (customerId: string) => |
| 139 | Effect.gen(function* () { |
| 140 | const response = yield* Effect.promise(() => |
| 141 | fetch(`${autumnUrl}/v1/balances.track`, { |
| 142 | method: "POST", |
| 143 | headers: { "content-type": "application/json" }, |
| 144 | body: JSON.stringify({ |
| 145 | customer_id: customerId, |
| 146 | feature_id: "executions", |
no test coverage detected