@@ -6,38 +6,35 @@ import {
66 type OnePasswordAccount ,
77 type SecretProviderConfig ,
88} from "@t3tools/contracts" ;
9+ import * as Option from "effect/Option" ;
910import * as Result from "effect/Result" ;
1011import * as Schema from "effect/Schema" ;
1112
12- const decodeSecretProviderId = Schema . decodeResult ( SecretProviderId ) ;
13+ const decodeSecretProviderId = Schema . decodeOption ( SecretProviderId ) ;
1314const decodeOnePasswordConfig = Schema . decodeUnknownOption ( OnePasswordSecretProviderConfig ) ;
1415
1516export type ClassifiedSecretProvider =
16- | { readonly kind : "1password " ; readonly account : OnePasswordAccount }
17+ | { readonly kind : "one_password " ; readonly account : OnePasswordAccount }
1718 | { readonly kind : "unsupported" } ;
1819
1920/** Unknown kinds and configs that no longer decode are shown read-only; removal stays available. */
2021export function classifySecretProvider ( provider : SecretProviderConfig ) : ClassifiedSecretProvider {
21- if ( provider . kind !== "1password " ) return { kind : "unsupported" } ;
22+ if ( provider . kind !== "one_password " ) return { kind : "unsupported" } ;
2223 const decoded = decodeOnePasswordConfig ( provider . config ) ;
2324 return decoded . _tag === "Some"
24- ? { kind : "1password " , account : decoded . value . account }
25+ ? { kind : "one_password " , account : decoded . value . account }
2526 : { kind : "unsupported" } ;
2627}
2728
28- /** Stable and readable in settings.json; the id must also be a valid slug, which starts with a letter . */
29- function secretProviderIdFromName ( name : string ) : Result . Result < SecretProviderId , string > {
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 {
3031 const slug = name
3132 . toLowerCase ( )
3233 . replace ( / [ ^ a - z 0 - 9 ] + / g, "-" )
3334 . replace ( / ^ - | - $ / g, "" )
3435 . slice ( 0 , 64 )
3536 . replace ( / - $ / , "" ) ;
36- if ( slug . length === 0 ) return Result . fail ( "The name needs at least one letter or digit." ) ;
37- return Result . mapError (
38- decodeSecretProviderId ( slug ) ,
39- ( ) => "The name needs to start with a letter." ,
40- ) ;
37+ return Option . getOrNull ( decodeSecretProviderId ( slug ) ) ;
4138}
4239
4340export interface SecretProviderDraft {
@@ -49,7 +46,7 @@ export function draftFromSecretProvider(provider: SecretProviderConfig): SecretP
4946 const classified = classifySecretProvider ( provider ) ;
5047 return {
5148 name : provider . label ,
52- account : classified . kind === "1password " ? classified . account : "" ,
49+ account : classified . kind === "one_password " ? classified . account : "" ,
5350 } ;
5451}
5552
@@ -92,16 +89,10 @@ export function secretProviderFromDraft(
9289 if ( Result . isFailure ( label ) ) {
9390 nameIssue = label . failure ;
9491 } else {
95- if ( target . mode === "edit" ) {
96- id = target . id ;
97- } else {
98- const generated = secretProviderIdFromName ( label . success ) ;
99- if ( Result . isFailure ( generated ) ) nameIssue = generated . failure ;
100- else {
101- id = generated . success ;
102- if ( existingIds . has ( id ) ) nameIssue = "A secret provider with this name exists." ;
103- }
104- }
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." ;
10596 }
10697
10798 if (
@@ -118,7 +109,7 @@ export function secretProviderFromDraft(
118109 return Result . succeed ( {
119110 id,
120111 config : {
121- kind : SecretProviderKind . make ( "1password " ) ,
112+ kind : SecretProviderKind . make ( "one_password " ) ,
122113 label : label . success ,
123114 config : { account : account . success } ,
124115 } ,
0 commit comments