Skip to content

Commit

Permalink
Axios require some times need default (#47)
Browse files Browse the repository at this point in the history
* Axios require can be require .default depends on enviroment.

axios/axios#1975

* axios require fix for some environments

* bump version

* remove test id

* update paackage lock
  • Loading branch information
xinghengwang authored Nov 14, 2024
1 parent 53f2a83 commit 7b94732
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 48 deletions.
67 changes: 35 additions & 32 deletions lib/APIHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
*
*
*/

var stream = require('stream');

var hasOwn = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

var APIHelper = {

/**
* Replaces template parameters in the given url
* @param {String} queryBuilder The query string builder to replace the template parameters
* @param {Array} parameters The parameters to replace in the queryBuilder
* @param {Array} parameters The parameters to replace in the queryBuilder
*/
appendUrlWithTemplateParameters:function(queryBuilder, parameters) {
appendUrlWithTemplateParameters:function(queryBuilder, parameters) {
//perform parameter validation
if(queryBuilder == null) {
console.log('queryBuilder is null');
return;
}
if(parameters ==null) {
return queryBuilder;
}
//iterate and replace parameters
}
//iterate and replace parameters
for(var key in parameters) {
var replaceValue = "";
//load parameter value
Expand All @@ -38,15 +41,15 @@ var APIHelper = {
} else {
replaceValue = encodeURIComponent(element.toString());
}
queryBuilder = queryBuilder.replace('{'+(key)+'}', replaceValue)
queryBuilder = queryBuilder.replace('{'+(key)+'}', replaceValue)
}
return queryBuilder;
},

/**
* Appends the given set of parameters to the given query string
* @param {String} queryBuilder The query url string to append the parameters
* @param {Array} parameters The parameters to append
* @param {Array} parameters The parameters to append
*/
appendUrlWithQueryParameters:function(queryBuilder, parameters) {
//perform parameter validation
Expand All @@ -68,52 +71,52 @@ var APIHelper = {
/**
* Validates and processes the given Url
* @param {String} url The Url to process
* @return {String} Pocessed url
* @return {String} Pocessed url
*/
cleanUrl:function(url) {
//ensure that the urls are absolute
var re = /^https?:\/\/[^\/]+/;
var str = url;
cleanUrl:function(url) {
//ensure that the urls are absolute
var re = /^https?:\/\/[^\/]+/;
var str = url;
var match = url.match(re);
if(match==null) {
console.log('Invalid Url format');
return;
return;
}
//remove redundant forward slashes
var protocol = match[0];
var queryUrl = url.substring(protocol.length);
queryUrl = queryUrl.replace(/\/\/+/,"/");
var result = protocol+queryUrl;
queryUrl = queryUrl.replace(/\/\/+/,"/");
var result = protocol+queryUrl;
return result;
},
},

/**
* JSON Serialization of a given object.
* @param {Object} data The object to serialize into JSON
* @return The serialized Json string representation of the given object
* @return The serialized Json string representation of the given object
*/
jsonSerialize: function(data) {
return JSON.stringify(data);
},

/**
* Formats the template parameters in the string
* @param str The string containing the template
* @return The string with template parameters filled in.
* @return The string with template parameters filled in.
*/
formatString: function(str){
formatString: function(str){
if (!str || arguments.length <=1 ) return str;
var args = arguments;
for (var i = 1; i < arguments.length; i++) {
var reg = new RegExp("\\{" + (i - 1) + "\\}", "gm");
for (var i = 1; i < arguments.length; i++) {
var reg = new RegExp("\\{" + (i - 1) + "\\}", "gm");
str = str.replace(reg, arguments[i]);
}
return str;
},

/**
* Cleans the object by removing null properties.
* @param {object} input Object or dictionary.
* @param {object} input Object or dictionary.
* @return {object} Returns the cleaned version of the object.
*/
cleanObject: function(input){
Expand All @@ -133,21 +136,21 @@ var APIHelper = {
}
return input;
},

/**
* Converts request object property names
* Converts request object property names
* for IP address and user agent to snake case.
* @param {object} input Object.
* @param {object} input Object.
* @return {object} Returns the edited version of the object.
*/
snakeCase: function(input){
function convert(model) {
if (Object.hasOwn(model, "request")) {
if (Object.hasOwn(model.request, "ipAddress")) {
if (hasOwn(model, "request")) {
if (hasOwn(model.request, "ipAddress")) {
model.request["ip_address"] = model.request["ipAddress"]
delete model.request["ipAddress"]
}
if (Object.hasOwn(model.request, "userAgentString")) {
if (hasOwn(model.request, "userAgentString")) {
model.request["user_agent_string"] = model.request["userAgentString"]
delete model.request["userAgentString"]
}
Expand Down Expand Up @@ -182,7 +185,7 @@ var APIHelper = {
}
if(value.length == 0) return true;
return value.every(function (obj) {
return obj == null || obj == undefined || obj.constructor && [String, Number, Boolean].indexOf(obj.constructor) >=0
return obj == null || obj == undefined || obj.constructor && [String, Number, Boolean].indexOf(obj.constructor) >=0
});
},
/**
Expand Down Expand Up @@ -242,4 +245,4 @@ var APIHelper = {
return query.length ? query.substr(0, query.length - 1) : query;
}
}
module.exports = APIHelper;
module.exports = APIHelper;
23 changes: 18 additions & 5 deletions lib/Http/Client/RequestClient.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
var HttpContext = require("./HttpContext");
var HttpResponse = require("../Response/HttpResponse");
var https = require("https");
var axios = require("axios");
var axiosRetry = require("axios-retry");
var zlib = require("zlib");
var APIHelper = require("../../APIHelper");
var _configuration = require("../../configuration");
var axiosImported = require("axios");
// for some bundlers, they need to require axios from .default.
// When using Axios in a Node.js environment, depends on bundlers or setup, may need two ways of require
var axios;
if (axiosImported.default) {
axios = axiosImported.default;
} else {
axios = axiosImported;
}

var keepAliveAgent = new https.Agent({
keepAlive: true,
Expand Down Expand Up @@ -88,10 +96,15 @@ function executeRequest(req, callback) {

function executeRequestCombined(req, callback) {
if (_configuration.retry && !alreadySetReTry) {
axiosRetry(axios, {
retries: _configuration.retry,
retryDelay: axiosRetry.exponentialDelay
});
try {
axiosRetry(axios, {
retries: _configuration.retry,
retryDelay: axiosRetry.exponentialDelay
});
} catch (err) {
// nothing for now.
// just no retry.
}

alreadySetReTry = true;
}
Expand Down
8 changes: 1 addition & 7 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
*
*
*/
var pjson = require('../package.json');

// some bundlers prevents import of json files
var version = pjson ? pjson.version : '2.1.9';

var configuration = {
//The base Uri for API calls
BaseUri : "https://api.moesif.net",

//Your Application Id for authentication/authorization
ApplicationId : "SET_ME",
UserAgent : 'moesifapi-nodejs/' + version
UserAgent : 'moesifapi-nodejs/3.1.2'

};

module.exports = configuration;

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moesifapi",
"version": "3.1.1",
"version": "3.1.2",
"description": "Collection/Data Ingestion API for Moesif",
"main": "./lib/index.js",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions test/ApiControllerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ActionModel = require('../lib/Models/ActionModel.js');
var expect = require('chai').expect;
var config = moesifapi.configuration;

config.ApplicationId = "Your Moesif application Id";
config.ApplicationId = "Your Application Id";

describe('TestAddEvent', function() {
it('createEvent() should return 201 HTTP status', function(done) {
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('TestSendAction', function() {
},
request: req_contextA
};

var actionB = {
transactionId: "a3765024-46ee-45dd-bc83-b136c8d1d250",
actionName: "Viewed pricing",
Expand Down

0 comments on commit 7b94732

Please sign in to comment.