(autumnUrl: string)
| 131 | } |
| 132 | |
| 133 | export const makeAutumnSurface = (autumnUrl: string): AutumnSurface => { |
| 134 | const usageEvents = (query: UsageQuery) => |
| 135 | Effect.gen(function* () { |
| 136 | // The emulator's ledger endpoint returns every recorded track event; |
| 137 | // filter to this customer + feature. (autumn-js drives /v1/* RPC-style.) |
| 138 | const response = yield* Effect.promise(() => |
| 139 | fetch(`${autumnUrl}/v1/events.list`, { |
| 140 | method: "POST", |
| 141 | headers: { "content-type": "application/json" }, |
| 142 | body: "{}", |
| 143 | }), |
| 144 | ); |
| 145 | if (!response.ok) { |
| 146 | return yield* Effect.fail( |
| 147 | `autumn events.list responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 148 | ); |
| 149 | } |
| 150 | const body = (yield* Effect.promise(() => response.json())) as { |
| 151 | readonly list?: ReadonlyArray<{ |
| 152 | readonly customer_id: string; |
| 153 | readonly feature_id: string; |
| 154 | readonly value: number; |
| 155 | }>; |
| 156 | }; |
| 157 | return (body.list ?? []) |
| 158 | .filter( |
| 159 | (event) => event.customer_id === query.customerId && event.feature_id === query.featureId, |
| 160 | ) |
| 161 | .map((event) => ({ |
| 162 | customerId: event.customer_id, |
| 163 | featureId: event.feature_id, |
| 164 | value: event.value, |
| 165 | })); |
| 166 | }); |
| 167 | |
| 168 | const customerIds = () => |
| 169 | Effect.gen(function* () { |
| 170 | const response = yield* Effect.promise(() => |
| 171 | fetch(`${autumnUrl}/v1/customers.list`, { |
| 172 | method: "POST", |
| 173 | headers: { "content-type": "application/json" }, |
| 174 | body: "{}", |
| 175 | }), |
| 176 | ); |
| 177 | if (!response.ok) { |
| 178 | return yield* Effect.fail( |
| 179 | `autumn customers.list responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 180 | ); |
| 181 | } |
| 182 | const body = (yield* Effect.promise(() => response.json())) as { |
| 183 | readonly list?: ReadonlyArray<{ readonly id?: string }>; |
| 184 | }; |
| 185 | return (body.list ?? []).map((customer) => customer.id ?? ""); |
| 186 | }); |
| 187 | |
| 188 | const settleCheckout = (sessionId: string) => |
| 189 | Effect.gen(function* () { |
| 190 | const response = yield* Effect.promise(() => |
no test coverage detected