@mokup/shared 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/config-core.cjs +9 -0
  2. package/dist/config-core.d.cts +4 -0
  3. package/dist/config-core.d.mts +4 -0
  4. package/dist/config-core.d.ts +4 -0
  5. package/dist/config-core.mjs +6 -0
  6. package/dist/config-utils.cjs +179 -0
  7. package/dist/config-utils.d.cts +62 -0
  8. package/dist/config-utils.d.mts +62 -0
  9. package/dist/config-utils.d.ts +62 -0
  10. package/dist/config-utils.mjs +171 -0
  11. package/dist/define-config.cjs +168 -0
  12. package/dist/define-config.d.cts +20 -0
  13. package/dist/define-config.d.mts +20 -0
  14. package/dist/define-config.d.ts +20 -0
  15. package/dist/define-config.mjs +166 -0
  16. package/dist/index.cjs +48 -0
  17. package/dist/index.d.cts +10 -98
  18. package/dist/index.d.mts +10 -98
  19. package/dist/index.d.ts +10 -98
  20. package/dist/index.mjs +18 -1
  21. package/dist/jsonc-utils.cjs +25 -0
  22. package/dist/jsonc-utils.d.cts +7 -0
  23. package/dist/jsonc-utils.d.mts +7 -0
  24. package/dist/jsonc-utils.d.ts +7 -0
  25. package/dist/jsonc-utils.mjs +23 -0
  26. package/dist/load-rules.cjs +39 -0
  27. package/dist/load-rules.d.cts +14 -0
  28. package/dist/load-rules.d.mts +14 -0
  29. package/dist/load-rules.d.ts +14 -0
  30. package/dist/load-rules.mjs +37 -0
  31. package/dist/mock-files.cjs +65 -0
  32. package/dist/mock-files.d.cts +10 -0
  33. package/dist/mock-files.d.mts +10 -0
  34. package/dist/mock-files.d.ts +10 -0
  35. package/dist/mock-files.mjs +61 -0
  36. package/dist/module-loader.cjs +93 -0
  37. package/dist/module-loader.d.cts +13 -0
  38. package/dist/module-loader.d.mts +13 -0
  39. package/dist/module-loader.d.ts +13 -0
  40. package/dist/module-loader.mjs +83 -0
  41. package/dist/path-utils.cjs +57 -0
  42. package/dist/path-utils.d.cts +7 -0
  43. package/dist/path-utils.d.mts +7 -0
  44. package/dist/path-utils.d.ts +7 -0
  45. package/dist/path-utils.mjs +51 -0
  46. package/dist/playground-grouping.cjs +95 -0
  47. package/dist/playground-grouping.d.cts +12 -0
  48. package/dist/playground-grouping.d.mts +12 -0
  49. package/dist/playground-grouping.d.ts +12 -0
  50. package/dist/playground-grouping.mjs +90 -0
  51. package/dist/route-constants.cjs +30 -0
  52. package/dist/route-constants.d.cts +7 -0
  53. package/dist/route-constants.d.mts +7 -0
  54. package/dist/route-constants.d.ts +7 -0
  55. package/dist/route-constants.mjs +24 -0
  56. package/dist/route-utils.cjs +126 -0
  57. package/dist/route-utils.d.cts +42 -0
  58. package/dist/route-utils.d.mts +42 -0
  59. package/dist/route-utils.d.ts +42 -0
  60. package/dist/route-utils.mjs +124 -0
  61. package/dist/scan-utils.cjs +39 -0
  62. package/dist/scan-utils.d.cts +114 -0
  63. package/dist/scan-utils.d.mts +114 -0
  64. package/dist/scan-utils.d.ts +114 -0
  65. package/dist/scan-utils.mjs +34 -0
  66. package/dist/timing.cjs +20 -0
  67. package/dist/timing.d.cts +4 -0
  68. package/dist/timing.d.mts +4 -0
  69. package/dist/timing.d.ts +4 -0
  70. package/dist/timing.mjs +17 -0
  71. package/package.json +81 -18
