@@ -46,37 +46,41 @@ export async function POST(request: NextRequest) {
4646 if ( creds . mode === 'service_account' ) {
4747 const client = await createOnePasswordClient ( creds . serviceAccountToken ! )
4848
49+ const existing = await client . items . get ( params . vaultId , params . itemId )
50+
4951 const sdkItem = {
52+ ...existing ,
5053 id : params . itemId ,
51- title : itemData . title || '' ,
52- category : toSdkCategory ( itemData . category || 'LOGIN' ) ,
54+ title : itemData . title || existing . title ,
55+ category : itemData . category ? toSdkCategory ( itemData . category ) : existing . category ,
5356 vaultId : params . vaultId ,
54- fields : ( itemData . fields ?? [ ] ) . map ( ( f : Record < string , any > ) => ( {
55- id : f . id || '' ,
56- title : f . label || f . title || '' ,
57- fieldType : toSdkFieldType ( f . type || 'STRING' ) ,
58- value : f . value || '' ,
59- sectionId : f . section ?. id ?? f . sectionId ,
60- } ) ) ,
61- sections : ( itemData . sections ?? [ ] ) . map ( ( s : Record < string , any > ) => ( {
62- id : s . id || '' ,
63- title : s . label || s . title || '' ,
64- } ) ) ,
65- notes : itemData . notes || '' ,
66- tags : itemData . tags ?? [ ] ,
67- websites : ( itemData . urls ?? itemData . websites ?? [ ] ) . map ( ( u : Record < string , any > ) => ( {
68- url : u . href || u . url || '' ,
69- label : u . label || '' ,
70- autofillBehavior : 'AnywhereOnWebsite' as const ,
71- } ) ) ,
72- version : itemData . version ?? 0 ,
73- files : [ ] ,
74- createdAt : new Date ( ) ,
75- updatedAt : new Date ( ) ,
57+ fields : itemData . fields
58+ ? ( itemData . fields as Array < Record < string , any > > ) . map ( ( f ) => ( {
59+ id : f . id || randomUUID ( ) . slice ( 0 , 8 ) ,
60+ title : f . label || f . title || '' ,
61+ fieldType : toSdkFieldType ( f . type || 'STRING' ) ,
62+ value : f . value || '' ,
63+ sectionId : f . section ?. id ?? f . sectionId ,
64+ } ) )
65+ : existing . fields ,
66+ sections : itemData . sections
67+ ? ( itemData . sections as Array < Record < string , any > > ) . map ( ( s ) => ( {
68+ id : s . id || '' ,
69+ title : s . label || s . title || '' ,
70+ } ) )
71+ : existing . sections ,
72+ notes : itemData . notes ?? existing . notes ,
73+ tags : itemData . tags ?? existing . tags ,
74+ websites :
75+ itemData . urls || itemData . websites
76+ ? ( itemData . urls ?? itemData . websites ?? [ ] ) . map ( ( u : Record < string , any > ) => ( {
77+ url : u . href || u . url || '' ,
78+ label : u . label || '' ,
79+ autofillBehavior : 'AnywhereOnWebsite' as const ,
80+ } ) )
81+ : existing . websites ,
7682 }
7783
78- // Cast to any because toSdkCategory/toSdkFieldType return string literals
79- // that match SDK enum values but TypeScript can't verify this at compile time
8084 const result = await client . items . put ( sdkItem as any )
8185 return NextResponse . json ( normalizeSdkItem ( result ) )
8286 }
0 commit comments