@shockpkg/core
Version:
shockpkg core
82 lines (68 loc) • 1.94 kB
JavaScript
import _initializerDefineProperty from "@babel/runtime/helpers/initializerDefineProperty.js";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/applyDecoratedDescriptor.js";
import _initializerWarningHelper from "@babel/runtime/helpers/initializerWarningHelper.js";
var _dec, _class, _descriptor, _temp;
import request from 'request';
import { property } from "./decorators.mjs";
/**
* Request wrapper around the request module.
*
* @param defaults Default options.
*/
export let Request = (_dec = property(false), (_class = (_temp = class Request extends Object {
/**
* Request instance.
*/
constructor(defaults = {}) {
super();
_initializerDefineProperty(this, "_request", _descriptor, this);
this._request = this._createRequest(defaults);
}
/**
* Make request with an optional callback.
*
* @param options Request options.
* @param cb An optional callback function.
* @return Stream object.
*/
stream(options, cb) {
const req = this._request;
return cb ? req(options, cb) : req(options);
}
/**
* Make request with promise.
*
* @param options Request options.
* @return Stream response and body.
*/
async promise(options) {
return new Promise((resolve, reject) => {
const stream = this.stream(options, (error, response, body) => {
if (error) {
reject(error);
return;
}
resolve({
stream,
response,
body
});
});
});
}
/**
* Make a request object.
*
* @param defaults Request defaults.
* @return Request instance.
*/
_createRequest(defaults = {}) {
return request.defaults(defaults);
}
}, _temp), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "_request", [_dec], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
})), _class));
//# sourceMappingURL=request.mjs.map