Skip to content

Commit

Permalink
Remove path-to-regexp and url-parse dependencies (apidoc#1202)
Browse files Browse the repository at this point in the history
and use URL instead
  • Loading branch information
Rezyan authored Apr 8, 2022
1 parent bd1e930 commit c243b08
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 76 deletions.
10 changes: 7 additions & 3 deletions lib/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,13 @@ function _sanityChecks (parsedBlocks, log, filename) {
}
const urlParams = [];
if (block.local.url) {
for (const urlFrag of block.local.url.split('/')) {
if (urlFrag[0] === ':') {
urlParams.push(urlFrag.slice(1));
// The dummy URL base is only used for parses of relative URLs.
const url = new URL(block.local.url, 'https://dummy.base');

// For API parameters in the URL parts delimited by `/` (e.g. `/:foo/:bar`).
for (const pathnamePart of url.pathname.split('/')) {
if (pathnamePart.charAt(0) === ':') {
urlParams.push(pathnamePart.slice(1));
}
}
}
Expand Down
50 changes: 0 additions & 50 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@
"lodash": "^4.17.21",
"markdown-it": "^12.2.0",
"nodemon": "^2.0.15",
"path-to-regexp": "^6.2.0",
"prismjs": "^1.25.0",
"semver": "^7.3.5",
"style-loader": "^3.3.1",
"url-parse": "^1.5.3",
"webpack": "^5.64.2",
"webpack-cli": "^4.9.1",
"winston": "^3.3.3"
Expand Down
44 changes: 23 additions & 21 deletions template/src/sampreq_url_processor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@
* Copyright (c) 2013 inveris OHG
* Licensed under the MIT license.
*/
import URL from 'url-parse';
import { pathToRegexp } from 'path-to-regexp';

export default class UrlProcessor {
// Replace parameters from url (:id) by the parameters from input values
hydrate (url, queryParameters) {
const urlOrig = url;
// path-to-regexp only wants a path not a full url, so we split it
const parsedUrl = new URL(url);
// convert/the:path to regexp
// this array will hold the results
const keys = [];
pathToRegexp(parsedUrl.pathname, keys);
// loop over all the keys and replace them in the url
keys.forEach(key => {
url = url.replace(':' + key.name, encodeURIComponent(queryParameters[key.name]));
});
// The dummy URL base is only used for parses of relative URLs in Node.js.
const parsedUrl = new URL(url, typeof window === 'undefined' ? 'https://dummy.base' : window.location.origin);
const queryParametersChangedInPathname = {};

// For API parameters in the URL parts delimited by `/` (e.g. `/:foo/:bar`).
parsedUrl.pathname.split('/').forEach((pathnamePart, i) => {
if (pathnamePart.charAt(0) === ':') {
const realPathnamePart = pathnamePart.slice(1);

// if some parameters do not have url pattern, add them as standard query
// string parameters (key=value)
url += url.indexOf('?') === -1 ? '?' : '&';
Object.keys(queryParameters).forEach(key => {
if (urlOrig.indexOf(':' + key) === -1) {
url += key + '=' + encodeURIComponent(queryParameters[key]) + '&';
if (typeof queryParameters[realPathnamePart] !== 'undefined') {
parsedUrl.pathname = parsedUrl.pathname.replace(pathnamePart, encodeURIComponent(queryParameters[realPathnamePart]));
queryParametersChangedInPathname[realPathnamePart] = queryParameters[realPathnamePart];
}
}
});

return url.replace(/[?&]$/, '');
// For API parameters in the URL query string (e.g. `?foo=:foo&bar=:bar`).
for (const key in queryParameters) {
if (
typeof queryParametersChangedInPathname[key] === 'undefined' || // Avoid adding query parameter if it has already been changed in pathname.
parsedUrl.searchParams.has(key)
) {
parsedUrl.searchParams.set(key, queryParameters[key]);
}
}

return parsedUrl.toString();
}
}

0 comments on commit c243b08

Please sign in to comment.