mock-service-plugin
Version:
Simulated backend interface service
57 lines (55 loc) • 1.62 kB
JavaScript
import fs from 'fs';
import nodePath from 'path';
import { fileURLToPath } from 'url';
// src/util.ts
function getDirname(importMetaUrl) {
return nodePath.dirname(fileURLToPath(importMetaUrl));
}
function walk(dir) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
file = dir + "/" + file;
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(walk(file));
} else {
results.push(file);
}
});
return results;
}
function isJavacriptFile(file) {
return /\.js$/.test(file);
}
function matchRoute(routes, method, urlpath) {
const pathWithoutQuery = urlpath.split("?")[0];
const exactMatch = routes.find(
(r) => r.method === method && r.restfulTemplateUrl === urlpath
);
if (exactMatch) return exactMatch;
const pathParts = pathWithoutQuery.split("/").filter((part) => part !== "");
for (const route of routes) {
if (route.method !== method) continue;
const routeParts = route.path.split("/").filter((part) => part !== "");
if (routeParts.length !== pathParts.length) continue;
let isMatch = true;
for (let i = 0; i < routeParts.length; i++) {
const routePart = routeParts[i];
const pathPart = pathParts[i];
if (routePart.startsWith(":")) {
routePart.slice(1);
} else if (routePart !== pathPart) {
isMatch = false;
break;
}
}
if (isMatch) {
return route;
}
}
return null;
}
export { getDirname, isJavacriptFile, matchRoute, walk };
//# sourceMappingURL=util.js.map
//# sourceMappingURL=util.js.map