The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Query, type UnwrapPlan } from "@distilled.cloud/core/query"; |
| 2 | import { Railway, type Region } from "@distilled.cloud/railway"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { resolveWorkspace } from "./Environment.ts"; |
| 6 | |
| 7 | const regionFields = <E>(region: Query<Region, E>) => ({ |
| 8 | id: region.id, |
| 9 | name: region.name, |
| 10 | region: region.region, |
| 11 | }); |
| 12 | type RegionsResultItem = UnwrapPlan<ReturnType<typeof regionFields>>; |
| 13 | |
| 14 | const readRegions = Query.fn((projectId?: string) => |
| 15 | Railway.regions(projectId === undefined ? {} : { projectId }).pipe(Query.map(regionFields)), |
| 16 | ); |
| 17 | |
| 18 | export type CatalogKind = "region" | "workspace"; |
| 19 | |
| 20 | export class CatalogNotFound extends Data.TaggedError("Railway.CatalogNotFound")<{ |
| 21 | kind: CatalogKind; |
| 22 | ref: string; |
| 23 | }> {} |
| 24 | |
| 25 | const notFound = (kind: CatalogKind, ref: string) => new CatalogNotFound({ kind, ref }); |
| 26 | |
| 27 | /** |
| 28 | * Current token workspace (`me.workspace ?? me.workspaces[0]`). |
| 29 | * Feeds `projectCreate({ input: { workspaceId } })`. Not a resource. |
| 30 | */ |
| 31 | export const currentWorkspace = resolveWorkspace; |
| 32 | |
| 33 | /** |
| 34 | * List Railway regions via `regions`. Pass `projectId` to scope the |
| 35 | * catalog to a project; omit it for the workspace default set. |
| 36 | */ |
| 37 | export const listRegions = Effect.fn(function* (projectId?: string) { |
| 38 | const regions = yield* readRegions(projectId); |
| 39 | return regions ?? []; |
| 40 | }); |
| 41 | |
| 42 | /** |
| 43 | * Resolve a Railway region by code (`us-west2`, `us-east4`, …), display |
| 44 | * name, or id. |
| 45 | */ |
| 46 | export const findRegion = (ref: string) => |
| 47 | Effect.gen(function* () { |
| 48 | const regions = yield* listRegions(); |
| 49 | const found = regions.find( |
| 50 | (item) => item.region === ref || item.name === ref || item.id === ref, |
| 51 | ); |
| 52 | if (found === undefined) { |
| 53 | return yield* notFound("region", ref); |
| 54 | } |
| 55 | return found; |
| 56 | }); |
| 57 | |
| 58 | export type RailwayRegion = RegionsResultItem; |
| 59 | |
| 60 | /** |
| 61 | * Workspace audit logs. Query-only — Railway has no audit-log |
| 62 | * create/update/delete, so this is not a resource. |
| 63 | */ |
| 64 | export { |
| 65 | AuditLog, |
| 66 | getAuditLog, |
| 67 | listAuditLogEventTypes, |
| 68 | listAuditLogs, |
| 69 | type AuditLogEntry, |
| 70 | type AuditLogEnvironment, |
| 71 | type AuditLogEventType, |
| 72 | type AuditLogProject, |
| 73 | type ListAuditLogsOptions, |
| 74 | } from "./AuditLog.ts"; |