The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { withRequestOptions } from "@distilled.cloud/stripe"; |
| 2 | import { |
| 3 | DeleteRadarValueListItem, |
| 4 | GetRadarValueListItems, |
| 5 | GetRadarValueListItem, |
| 6 | GetRadarValueLists, |
| 7 | CreateRadarValueListItem, |
| 8 | type RadarValueList as StripeRadarValueList, |
| 9 | type RadarValueListItem as StripeRadarValueListItem, |
| 10 | } from "@distilled.cloud/stripe/stripe"; |
| 11 | import { isResolved } from "alchemy/Diff"; |
| 12 | import * as Provider from "alchemy/Provider"; |
| 13 | import { Resource } from "alchemy/Resource"; |
| 14 | import { tagRecord } from "alchemy/Tags"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import { alchemyMetadataKeys } from "./Metadata.ts"; |
| 17 | import { isMissingStripeResource } from "./missing.ts"; |
| 18 | import type { Providers } from "./Providers.ts"; |
| 19 | |
| 20 | const LIST_PAGE_SIZE = 100; |
| 21 | const LIST_MAX_PAGES = 100; |
| 22 | const LIST_CONCURRENCY = 10; |
| 23 | |
| 24 | export interface RadarValueListItemProps { |
| 25 | /** |
| 26 | * Id of the parent Radar value list (`rsl_…`). Changing it replaces |
| 27 | * the item. |
| 28 | */ |
| 29 | valueList: string; |
| 30 | /** |
| 31 | * Value stored on the list. Its type must match the parent list's |
| 32 | * `item_type` (e.g. an email, IP, country code, or fingerprint). |
| 33 | * Changing it replaces the item. |
| 34 | */ |
| 35 | value: string; |
| 36 | } |
| 37 | |
| 38 | export type RadarValueListItem = Resource< |
| 39 | "Stripe.RadarValueListItem", |
| 40 | RadarValueListItemProps, |
| 41 | { |
| 42 | /** Stripe value list item id (`rsli_…`). */ |
| 43 | id: string; |
| 44 | /** Id of the parent value list (`rsl_…`). */ |
| 45 | valueList: string; |
| 46 | /** Value stored on the list. */ |
| 47 | value: string; |
| 48 | /** Unix timestamp when the item was created. */ |
| 49 | created: number; |
| 50 | /** Name or email of the user who added the item. */ |
| 51 | createdBy: string; |
| 52 | /** Whether the item exists in live mode. */ |
| 53 | livemode: boolean; |
| 54 | }, |
| 55 | never, |
| 56 | Providers |
| 57 | >; |
| 58 | |
| 59 | /** |
| 60 | * A Stripe Radar Value List Item — one entry on a Radar value list used |
| 61 | * in Radar rules. Existence-only: there is nothing to update in place; |
| 62 | * changing `valueList` or `value` replaces the item. Destroy deletes it. |
| 63 | * |
| 64 | * Value list items have no metadata of their own. Ownership for |
| 65 | * account-wide `list()` (nuke) is inferred from the parent value list's |
| 66 | * Alchemy metadata. |
| 67 | * |
| 68 | * @see https://docs.stripe.com/api/radar/value_list_items |
| 69 | * |
| 70 | * ### Adding an Item |
| 71 | * **Example:** Add an email to a value list |
| 72 | * ```typescript |
| 73 | * const blocked = yield* Stripe.RadarValueList("blocked-emails", { |
| 74 | * alias: "blocked_emails", |
| 75 | * name: "Blocked emails", |
| 76 | * itemType: "email", |
| 77 | * }); |
| 78 | * const item = yield* Stripe.RadarValueListItem("spammer", { |
| 79 | * valueList: blocked.id, |
| 80 | * value: "spammer@example.com", |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * ### Replacing an Item |
| 85 | * **Example:** Point the item at a different value |
| 86 | * ```typescript |
| 87 | * const item = yield* Stripe.RadarValueListItem("spammer", { |
| 88 | * valueList: blocked.id, |
| 89 | * value: "other@example.com", |
| 90 | * }); |
| 91 | * ``` |
| 92 | * |
| 93 | * @resource |
| 94 | * @product Radar |
| 95 | */ |
| 96 | export const RadarValueListItem = Resource<RadarValueListItem>("Stripe.RadarValueListItem"); |
| 97 | |
| 98 | type RadarValueListItemAttributes = RadarValueListItem["Attributes"]; |
| 99 | |
| 100 | const toAttrs = (item: StripeRadarValueListItem): RadarValueListItemAttributes => ({ |
| 101 | id: item.id, |
| 102 | valueList: item.value_list, |
| 103 | value: item.value, |
| 104 | created: item.created, |
| 105 | createdBy: item.created_by, |
| 106 | livemode: item.livemode, |
| 107 | }); |
| 108 | |
| 109 | const isMissing = isMissingStripeResource; |
| 110 | |
| 111 | const getById = (item: string) => |
| 112 | GetRadarValueListItem({ item }).pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 113 | |
| 114 | const listItems = Effect.fn(function* (valueList: string) { |
| 115 | const items: StripeRadarValueListItem[] = []; |
| 116 | let startingAfter: string | undefined; |
| 117 | for (let page = 0; page < LIST_MAX_PAGES; page++) { |
| 118 | const response = yield* GetRadarValueListItems({ |
| 119 | value_list: valueList, |
| 120 | limit: LIST_PAGE_SIZE, |
| 121 | ...(startingAfter !== undefined ? { starting_after: startingAfter } : {}), |
| 122 | }).pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 123 | if (response === undefined) { |
| 124 | break; |
| 125 | } |
| 126 | items.push(...response.data); |
| 127 | if (!response.has_more || response.data.length === 0) { |
| 128 | break; |
| 129 | } |
| 130 | startingAfter = response.data[response.data.length - 1]?.id; |
| 131 | if (startingAfter === undefined) { |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | return items; |
| 136 | }); |
| 137 | |
| 138 | const findByValue = Effect.fn(function* (valueList: string, value: string) { |
| 139 | const matches: StripeRadarValueListItem[] = []; |
| 140 | let startingAfter: string | undefined; |
| 141 | for (let page = 0; page < LIST_MAX_PAGES; page++) { |
| 142 | const response = yield* GetRadarValueListItems({ |
| 143 | value_list: valueList, |
| 144 | value, |
| 145 | limit: LIST_PAGE_SIZE, |
| 146 | ...(startingAfter !== undefined ? { starting_after: startingAfter } : {}), |
| 147 | }).pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 148 | if (response === undefined) { |
| 149 | break; |
| 150 | } |
| 151 | for (const item of response.data) { |
| 152 | if (item.value === value) { |
| 153 | matches.push(item); |
| 154 | } |
| 155 | } |
| 156 | if (!response.has_more || response.data.length === 0) { |
| 157 | break; |
| 158 | } |
| 159 | startingAfter = response.data[response.data.length - 1]?.id; |
| 160 | if (startingAfter === undefined) { |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | matches.sort((a, b) => b.created - a.created); |
| 165 | return matches[0]; |
| 166 | }); |
| 167 | |
| 168 | const listValueLists = Effect.fn(function* () { |
| 169 | const lists: StripeRadarValueList[] = []; |
| 170 | let startingAfter: string | undefined; |
| 171 | for (let page = 0; page < LIST_MAX_PAGES; page++) { |
| 172 | const response = yield* GetRadarValueLists({ |
| 173 | limit: LIST_PAGE_SIZE, |
| 174 | ...(startingAfter !== undefined ? { starting_after: startingAfter } : {}), |
| 175 | }); |
| 176 | lists.push(...response.data); |
| 177 | if (!response.has_more || response.data.length === 0) { |
| 178 | break; |
| 179 | } |
| 180 | startingAfter = response.data[response.data.length - 1]?.id; |
| 181 | if (startingAfter === undefined) { |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return lists; |
| 186 | }); |
| 187 | |
| 188 | const listAlchemyValueLists = Effect.fn(function* () { |
| 189 | const lists = yield* listValueLists(); |
| 190 | return lists.filter((list) => tagRecord(list.metadata)[alchemyMetadataKeys.stack] !== undefined); |
| 191 | }); |
| 192 | |
| 193 | const observe = Effect.fn(function* (input: { id?: string; valueList?: string; value?: string }) { |
| 194 | if (input.id !== undefined) { |
| 195 | const byId = yield* getById(input.id); |
| 196 | if (byId !== undefined) return byId; |
| 197 | } |
| 198 | if (input.valueList !== undefined && input.value !== undefined) { |
| 199 | return yield* findByValue(input.valueList, input.value); |
| 200 | } |
| 201 | return undefined; |
| 202 | }); |
| 203 | |
| 204 | const shouldReplace = ( |
| 205 | news: RadarValueListItemProps, |
| 206 | output: RadarValueListItemAttributes | undefined, |
| 207 | ): boolean => { |
| 208 | if (output === undefined) return false; |
| 209 | if (news.valueList !== output.valueList) return true; |
| 210 | if (news.value !== output.value) return true; |
| 211 | return false; |
| 212 | }; |
| 213 | |
| 214 | export const RadarValueListItemProvider = () => |
| 215 | Provider.succeed(RadarValueListItem, { |
| 216 | stables: ["id", "valueList", "value", "created", "createdBy", "livemode"], |
| 217 | nuke: { dependsOn: ["Stripe.RadarValueList"] }, |
| 218 | |
| 219 | diff: Effect.fn(function* ({ news, output }) { |
| 220 | if (!isResolved(news)) return undefined; |
| 221 | if (shouldReplace(news, output)) { |
| 222 | return { action: "replace" } as const; |
| 223 | } |
| 224 | return undefined; |
| 225 | }), |
| 226 | |
| 227 | read: Effect.fn(function* ({ output, olds }) { |
| 228 | const valueList = |
| 229 | output?.valueList ?? (typeof olds?.valueList === "string" ? olds.valueList : undefined); |
| 230 | const value = output?.value ?? (typeof olds?.value === "string" ? olds.value : undefined); |
| 231 | const existing = yield* observe({ |
| 232 | id: output?.id, |
| 233 | valueList, |
| 234 | value, |
| 235 | }); |
| 236 | if (existing === undefined) return undefined; |
| 237 | return toAttrs(existing); |
| 238 | }), |
| 239 | |
| 240 | list: Effect.fn(function* () { |
| 241 | // Value list items have no metadata. Fan out from alchemy-owned |
| 242 | // value lists so nuke only tears down items on lists we created. |
| 243 | const lists = yield* listAlchemyValueLists(); |
| 244 | const rows = yield* Effect.forEach( |
| 245 | lists, |
| 246 | (list) => listItems(list.id).pipe(Effect.map((items) => items.map(toAttrs))), |
| 247 | { concurrency: LIST_CONCURRENCY }, |
| 248 | ); |
| 249 | return rows.flat(); |
| 250 | }), |
| 251 | |
| 252 | reconcile: Effect.fn(function* ({ news, output, instanceId }) { |
| 253 | let current = yield* observe({ |
| 254 | id: output?.id, |
| 255 | valueList: news.valueList, |
| 256 | value: news.value, |
| 257 | }); |
| 258 | if (current !== undefined && shouldReplace(news, toAttrs(current))) { |
| 259 | current = undefined; |
| 260 | } |
| 261 | |
| 262 | if (current === undefined) { |
| 263 | current = yield* CreateRadarValueListItem({ |
| 264 | value_list: news.valueList, |
| 265 | value: news.value, |
| 266 | }).pipe( |
| 267 | withRequestOptions({ |
| 268 | idempotencyKey: `alchemy-radar-value-list-item-${instanceId}`, |
| 269 | }), |
| 270 | Effect.catchIf( |
| 271 | (e) => e._tag === "InvalidRequestError" || e._tag === "Conflict", |
| 272 | (e) => |
| 273 | observe({ |
| 274 | valueList: news.valueList, |
| 275 | value: news.value, |
| 276 | }).pipe( |
| 277 | Effect.flatMap((found) => |
| 278 | found !== undefined ? Effect.succeed(found) : Effect.fail(e), |
| 279 | ), |
| 280 | ), |
| 281 | ), |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | return toAttrs(current); |
| 286 | }), |
| 287 | |
| 288 | delete: Effect.fn(function* ({ output }) { |
| 289 | yield* DeleteRadarValueListItem({ item: output.id }).pipe( |
| 290 | Effect.catchIf(isMissing, () => Effect.void), |
| 291 | ); |
| 292 | }), |
| 293 | }); |