aveazul 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/aveazul.js +97 -0
- package/package.json +4 -4
package/lib/aveazul.js
CHANGED
|
@@ -353,4 +353,101 @@ AveAzul.reduce = (array, fn, initialValue) => {
|
|
|
353
353
|
*/
|
|
354
354
|
AveAzul.throw = reason => AveAzul.reject(reason);
|
|
355
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Bluebird-style promisifyAll() for converting all methods of an object or class to promise-based versions
|
|
358
|
+
* Similar to Bluebird's Promise.promisifyAll()
|
|
359
|
+
*
|
|
360
|
+
* @param {Object|Function} target - The object or class to promisify
|
|
361
|
+
* @param {Object} [options] - Configuration options
|
|
362
|
+
* @param {string} [options.suffix='Async'] - Suffix to append to promisified method names
|
|
363
|
+
* @param {Function} [options.filter] - Filter function to determine which methods to promisify
|
|
364
|
+
* @param {Function} [options.promisifier] - Custom function to handle promisification
|
|
365
|
+
* @param {boolean} [options.multiArgs=false] - Whether to support multiple callback arguments
|
|
366
|
+
* @param {boolean} [options.excludeMain=false] - Whether to exclude promisifying the main object/class
|
|
367
|
+
* @param {Object} [options.context] - The context (this) to use when calling methods
|
|
368
|
+
* @returns {Object|Function} The promisified object or class
|
|
369
|
+
* @throws {TypeError} If target is null, undefined, or not an object/function
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* // Promisify an object
|
|
373
|
+
* const obj = {
|
|
374
|
+
* method(cb) { cb(null, 'result'); }
|
|
375
|
+
* };
|
|
376
|
+
* AveAzul.promisifyAll(obj);
|
|
377
|
+
* const result = await obj.methodAsync();
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* // Promisify a class
|
|
381
|
+
* class MyClass {
|
|
382
|
+
* method(cb) { cb(null, 'result'); }
|
|
383
|
+
* }
|
|
384
|
+
* AveAzul.promisifyAll(MyClass);
|
|
385
|
+
* const instance = new MyClass();
|
|
386
|
+
* const result = await instance.methodAsync();
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* // With custom options
|
|
390
|
+
* const obj = {
|
|
391
|
+
* method(cb) { cb(null, 'result1', 'result2'); }
|
|
392
|
+
* };
|
|
393
|
+
* AveAzul.promisifyAll(obj, {
|
|
394
|
+
* suffix: 'Promise',
|
|
395
|
+
* multiArgs: true,
|
|
396
|
+
* filter: (name) => name === 'method'
|
|
397
|
+
* });
|
|
398
|
+
* const [result1, result2] = await obj.methodPromise();
|
|
399
|
+
*/
|
|
400
|
+
AveAzul.promisifyAll = (target, options = {}) => {
|
|
401
|
+
const {
|
|
402
|
+
suffix = 'Async',
|
|
403
|
+
filter = (name, func, targetObj, passedOptions) => {
|
|
404
|
+
return (
|
|
405
|
+
typeof func === 'function' &&
|
|
406
|
+
!func.name.startsWith('_') &&
|
|
407
|
+
!func.name.startsWith('promisify') &&
|
|
408
|
+
!func.name.startsWith('promisifyAll')
|
|
409
|
+
);
|
|
410
|
+
},
|
|
411
|
+
promisifier = (fn, context, multiArgs) => {
|
|
412
|
+
if (multiArgs) {
|
|
413
|
+
return (...args) => {
|
|
414
|
+
return new AveAzul((resolve, reject) => {
|
|
415
|
+
args.push((err, ...results) => {
|
|
416
|
+
if (err) reject(err);
|
|
417
|
+
else resolve(results);
|
|
418
|
+
});
|
|
419
|
+
fn.apply(context, args);
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
return AveAzul.promisify(fn, { context });
|
|
424
|
+
},
|
|
425
|
+
multiArgs = false,
|
|
426
|
+
excludeMain = false,
|
|
427
|
+
context = target
|
|
428
|
+
} = options;
|
|
429
|
+
|
|
430
|
+
if (target == null || (typeof target !== 'object' && typeof target !== 'function')) {
|
|
431
|
+
throw new TypeError('target must be an object');
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const targetObj = target.prototype || target;
|
|
435
|
+
const keys = Object.getOwnPropertyNames(targetObj);
|
|
436
|
+
|
|
437
|
+
for (const key of keys) {
|
|
438
|
+
const func = targetObj[key];
|
|
439
|
+
if (filter(key, func, targetObj, options)) {
|
|
440
|
+
const promisifiedKey = key + suffix;
|
|
441
|
+
targetObj[promisifiedKey] = promisifier(func, context, multiArgs);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (!excludeMain && typeof target === 'function') {
|
|
446
|
+
target.promisify = AveAzul.promisify;
|
|
447
|
+
target.promisifyAll = AveAzul.promisifyAll;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return target;
|
|
451
|
+
};
|
|
452
|
+
|
|
356
453
|
module.exports = AveAzul;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aveazul",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Extend native Promise with bluebird like APIs",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Extend native Promise with bluebird like APIs for Node.js",
|
|
5
5
|
"main": "lib/aveazul.js",
|
|
6
|
-
"homepage": "",
|
|
6
|
+
"homepage": "https://github.com/jchip/aveazul",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "jest test",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
],
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
25
|
-
"url": ""
|
|
25
|
+
"url": "git+https://github.com/jchip/aveazul.git"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"xaa": "^1.7.3"
|