@@ -578,6 +578,7 @@ class OnePasswordSDK {
578
578
}
579
579
}
580
580
581
+ // Create an item in a vault, handling Documents and additional files
581
582
// Create an item in a vault, handling Documents and additional files
582
583
async createItem ( vaultId , item ) {
583
584
if ( ! this . client ) await this . initializeClient ( ) ;
@@ -602,15 +603,14 @@ class OnePasswordSDK {
602
603
contentType : 'application/octet-stream'
603
604
}
604
605
} ;
605
- createdItem = await this . client . items . create ( newItem ) ;
606
- console . log ( `Document "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
607
- backupLog . push ( `[INFO] Document "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
608
606
609
- // Attach any additional files for Document items (e.g., for "DeleteVaults")
607
+ // Include additional files in client. items.create
610
608
if ( item . files && item . files . length > 0 ) {
611
609
console . log ( `Attaching ${ item . files . length } additional files to Document "${ item . title } "...` ) ;
612
610
backupLog . push ( `[INFO] Attaching ${ item . files . length } additional files to Document "${ item . title } "` ) ;
613
- for ( const file of item . files ) {
611
+ newItem . files = [ ] ;
612
+ const fileSectionIds = new Set ( ) ;
613
+ for ( const [ index , file ] of item . files . entries ( ) ) {
614
614
if ( ! file . content ) {
615
615
console . error ( `Skipping file "${ file . name } " for "${ item . title } ": content is missing` ) ;
616
616
backupLog . push ( `[ERROR] Skipping file "${ file . name } " for "${ item . title } ": content is missing` ) ;
@@ -620,17 +620,32 @@ class OnePasswordSDK {
620
620
const fileContent = new Uint8Array ( fileBuffer . buffer , fileBuffer . byteOffset , fileBuffer . byteLength ) ;
621
621
console . log ( `Attaching file "${ file . name } ", size: ${ fileContent . length } ` ) ;
622
622
backupLog . push ( `[INFO] Attaching file "${ file . name } ", size: ${ fileContent . length } ` ) ;
623
- await this . client . items . files . attach ( createdItem , {
623
+ const fileSectionId = file . sectionId || "restored-section" ;
624
+ const fileFieldId = file . fieldId || `restored-file-${ Date . now ( ) } -${ index } ` ;
625
+ newItem . files . push ( {
624
626
name : file . name ,
625
627
content : fileContent ,
626
- sectionId : file . sectionId || "restored-section" ,
627
- fieldId : file . fieldId || `restored-file- ${ Date . now ( ) } ` ,
628
+ sectionId : fileSectionId ,
629
+ fieldId : fileFieldId ,
628
630
contentType : 'application/octet-stream'
629
631
} ) ;
632
+ fileSectionIds . add ( fileSectionId ) ;
630
633
console . log ( `File "${ file . name } " attached to "${ item . title } "` ) ;
631
634
backupLog . push ( `[INFO] File "${ file . name } " attached to "${ item . title } "` ) ;
632
635
}
636
+
637
+ // Ensure file sectionIds are in newItem.sections
638
+ newItem . sections = newItem . sections || [ ] ;
639
+ for ( const sectionId of fileSectionIds ) {
640
+ if ( ! newItem . sections . some ( section => section . id === sectionId ) ) {
641
+ newItem . sections . push ( { id : sectionId , title : sectionId === "restored-section" ? "Restored Section" : sectionId } ) ;
642
+ }
643
+ }
633
644
}
645
+
646
+ createdItem = await this . client . items . create ( newItem ) ;
647
+ console . log ( `Document "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
648
+ backupLog . push ( `[INFO] Document "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
634
649
} else {
635
650
// Handle non-Document items (Logins, Secure Notes, etc.)
636
651
const sectionIdsFromFields = new Set ( ) ;
@@ -657,17 +672,14 @@ class OnePasswordSDK {
657
672
websites : item . websites || [ ] ,
658
673
notes : item . notes || ""
659
674
} ;
660
- console . log ( `Creating item "${ item . title } "...` ) ;
661
- backupLog . push ( `[INFO] Creating item "${ item . title } "` ) ;
662
- createdItem = await this . client . items . create ( newItem ) ;
663
- console . log ( `Item "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
664
- backupLog . push ( `[INFO] Item "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
665
675
666
- // Attach any files for non-Document items
676
+ // Include files in client. items.create
667
677
if ( item . files && item . files . length > 0 ) {
668
678
console . log ( `Attaching ${ item . files . length } files to "${ item . title } "...` ) ;
669
679
backupLog . push ( `[INFO] Attaching ${ item . files . length } files to "${ item . title } "` ) ;
670
- for ( const file of item . files ) {
680
+ newItem . files = [ ] ;
681
+ const fileSectionIds = new Set ( ) ;
682
+ for ( const [ index , file ] of item . files . entries ( ) ) {
671
683
if ( ! file . content ) {
672
684
console . error ( `Skipping file "${ file . name } " for "${ item . title } ": content is missing` ) ;
673
685
backupLog . push ( `[ERROR] Skipping file "${ file . name } " for "${ item . title } ": content is missing` ) ;
@@ -677,17 +689,33 @@ class OnePasswordSDK {
677
689
const fileContent = new Uint8Array ( fileBuffer . buffer , fileBuffer . byteOffset , fileBuffer . byteLength ) ;
678
690
console . log ( `Attaching file "${ file . name } ", size: ${ fileContent . length } ` ) ;
679
691
backupLog . push ( `[INFO] Attaching file "${ file . name } ", size: ${ fileContent . length } ` ) ;
680
- await this . client . items . files . attach ( createdItem , {
692
+ const fileSectionId = file . sectionId || "restored-section" ;
693
+ const fileFieldId = file . fieldId || `restored-file-${ Date . now ( ) } -${ index } ` ;
694
+ newItem . files . push ( {
681
695
name : file . name ,
682
696
content : fileContent ,
683
- sectionId : file . sectionId || "restored-section" ,
684
- fieldId : file . fieldId || `restored-file- ${ Date . now ( ) } ` ,
697
+ sectionId : fileSectionId ,
698
+ fieldId : fileFieldId ,
685
699
contentType : 'application/octet-stream'
686
700
} ) ;
701
+ fileSectionIds . add ( fileSectionId ) ;
687
702
console . log ( `File "${ file . name } " attached to "${ item . title } "` ) ;
688
703
backupLog . push ( `[INFO] File "${ file . name } " attached to "${ item . title } "` ) ;
689
704
}
705
+
706
+ // Ensure file sectionIds are in newItem.sections
707
+ for ( const sectionId of fileSectionIds ) {
708
+ if ( ! newItem . sections . some ( section => section . id === sectionId ) ) {
709
+ newItem . sections . push ( { id : sectionId , title : sectionId === "restored-section" ? "Restored Section" : sectionId } ) ;
710
+ }
711
+ }
690
712
}
713
+
714
+ console . log ( `Creating item "${ item . title } "...` ) ;
715
+ backupLog . push ( `[INFO] Creating item "${ item . title } "` ) ;
716
+ createdItem = await this . client . items . create ( newItem ) ;
717
+ console . log ( `Item "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
718
+ backupLog . push ( `[INFO] Item "${ item . title } " created with ID: ${ createdItem . id } ` ) ;
691
719
}
692
720
} catch ( error ) {
693
721
console . error ( `Item creation failed for "${ item . title } ": ${ error . message } ` ) ;
0 commit comments