diff --git a/lib/NodeRestClient.js b/lib/NodeRestClient.js index fee5aa6..3b51d1b 100644 --- a/lib/NodeRestClient.js +++ b/lib/NodeRestClient.js @@ -10,7 +10,7 @@ import debug from './NrcDebugger.js'; /** - * Description placeholder + * Main node-rest-client component * @author aacerox * * @export @@ -19,14 +19,14 @@ import debug from './NrcDebugger.js'; */ export default class NodeRestClient extends events.EventEmitter { /** - * Description placeholder + * nrc config * @author aacerox * * @type {options} */ #config = {}; /** - * Description placeholder + * nrc parsers and serializers facade * @author aacerox * * @type {*} @@ -35,61 +35,69 @@ export default class NodeRestClient extends events.EventEmitter { #apis = {}; - // public namespaces /** - * Description placeholder + * method registry * @author aacerox * * @type {{}} */ - #parsers = {}; + #methods = {}; + /** - * Description placeholder + * parsers registry * @author aacerox * * @type {{}} */ - #methods = {}; + #parsers = {}; /** - * Description placeholder + * serializers registry * @author aacerox * * @type {{}} */ #serializers = {}; - // default http methods /** - * Description placeholder + * private GET HTTP method where callback + * and promise logic are managed * @author aacerox * * @type {*} */ #get; + /** - * Description placeholder + * private POST HTTP method where callback + * and promise logic are managed * @author aacerox * * @type {*} */ #post; + /** - * Description placeholder + * private PUT HTTP method where callback + * and promise logic are managed * @author aacerox * * @type {*} */ #put; + /** - * Description placeholder + * private DELETE HTTP method where callback + * and promise logic are managed * @author aacerox * * @type {*} */ #delete; + /** - * Description placeholder + * private PATCH HTTP method where callback + * and promise logic are managed * @author aacerox * * @type {*} @@ -98,12 +106,13 @@ export default class NodeRestClient extends events.EventEmitter { #usePromises; /** - * Creates an instance of NodeRestClient. + * Creates a NodeRestClient instance. * @author aacerox * * @constructor * @param {options} options to initialize client - * @param {usePromises} usePromises instead of callbacks + * @param {usePromises} usePromises if true nrc will use promises + * instead of callbacks */ constructor( options = { @@ -115,7 +124,7 @@ export default class NodeRestClient extends events.EventEmitter { }, ) { super(); - const useProxy = options.proxy ? true : false; + const useProxy = !!options.proxy; const useProxyTunnel = !useProxy || options.proxy.tunnel === undefined ? false : @@ -131,7 +140,8 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * main initialization method where HTTP methods parsers and serializers + * are initialized * @author aacerox */ #initialize() { @@ -191,7 +201,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns the GET HTTP method wrapper * @author aacerox * * @readonly @@ -202,7 +212,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns the POST HTTP method wrapper * @author aacerox * * @readonly @@ -213,7 +223,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns the PUT HTTP method wrapper * @author aacerox * * @readonly @@ -224,7 +234,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns the DELETE HTTP method wrapper * @author aacerox * * @readonly @@ -235,7 +245,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns the PATCH HTTP method wrapper * @author aacerox * * @readonly @@ -246,7 +256,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * return all the nrc configured response parsers * @author aacerox * * @readonly @@ -257,7 +267,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns all the nrc configured request serializers * @author aacerox * * @readonly @@ -268,7 +278,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns all nrc registered methods * @author aacerox * * @readonly @@ -279,7 +289,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * add a new custom method to the method registry * @author aacerox * * @param {*} name @@ -289,13 +299,13 @@ export default class NodeRestClient extends events.EventEmitter { registerMethod(name, url, method) { // create method in method registry with pre-configured REST invocation // method - this.#methods[name] = this.#usePromises? - this.#getHttpPromiseFn(url, method): + this.#methods[name] = this.#usePromises ? + this.#getHttpPromiseFn(url, method) : this.#getHttpCallbackFn(url, method); } /** - * Description placeholder + * unregister previously registered method * @author aacerox * * @param {*} name @@ -304,9 +314,8 @@ export default class NodeRestClient extends events.EventEmitter { delete this.methods[name]; } - /** - * Description placeholder + * returns a new HTTP method callback function * @author aacerox * * @param {*} url @@ -322,7 +331,7 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns a new HTTP method promise * @author aacerox * * @param {*} url @@ -343,7 +352,8 @@ export default class NodeRestClient extends events.EventEmitter { } /** - * Description placeholder + * returns a new IOFacade object to access response parsers + * and request serializers * @author aacerox * * @param {*} parserManager diff --git a/lib/NrcClientRequest.js b/lib/NrcClientRequest.js index f30e5df..329c793 100644 --- a/lib/NrcClientRequest.js +++ b/lib/NrcClientRequest.js @@ -13,16 +13,7 @@ import events from 'events'; */ export default class ClientRequest extends events.EventEmitter { /** - * Creates an instance of ClientRequest. - * @author aacerox - * - * @constructor - */ - constructor() { - super(); - } - /** - * Description placeholder + * finalize current http request * @author aacerox */ end() { @@ -31,7 +22,7 @@ export default class ClientRequest extends events.EventEmitter { } } /** - * Description placeholder + * set new http request * @author aacerox * * @param {*} req diff --git a/lib/NrcConnectManager.js b/lib/NrcConnectManager.js index d3853f6..c29943f 100644 --- a/lib/NrcConnectManager.js +++ b/lib/NrcConnectManager.js @@ -7,7 +7,7 @@ import debug from './NrcDebugger.js'; const {http, https} = followRedirect; /** - * Description placeholder + * Manage proxy and non-proxy underlying connections * @author aacerox * * @export @@ -16,21 +16,32 @@ const {http, https} = followRedirect; */ export class NrcConnectManager { /** - * Description placeholder + * response parser manager * @author aacerox * * @type {*} */ #parserManager; /** - * Description placeholder + * nrc instance managed by connect manager * @author aacerox * * @type {*} */ #client; + /** + * if true the manager will manage HTTP methods using promises + * if false will manage HTTP methods with function callbacks + * + * By default is false + * + * @date 8/9/2023 - 5:30:01 PM + * + * @type {*} + */ #usePromises; + /** * Creates an instance of NrcConnectManager. * @author aacerox @@ -40,14 +51,14 @@ export class NrcConnectManager { * @param {*} parserManager * @param {*} usePromises */ - constructor(client, parserManager, usePromises= false) { + constructor(client, parserManager, usePromises = false) { this.#parserManager = parserManager; this.#client = client; this.#usePromises = usePromises; } /** - * Description placeholder + * connect using configured proxy * @author aacerox * * @param {*} options @@ -123,7 +134,7 @@ export class NrcConnectManager { // handle request errors and handle them by request or general // error handler - request.on('error', function (err) { + request.on('error', function(err) { if (clientRequest !== undefined && typeof clientRequest === 'object') { // add request as property of error err.request = clientRequest; @@ -143,7 +154,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * direct connection to target (do not use proxy) * @author aacerox * * @param {*} options @@ -162,7 +173,7 @@ export class NrcConnectManager { // add request options to request returned to calling method clientRequest.options = options; - const request = protocol.request(options, function (res) { + const request = protocol.request(options, function(res) { // configure response self.#configureResponse(res, responseConfig, clientRequest); @@ -205,7 +216,7 @@ export class NrcConnectManager { // handle request errors and handle them by request or general // error handler - request.on('error', function (err) { + request.on('error', function(err) { if (clientRequest !== undefined && typeof clientRequest === 'object') { // add request as property of error err.request = clientRequest; @@ -220,7 +231,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * prepare request using global or method config * @author aacerox * * @param {*} req @@ -230,7 +241,7 @@ export class NrcConnectManager { #configureRequest(req, config, clientRequest) { if (config.timeout) { req.setTimeout(config.timeout, () => - clientRequest.emit('requestTimeout', req) + clientRequest.emit('requestTimeout', req), ); } @@ -244,7 +255,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * prepare response using global or method config * @author aacerox * * @param {*} res @@ -261,7 +272,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * configure connect manager according to passed options * @author aacerox * * @param {*} options @@ -286,7 +297,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * handle requests * @author aacerox * * @param {*} res @@ -308,7 +319,7 @@ export class NrcConnectManager { } else if (encoding !== undefined && encoding.indexOf('deflate') >= 0) { debug('inflate'); zlib.inflate(Buffer.concat(buffer), (er, inflated) => - this.#handleResponse(res, inflated, callback) + this.#handleResponse(res, inflated, callback), ); } else { debug('not compressed'); @@ -317,7 +328,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * handle responses * @author aacerox * * @param {*} res @@ -327,17 +338,18 @@ export class NrcConnectManager { #handleResponse(res, data, callback) { // find valid parser to be used with response content type, first one // found - const parserCallbackFn = this.#usePromises - ? (parsedData) => callback({ data: parsedData, response: res }) - : (parsedData) => callback(parsedData, res); + const parserCallbackFn = this.#usePromises ? + (parsedData) => callback({data: parsedData, response: res}) : + (parsedData) => callback(parsedData, res); this.#parserManager - .get(res) - .parse(data, this.#clientEmitterWrapper(this.#client), parserCallbackFn); + .get(res) + .parse(data, + this.#clientEmitterWrapper(this.#client), parserCallbackFn); } /** - * Description placeholder + * client event emitter wrapper * @author aacerox * * @param {*} client @@ -348,7 +360,7 @@ export class NrcConnectManager { } /** - * Description placeholder + * prepare write HTTP method data * @author aacerox * * @param {*} data diff --git a/lib/NrcDebugger.js b/lib/NrcDebugger.js index 79d475f..31b5597 100644 --- a/lib/NrcDebugger.js +++ b/lib/NrcDebugger.js @@ -3,7 +3,7 @@ import debugManager from 'debug'; /** - * Description placeholder + * main debug manager * @author aacerox * * @type {*} @@ -12,7 +12,7 @@ const nodeDebug = debugManager('NRC'); /** - * Description placeholder + * main debug function * @author aacerox * * @param {...{}} args @@ -21,7 +21,7 @@ export default function debugFn(...args) { if (!process.env.DEBUG) return; /** - * Description placeholder + * returns debug function callers name to be logged on debug trace * @author aacerox * * @return {*} diff --git a/lib/NrcIoManager.js b/lib/NrcIoManager.js index 71be06c..2456997 100644 --- a/lib/NrcIoManager.js +++ b/lib/NrcIoManager.js @@ -11,7 +11,7 @@ const CONSTANTS = { }; /** - * Description placeholder + * Manage the creation and configuration of HTTP methods * @author aacerox * * @export @@ -19,16 +19,36 @@ const CONSTANTS = { * @typedef {NrcIoManager} */ export default class NrcIoManager extends events.EventEmitter { + /** + * nrc connect manager instance + * @date 8/9/2023 - 5:46:33 PM + * + * @type {*} + */ #connectManager; + /** - * Description placeholder + * nrc instance * @author aacerox * * @type {*} */ #client; + /** + * nrc instance request serializers + * @date 8/9/2023 - 5:47:16 PM + * + * @type {*} + */ #serializerManager; + + /** + * nrc instance response parsers + * @date 8/9/2023 - 5:47:20 PM + * + * @type {*} + */ #parserManager; /** @@ -50,7 +70,7 @@ export default class NrcIoManager extends events.EventEmitter { } /** - * Description placeholder + * create an HTTP method by using promises or function callbacks * @author aacerox * * @param {*} methodName @@ -61,22 +81,23 @@ export default class NrcIoManager extends events.EventEmitter { createHttpMethod(methodName, config, usePromises) { const self = this; if (usePromises) { - return (url, args) => new Promise( (successFn, errorFn)=>{ - try { - const clientRequest = new NrcClientRequest(); - self.#connect( - config, - methodName.toUpperCase(), - url, - args, - successFn, - clientRequest, - ); - return clientRequest; - } catch (err) { - errorFn(err); - } - }); + return (url, args) => + new Promise((successFn, errorFn) => { + try { + const clientRequest = new NrcClientRequest(); + self.#connect( + config, + methodName.toUpperCase(), + url, + args, + successFn, + clientRequest, + ); + return clientRequest; + } catch (err) { + errorFn(err); + } + }); } else { return (url, args, callback) => { const clientRequest = new NrcClientRequest(); @@ -94,10 +115,11 @@ export default class NrcIoManager extends events.EventEmitter { } /** - * Description placeholder + * add passed mimetypes to response parsers * @author aacerox * * @param {*} mimetypes + * @deprecated */ mergeMimeTypes(mimetypes) { // this function is left for backward compatibility, but will be @@ -131,7 +153,7 @@ export default class NrcIoManager extends events.EventEmitter { } /** - * Description placeholder + * Add default HTTPS port to host if not present * @author aacerox * * @param {*} url @@ -148,7 +170,7 @@ export default class NrcIoManager extends events.EventEmitter { } /** - * Description placeholder + * create proxy security and port-forwarding headers * @author aacerox * * @param {*} config @@ -174,7 +196,8 @@ export default class NrcIoManager extends events.EventEmitter { } /** - * Description placeholder + * Create connection options for passed HTTP method + * according nrc global or method config * @author aacerox * * @param {*} config @@ -266,7 +289,7 @@ export default class NrcIoManager extends events.EventEmitter { * @return {{}} */ #decodeQueryFromURL(connectURL) { - const url = urlParser.parse(connectURL); + const url = new URL(connectURL); const query = url.query.substring(1).split('&'); let keyValue; const result = {}; diff --git a/lib/nrc-parser-manager.js b/lib/nrc-parser-manager.js index 1f5ca97..2713584 100644 --- a/lib/nrc-parser-manager.js +++ b/lib/nrc-parser-manager.js @@ -3,7 +3,7 @@ import xmlParser from 'xml2js'; /** - * node-rest-client parser manager + * node-rest-client response parser manager * * @class ParserManager * @typedef {ParserManager} diff --git a/lib/nrc-serializer-manager.js b/lib/nrc-serializer-manager.js index 41ffdee..03c2efe 100644 --- a/lib/nrc-serializer-manager.js +++ b/lib/nrc-serializer-manager.js @@ -1,7 +1,7 @@ import XmlSerializer from 'xml2js'; /** - * Serializer manager for node-rest-client + * request Serializer manager for node-rest-client * @date 4/26/2023 - 12:45:40 PM * * @class SerializerManager @@ -16,7 +16,7 @@ class SerializerManager { */ #registry = new Map(); /** - * default serializer used when no match serializer found for request + * default serializer used when no match serializer found for reponse * @date 4/26/2023 - 12:45:40 PM * * @type {*}