| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Redacted from "effect/Redacted"; |
| 3 | import { getEnvRedactedRequired } from "../Auth/Env.ts"; |
| 4 | import { |
| 5 | makeStoredAuthProvider, |
| 6 | storedSecret, |
| 7 | type StoredAuthConfig, |
| 8 | } from "../Auth/StoredAuthProvider.ts"; |
| 9 | |
| 10 | export const NEON_AUTH_PROVIDER_NAME = "Neon"; |
| 11 | |
| 12 | export type NeonAuthConfig = StoredAuthConfig; |
| 13 | |
| 14 | export type NeonResolvedCredentials = { |
| 15 | type: "apiKey"; |
| 16 | apiKey: Redacted.Redacted<string>; |
| 17 | source: { type: NeonAuthConfig["method"] | "env"; details?: string }; |
| 18 | }; |
| 19 | |
| 20 | const neonAuth = makeStoredAuthProvider<NeonResolvedCredentials>({ |
| 21 | provider: NEON_AUTH_PROVIDER_NAME, |
| 22 | fields: [{ name: "apiKey", label: "Neon API Key", secret: true }], |
| 23 | toResolved: (values) => ({ |
| 24 | type: "apiKey", |
| 25 | apiKey: storedSecret(values.apiKey) ?? Redacted.make(""), |
| 26 | source: { type: "stored" }, |
| 27 | }), |
| 28 | readEnvironment: getEnvRedactedRequired("NEON_API_KEY").pipe( |
| 29 | Effect.map((apiKey) => ({ |
| 30 | type: "apiKey" as const, |
| 31 | apiKey, |
| 32 | source: { type: "env" as const }, |
| 33 | })), |
| 34 | ), |
| 35 | environment: [ |
| 36 | { |
| 37 | name: "NEON_API_KEY", |
| 38 | required: true, |
| 39 | secret: true, |
| 40 | }, |
| 41 | ], |
| 42 | }); |
| 43 | |
| 44 | /** |
| 45 | * Layer that registers the Neon {@link AuthProvider} into the |
| 46 | * {@link AuthProviders} registry. |
| 47 | */ |
| 48 | export const NeonAuth = neonAuth.layer; |