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 { Query, type UnwrapPlan } from "@distilled.cloud/core/query"; |
| 2 | import { |
| 3 | Railway, |
| 4 | type AuditLog as RailwayAuditLog, |
| 5 | type AuditLogFilterInput, |
| 6 | } from "@distilled.cloud/railway"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { resolveWorkspaceId } from "./Environment.ts"; |
| 9 | |
| 10 | const auditLogFields = <E>(row: Query<RailwayAuditLog, E>) => ({ |
| 11 | id: row.id, |
| 12 | eventType: row.eventType, |
| 13 | createdAt: row.createdAt, |
| 14 | workspaceId: row.workspaceId, |
| 15 | projectId: row.projectId, |
| 16 | environmentId: row.environmentId, |
| 17 | payload: row.payload, |
| 18 | context: row.context, |
| 19 | }); |
| 20 | type AuditLogRow = UnwrapPlan<ReturnType<typeof auditLogFields>>; |
| 21 | |
| 22 | const readAuditLogs = Query.fn( |
| 23 | (args: { |
| 24 | workspaceId: string; |
| 25 | first: number; |
| 26 | sort?: "asc" | "desc"; |
| 27 | filter?: AuditLogFilterInput; |
| 28 | }) => Railway.auditLogs(args).pipe(Query.map(auditLogFields)), |
| 29 | ); |
| 30 | |
| 31 | const readAuditLog = Query.fn((id: string, workspaceId: string) => |
| 32 | auditLogFields(Railway.auditLog({ id, workspaceId })), |
| 33 | ); |
| 34 | |
| 35 | const readAuditLogEventTypes = Query.fn(() => |
| 36 | Railway.auditLogEventTypeInfo().pipe( |
| 37 | Query.map((info) => ({ description: info.description, eventType: info.eventType })), |
| 38 | ), |
| 39 | ); |
| 40 | type AuditLogEventTypeInfoResultItem = UnwrapPlan< |
| 41 | ReturnType<typeof readAuditLogEventTypes> |
| 42 | >[number]; |
| 43 | |
| 44 | /** |
| 45 | * Project identity for {@link listAuditLogs}. Accepts a `Railway.Project` |
| 46 | * or a `{ projectId }` stub. |
| 47 | */ |
| 48 | export type AuditLogProject = { readonly projectId: string }; |
| 49 | |
| 50 | /** |
| 51 | * Environment identity for {@link listAuditLogs}. Accepts a |
| 52 | * `Railway.Project` (primary environment), a `Railway.Environment`, or |
| 53 | * `{ environmentId }`. |
| 54 | */ |
| 55 | export type AuditLogEnvironment = { readonly environmentId: string }; |
| 56 | |
| 57 | export interface ListAuditLogsOptions { |
| 58 | /** |
| 59 | * Restrict to a project. Accepts a `Railway.Project` or `{ projectId }`. |
| 60 | */ |
| 61 | project?: AuditLogProject | string; |
| 62 | /** |
| 63 | * Restrict to an environment. Accepts a `Railway.Project`, a |
| 64 | * `Railway.Environment`, or `{ environmentId }`. |
| 65 | */ |
| 66 | environment?: AuditLogEnvironment | string; |
| 67 | /** |
| 68 | * Event type names from {@link listAuditLogEventTypes}. |
| 69 | */ |
| 70 | eventTypes?: readonly string[]; |
| 71 | /** |
| 72 | * Inclusive start (RFC3339). |
| 73 | */ |
| 74 | startDate?: string; |
| 75 | /** |
| 76 | * Inclusive end (RFC3339). |
| 77 | */ |
| 78 | endDate?: string; |
| 79 | /** |
| 80 | * Workspace id. Defaults to the current token workspace. |
| 81 | */ |
| 82 | workspaceId?: string; |
| 83 | /** |
| 84 | * Page size. Defaults to 50. This helper fetches one page — it does |
| 85 | * not walk the whole workspace history. |
| 86 | * @default 50 |
| 87 | */ |
| 88 | first?: number; |
| 89 | /** |
| 90 | * Sort by `createdAt`. Railway default is `desc`. |
| 91 | */ |
| 92 | sort?: "asc" | "desc"; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * One workspace audit-log row from `auditLogs` / `auditLog`. |
| 97 | */ |
| 98 | export interface AuditLogEntry { |
| 99 | /** Audit log id. */ |
| 100 | readonly id: string; |
| 101 | /** Event type name (see {@link listAuditLogEventTypes}). */ |
| 102 | readonly eventType: string; |
| 103 | /** RFC3339 creation timestamp. */ |
| 104 | readonly createdAt: string; |
| 105 | /** Workspace the event was recorded in. */ |
| 106 | readonly workspaceId: string | undefined; |
| 107 | /** Project the event targeted, when Railway attached one. */ |
| 108 | readonly projectId: string | undefined; |
| 109 | /** Environment the event targeted, when Railway attached one. */ |
| 110 | readonly environmentId: string | undefined; |
| 111 | /** Event payload. Shape depends on `eventType`. */ |
| 112 | readonly payload: unknown; |
| 113 | /** Extra context Railway attached to the event. */ |
| 114 | readonly context: unknown; |
| 115 | } |
| 116 | |
| 117 | export type AuditLogEventType = AuditLogEventTypeInfoResultItem; |
| 118 | |
| 119 | const toEntry = (row: AuditLogRow): AuditLogEntry => ({ |
| 120 | id: row.id, |
| 121 | eventType: row.eventType, |
| 122 | createdAt: row.createdAt, |
| 123 | workspaceId: row.workspaceId ?? undefined, |
| 124 | projectId: row.projectId ?? undefined, |
| 125 | environmentId: row.environmentId ?? undefined, |
| 126 | payload: row.payload ?? undefined, |
| 127 | context: row.context ?? undefined, |
| 128 | }); |
| 129 | |
| 130 | const projectIdOf = (value: AuditLogProject | string | undefined): string | undefined => { |
| 131 | if (value === undefined) return undefined; |
| 132 | if (typeof value === "string") return value; |
| 133 | return value.projectId; |
| 134 | }; |
| 135 | |
| 136 | const environmentIdOf = (value: AuditLogEnvironment | string | undefined): string | undefined => { |
| 137 | if (value === undefined) return undefined; |
| 138 | if (typeof value === "string") return value; |
| 139 | return value.environmentId; |
| 140 | }; |
| 141 | |
| 142 | const workspaceOf = (workspaceId: string | undefined) => |
| 143 | workspaceId !== undefined ? Effect.succeed(workspaceId) : resolveWorkspaceId(); |
| 144 | |
| 145 | /** |
| 146 | * List workspace audit logs. Query-only — Railway has no audit-log |
| 147 | * create/update/delete, so this is a catalog helper, not a resource. |
| 148 | * |
| 149 | * Defaults to the current token workspace. Pass `project` / |
| 150 | * `environment` to filter. The result may be empty. |
| 151 | * |
| 152 | * @see https://docs.railway.com/reference/audit-logs |
| 153 | * |
| 154 | * ### List logs |
| 155 | * **Example:** Workspace logs |
| 156 | * ```typescript |
| 157 | * const logs = yield* Railway.listAuditLogs(); |
| 158 | * ``` |
| 159 | * |
| 160 | * **Example:** Filter by project |
| 161 | * ```typescript |
| 162 | * const site = yield* Railway.Project("Site"); |
| 163 | * const logs = yield* Railway.listAuditLogs({ |
| 164 | * project: site, |
| 165 | * environment: site, |
| 166 | * }); |
| 167 | * ``` |
| 168 | * |
| 169 | * ### Get by id |
| 170 | * **Example:** One row |
| 171 | * ```typescript |
| 172 | * const log = yield* Railway.getAuditLog({ id: logs[0].id }); |
| 173 | * ``` |
| 174 | * |
| 175 | * ### Event types |
| 176 | * **Example:** Catalog |
| 177 | * ```typescript |
| 178 | * const types = yield* Railway.listAuditLogEventTypes(); |
| 179 | * ``` |
| 180 | * |
| 181 | * @resource |
| 182 | * @product Workspace |
| 183 | */ |
| 184 | export const AuditLog = Effect.fn(function* (options?: ListAuditLogsOptions) { |
| 185 | const workspaceId = yield* workspaceOf(options?.workspaceId); |
| 186 | const projectId = projectIdOf(options?.project); |
| 187 | const environmentId = environmentIdOf(options?.environment); |
| 188 | const first = options?.first ?? 50; |
| 189 | const filter = |
| 190 | projectId !== undefined || |
| 191 | environmentId !== undefined || |
| 192 | options?.eventTypes !== undefined || |
| 193 | options?.startDate !== undefined || |
| 194 | options?.endDate !== undefined |
| 195 | ? { |
| 196 | ...(projectId !== undefined ? { projectId } : {}), |
| 197 | ...(environmentId !== undefined ? { environmentId } : {}), |
| 198 | ...(options?.eventTypes !== undefined ? { eventTypes: [...options.eventTypes] } : {}), |
| 199 | ...(options?.startDate !== undefined ? { startDate: options.startDate } : {}), |
| 200 | ...(options?.endDate !== undefined ? { endDate: options.endDate } : {}), |
| 201 | } |
| 202 | : undefined; |
| 203 | |
| 204 | const rows = yield* readAuditLogs({ |
| 205 | workspaceId, |
| 206 | first, |
| 207 | ...(options?.sort !== undefined ? { sort: options.sort } : {}), |
| 208 | ...(filter !== undefined ? { filter } : {}), |
| 209 | }); |
| 210 | |
| 211 | return rows.map(toEntry); |
| 212 | }); |
| 213 | |
| 214 | /** Alias of {@link AuditLog}. */ |
| 215 | export const listAuditLogs = AuditLog; |
| 216 | |
| 217 | /** |
| 218 | * Fetch one audit log by id. Workspace defaults to the current token |
| 219 | * workspace. |
| 220 | * |
| 221 | * @example Get by id |
| 222 | * ```typescript |
| 223 | * const log = yield* Railway.getAuditLog({ id: logs[0].id }); |
| 224 | * ``` |
| 225 | */ |
| 226 | export const getAuditLog = Effect.fn(function* (options: { id: string; workspaceId?: string }) { |
| 227 | const workspaceId = yield* workspaceOf(options.workspaceId); |
| 228 | const row = yield* readAuditLog(options.id, workspaceId); |
| 229 | return toEntry(row); |
| 230 | }); |
| 231 | |
| 232 | /** |
| 233 | * Event types Railway can record, with descriptions. |
| 234 | * |
| 235 | * @example Event type catalog |
| 236 | * ```typescript |
| 237 | * const types = yield* Railway.listAuditLogEventTypes(); |
| 238 | * ``` |
| 239 | */ |
| 240 | export const listAuditLogEventTypes = Effect.fn(function* () { |
| 241 | const types = yield* readAuditLogEventTypes(); |
| 242 | return types ?? []; |
| 243 | }); |