deleteFSEntry.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as utils from '../../../lib/utils.js';
  2. import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
  3. // why is this called deleteFSEntry instead of just delete?
  4. // because delete is a reserved keyword in javascript
  5. const deleteFSEntry = async function(...args) {
  6. let options;
  7. // If first argument is an object, it's the options
  8. if (typeof args[0] === 'object' && args[0] !== null) {
  9. options = args[0];
  10. }
  11. // Otherwise, we assume separate arguments are provided
  12. else {
  13. options = {
  14. paths: args[0],
  15. recursive: args[1]?.recursive ?? true,
  16. descendantsOnly: args[1]?.descendantsOnly ?? false,
  17. };
  18. }
  19. // If paths is a string, convert to array
  20. // this is to make it easier for the user to provide a single path without having to wrap it in an array
  21. let paths = options.paths;
  22. if(typeof paths === 'string')
  23. paths = [paths];
  24. return new Promise(async (resolve, reject) => {
  25. // If auth token is not provided and we are in the web environment,
  26. // try to authenticate with Puter
  27. if(!puter.authToken && puter.env === 'web'){
  28. try{
  29. await puter.ui.authenticateWithPuter();
  30. }catch(e){
  31. // if authentication fails, throw an error
  32. reject('Authentication failed.');
  33. }
  34. }
  35. // create xhr object
  36. const xhr = utils.initXhr('/delete', this.APIOrigin, this.authToken);
  37. // set up event handlers for load and error events
  38. utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
  39. // convert paths to absolute paths
  40. paths = paths.map((path) => {
  41. return getAbsolutePathForApp(path);
  42. })
  43. xhr.send(JSON.stringify({
  44. paths: paths,
  45. descendants_only: (options.descendants_only || options.descendantsOnly) ?? false,
  46. recursive: options.recursive ?? true,
  47. }));
  48. })
  49. }
  50. export default deleteFSEntry;