The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Config from "effect/Config"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | const DEFAULT_BASE_URL = "https://api.prisma.io"; |
| 6 | const PRISMA_API_URL_ENV = "PRISMA_API_URL"; |
| 7 | const PRISMA_MANAGEMENT_API_URL_ENV = "PRISMA_MANAGEMENT_API_URL"; |
| 8 | |
| 9 | const isLoopbackHost = (hostname: string) => |
| 10 | hostname === "localhost" || |
| 11 | hostname.endsWith(".localhost") || |
| 12 | hostname === "127.0.0.1" || |
| 13 | hostname === "[::1]"; |
| 14 | |
| 15 | export class InvalidBaseUrlError extends Data.TaggedError("InvalidBaseUrlError")<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | const normalizeBaseUrl = (value: string) => |
| 21 | Effect.try({ |
| 22 | try: () => { |
| 23 | const url = new URL(value); |
| 24 | if (url.protocol !== "https:" && url.protocol !== "http:") { |
| 25 | throw new Error("Prisma Management API URL must use HTTP or HTTPS."); |
| 26 | } |
| 27 | if (url.username.length > 0 || url.password.length > 0) { |
| 28 | throw new Error("Prisma Management API URL must not contain credentials."); |
| 29 | } |
| 30 | if (url.protocol === "http:" && !isLoopbackHost(url.hostname)) { |
| 31 | throw new Error( |
| 32 | "Prisma Management API URL must use HTTPS unless it targets a loopback host.", |
| 33 | ); |
| 34 | } |
| 35 | if ( |
| 36 | (url.pathname !== "/" && url.pathname !== "") || |
| 37 | url.search.length > 0 || |
| 38 | url.hash.length > 0 |
| 39 | ) { |
| 40 | throw new Error( |
| 41 | "Prisma Management API URL must be an origin without a path, query, or fragment.", |
| 42 | ); |
| 43 | } |
| 44 | return url.origin; |
| 45 | }, |
| 46 | catch: (cause) => |
| 47 | new InvalidBaseUrlError({ |
| 48 | message: |
| 49 | cause instanceof Error |
| 50 | ? cause.message |
| 51 | : `Invalid Prisma Management API URL: ${String(cause)}`, |
| 52 | cause, |
| 53 | }), |
| 54 | }); |
| 55 | |
| 56 | /** The Management API origin: `PRISMA_API_URL`, `PRISMA_MANAGEMENT_API_URL`, or the default. */ |
| 57 | export const resolveBaseUrl = Config.String(PRISMA_API_URL_ENV).pipe( |
| 58 | Config.orElse(() => Config.String(PRISMA_MANAGEMENT_API_URL_ENV)), |
| 59 | Config.withDefault(DEFAULT_BASE_URL), |
| 60 | Effect.flatMap(normalizeBaseUrl), |
| 61 | ); |