Skip to content

Commit

Permalink
Merge pull request #115 from BedrockStreaming/prettier-refactor
Browse files Browse the repository at this point in the history
chore(linter): refactor of prettier
  • Loading branch information
Tetsu9901 committed Jul 31, 2023
2 parents 1435251 + b116f69 commit b9a1d5e
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 265 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"printWidth": 120,
"trailingComma": "all",
"bracketSpacing": false
}
8 changes: 4 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: '3'
}
]
]
corejs: '3',
},
],
],
};
76 changes: 42 additions & 34 deletions src/superagent-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ module.exports = function (superagent, config, logger) {
/**
* Loop over the patterns and use @callback when the @query matches
*/
const forEachPatternMatch = function () {
const forEachPatternMatch = (function () {
const configLength = config.length;

return function (query, callback){
return function (query, callback) {
for (let i = 0; i < configLength; i++) {
if (new RegExp(config[i].pattern, 'g').test(query)){
if (new RegExp(config[i].pattern, 'g').test(query)) {
callback(config[i]);
}
}
};
}();
})();

/**
* Chains progress events and eventually returns the response
Expand All @@ -56,14 +56,14 @@ module.exports = function (superagent, config, logger) {
var handleProgress = function (ProgressEvent, request, progress, remainingParts, callback) {
setTimeout(function () {
var total = progress.total || 100;
var loaded = total * (progress.parts - remainingParts) / progress.parts;
var loaded = (total * (progress.parts - remainingParts)) / progress.parts;
var lengthComputable = progress.lengthComputable !== false; // default is true
var event = ProgressEvent.initProgressEvent
? ProgressEvent.initProgressEvent('', true, false, lengthComputable, loaded, total) // IE10+
: new ProgressEvent('', {lengthComputable: lengthComputable, loaded: loaded, total: total});

if (event.total > 0) {
event.percent = event.loaded / event.total * 100;
event.percent = (event.loaded / event.total) * 100;
}
event.direction = progress.direction || 'upload';

Expand Down Expand Up @@ -96,13 +96,15 @@ module.exports = function (superagent, config, logger) {
} else {
// superagent < 3.6

if (isNodeServer) { // node server
if (isNodeServer) {
// node server
const originalPath = this.path;
this.path = this.url;
this._appendQueryString(this); // use superagent implementation of adding the query
path = this.path; // save the url together with the query
this.path = originalPath; // reverse the addition of query to path by _appendQueryString
} else { // client
} else {
// client
const originalUrl = this.url;
this._appendQueryString(this); // use superagent implementation of adding the query
path = this.url; // save the url together with the query
Expand All @@ -117,7 +119,7 @@ module.exports = function (superagent, config, logger) {
parser = matched;
} else if (logEnabled) {
currentLog.warnings = (currentLog.warnings || []).concat([
'This other pattern matches the query but was ignored: ' + matched.pattern
'This other pattern matches the query but was ignored: ' + matched.pattern,
]);
}
});
Expand All @@ -126,7 +128,7 @@ module.exports = function (superagent, config, logger) {
if (!parser && logEnabled) {
forEachPatternMatch(this.url, function (matched) {
currentLog.warnings = (currentLog.warnings || []).concat([
'This pattern was ignored because it doesn\'t matches the query params: ' + matched.pattern
'This pattern was ignored because it doesn\'t matches the query params: ' + matched.pattern,
]);
});
}
Expand Down Expand Up @@ -169,26 +171,30 @@ module.exports = function (superagent, config, logger) {
const parserMethod = parser[method] || parser.callback;
response = parserMethod(match, fixtures);
} catch (err) {
error = err;
response = new superagent.Response({
res: {
headers: {},
setEncoding: function (){},
on: function (){},
body: err.responseBody || {}
error = err;
response = new superagent.Response({
res: {
headers: {},
setEncoding: function () {},
on: function () {},
body: err.responseBody || {},
},
req: {
method: function () {},
path: path,
url: path,
},

xhr: {
responseType: '',
responseText: err.responseText || '',
getAllResponseHeaders: function () {
return 'a header';
},
req: {
method: function (){},
path: path,
url: path
getResponseHeader: function () {
return err.responseHeader || 'a header';
},

xhr: {
responseType: '',
responseText: err.responseText || '',
getAllResponseHeaders: function () {return 'a header';},
getResponseHeader: function () {return err.responseHeader || 'a header';}
}
},
});
if (response._setStatusProperties) {
// Error() forces its message to a string when a status must be a number
Expand All @@ -213,15 +219,18 @@ module.exports = function (superagent, config, logger) {
if (logEnabled) {
currentLog.error = error;
}

// Check if a callback for progress events was specified as part of the request
var progressEventsUsed = isNodeServer ? !!this._formData : this.hasListeners && this.hasListeners('progress');

if (context.progress && progressEventsUsed) {
var ProgressEvent = global.ProgressEvent || function (type, rest) { // polyfill
var ProgressEvent =
global.ProgressEvent ||
function (type, rest) {
// polyfill
rest.type = type;
return rest;
};
};
// if no delay for parts was specified but an overall delay was, then divide the delay between the parts.
if (!context.progress.delay && context.delay) {
context.progress.delay = context.delay / context.progress.parts;
Expand All @@ -240,8 +249,7 @@ module.exports = function (superagent, config, logger) {
}
fn(error, response);
}, context.delay);
}
else {
} else {
if (logEnabled) {
flushLog();
}
Expand All @@ -253,6 +261,6 @@ module.exports = function (superagent, config, logger) {
return {
unset: function () {
Request.prototype.end = oldEnd;
}
},
};
};
42 changes: 21 additions & 21 deletions tests/support/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data, status: 201};
}
},
},
{
pattern: 'https://domain.params.example/list(?:[?|&]((?:limit|offset)=[0-9]+))?(?:[?|&]((?:limit|offset)=[0-9]+))?',
Expand All @@ -31,7 +31,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://domain.strict-params.example/search\\?(q=\\w+)&(page=\\d+)',
Expand All @@ -46,7 +46,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://forget.query.params$',
Expand All @@ -61,7 +61,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://match.example/(\\w+)',
Expand All @@ -76,7 +76,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://match.example/foo',
Expand All @@ -88,7 +88,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://error.example/(\\w+)',
Expand All @@ -107,24 +107,24 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://validation.example',
fixtures: function (match, params, headers) {
var error = new Error( 422 );
var error = new Error(422);
var status = (match || [])[1] || 422;
var newErr = new Error(Number(status));
newErr.response = http.STATUS_CODES[status];
newErr.status = status;
newErr.responseHeader = 'application/json';
newErr.responseText = '{"password": "missing"}';
newErr.responseBody = { password: 'missing' };
newErr.responseBody = {password: 'missing'};
throw newErr;
},
post: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://domain.send.example/(\\w+)',
Expand All @@ -139,12 +139,12 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://authorized.example',
fixtures: function (match, params, headers) {
return 'your token: ' + headers['Authorization']
return 'your token: ' + headers['Authorization'];
},
get: function (match, data) {
return {match: match, data: data};
Expand All @@ -154,12 +154,12 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://multiple-headers.example',
fixtures: function (match, params, headers) {
return 'X-API-Key: ' + headers['X-API-Key'] + '; Content-Type: ' + headers['Content-Type']
return 'X-API-Key: ' + headers['X-API-Key'] + '; Content-Type: ' + headers['Content-Type'];
},
get: function (match, data) {
return {match: match, data: data};
Expand All @@ -169,7 +169,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://callback.method.example',
Expand All @@ -178,7 +178,7 @@ module.exports = [
},
callback: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://context.cancel.example/([\\w-]+)',
Expand All @@ -196,7 +196,7 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://context.delay.example/([\\w-]+)',
Expand All @@ -212,12 +212,12 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
},
},
{
pattern: 'https://context.progress.example/([\\w-]+)',
fixtures: function (match, data, headers, context) {
context.progress = { parts: Number(match[1]), delay: 1000 };
context.progress = {parts: Number(match[1]), delay: 1000};
return match && match[1];
},
get: function (match, data) {
Expand All @@ -228,6 +228,6 @@ module.exports = [
},
put: function (match, data) {
return {match: match, data: data};
}
}
},
},
];
Loading

0 comments on commit b9a1d5e

Please sign in to comment.