@@ -0,0 +1,83 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { tmpdir } from 'node:os';
4
+ import process from 'node:process';
5
+ import { pathToFileURL } from 'node:url';
6
+ import { resolve, extname } from 'pathe';
7
+
8
+ function createTsxConfigFile(options) {
9
+ const config = {
10
+ compilerOptions: {
11
+ baseUrl: options.baseUrl,
12
+ paths: options.paths
13
+ }
14
+ };
15
+ const configPath = options.fileName ?? resolve(tmpdir(), `mokup-tsx-${process.pid}.json`);
16
+ writeFileSync(configPath, `${JSON.stringify(config, null, 2)}
17
+ `, "utf8");
18
+ return configPath;
19
+ }
20
+ let sourceMapsEnabled = false;
21
+ let tsxRegisterPromise = null;
22
+ let tsxRegisteredConfig = null;
23
+ function resetModuleLoaderForTests() {
24
+ sourceMapsEnabled = false;
25
+ tsxRegisterPromise = null;
26
+ tsxRegisteredConfig = null;
27
+ }
28
+ function ensureSourceMapsEnabled() {
29
+ if (sourceMapsEnabled) {
30
+ return;
31
+ }
32
+ const setSourceMapsEnabled = process.setSourceMapsEnabled;
33
+ if (typeof setSourceMapsEnabled === "function") {
34
+ setSourceMapsEnabled(true);
35
+ }
36
+ sourceMapsEnabled = true;
37
+ }
38
+ async function ensureTsxRegister(tsconfigPath) {
39
+ const desired = tsconfigPath ?? null;
40
+ if (tsxRegisterPromise) {
41
+ await tsxRegisterPromise;
42
+ if (desired && tsxRegisteredConfig !== desired) {
43
+ tsxRegisterPromise = (async () => {
44
+ ensureSourceMapsEnabled();
45
+ const { register } = await import('tsx/esm/api');
46
+ register({ tsconfig: desired });
47
+ tsxRegisteredConfig = desired;
48
+ })();
49
+ await tsxRegisterPromise;
50
+ }
51
+ return tsxRegisterPromise;
52
+ }
53
+ tsxRegisterPromise = (async () => {
54
+ ensureSourceMapsEnabled();
55
+ const { register } = await import('tsx/esm/api');
56
+ if (desired) {
57
+ register({ tsconfig: desired });
58
+ tsxRegisteredConfig = desired;
59
+ } else {
60
+ register();
61
+ tsxRegisteredConfig = null;
62
+ }
63
+ })();
64
+ return tsxRegisterPromise;
65
+ }
66
+ async function loadModule(file, options) {
67
+ const ext = extname(file).toLowerCase();
68
+ if (ext === ".cjs") {
69
+ const require = createRequire(import.meta.url);
70
+ delete require.cache[file];
71
+ return require(file);
72
+ }
73
+ if (ext === ".js" || ext === ".mjs") {
74
+ return import(`${pathToFileURL(file).href}?t=${Date.now()}`);
75
+ }
76
+ if (ext === ".ts") {
77
+ await ensureTsxRegister(options?.tsconfigPath ?? null);
78
+ return import(`${pathToFileURL(file).href}?t=${Date.now()}`);
79
+ }
80
+ return null;
81
+ }
82
+
83
+ export { createTsxConfigFile, ensureTsxRegister, loadModule, resetModuleLoaderForTests };
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ const process = require('node:process');
4
+ const pathe = require('pathe');
5
+
6
+ function isWindowsLikePath(normalized) {
7
+ return process.platform === "win32" || /^[a-z]:\//i.test(normalized) || normalized.startsWith("//");
8
+ }
9
+ function toPosix(value) {
10
+ return value.replace(/\\/g, "/");
11
+ }
12
+ function normalizePath(value) {
13
+ return pathe.normalize(toPosix(value));
14
+ }
15
+ function normalizePathForComparison(value) {
16
+ const normalized = normalizePath(value);
17
+ return isWindowsLikePath(normalized) ? normalized.toLowerCase() : normalized;
18
+ }
19
+ function isInDirs(file, dirs) {
20
+ const normalized = normalizePathForComparison(file);
21
+ return dirs.some((dir) => {
22
+ const normalizedDir = normalizePathForComparison(dir).replace(/\/$/, "");
23
+ return normalized === normalizedDir || normalized.startsWith(`${normalizedDir}/`);
24
+ });
25
+ }
26
+ function testPatterns(patterns, value) {
27
+ const list = Array.isArray(patterns) ? patterns : [patterns];
28
+ return list.some((pattern) => pattern.test(value));
29
+ }
30
+ function matchesFilter(file, include, exclude) {
31
+ const normalized = normalizePathForComparison(file);
32
+ if (exclude && testPatterns(exclude, normalized)) {
33
+ return false;
34
+ }
35
+ if (include) {
36
+ return testPatterns(include, normalized);
37
+ }
38
+ return true;
39
+ }
40
+ function hasIgnoredPrefix(file, rootDir, prefixes) {
41
+ if (prefixes.length === 0) {
42
+ return false;
43
+ }
44
+ const normalizedRoot = normalizePathForComparison(rootDir);
45
+ const normalizedFile = normalizePathForComparison(file);
46
+ const relativePath = toPosix(pathe.relative(normalizedRoot, normalizedFile));
47
+ const segments = relativePath.split("/");
48
+ return segments.some(
49
+ (segment) => prefixes.some((prefix) => segment.startsWith(prefix))
50
+ );
51
+ }
52
+
53
+ exports.hasIgnoredPrefix = hasIgnoredPrefix;
54
+ exports.isInDirs = isInDirs;
55
+ exports.matchesFilter = matchesFilter;
56
+ exports.normalizePathForComparison = normalizePathForComparison;
57
+ exports.toPosix = toPosix;
@@ -0,0 +1,7 @@
1
+ declare function toPosix(value: string): string;
2
+ declare function normalizePathForComparison(value: string): string;
3
+ declare function isInDirs(file: string, dirs: string[]): boolean;
4
+ declare function matchesFilter(file: string, include?: RegExp | RegExp[], exclude?: RegExp | RegExp[]): boolean;
5
+ declare function hasIgnoredPrefix(file: string, rootDir: string, prefixes: string[]): boolean;
6
+
7
+ export { hasIgnoredPrefix, isInDirs, matchesFilter, normalizePathForComparison, toPosix };
@@ -0,0 +1,7 @@
1
+ declare function toPosix(value: string): string;
2
+ declare function normalizePathForComparison(value: string): string;
3
+ declare function isInDirs(file: string, dirs: string[]): boolean;
4
+ declare function matchesFilter(file: string, include?: RegExp | RegExp[], exclude?: RegExp | RegExp[]): boolean;
5
+ declare function hasIgnoredPrefix(file: string, rootDir: string, prefixes: string[]): boolean;
6
+
7
+ export { hasIgnoredPrefix, isInDirs, matchesFilter, normalizePathForComparison, toPosix };
@@ -0,0 +1,7 @@
1
+ declare function toPosix(value: string): string;
2
+ declare function normalizePathForComparison(value: string): string;
3
+ declare function isInDirs(file: string, dirs: string[]): boolean;
4
+ declare function matchesFilter(file: string, include?: RegExp | RegExp[], exclude?: RegExp | RegExp[]): boolean;
5
+ declare function hasIgnoredPrefix(file: string, rootDir: string, prefixes: string[]): boolean;
6
+
7
+ export { hasIgnoredPrefix, isInDirs, matchesFilter, normalizePathForComparison, toPosix };
@@ -0,0 +1,51 @@
1
+ import { platform } from 'node:process';
2
+ import { relative, normalize } from 'pathe';
3
+
4
+ function isWindowsLikePath(normalized) {
5
+ return platform === "win32" || /^[a-z]:\//i.test(normalized) || normalized.startsWith("//");
6
+ }
7
+ function toPosix(value) {
8
+ return value.replace(/\\/g, "/");
9
+ }
10
+ function normalizePath(value) {
11
+ return normalize(toPosix(value));
12
+ }
13
+ function normalizePathForComparison(value) {
14
+ const normalized = normalizePath(value);
15
+ return isWindowsLikePath(normalized) ? normalized.toLowerCase() : normalized;
16
+ }
17
+ function isInDirs(file, dirs) {
18
+ const normalized = normalizePathForComparison(file);
19
+ return dirs.some((dir) => {
20
+ const normalizedDir = normalizePathForComparison(dir).replace(/\/$/, "");
21
+ return normalized === normalizedDir || normalized.startsWith(`${normalizedDir}/`);
22
+ });
23
+ }
24
+ function testPatterns(patterns, value) {
25
+ const list = Array.isArray(patterns) ? patterns : [patterns];
26
+ return list.some((pattern) => pattern.test(value));
27
+ }
28
+ function matchesFilter(file, include, exclude) {
29
+ const normalized = normalizePathForComparison(file);
30
+ if (exclude && testPatterns(exclude, normalized)) {
31
+ return false;
32
+ }
33
+ if (include) {
34
+ return testPatterns(include, normalized);
35
+ }
36
+ return true;
37
+ }
38
+ function hasIgnoredPrefix(file, rootDir, prefixes) {
39
+ if (prefixes.length === 0) {
40
+ return false;
41
+ }
42
+ const normalizedRoot = normalizePathForComparison(rootDir);
43
+ const normalizedFile = normalizePathForComparison(file);
44
+ const relativePath = toPosix(relative(normalizedRoot, normalizedFile));
45
+ const segments = relativePath.split("/");
46
+ return segments.some(
47
+ (segment) => prefixes.some((prefix) => segment.startsWith(prefix))
48
+ );
49
+ }
50
+
51
+ export { hasIgnoredPrefix, isInDirs, matchesFilter, normalizePathForComparison, toPosix };
@@ -0,0 +1,95 @@
1
+ 'use strict';
2
+
3
+ const process = require('node:process');
4
+ const pathe = require('pathe');
5
+ const pathUtils = require('./path-utils.cjs');
6
+
7
+ function normalizePath(value) {
8
+ return pathe.normalize(pathUtils.toPosix(value));
9
+ }
10
+ function isAncestor(parent, child) {
11
+ const normalizedParent = pathUtils.normalizePathForComparison(parent).replace(/\/$/, "");
12
+ const normalizedChild = pathUtils.normalizePathForComparison(child);
13
+ return normalizedChild === normalizedParent || normalizedChild.startsWith(`${normalizedParent}/`);
14
+ }
15
+ function resolveGroupRoot(dirs, serverRoot) {
16
+ if (!dirs || dirs.length === 0) {
17
+ return serverRoot ?? process.cwd();
18
+ }
19
+ if (serverRoot) {
20
+ const normalizedRoot = normalizePath(serverRoot);
21
+ const canUseRoot = dirs.every((dir) => isAncestor(normalizedRoot, dir));
22
+ if (canUseRoot) {
23
+ return normalizedRoot;
24
+ }
25
+ }
26
+ if (dirs.length === 1) {
27
+ return normalizePath(pathe.dirname(dirs[0]));
28
+ }
29
+ let common = normalizePath(dirs[0]);
30
+ for (const dir of dirs.slice(1)) {
31
+ const normalizedDir = normalizePath(dir);
32
+ while (common && !isAncestor(common, normalizedDir)) {
33
+ const parent = normalizePath(pathe.dirname(common));
34
+ if (parent === common) {
35
+ break;
36
+ }
37
+ common = parent;
38
+ }
39
+ }
40
+ if (!common || common === "/") {
41
+ return serverRoot ?? process.cwd();
42
+ }
43
+ return common;
44
+ }
45
+ function resolveGroups(dirs, root) {
46
+ const groups = [];
47
+ const seen = /* @__PURE__ */ new Set();
48
+ for (const dir of dirs) {
49
+ const normalized = normalizePath(dir);
50
+ const compareKey = pathUtils.normalizePathForComparison(dir);
51
+ if (seen.has(compareKey)) {
52
+ continue;
53
+ }
54
+ seen.add(compareKey);
55
+ const rel = pathUtils.toPosix(pathe.relative(root, normalized));
56
+ const label = rel && !rel.startsWith("..") ? rel : normalized;
57
+ groups.push({
58
+ key: normalized,
59
+ label,
60
+ path: normalized
61
+ });
62
+ }
63
+ return groups;
64
+ }
65
+ function resolveRouteGroup(routeFile, groups) {
66
+ if (groups.length === 0) {
67
+ return void 0;
68
+ }
69
+ const normalizedFile = pathUtils.normalizePathForComparison(routeFile);
70
+ let matched;
71
+ for (const group of groups) {
72
+ const normalizedGroup = pathUtils.normalizePathForComparison(group.path);
73
+ if (normalizedFile === normalizedGroup || normalizedFile.startsWith(`${normalizedGroup}/`)) {
74
+ if (!matched || group.path.length > matched.path.length) {
75
+ matched = group;
76
+ }
77
+ }
78
+ }
79
+ return matched;
80
+ }
81
+ function formatRouteFile(file, root) {
82
+ if (!root) {
83
+ return pathUtils.toPosix(file);
84
+ }
85
+ const rel = pathUtils.toPosix(pathe.relative(root, file));
86
+ if (!rel || rel.startsWith("..")) {
87
+ return pathUtils.toPosix(file);
88
+ }
89
+ return rel;
90
+ }
91
+
92
+ exports.formatRouteFile = formatRouteFile;
93
+ exports.resolveGroupRoot = resolveGroupRoot;
94
+ exports.resolveGroups = resolveGroups;
95
+ exports.resolveRouteGroup = resolveRouteGroup;
@@ -0,0 +1,12 @@
1
+ interface PlaygroundGroup {
2
+ key: string;
3
+ label: string;
4
+ path: string;
5
+ }
6
+ declare function resolveGroupRoot(dirs: string[], serverRoot?: string): string;
7
+ declare function resolveGroups(dirs: string[], root: string): PlaygroundGroup[];
8
+ declare function resolveRouteGroup(routeFile: string, groups: PlaygroundGroup[]): PlaygroundGroup | undefined;
9
+ declare function formatRouteFile(file: string, root?: string): string;
10
+
11
+ export { formatRouteFile, resolveGroupRoot, resolveGroups, resolveRouteGroup };
12
+ export type { PlaygroundGroup };
@@ -0,0 +1,12 @@
1
+ interface PlaygroundGroup {
2
+ key: string;
3
+ label: string;
4
+ path: string;
5
+ }
6
+ declare function resolveGroupRoot(dirs: string[], serverRoot?: string): string;
7
+ declare function resolveGroups(dirs: string[], root: string): PlaygroundGroup[];
8
+ declare function resolveRouteGroup(routeFile: string, groups: PlaygroundGroup[]): PlaygroundGroup | undefined;
9
+ declare function formatRouteFile(file: string, root?: string): string;
10
+
11
+ export { formatRouteFile, resolveGroupRoot, resolveGroups, resolveRouteGroup };
12
+ export type { PlaygroundGroup };
@@ -0,0 +1,12 @@
1
+ interface PlaygroundGroup {
2
+ key: string;
3
+ label: string;
4
+ path: string;
5
+ }
6
+ declare function resolveGroupRoot(dirs: string[], serverRoot?: string): string;
7
+ declare function resolveGroups(dirs: string[], root: string): PlaygroundGroup[];
8
+ declare function resolveRouteGroup(routeFile: string, groups: PlaygroundGroup[]): PlaygroundGroup | undefined;
9
+ declare function formatRouteFile(file: string, root?: string): string;
10
+
11
+ export { formatRouteFile, resolveGroupRoot, resolveGroups, resolveRouteGroup };
12
+ export type { PlaygroundGroup };
@@ -0,0 +1,90 @@
1
+ import { cwd } from 'node:process';
2
+ import { relative, dirname, normalize } from 'pathe';
3
+ import { toPosix, normalizePathForComparison } from './path-utils.mjs';
4
+
5
+ function normalizePath(value) {
6
+ return normalize(toPosix(value));
7
+ }
8
+ function isAncestor(parent, child) {
9
+ const normalizedParent = normalizePathForComparison(parent).replace(/\/$/, "");
10
+ const normalizedChild = normalizePathForComparison(child);
11
+ return normalizedChild === normalizedParent || normalizedChild.startsWith(`${normalizedParent}/`);
12
+ }
13
+ function resolveGroupRoot(dirs, serverRoot) {
14
+ if (!dirs || dirs.length === 0) {
15
+ return serverRoot ?? cwd();
16
+ }
17
+ if (serverRoot) {
18
+ const normalizedRoot = normalizePath(serverRoot);
19
+ const canUseRoot = dirs.every((dir) => isAncestor(normalizedRoot, dir));
20
+ if (canUseRoot) {
21
+ return normalizedRoot;
22
+ }
23
+ }
24
+ if (dirs.length === 1) {
25
+ return normalizePath(dirname(dirs[0]));
26
+ }
27
+ let common = normalizePath(dirs[0]);
28
+ for (const dir of dirs.slice(1)) {
29
+ const normalizedDir = normalizePath(dir);
30
+ while (common && !isAncestor(common, normalizedDir)) {
31
+ const parent = normalizePath(dirname(common));
32
+ if (parent === common) {
33
+ break;
34
+ }
35
+ common = parent;
36
+ }
37
+ }
38
+ if (!common || common === "/") {
39
+ return serverRoot ?? cwd();
40
+ }
41
+ return common;
42
+ }
43
+ function resolveGroups(dirs, root) {
44
+ const groups = [];
45
+ const seen = /* @__PURE__ */ new Set();
46
+ for (const dir of dirs) {
47
+ const normalized = normalizePath(dir);
48
+ const compareKey = normalizePathForComparison(dir);
49
+ if (seen.has(compareKey)) {
50
+ continue;
51
+ }
52
+ seen.add(compareKey);
53
+ const rel = toPosix(relative(root, normalized));
54
+ const label = rel && !rel.startsWith("..") ? rel : normalized;
55
+ groups.push({
56
+ key: normalized,
57
+ label,
58
+ path: normalized
59
+ });
60
+ }
61
+ return groups;
62
+ }
63
+ function resolveRouteGroup(routeFile, groups) {
64
+ if (groups.length === 0) {
65
+ return void 0;
66
+ }
67
+ const normalizedFile = normalizePathForComparison(routeFile);
68
+ let matched;
69
+ for (const group of groups) {
70
+ const normalizedGroup = normalizePathForComparison(group.path);
71
+ if (normalizedFile === normalizedGroup || normalizedFile.startsWith(`${normalizedGroup}/`)) {
72
+ if (!matched || group.path.length > matched.path.length) {
73
+ matched = group;
74
+ }
75
+ }
76
+ }
77
+ return matched;
78
+ }
79
+ function formatRouteFile(file, root) {
80
+ if (!root) {
81
+ return toPosix(file);
82
+ }
83
+ const rel = toPosix(relative(root, file));
84
+ if (!rel || rel.startsWith("..")) {
85
+ return toPosix(file);
86
+ }
87
+ return rel;
88
+ }
89
+
90
+ export { formatRouteFile, resolveGroupRoot, resolveGroups, resolveRouteGroup };
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ const methodSet = /* @__PURE__ */ new Set([
4
+ "GET",
5
+ "POST",
6
+ "PUT",
7
+ "PATCH",
8
+ "DELETE",
9
+ "OPTIONS",
10
+ "HEAD"
11
+ ]);
12
+ const methodSuffixSet = new Set(
13
+ Array.from(methodSet, (method) => method.toLowerCase())
14
+ );
15
+ const supportedExtensions = /* @__PURE__ */ new Set([
16
+ ".json",
17
+ ".jsonc",
18
+ ".ts",
19
+ ".js",
20
+ ".mjs",
21
+ ".cjs"
22
+ ]);
23
+ const configExtensions = [".ts", ".js", ".mjs", ".cjs"];
24
+ const jsonExtensions = /* @__PURE__ */ new Set([".json", ".jsonc"]);
25
+
26
+ exports.configExtensions = configExtensions;
27
+ exports.jsonExtensions = jsonExtensions;
28
+ exports.methodSet = methodSet;
29
+ exports.methodSuffixSet = methodSuffixSet;
30
+ exports.supportedExtensions = supportedExtensions;
@@ -0,0 +1,7 @@
1
+ declare const methodSet: Set<string>;
2
+ declare const methodSuffixSet: Set<string>;
3
+ declare const supportedExtensions: Set<string>;
4
+ declare const configExtensions: readonly [".ts", ".js", ".mjs", ".cjs"];
5
+ declare const jsonExtensions: Set<string>;
6
+
7
+ export { configExtensions, jsonExtensions, methodSet, methodSuffixSet, supportedExtensions };
@@ -0,0 +1,7 @@
1
+ declare const methodSet: Set<string>;
2
+ declare const methodSuffixSet: Set<string>;
3
+ declare const supportedExtensions: Set<string>;
4
+ declare const configExtensions: readonly [".ts", ".js", ".mjs", ".cjs"];
5
+ declare const jsonExtensions: Set<string>;
6
+
7
+ export { configExtensions, jsonExtensions, methodSet, methodSuffixSet, supportedExtensions };
@@ -0,0 +1,7 @@
1
+ declare const methodSet: Set<string>;
2
+ declare const methodSuffixSet: Set<string>;
3
+ declare const supportedExtensions: Set<string>;
4
+ declare const configExtensions: readonly [".ts", ".js", ".mjs", ".cjs"];
5
+ declare const jsonExtensions: Set<string>;
6
+
7
+ export { configExtensions, jsonExtensions, methodSet, methodSuffixSet, supportedExtensions };
@@ -0,0 +1,24 @@
1
+ const methodSet = /* @__PURE__ */ new Set([
2
+ "GET",
3
+ "POST",
4
+ "PUT",
5
+ "PATCH",
6
+ "DELETE",
7
+ "OPTIONS",
8
+ "HEAD"
9
+ ]);
10
+ const methodSuffixSet = new Set(
11
+ Array.from(methodSet, (method) => method.toLowerCase())
12
+ );
13
+ const supportedExtensions = /* @__PURE__ */ new Set([
14
+ ".json",
15
+ ".jsonc",
16
+ ".ts",
17
+ ".js",
18
+ ".mjs",
19
+ ".cjs"
20
+ ]);
21
+ const configExtensions = [".ts", ".js", ".mjs", ".cjs"];
22
+ const jsonExtensions = /* @__PURE__ */ new Set([".json", ".jsonc"]);
23
+
24
+ export { configExtensions, jsonExtensions, methodSet, methodSuffixSet, supportedExtensions };
@@ -0,0 +1,126 @@
1
+ 'use strict';
2
+
3
+ const pathUtils = require('./path-utils.cjs');
4
+ const pathe = require('pathe');
5
+ const routeConstants = require('./route-constants.cjs');
6
+ const scanUtils = require('./scan-utils.cjs');
7
+ require('node:process');
8
+
9
+ function createRouteUtils(params) {
10
+ const { parseRouteTemplate, compareRouteScore } = params;
11
+ function resolveTemplate(template, prefix) {
12
+ const normalized = template.startsWith("/") ? template : `/${template}`;
13
+ if (!prefix) {
14
+ return normalized;
15
+ }
16
+ const normalizedPrefix = scanUtils.normalizePrefix(prefix);
17
+ if (!normalizedPrefix) {
18
+ return normalized;
19
+ }
20
+ if (normalized === normalizedPrefix || normalized.startsWith(`${normalizedPrefix}/`)) {
21
+ return normalized;
22
+ }
23
+ if (normalized === "/") {
24
+ return `${normalizedPrefix}/`;
25
+ }
26
+ return `${normalizedPrefix}${normalized}`;
27
+ }
28
+ function stripMethodSuffix(base) {
29
+ const segments = base.split(".");
30
+ const last = segments.at(-1);
31
+ if (last && routeConstants.methodSuffixSet.has(last.toLowerCase())) {
32
+ segments.pop();
33
+ return {
34
+ name: segments.join("."),
35
+ method: last.toUpperCase()
36
+ };
37
+ }
38
+ return {
39
+ name: base,
40
+ method: void 0
41
+ };
42
+ }
43
+ function deriveRouteFromFile(file, rootDir, warn) {
44
+ const rel = pathUtils.toPosix(pathe.relative(rootDir, file));
45
+ const ext = pathe.extname(rel);
46
+ const withoutExt = rel.slice(0, rel.length - ext.length);
47
+ const dir = pathe.dirname(withoutExt);
48
+ const base = pathe.basename(withoutExt);
49
+ const { name, method } = stripMethodSuffix(base);
50
+ const resolvedMethod = method ?? (routeConstants.jsonExtensions.has(ext) ? "GET" : void 0);
51
+ if (!resolvedMethod) {
52
+ warn?.(`Skip mock without method suffix: ${file}`);
53
+ return null;
54
+ }
55
+ if (!name) {
56
+ warn?.(`Skip mock with empty route name: ${file}`);
57
+ return null;
58
+ }
59
+ const joined = dir === "." ? name : pathe.join(dir, name);
60
+ const segments = pathUtils.toPosix(joined).split("/");
61
+ if (segments.at(-1) === "index") {
62
+ segments.pop();
63
+ }
64
+ const template = segments.length === 0 ? "/" : `/${segments.join("/")}`;
65
+ const parsed = parseRouteTemplate(template);
66
+ if (parsed.errors.length > 0) {
67
+ for (const error of parsed.errors) {
68
+ warn?.(`${error} in ${file}`);
69
+ }
70
+ return null;
71
+ }
72
+ for (const warning of parsed.warnings) {
73
+ warn?.(`${warning} in ${file}`);
74
+ }
75
+ return {
76
+ template: parsed.template,
77
+ method: resolvedMethod,
78
+ tokens: parsed.tokens,
79
+ score: parsed.score
80
+ };
81
+ }
82
+ function resolveRule(input) {
83
+ const method = input.derivedMethod;
84
+ if (!method) {
85
+ input.warn?.(`Skip mock without method suffix: ${input.file}`);
86
+ return null;
87
+ }
88
+ const template = resolveTemplate(input.derivedTemplate, input.prefix);
89
+ const parsed = parseRouteTemplate(template);
90
+ if (parsed.errors.length > 0) {
91
+ for (const error of parsed.errors) {
92
+ input.warn?.(`${error} in ${input.file}`);
93
+ }
94
+ return null;
95
+ }
96
+ for (const warning of parsed.warnings) {
97
+ input.warn?.(`${warning} in ${input.file}`);
98
+ }
99
+ const base = {
100
+ template: parsed.template,
101
+ method,
102
+ tokens: parsed.tokens,
103
+ score: parsed.score
104
+ };
105
+ return input.build ? input.build(base, input.rule) : base;
106
+ }
107
+ function sortRoutes(routes) {
108
+ return routes.sort((a, b) => {
109
+ if (a.method !== b.method) {
110
+ return a.method.localeCompare(b.method);
111
+ }
112
+ const scoreCompare = compareRouteScore(a.score, b.score);
113
+ if (scoreCompare !== 0) {
114
+ return scoreCompare;
115
+ }
116
+ return a.template.localeCompare(b.template);
117
+ });
118
+ }
119
+ return {
120
+ deriveRouteFromFile,
121
+ resolveRule,
122
+ sortRoutes
123
+ };
124
+ }
125
+
126
+ exports.createRouteUtils = createRouteUtils;