|
| 1 | +import { |
| 2 | + OnePasswordSecretProviderConfig, |
| 3 | + SecretProviderId, |
| 4 | + SecretProviderKind, |
| 5 | + SecretProviderLabel, |
| 6 | + type OnePasswordAccount, |
| 7 | + type SecretProviderConfig, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import * as Option from "effect/Option"; |
| 10 | +import * as Result from "effect/Result"; |
| 11 | +import * as Schema from "effect/Schema"; |
| 12 | + |
| 13 | +const decodeSecretProviderId = Schema.decodeOption(SecretProviderId); |
| 14 | +const decodeOnePasswordConfig = Schema.decodeUnknownOption(OnePasswordSecretProviderConfig); |
| 15 | + |
| 16 | +export type ClassifiedSecretProvider = |
| 17 | + | { readonly kind: "one_password"; readonly account: OnePasswordAccount } |
| 18 | + | { readonly kind: "unsupported" }; |
| 19 | + |
| 20 | +/** Unknown kinds and configs that no longer decode are shown read-only; removal stays available. */ |
| 21 | +export function classifySecretProvider(provider: SecretProviderConfig): ClassifiedSecretProvider { |
| 22 | + if (provider.kind !== "one_password") return { kind: "unsupported" }; |
| 23 | + const decoded = decodeOnePasswordConfig(provider.config); |
| 24 | + return decoded._tag === "Some" |
| 25 | + ? { kind: "one_password", account: decoded.value.account } |
| 26 | + : { kind: "unsupported" }; |
| 27 | +} |
| 28 | + |
| 29 | +/** Stable and readable in settings.json, so re-adding a provider under the same name restores its variables. */ |
| 30 | +function secretProviderIdFromName(name: string): SecretProviderId | null { |
| 31 | + const slug = name |
| 32 | + .toLowerCase() |
| 33 | + .replace(/[^a-z0-9]+/g, "-") |
| 34 | + .replace(/^-|-$/g, "") |
| 35 | + .slice(0, 64) |
| 36 | + .replace(/-$/, ""); |
| 37 | + return Option.getOrNull(decodeSecretProviderId(slug)); |
| 38 | +} |
| 39 | + |
| 40 | +export interface SecretProviderDraft { |
| 41 | + readonly name: string; |
| 42 | + readonly account: string; |
| 43 | +} |
| 44 | + |
| 45 | +export function draftFromSecretProvider(provider: SecretProviderConfig): SecretProviderDraft { |
| 46 | + const classified = classifySecretProvider(provider); |
| 47 | + return { |
| 48 | + name: provider.label, |
| 49 | + account: classified.kind === "one_password" ? classified.account : "", |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +export type SecretProviderTarget = |
| 54 | + | { readonly mode: "add" } |
| 55 | + | { readonly mode: "edit"; readonly id: SecretProviderId }; |
| 56 | + |
| 57 | +export type SecretProviderDraftIssues = { |
| 58 | + readonly [Field in keyof SecretProviderDraft]?: string; |
| 59 | +}; |
| 60 | + |
| 61 | +function decodeField<S extends Schema.ConstraintDecoder<unknown>>( |
| 62 | + schema: S, |
| 63 | + value: string, |
| 64 | + emptyMessage: string, |
| 65 | +): Result.Result<S["Type"], string> { |
| 66 | + const trimmed = value.trim(); |
| 67 | + if (trimmed.length === 0) return Result.fail(emptyMessage); |
| 68 | + return Result.mapError(Schema.decodeResult(schema)(trimmed), (error) => error.message); |
| 69 | +} |
| 70 | + |
| 71 | +/** The settings entry a draft adds or replaces, or every field that blocks it, one message each. */ |
| 72 | +export function secretProviderFromDraft( |
| 73 | + draft: SecretProviderDraft, |
| 74 | + existingIds: ReadonlySet<string>, |
| 75 | + target: SecretProviderTarget, |
| 76 | +): Result.Result< |
| 77 | + { readonly id: SecretProviderId; readonly config: SecretProviderConfig }, |
| 78 | + SecretProviderDraftIssues |
| 79 | +> { |
| 80 | + const label = decodeField(SecretProviderLabel, draft.name, "Enter a name."); |
| 81 | + const account = decodeField( |
| 82 | + OnePasswordSecretProviderConfig.fields.account, |
| 83 | + draft.account, |
| 84 | + "Enter the 1Password account.", |
| 85 | + ); |
| 86 | + |
| 87 | + let id: SecretProviderId | null = null; |
| 88 | + let nameIssue: string | undefined; |
| 89 | + if (Result.isFailure(label)) { |
| 90 | + nameIssue = label.failure; |
| 91 | + } else { |
| 92 | + id = target.mode === "edit" ? target.id : secretProviderIdFromName(label.success); |
| 93 | + if (id === null) nameIssue = "The name needs at least one letter or digit."; |
| 94 | + else if (target.mode === "add" && existingIds.has(id)) |
| 95 | + nameIssue = "A secret provider with this name exists."; |
| 96 | + } |
| 97 | + |
| 98 | + if ( |
| 99 | + id === null || |
| 100 | + nameIssue !== undefined || |
| 101 | + Result.isFailure(label) || |
| 102 | + Result.isFailure(account) |
| 103 | + ) { |
| 104 | + return Result.fail({ |
| 105 | + ...(nameIssue === undefined ? {} : { name: nameIssue }), |
| 106 | + ...(Result.isFailure(account) ? { account: account.failure } : {}), |
| 107 | + }); |
| 108 | + } |
| 109 | + return Result.succeed({ |
| 110 | + id, |
| 111 | + config: { |
| 112 | + kind: SecretProviderKind.make("one_password"), |
| 113 | + label: label.success, |
| 114 | + config: { account: account.success }, |
| 115 | + }, |
| 116 | + }); |
| 117 | +} |
0 commit comments