test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var dav = require('dav');
  2. var VCard = require( 'vcard' )
  3. var VCF = require( 'vcf' )
  4. var xhr = new dav.transport.Basic(
  5. new dav.Credentials({
  6. username: '',
  7. password: ''
  8. })
  9. );
  10. var client = new dav.Client(xhr);
  11. // No transport arg
  12. client.createAccount({
  13. server: 'https://example.com/dav.php',
  14. accountType: 'carddav'
  15. })
  16. .then(function(account) {
  17. account.addressBooks.forEach(function(addressBook) {
  18. console.log('Found address book name ' + addressBook.displayName);
  19. const card = new VCard({fn: 'name', bday: new Date()});
  20. card.validate();
  21. console.log(vCardToString(card))
  22. client.createCard(addressBook, {
  23. data: vCardToString(card),
  24. filename: `${card.uid}.vcf`,
  25. xhr: xhr
  26. })
  27. client.syncAddressBook(addressBook).then(function(x) {
  28. for(const vcard of x.objects) {
  29. console.log(vcard.addressData);
  30. VCF.parse(vcard.addressData, function(card) {
  31. console.log(card, vCardToString(card))
  32. })
  33. }
  34. })
  35. });
  36. });
  37. function vCardToString(vCard) {
  38. return `BEGIN:VCARD
  39. VERSION:3.0
  40. UID:${vCard.uid}
  41. N:;${vCard.fn};;;
  42. FN:${vCard.fn}
  43. NOTE:${vCard.note && vCard.note[0] ? vCard.note[0] : ""}
  44. REV:${(vCard.rev instanceof Date ? vCard.rev.toISOString().replace(/[-:]|\.000/g, '') : vCard.rev)}
  45. BDAY;VALUE=date:${vCard.bday.toISOString().substring(0, 10).replace(/-/g, '')}
  46. PRODID:-//birthday-script//EN
  47. END:VCARD
  48. `;
  49. }