ServicePatch.js 923 B

123456789101112131415161718192021222324252627
  1. const { AdvancedBase } = require("@heyputer/puter-js-common");
  2. class ServicePatch extends AdvancedBase {
  3. patch ({ original_service }) {
  4. const patch_methods = this._get_merged_static_object('PATCH_METHODS');
  5. for ( const k in patch_methods ) {
  6. if ( typeof patch_methods[k] !== 'function' ) {
  7. throw new Error(`Patch method ${k} to ${original_service.service_name} ` +
  8. `from ${this.constructor.name} ` +
  9. `is not a function.`)
  10. }
  11. const patch_method = patch_methods[k];
  12. const patch_arguments = {
  13. that: original_service,
  14. original: original_service[k].bind(original_service),
  15. };
  16. original_service[k] = (...a) => {
  17. return patch_method.call(this, patch_arguments, ...a);
  18. }
  19. }
  20. }
  21. }
  22. module.exports = ServicePatch;