@topogram/cli 0.3.34
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.
- package/ARCHITECTURE.md +67 -0
- package/CHANGELOG.md +240 -0
- package/README.md +223 -0
- package/package.json +51 -0
- package/src/adoption/index.js +3 -0
- package/src/adoption/plan.js +702 -0
- package/src/adoption/reporting.js +464 -0
- package/src/adoption/review-groups.js +313 -0
- package/src/agent-ops/query-builders.js +5012 -0
- package/src/archive/archive.js +141 -0
- package/src/archive/compact.js +26 -0
- package/src/archive/jsonl.js +70 -0
- package/src/archive/resolver-bridge.js +82 -0
- package/src/archive/schema.js +87 -0
- package/src/archive/unarchive.js +108 -0
- package/src/catalog.js +752 -0
- package/src/cli/catalog-alias.js +166 -0
- package/src/cli.js +9738 -0
- package/src/component-behavior.js +173 -0
- package/src/example-implementation.js +91 -0
- package/src/format.js +19 -0
- package/src/generator/adapters.d.ts +4 -0
- package/src/generator/adapters.js +325 -0
- package/src/generator/api.d.ts +1 -0
- package/src/generator/api.js +1196 -0
- package/src/generator/check.js +355 -0
- package/src/generator/component-conformance.js +767 -0
- package/src/generator/components.js +39 -0
- package/src/generator/context/bundle.js +291 -0
- package/src/generator/context/diff.js +256 -0
- package/src/generator/context/digest.js +182 -0
- package/src/generator/context/domain-coverage.js +94 -0
- package/src/generator/context/domain-page.js +137 -0
- package/src/generator/context/index.js +42 -0
- package/src/generator/context/report.js +121 -0
- package/src/generator/context/shared.js +1397 -0
- package/src/generator/context/slice.js +703 -0
- package/src/generator/context/task-mode.js +466 -0
- package/src/generator/docs.js +327 -0
- package/src/generator/index.js +161 -0
- package/src/generator/native/parity-bundle.js +311 -0
- package/src/generator/output.js +300 -0
- package/src/generator/registry.js +482 -0
- package/src/generator/runtime/app-bundle.js +456 -0
- package/src/generator/runtime/bundle-shared.js +166 -0
- package/src/generator/runtime/compile-check.js +163 -0
- package/src/generator/runtime/deployment.js +287 -0
- package/src/generator/runtime/environment.js +635 -0
- package/src/generator/runtime/index.js +32 -0
- package/src/generator/runtime/runtime-check.js +554 -0
- package/src/generator/runtime/shared.js +515 -0
- package/src/generator/runtime/smoke.js +219 -0
- package/src/generator/schema.js +204 -0
- package/src/generator/sdlc/board.js +66 -0
- package/src/generator/sdlc/doc-page.js +53 -0
- package/src/generator/sdlc/index.js +23 -0
- package/src/generator/sdlc/release-notes.js +62 -0
- package/src/generator/sdlc/traceability-matrix.js +65 -0
- package/src/generator/shared.js +29 -0
- package/src/generator/surfaces/contracts.js +146 -0
- package/src/generator/surfaces/databases/contract.js +40 -0
- package/src/generator/surfaces/databases/index.js +84 -0
- package/src/generator/surfaces/databases/lifecycle-shared.d.ts +1 -0
- package/src/generator/surfaces/databases/lifecycle-shared.js +612 -0
- package/src/generator/surfaces/databases/migration-plan.js +281 -0
- package/src/generator/surfaces/databases/postgres/capabilities.js +14 -0
- package/src/generator/surfaces/databases/postgres/drizzle.js +99 -0
- package/src/generator/surfaces/databases/postgres/index.js +9 -0
- package/src/generator/surfaces/databases/postgres/lifecycle.js +16 -0
- package/src/generator/surfaces/databases/postgres/prisma.js +159 -0
- package/src/generator/surfaces/databases/postgres/sql-migration.js +102 -0
- package/src/generator/surfaces/databases/postgres/sql-schema.js +34 -0
- package/src/generator/surfaces/databases/shared.d.ts +1 -0
- package/src/generator/surfaces/databases/shared.js +350 -0
- package/src/generator/surfaces/databases/snapshot.js +96 -0
- package/src/generator/surfaces/databases/sqlite/capabilities.js +14 -0
- package/src/generator/surfaces/databases/sqlite/index.js +8 -0
- package/src/generator/surfaces/databases/sqlite/lifecycle.js +16 -0
- package/src/generator/surfaces/databases/sqlite/prisma.js +143 -0
- package/src/generator/surfaces/databases/sqlite/sql-migration.js +65 -0
- package/src/generator/surfaces/databases/sqlite/sql-schema.js +27 -0
- package/src/generator/surfaces/index.js +25 -0
- package/src/generator/surfaces/native/swiftui-app.js +38 -0
- package/src/generator/surfaces/native/swiftui-templates/Package.swift.txt +20 -0
- package/src/generator/surfaces/native/swiftui-templates/README.generated.md +26 -0
- package/src/generator/surfaces/native/swiftui-templates/runtime/DynamicScreens.swift +682 -0
- package/src/generator/surfaces/native/swiftui-templates/runtime/TodoAPIClient.swift +156 -0
- package/src/generator/surfaces/native/swiftui-templates/runtime/TodoSwiftUIApp.swift +44 -0
- package/src/generator/surfaces/native/swiftui-templates/runtime/Visibility.swift +183 -0
- package/src/generator/surfaces/services/express.d.ts +1 -0
- package/src/generator/surfaces/services/express.js +766 -0
- package/src/generator/surfaces/services/hono.d.ts +1 -0
- package/src/generator/surfaces/services/hono.js +204 -0
- package/src/generator/surfaces/services/index.js +42 -0
- package/src/generator/surfaces/services/persistence-wiring.js +240 -0
- package/src/generator/surfaces/services/runtime-helpers.js +631 -0
- package/src/generator/surfaces/services/server-contract.js +80 -0
- package/src/generator/surfaces/services/stateless.d.ts +1 -0
- package/src/generator/surfaces/services/stateless.js +97 -0
- package/src/generator/surfaces/shared.js +64 -0
- package/src/generator/surfaces/web/api-client.js +1 -0
- package/src/generator/surfaces/web/forms.js +1 -0
- package/src/generator/surfaces/web/index.d.ts +2 -0
- package/src/generator/surfaces/web/index.js +53 -0
- package/src/generator/surfaces/web/react-components.js +248 -0
- package/src/generator/surfaces/web/react.js +538 -0
- package/src/generator/surfaces/web/routes.js +1 -0
- package/src/generator/surfaces/web/screens.js +1 -0
- package/src/generator/surfaces/web/shared.js +369 -0
- package/src/generator/surfaces/web/sveltekit-actions.js +28 -0
- package/src/generator/surfaces/web/sveltekit-components.js +234 -0
- package/src/generator/surfaces/web/sveltekit.js +426 -0
- package/src/generator/surfaces/web/ui-web-contract.js +65 -0
- package/src/generator/surfaces/web/vanilla.js +239 -0
- package/src/generator/verification.js +84 -0
- package/src/generator.js +1 -0
- package/src/import/core/context.js +52 -0
- package/src/import/core/contracts.js +23 -0
- package/src/import/core/registry.js +81 -0
- package/src/import/core/runner.js +646 -0
- package/src/import/core/shared.js +910 -0
- package/src/import/enrichers/auth-session.js +18 -0
- package/src/import/enrichers/django-rest.js +226 -0
- package/src/import/enrichers/doc-linking.js +20 -0
- package/src/import/enrichers/rails-controllers.js +246 -0
- package/src/import/enrichers/rails-models.js +130 -0
- package/src/import/enrichers/workflow-target-state.js +10 -0
- package/src/import/extractors/api/aspnet-core.js +304 -0
- package/src/import/extractors/api/django-routes.js +318 -0
- package/src/import/extractors/api/express.js +154 -0
- package/src/import/extractors/api/fastify.js +371 -0
- package/src/import/extractors/api/flutter-dio.js +135 -0
- package/src/import/extractors/api/generic-route-fallback.js +90 -0
- package/src/import/extractors/api/graphql-code-first.js +565 -0
- package/src/import/extractors/api/graphql-sdl.js +309 -0
- package/src/import/extractors/api/jaxrs.js +303 -0
- package/src/import/extractors/api/micronaut.js +213 -0
- package/src/import/extractors/api/next-route.js +50 -0
- package/src/import/extractors/api/next-server-action.js +51 -0
- package/src/import/extractors/api/nextauth.js +52 -0
- package/src/import/extractors/api/openapi-code.js +242 -0
- package/src/import/extractors/api/openapi.js +232 -0
- package/src/import/extractors/api/rails-routes.js +230 -0
- package/src/import/extractors/api/react-native-repository.js +128 -0
- package/src/import/extractors/api/retrofit.js +103 -0
- package/src/import/extractors/api/spring-web.js +372 -0
- package/src/import/extractors/api/swift-webapi.js +116 -0
- package/src/import/extractors/api/trpc.js +212 -0
- package/src/import/extractors/db/django-models.js +232 -0
- package/src/import/extractors/db/dotnet-models.js +93 -0
- package/src/import/extractors/db/drizzle.js +242 -0
- package/src/import/extractors/db/ef-core.js +221 -0
- package/src/import/extractors/db/flutter-entities.js +120 -0
- package/src/import/extractors/db/jpa.js +120 -0
- package/src/import/extractors/db/liquibase.js +180 -0
- package/src/import/extractors/db/mybatis-xml.js +145 -0
- package/src/import/extractors/db/prisma.js +185 -0
- package/src/import/extractors/db/rails-schema.js +175 -0
- package/src/import/extractors/db/react-native-entities.js +95 -0
- package/src/import/extractors/db/room.js +193 -0
- package/src/import/extractors/db/snapshot.js +112 -0
- package/src/import/extractors/db/sql.js +180 -0
- package/src/import/extractors/db/swiftdata.js +137 -0
- package/src/import/extractors/ui/android-compose.js +230 -0
- package/src/import/extractors/ui/backend-only.js +70 -0
- package/src/import/extractors/ui/blazor.js +227 -0
- package/src/import/extractors/ui/flutter-screens.js +152 -0
- package/src/import/extractors/ui/maui-xaml.js +135 -0
- package/src/import/extractors/ui/next-app-router.js +83 -0
- package/src/import/extractors/ui/next-pages-router.js +141 -0
- package/src/import/extractors/ui/razor-pages.js +181 -0
- package/src/import/extractors/ui/react-native-screens.js +166 -0
- package/src/import/extractors/ui/react-router.js +139 -0
- package/src/import/extractors/ui/sveltekit.js +123 -0
- package/src/import/extractors/ui/swiftui.js +193 -0
- package/src/import/extractors/ui/uikit.js +175 -0
- package/src/import/extractors/verification/generic.js +290 -0
- package/src/import/extractors/workflows/generic.js +137 -0
- package/src/import/index.js +7 -0
- package/src/import/provenance.js +158 -0
- package/src/new-project.js +2107 -0
- package/src/parser.js +439 -0
- package/src/policy/review-boundaries.js +165 -0
- package/src/project-config.js +535 -0
- package/src/proofs/backend-parity.js +19 -0
- package/src/proofs/contract-audit.js +220 -0
- package/src/proofs/ios-parity.js +7 -0
- package/src/proofs/issues-parity.js +10 -0
- package/src/proofs/web-parity.js +50 -0
- package/src/realization/api/build-api-realization.js +5 -0
- package/src/realization/api/index.js +1 -0
- package/src/realization/backend/build-backend-runtime-realization.js +82 -0
- package/src/realization/backend/index.d.ts +1 -0
- package/src/realization/backend/index.js +4 -0
- package/src/realization/db/build-db-realization.js +17 -0
- package/src/realization/db/index.js +3 -0
- package/src/realization/db/migration-plan.js +5 -0
- package/src/realization/db/snapshot.js +5 -0
- package/src/realization/ui/build-ui-shared-realization.js +305 -0
- package/src/realization/ui/build-web-realization.js +189 -0
- package/src/realization/ui/index.js +2 -0
- package/src/reconcile/docs.js +280 -0
- package/src/reconcile/index.js +3 -0
- package/src/reconcile/journeys.js +441 -0
- package/src/resolver/docs.js +1 -0
- package/src/resolver/enrich/acceptance-criterion.js +14 -0
- package/src/resolver/enrich/bug.js +12 -0
- package/src/resolver/enrich/component.js +2 -0
- package/src/resolver/enrich/index.js +1 -0
- package/src/resolver/enrich/pitch.js +18 -0
- package/src/resolver/enrich/requirement.js +20 -0
- package/src/resolver/enrich/task.js +16 -0
- package/src/resolver/expressions.js +1 -0
- package/src/resolver/index.js +2422 -0
- package/src/resolver/normalize.js +1 -0
- package/src/resolver.js +1 -0
- package/src/sdlc/adopt.js +65 -0
- package/src/sdlc/check.js +86 -0
- package/src/sdlc/dod/acceptance-criterion.js +22 -0
- package/src/sdlc/dod/bug.js +26 -0
- package/src/sdlc/dod/document.js +23 -0
- package/src/sdlc/dod/index.js +25 -0
- package/src/sdlc/dod/pitch.js +23 -0
- package/src/sdlc/dod/requirement.js +34 -0
- package/src/sdlc/dod/task.js +39 -0
- package/src/sdlc/explain.js +116 -0
- package/src/sdlc/history.js +80 -0
- package/src/sdlc/paths.js +11 -0
- package/src/sdlc/release.js +106 -0
- package/src/sdlc/scaffold.js +89 -0
- package/src/sdlc/status-filter.js +54 -0
- package/src/sdlc/transition.js +112 -0
- package/src/sdlc/transitions/acceptance-criterion.js +28 -0
- package/src/sdlc/transitions/bug.js +31 -0
- package/src/sdlc/transitions/document.js +29 -0
- package/src/sdlc/transitions/index.js +56 -0
- package/src/sdlc/transitions/pitch.js +34 -0
- package/src/sdlc/transitions/requirement.js +31 -0
- package/src/sdlc/transitions/task.js +34 -0
- package/src/template-trust.js +597 -0
- package/src/validator/expressions.js +1 -0
- package/src/validator/index.js +3424 -0
- package/src/validator/kinds.js +346 -0
- package/src/validator/per-kind/acceptance-criterion.js +91 -0
- package/src/validator/per-kind/bug.js +77 -0
- package/src/validator/per-kind/component.js +274 -0
- package/src/validator/per-kind/domain.js +205 -0
- package/src/validator/per-kind/pitch.js +101 -0
- package/src/validator/per-kind/requirement.js +75 -0
- package/src/validator/per-kind/task.js +96 -0
- package/src/validator/registry.js +1 -0
- package/src/validator/utils.js +12 -0
- package/src/validator.js +1 -0
- package/src/workflows.js +7597 -0
- package/src/workspace-docs.js +265 -0
- package/template-helpers/react.js +5 -0
- package/template-helpers/sveltekit.js +5 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canonicalCandidateTerm,
|
|
3
|
+
dedupeCandidateRecords,
|
|
4
|
+
findImportFiles,
|
|
5
|
+
idHintify,
|
|
6
|
+
makeCandidateRecord,
|
|
7
|
+
pluralizeCandidateTerm,
|
|
8
|
+
readTextIfExists,
|
|
9
|
+
relativeTo,
|
|
10
|
+
titleCase
|
|
11
|
+
} from "../../core/shared.js";
|
|
12
|
+
|
|
13
|
+
function extractTemplateTypeDefs(text) {
|
|
14
|
+
const blocks = [];
|
|
15
|
+
for (const match of text.matchAll(/(?:typeDefs|schema)\s*=\s*(?:\/\*[\s\S]*?\*\/\s*)?`([\s\S]*?)`/g)) {
|
|
16
|
+
blocks.push(match[1]);
|
|
17
|
+
}
|
|
18
|
+
for (const match of text.matchAll(/\bgql\s*`([\s\S]*?)`/g)) {
|
|
19
|
+
blocks.push(match[1]);
|
|
20
|
+
}
|
|
21
|
+
return blocks;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function splitTopLevelComma(value) {
|
|
25
|
+
const items = [];
|
|
26
|
+
let current = "";
|
|
27
|
+
let depth = 0;
|
|
28
|
+
for (const char of String(value || "")) {
|
|
29
|
+
if (char === "(" || char === "[" || char === "{") {
|
|
30
|
+
depth += 1;
|
|
31
|
+
} else if (char === ")" || char === "]" || char === "}") {
|
|
32
|
+
depth -= 1;
|
|
33
|
+
} else if (char === "," && depth === 0) {
|
|
34
|
+
if (current.trim()) items.push(current.trim());
|
|
35
|
+
current = "";
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
current += char;
|
|
39
|
+
}
|
|
40
|
+
if (current.trim()) items.push(current.trim());
|
|
41
|
+
return items;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function splitGraphqlFieldEntries(body) {
|
|
45
|
+
const entries = [];
|
|
46
|
+
let current = "";
|
|
47
|
+
let parenDepth = 0;
|
|
48
|
+
for (const rawLine of String(body || "").split("\n")) {
|
|
49
|
+
const line = rawLine.replace(/#.*/, "").trim();
|
|
50
|
+
if (!line) continue;
|
|
51
|
+
current = current ? `${current} ${line}` : line;
|
|
52
|
+
parenDepth += (line.match(/\(/g) || []).length;
|
|
53
|
+
parenDepth -= (line.match(/\)/g) || []).length;
|
|
54
|
+
if (parenDepth <= 0) {
|
|
55
|
+
entries.push(current.trim());
|
|
56
|
+
current = "";
|
|
57
|
+
parenDepth = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (current.trim()) {
|
|
61
|
+
entries.push(current.trim());
|
|
62
|
+
}
|
|
63
|
+
return entries;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function parseGraphqlSchemaBlocks(schemaText) {
|
|
67
|
+
const blocks = new Map();
|
|
68
|
+
for (const match of String(schemaText || "").matchAll(/\b(type|input|enum)\s+([A-Za-z_][A-Za-z0-9_]*)[^{]*\{([\s\S]*?)\}/g)) {
|
|
69
|
+
const kind = match[1];
|
|
70
|
+
const name = match[2];
|
|
71
|
+
const body = match[3];
|
|
72
|
+
blocks.set(name, {
|
|
73
|
+
kind,
|
|
74
|
+
name,
|
|
75
|
+
entries: splitGraphqlFieldEntries(body)
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return blocks;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function parseGraphqlField(entry) {
|
|
82
|
+
const compact = String(entry || "").replace(/\s+/g, " ").trim();
|
|
83
|
+
const match = compact.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*(?:\((.*)\))?\s*:\s*(.+)$/);
|
|
84
|
+
if (!match) return null;
|
|
85
|
+
return {
|
|
86
|
+
name: match[1],
|
|
87
|
+
args: match[2] || "",
|
|
88
|
+
type: match[3].trim()
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function unwrapGraphqlType(typeName) {
|
|
93
|
+
return String(typeName || "").replace(/[!\[\]\s]/g, "");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isGraphqlListType(typeName) {
|
|
97
|
+
return /\[/.test(String(typeName || ""));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function flattenInputFields(typeName, inputTypes, seen = new Set()) {
|
|
101
|
+
const baseType = unwrapGraphqlType(typeName);
|
|
102
|
+
if (!baseType) return [];
|
|
103
|
+
if (seen.has(baseType)) return [];
|
|
104
|
+
const block = inputTypes.get(baseType);
|
|
105
|
+
if (!block) return [];
|
|
106
|
+
seen.add(baseType);
|
|
107
|
+
const fields = [];
|
|
108
|
+
for (const entry of block.entries) {
|
|
109
|
+
const field = parseGraphqlField(entry);
|
|
110
|
+
if (!field) continue;
|
|
111
|
+
const nested = flattenInputFields(field.type, inputTypes, seen);
|
|
112
|
+
if (nested.length > 0) {
|
|
113
|
+
fields.push(...nested);
|
|
114
|
+
} else {
|
|
115
|
+
fields.push(field.name);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
seen.delete(baseType);
|
|
119
|
+
return [...new Set(fields)].sort();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function fieldsForOutputType(typeName, outputTypes) {
|
|
123
|
+
const baseType = unwrapGraphqlType(typeName);
|
|
124
|
+
const block = outputTypes.get(baseType);
|
|
125
|
+
if (!block) return [];
|
|
126
|
+
return [...new Set(block.entries.map((entry) => parseGraphqlField(entry)?.name).filter(Boolean))].sort();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function inferEntityId(operationName, returnType) {
|
|
130
|
+
const baseType = unwrapGraphqlType(returnType);
|
|
131
|
+
if (baseType && !["String", "Int", "Float", "Boolean", "ID", "DateTime"].includes(baseType)) {
|
|
132
|
+
return `entity_${idHintify(canonicalCandidateTerm(baseType))}`;
|
|
133
|
+
}
|
|
134
|
+
const name = String(operationName || "");
|
|
135
|
+
const suffixMatch = name.match(/([A-Z][A-Za-z0-9_]*)$/);
|
|
136
|
+
return `entity_${idHintify(canonicalCandidateTerm(suffixMatch ? suffixMatch[1] : name || "item"))}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function inferCapabilityId(operationName, returnType, rootType) {
|
|
140
|
+
const entityStem = inferEntityId(operationName, returnType).replace(/^entity_/, "");
|
|
141
|
+
const normalizedName = idHintify(operationName);
|
|
142
|
+
if (/(signup|register)/i.test(operationName)) return `cap_register_${entityStem}`;
|
|
143
|
+
if (/(signin|sign_in|login|authenticate)/i.test(normalizedName)) return `cap_sign_in_${entityStem}`;
|
|
144
|
+
if (/(delete|remove)/i.test(operationName)) return `cap_delete_${entityStem}`;
|
|
145
|
+
if (/(togglepublish|publish)/i.test(normalizedName)) return `cap_publish_${entityStem}`;
|
|
146
|
+
if (/increment.*view.*count/i.test(normalizedName)) return `cap_update_${entityStem}_view_count`;
|
|
147
|
+
if (/(create|add|new)/i.test(operationName)) return `cap_create_${entityStem}`;
|
|
148
|
+
if (rootType === "Query" && (isGraphqlListType(returnType) || /^(all|list|feed|drafts)/i.test(operationName))) {
|
|
149
|
+
return `cap_list_${pluralizeCandidateTerm(entityStem)}`;
|
|
150
|
+
}
|
|
151
|
+
if (rootType === "Query" && /(byid|get|find|detail)/i.test(operationName)) {
|
|
152
|
+
return `cap_get_${entityStem}`;
|
|
153
|
+
}
|
|
154
|
+
if (rootType === "Mutation") {
|
|
155
|
+
return `cap_update_${entityStem}`;
|
|
156
|
+
}
|
|
157
|
+
return `${rootType === "Mutation" ? "cap_update_" : "cap_get_"}${entityStem}_${normalizedName}`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function inferTargetState(operationName, capabilityId) {
|
|
161
|
+
if (/(signup|register)/i.test(operationName)) return "registered";
|
|
162
|
+
if (/(signin|login|authenticate)/i.test(operationName)) return "authenticated";
|
|
163
|
+
if (/(togglepublish|publish)/i.test(operationName)) return "published";
|
|
164
|
+
if (/delete/i.test(operationName) || capabilityId.startsWith("cap_delete_")) return "deleted";
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function inferEndpointPath(context, graphqlFiles) {
|
|
169
|
+
for (const filePath of graphqlFiles) {
|
|
170
|
+
const text = readTextIfExists(filePath);
|
|
171
|
+
if (!text) continue;
|
|
172
|
+
const endpointMatch = text.match(/graphqlEndpoint\s*:\s*["'`]([^"'`]+)["'`]/);
|
|
173
|
+
if (endpointMatch) {
|
|
174
|
+
return endpointMatch[1];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const packageJsonPath = findImportFiles(context.paths, (filePath) => /package\.json$/i.test(filePath))[0];
|
|
178
|
+
const packageText = packageJsonPath ? readTextIfExists(packageJsonPath) : null;
|
|
179
|
+
if (packageText && /graphql-yoga|apollo-server|@apollo\/server/.test(packageText)) {
|
|
180
|
+
return "/graphql";
|
|
181
|
+
}
|
|
182
|
+
return "/graphql";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function extractGraphqlSchemaSources(context) {
|
|
186
|
+
const files = findImportFiles(
|
|
187
|
+
context.paths,
|
|
188
|
+
(filePath) =>
|
|
189
|
+
/\/src\/.+\.(ts|tsx|js|jsx)$/i.test(filePath) ||
|
|
190
|
+
/\/schema\.(graphql|gql)$/i.test(filePath) ||
|
|
191
|
+
/\.(graphql|gql)$/i.test(filePath)
|
|
192
|
+
).filter((filePath) => !/\.test\./i.test(filePath));
|
|
193
|
+
const schemas = [];
|
|
194
|
+
for (const filePath of files) {
|
|
195
|
+
const text = readTextIfExists(filePath) || "";
|
|
196
|
+
if (/\.(graphql|gql)$/i.test(filePath)) {
|
|
197
|
+
schemas.push({ file: filePath, schema: text });
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
for (const schema of extractTemplateTypeDefs(text)) {
|
|
201
|
+
schemas.push({ file: filePath, schema });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return schemas;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const graphQlSdlExtractor = {
|
|
208
|
+
id: "api.graphql-sdl",
|
|
209
|
+
track: "api",
|
|
210
|
+
detect(context) {
|
|
211
|
+
const schemaSources = extractGraphqlSchemaSources(context);
|
|
212
|
+
const hasOperations = schemaSources.some(({ schema }) => /\btype\s+Query\b|\btype\s+Mutation\b/.test(schema));
|
|
213
|
+
const packageJsonPath = findImportFiles(context.paths, (filePath) => /package\.json$/i.test(filePath))[0];
|
|
214
|
+
const packageText = packageJsonPath ? readTextIfExists(packageJsonPath) : "";
|
|
215
|
+
const hasGraphqlRuntime = /graphql-yoga|graphql|apollo-server|@apollo\/server/.test(packageText || "");
|
|
216
|
+
return {
|
|
217
|
+
score: hasOperations ? (hasGraphqlRuntime ? 90 : 75) : 0,
|
|
218
|
+
reasons: hasOperations ? ["Found GraphQL SDL with Query/Mutation operations"] : []
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
extract(context) {
|
|
222
|
+
const schemaSources = extractGraphqlSchemaSources(context);
|
|
223
|
+
const endpointPath = inferEndpointPath(context, findImportFiles(context.paths, (filePath) => /\/src\/.+\.(ts|tsx|js|jsx)$/i.test(filePath)));
|
|
224
|
+
const mergedSchema = schemaSources.map(({ schema }) => schema).join("\n\n");
|
|
225
|
+
const blocks = parseGraphqlSchemaBlocks(mergedSchema);
|
|
226
|
+
const inputTypes = new Map([...blocks.entries()].filter(([, block]) => block.kind === "input"));
|
|
227
|
+
const outputTypes = new Map([...blocks.entries()].filter(([, block]) => block.kind === "type"));
|
|
228
|
+
const findings = [];
|
|
229
|
+
const candidates = { capabilities: [], routes: [], stacks: [] };
|
|
230
|
+
const queryBlock = blocks.get("Query");
|
|
231
|
+
const mutationBlock = blocks.get("Mutation");
|
|
232
|
+
|
|
233
|
+
const operations = [
|
|
234
|
+
...(queryBlock?.entries || []).map((entry) => ({ rootType: "Query", field: parseGraphqlField(entry), sourceFile: schemaSources[0]?.file || null })),
|
|
235
|
+
...(mutationBlock?.entries || []).map((entry) => ({ rootType: "Mutation", field: parseGraphqlField(entry), sourceFile: schemaSources[0]?.file || null }))
|
|
236
|
+
].filter((entry) => entry.field);
|
|
237
|
+
|
|
238
|
+
for (const operation of operations) {
|
|
239
|
+
const field = operation.field;
|
|
240
|
+
const entityId = inferEntityId(field.name, field.type);
|
|
241
|
+
const capabilityId = inferCapabilityId(field.name, field.type, operation.rootType);
|
|
242
|
+
const inputFields = [];
|
|
243
|
+
for (const arg of splitTopLevelComma(field.args)) {
|
|
244
|
+
const argMatch = arg.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.+)$/);
|
|
245
|
+
if (!argMatch) continue;
|
|
246
|
+
const nestedFields = flattenInputFields(argMatch[2], inputTypes);
|
|
247
|
+
if (nestedFields.length > 0) {
|
|
248
|
+
inputFields.push(...nestedFields);
|
|
249
|
+
} else {
|
|
250
|
+
inputFields.push(argMatch[1]);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const outputFields = fieldsForOutputType(field.type, outputTypes);
|
|
254
|
+
const method = operation.rootType === "Mutation" ? "POST" : "GET";
|
|
255
|
+
const provenance = `${relativeTo(context.paths.repoRoot, operation.sourceFile || schemaSources[0]?.file || "")}#${operation.rootType}.${field.name}`;
|
|
256
|
+
candidates.capabilities.push(
|
|
257
|
+
makeCandidateRecord({
|
|
258
|
+
kind: "capability",
|
|
259
|
+
idHint: capabilityId,
|
|
260
|
+
label: titleCase(capabilityId.replace(/^cap_/, "")),
|
|
261
|
+
confidence: "high",
|
|
262
|
+
sourceKind: "schema",
|
|
263
|
+
provenance,
|
|
264
|
+
endpoint: {
|
|
265
|
+
method,
|
|
266
|
+
path: endpointPath
|
|
267
|
+
},
|
|
268
|
+
path_params: [],
|
|
269
|
+
query_params: [],
|
|
270
|
+
header_params: [],
|
|
271
|
+
input_fields: [...new Set(inputFields)].sort(),
|
|
272
|
+
output_fields: outputFields,
|
|
273
|
+
auth_hint: "public",
|
|
274
|
+
entity_id: entityId,
|
|
275
|
+
graphql_operation: {
|
|
276
|
+
root_type: operation.rootType,
|
|
277
|
+
field: field.name
|
|
278
|
+
},
|
|
279
|
+
target_state: inferTargetState(field.name, capabilityId),
|
|
280
|
+
track: "api"
|
|
281
|
+
})
|
|
282
|
+
);
|
|
283
|
+
candidates.routes.push({
|
|
284
|
+
path: endpointPath,
|
|
285
|
+
method,
|
|
286
|
+
confidence: "high",
|
|
287
|
+
source_kind: "schema",
|
|
288
|
+
provenance
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (operations.length > 0) {
|
|
293
|
+
findings.push({
|
|
294
|
+
kind: "graphql_operations",
|
|
295
|
+
files: [...new Set(schemaSources.map(({ file }) => relativeTo(context.paths.repoRoot, file)))],
|
|
296
|
+
operation_count: operations.length
|
|
297
|
+
});
|
|
298
|
+
candidates.stacks.push("graphql_sdl");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
candidates.capabilities = dedupeCandidateRecords(candidates.capabilities, (record) => record.id_hint);
|
|
302
|
+
candidates.routes = dedupeCandidateRecords(
|
|
303
|
+
candidates.routes.map((route) => ({ ...route, id_hint: `${route.method}_${route.path}` })),
|
|
304
|
+
(record) => `${record.method}:${record.path}:${record.source_kind}`
|
|
305
|
+
).map(({ id_hint, ...route }) => route);
|
|
306
|
+
candidates.stacks = [...new Set(candidates.stacks)].sort();
|
|
307
|
+
return { findings, candidates };
|
|
308
|
+
}
|
|
309
|
+
};
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import {
|
|
2
|
+
dedupeCandidateRecords,
|
|
3
|
+
findImportFiles,
|
|
4
|
+
inferApiEntityIdFromPath,
|
|
5
|
+
inferRouteCapabilityId,
|
|
6
|
+
makeCandidateRecord,
|
|
7
|
+
normalizeOpenApiPath,
|
|
8
|
+
readTextIfExists,
|
|
9
|
+
relativeTo,
|
|
10
|
+
titleCase
|
|
11
|
+
} from "../../core/shared.js";
|
|
12
|
+
|
|
13
|
+
function buildJavaFileIndex(paths) {
|
|
14
|
+
const files = findImportFiles(paths, (filePath) => /\.java$/i.test(filePath));
|
|
15
|
+
return files.map((filePath) => ({
|
|
16
|
+
filePath,
|
|
17
|
+
relativePath: relativeTo(paths.repoRoot, filePath),
|
|
18
|
+
text: readTextIfExists(filePath) || ""
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function escapeRegExp(value) {
|
|
23
|
+
return String(value || "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function explicitHandlerHint(methodName, routePath, httpMethod) {
|
|
27
|
+
const low = String(methodName || "").toLowerCase();
|
|
28
|
+
if (httpMethod === "GET" && /\/articles\/feed$/.test(routePath)) return "feed_article";
|
|
29
|
+
if (httpMethod === "POST" && /\/articles\/\{slug\}\/favorite$/.test(routePath)) return "favorite_article";
|
|
30
|
+
if (httpMethod === "DELETE" && /\/articles\/\{slug\}\/favorite$/.test(routePath)) return "unfavorite_article";
|
|
31
|
+
if (httpMethod === "GET" && /\/articles\/\{slug\}\/comments$/.test(routePath)) return "list_comments";
|
|
32
|
+
if (httpMethod === "POST" && /\/articles\/\{slug\}\/comments$/.test(routePath)) return "create_comment";
|
|
33
|
+
if (httpMethod === "DELETE" && /\/articles\/\{slug\}\/comments\/\{id\}$/.test(routePath)) return "delete_comment";
|
|
34
|
+
if (httpMethod === "GET" && /\/profiles\/\{username\}$/.test(routePath)) return "get_profile";
|
|
35
|
+
if (httpMethod === "POST" && /\/profiles\/\{username\}\/follow$/.test(routePath)) return "follow_profile";
|
|
36
|
+
if (httpMethod === "DELETE" && /\/profiles\/\{username\}\/follow$/.test(routePath)) return "unfollow_profile";
|
|
37
|
+
if (httpMethod === "POST" && routePath === "/users") return "create_user";
|
|
38
|
+
if (httpMethod === "POST" && /\/users\/login$/.test(routePath)) return "sign_in_account";
|
|
39
|
+
if (httpMethod === "GET" && routePath === "/user") return "get_user";
|
|
40
|
+
if ((httpMethod === "PUT" || httpMethod === "PATCH") && routePath === "/user") return "update_user";
|
|
41
|
+
if (httpMethod === "GET" && routePath === "/tags") return "list_tags";
|
|
42
|
+
if (httpMethod === "GET" && routePath === "/tasks") return "list_tasks";
|
|
43
|
+
if (httpMethod === "POST" && routePath === "/tasks") return "create_task";
|
|
44
|
+
if (httpMethod === "GET" && /\/tasks\/\{id\}$/.test(routePath)) return "get_task";
|
|
45
|
+
if (httpMethod === "PUT" && /\/tasks\/\{id\}$/.test(routePath)) return "update_task";
|
|
46
|
+
if (httpMethod === "DELETE" && /\/tasks\/\{id\}$/.test(routePath)) return "delete_task";
|
|
47
|
+
if ((httpMethod === "PUT" || httpMethod === "PATCH") && /\/tasks\/\{id\}\/status$/.test(routePath)) return "update_task_status";
|
|
48
|
+
if (low.includes("favorite")) return low.startsWith("un") ? "unfavorite_article" : "favorite_article";
|
|
49
|
+
if (low.includes("comment")) {
|
|
50
|
+
if (low.startsWith("delete")) return "delete_comment";
|
|
51
|
+
if (low.startsWith("create") || low.startsWith("add")) return "create_comment";
|
|
52
|
+
return "list_comments";
|
|
53
|
+
}
|
|
54
|
+
if (low.includes("follow")) return low.startsWith("un") ? "unfollow_profile" : "follow_profile";
|
|
55
|
+
if (low === "login" || /\/users\/login$/.test(routePath)) return "sign_in_account";
|
|
56
|
+
if ((low === "create" || low === "register" || low === "save") && routePath === "/users") return "create_user";
|
|
57
|
+
if (low === "current" || low === "getuser") return "get_user";
|
|
58
|
+
if (low === "update" && routePath === "/user") return "update_user";
|
|
59
|
+
if (low === "alltasks") return "list_tasks";
|
|
60
|
+
if (low === "taskdetails") return "get_task";
|
|
61
|
+
if (low === "updatestatus") return "update_task_status";
|
|
62
|
+
if (low === "delete") {
|
|
63
|
+
if (/\/tasks\//.test(routePath)) return "delete_task";
|
|
64
|
+
if (/\/articles\/\{slug\}$/.test(routePath)) return "delete_article";
|
|
65
|
+
}
|
|
66
|
+
if (low === "create" && routePath === "/articles") return "create_article";
|
|
67
|
+
if (low === "update" && /\/articles\/\{slug\}$/.test(routePath)) return "update_article";
|
|
68
|
+
if (low === "findbyslug" && httpMethod === "GET") return "get_article";
|
|
69
|
+
if (low === "getarticles" || low === "findbyfilters") return "list_articles";
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseRecordFields(recordText) {
|
|
74
|
+
const ctorMatch = String(recordText || "").match(/record\s+[A-Za-z_][A-Za-z0-9_]*\s*\(([\s\S]*?)\)\s*(?:implements[^{]+)?\{/);
|
|
75
|
+
if (!ctorMatch) return [];
|
|
76
|
+
return ctorMatch[1]
|
|
77
|
+
.split(",")
|
|
78
|
+
.map((part) => part.trim())
|
|
79
|
+
.filter(Boolean)
|
|
80
|
+
.map((part) => {
|
|
81
|
+
const cleaned = part.replace(/@[A-Za-z_][A-Za-z0-9_.]*(?:\((?:[^()]|\([^)]*\))*\))?\s*/g, "").trim();
|
|
82
|
+
const match = cleaned.match(/([A-Za-z0-9_<>\[\]\?\.]+)\s+([A-Za-z_][A-Za-z0-9_]*)$/);
|
|
83
|
+
return match ? { type: match[1], name: match[2] } : null;
|
|
84
|
+
})
|
|
85
|
+
.filter(Boolean);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseClassFields(classText) {
|
|
89
|
+
return [...String(classText || "").matchAll(/((?:\s*@[\w.]+(?:\((?:[^()]|\([^)]*\))*\))?\s*)*)\s*private\s+([A-Za-z0-9_<>\[\]\?\.]+)\s+([A-Za-z_][A-Za-z0-9_]*)\s*;/g)]
|
|
90
|
+
.map((entry) => ({
|
|
91
|
+
annotations: entry[1] || "",
|
|
92
|
+
type: entry[2],
|
|
93
|
+
name: entry[3]
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function findJavaTypeBlock(typeName, files, preferredFile = null) {
|
|
98
|
+
const bare = String(typeName || "").split(".").pop();
|
|
99
|
+
if (!bare) return null;
|
|
100
|
+
const patterns = [
|
|
101
|
+
new RegExp(`(?:public\\s+)?record\\s+${escapeRegExp(bare)}\\b[\\s\\S]{0,2400}?\\n\\}`, "m"),
|
|
102
|
+
new RegExp(`(?:public\\s+)?class\\s+${escapeRegExp(bare)}\\b[\\s\\S]{0,3600}?\\n\\}`, "m"),
|
|
103
|
+
new RegExp(`(?:public\\s+)?interface\\s+${escapeRegExp(bare)}\\b[\\s\\S]{0,2400}?\\n\\}`, "m")
|
|
104
|
+
];
|
|
105
|
+
const orderedFiles = preferredFile
|
|
106
|
+
? [...files.filter((file) => file.filePath === preferredFile), ...files.filter((file) => file.filePath !== preferredFile)]
|
|
107
|
+
: files;
|
|
108
|
+
for (const file of orderedFiles) {
|
|
109
|
+
for (const pattern of patterns) {
|
|
110
|
+
const match = file.text.match(pattern);
|
|
111
|
+
if (match) {
|
|
112
|
+
return { filePath: file.filePath, text: match[0] };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function flattenFieldsFromType(typeName, files, seen = new Set(), preferredFile = null) {
|
|
120
|
+
const bare = String(typeName || "").split(".").pop();
|
|
121
|
+
const key = `${preferredFile || ""}:${bare}`;
|
|
122
|
+
if (!bare || seen.has(key)) return [];
|
|
123
|
+
seen.add(key);
|
|
124
|
+
const block = findJavaTypeBlock(typeName, files, preferredFile);
|
|
125
|
+
if (!block) return [];
|
|
126
|
+
const fields = [];
|
|
127
|
+
const recordFields = parseRecordFields(block.text);
|
|
128
|
+
if (recordFields.length > 0) {
|
|
129
|
+
for (const field of recordFields) {
|
|
130
|
+
const nested = flattenFieldsFromType(field.type, files, seen, block.filePath);
|
|
131
|
+
if (nested.length > 0 && ["article", "user", "data", "comment", "profile", "request"].includes(field.name.toLowerCase())) {
|
|
132
|
+
fields.push(...nested);
|
|
133
|
+
} else {
|
|
134
|
+
fields.push(field.name);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return [...new Set(fields)];
|
|
138
|
+
}
|
|
139
|
+
for (const field of parseClassFields(block.text)) {
|
|
140
|
+
fields.push(field.name);
|
|
141
|
+
}
|
|
142
|
+
return [...new Set(fields)];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function flattenOutputFieldsFromType(typeName, files, preferredFile = null) {
|
|
146
|
+
const block = findJavaTypeBlock(typeName, files, preferredFile);
|
|
147
|
+
if (!block) return [];
|
|
148
|
+
const recordFields = parseRecordFields(block.text);
|
|
149
|
+
if (recordFields.length > 0) return recordFields.map((field) => field.name);
|
|
150
|
+
return parseClassFields(block.text).map((field) => field.name);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function parsePathParams(parameters) {
|
|
154
|
+
return [...String(parameters || "").matchAll(/@PathParam\("([^"]+)"\)\s+([A-Za-z0-9_<>\[\]\?\.]+)\s+([A-Za-z_][A-Za-z0-9_]*)/g)]
|
|
155
|
+
.map((entry) => ({ name: entry[1] || entry[3], required: true, type: entry[2] || null }));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function parseQueryParams(parameters) {
|
|
159
|
+
return [...String(parameters || "").matchAll(/@QueryParam\("([^"]+)"\)\s+([A-Za-z0-9_<>\[\]\?\.]+)\s+([A-Za-z_][A-Za-z0-9_]*)/g)]
|
|
160
|
+
.map((entry) => ({ name: entry[1] || entry[3], required: false, type: entry[2] || null }));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function parseContextHeaders(parameters) {
|
|
164
|
+
if (/SecurityContext/.test(parameters) || /HttpHeaders/.test(parameters)) {
|
|
165
|
+
return [{ name: "authorization", required: false, type: "string" }];
|
|
166
|
+
}
|
|
167
|
+
return [];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function parseBodyType(parameters) {
|
|
171
|
+
const parts = String(parameters || "")
|
|
172
|
+
.split(",")
|
|
173
|
+
.map((part) => part.trim())
|
|
174
|
+
.filter(Boolean);
|
|
175
|
+
for (const part of parts) {
|
|
176
|
+
if (/@(PathParam|QueryParam|HeaderParam|BeanParam|Context)\b/.test(part)) continue;
|
|
177
|
+
if (/\b(SecurityContext|UriInfo|ResourceContext|HttpServletRequest|HttpHeaders)\b/.test(part)) continue;
|
|
178
|
+
const cleaned = part.replace(/@[A-Za-z_][A-Za-z0-9_.]*(?:\((?:[^()]|\([^)]*\))*\))?\s*/g, "").trim();
|
|
179
|
+
const match = cleaned.match(/([A-Za-z0-9_<>\[\]\?\.]+)\s+([A-Za-z_][A-Za-z0-9_]*)$/);
|
|
180
|
+
if (match) return match[1];
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function normalizeJoinedPath(basePath, childPath = "") {
|
|
186
|
+
return normalizeOpenApiPath(`/${[String(basePath || "").replace(/^\/+/, ""), String(childPath || "").replace(/^\/+/, "")].filter(Boolean).join("/")}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function inferAuthHint(pathValue, methodName, parameters = "", annotations = "") {
|
|
190
|
+
if (/@Secured/.test(annotations) || /SecurityContext/.test(parameters)) return "secured";
|
|
191
|
+
if (pathValue === "/users/login" || (pathValue === "/users" && /create|register|save/i.test(methodName)) || pathValue === "/tasks") {
|
|
192
|
+
return "public";
|
|
193
|
+
}
|
|
194
|
+
return "unknown";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function buildSubresourceBaseMap(files) {
|
|
198
|
+
const map = new Map();
|
|
199
|
+
for (const file of files) {
|
|
200
|
+
const classPath = file.text.match(/@Path\("([^"]+)"\)/)?.[1] || "";
|
|
201
|
+
for (const match of file.text.matchAll(/@Path\("([^"]+)"\)\s+public\s+([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z_][A-Za-z0-9_]*)\s*\([^)]*\)\s*\{/g)) {
|
|
202
|
+
const [, childPath, returnType] = match;
|
|
203
|
+
map.set(returnType, normalizeJoinedPath(classPath, childPath));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return map;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function classLevelPath(text) {
|
|
210
|
+
const classIndex = String(text || "").search(/\b(class|interface)\s+[A-Za-z_][A-Za-z0-9_]*/);
|
|
211
|
+
if (classIndex < 0) return "";
|
|
212
|
+
const head = String(text || "").slice(0, classIndex);
|
|
213
|
+
const matches = [...head.matchAll(/@Path\("([^"]+)"\)/g)];
|
|
214
|
+
return matches[matches.length - 1]?.[1] || "";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function parseJaxrsRoutes(files, repoRoot) {
|
|
218
|
+
const resourceFiles = files.filter((file) => /@Path\(|@(GET|POST|PUT|PATCH|DELETE)\b/.test(file.text));
|
|
219
|
+
const subresourceBaseMap = buildSubresourceBaseMap(files);
|
|
220
|
+
const routes = [];
|
|
221
|
+
const methodPattern = /((?:\s*@[\w.]+(?:\((?:[^()]|\([^)]*\))*\))?\s*)+)\s*(?:public\s+)?([A-Za-z0-9_<>\[\]\?., ]+)\s+([A-Za-z_][A-Za-z0-9_]*)\(([\s\S]*?)\)\s*(?:throws\s+[^{;]+)?\s*\{/g;
|
|
222
|
+
for (const file of resourceFiles) {
|
|
223
|
+
const className = file.text.match(/(?:class|interface)\s+([A-Za-z_][A-Za-z0-9_]*)/)?.[1] || "";
|
|
224
|
+
const classPath = classLevelPath(file.text) || subresourceBaseMap.get(className) || "";
|
|
225
|
+
for (const match of file.text.matchAll(methodPattern)) {
|
|
226
|
+
const [, annotations, returnTypeRaw, methodName, parameters] = match;
|
|
227
|
+
const verb = annotations.match(/@(GET|POST|PUT|PATCH|DELETE)\b/)?.[1];
|
|
228
|
+
if (!verb) continue;
|
|
229
|
+
const childPath = annotations.match(/@Path\("([^"]+)"\)/)?.[1] || "";
|
|
230
|
+
const pathValue = normalizeJoinedPath(classPath, childPath);
|
|
231
|
+
const bodyType = parseBodyType(parameters);
|
|
232
|
+
const returnType = String(returnTypeRaw || "").trim().replace(/^Response\s*$/, "").trim();
|
|
233
|
+
routes.push({
|
|
234
|
+
method: verb,
|
|
235
|
+
path: pathValue,
|
|
236
|
+
handler_hint: explicitHandlerHint(methodName, pathValue, verb),
|
|
237
|
+
controller: className || file.relativePath.split("/").pop()?.replace(/\.java$/, "") || "JaxrsResource",
|
|
238
|
+
action: methodName,
|
|
239
|
+
auth_hint: inferAuthHint(pathValue, methodName, parameters, annotations),
|
|
240
|
+
path_params: parsePathParams(parameters),
|
|
241
|
+
query_params: parseQueryParams(parameters),
|
|
242
|
+
header_params: parseContextHeaders(parameters),
|
|
243
|
+
input_fields: bodyType ? flattenFieldsFromType(bodyType, files, new Set(), file.filePath) : [],
|
|
244
|
+
output_fields: flattenOutputFieldsFromType(returnType, files, file.filePath),
|
|
245
|
+
provenance: `${relativeTo(repoRoot, file.filePath)}#${verb} ${pathValue}`
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return routes;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export const jaxRsExtractor = {
|
|
253
|
+
id: "api.jaxrs",
|
|
254
|
+
track: "api",
|
|
255
|
+
detect(context) {
|
|
256
|
+
const javaFiles = findImportFiles(context.paths, (filePath) => /\.java$/i.test(filePath));
|
|
257
|
+
const jaxrsCount = javaFiles.filter((filePath) => /@Path\(|@(GET|POST|PUT|PATCH|DELETE)\b/.test(readTextIfExists(filePath) || "")).length;
|
|
258
|
+
return {
|
|
259
|
+
score: jaxrsCount > 0 ? 92 : 0,
|
|
260
|
+
reasons: jaxrsCount > 0 ? ["Found JAX-RS resource annotations"] : []
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
extract(context) {
|
|
264
|
+
const javaFiles = buildJavaFileIndex(context.paths);
|
|
265
|
+
const routes = parseJaxrsRoutes(javaFiles, context.paths.repoRoot);
|
|
266
|
+
const findings = [];
|
|
267
|
+
const candidates = { capabilities: [], routes: [], stacks: [] };
|
|
268
|
+
if (routes.length > 0) {
|
|
269
|
+
findings.push({
|
|
270
|
+
kind: "jaxrs_routes",
|
|
271
|
+
route_count: routes.length,
|
|
272
|
+
files: [...new Set(routes.map((route) => route.provenance.split("#")[0]))]
|
|
273
|
+
});
|
|
274
|
+
candidates.stacks.push("jaxrs");
|
|
275
|
+
}
|
|
276
|
+
candidates.capabilities.push(...routes.map((route) => makeCandidateRecord({
|
|
277
|
+
kind: "capability",
|
|
278
|
+
idHint: inferRouteCapabilityId(route),
|
|
279
|
+
label: route.handler_hint ? titleCase(route.handler_hint) : `${route.method} ${route.path}`,
|
|
280
|
+
confidence: "high",
|
|
281
|
+
sourceKind: "route_code",
|
|
282
|
+
provenance: route.provenance,
|
|
283
|
+
endpoint: { method: route.method, path: route.path },
|
|
284
|
+
path_params: route.path_params,
|
|
285
|
+
query_params: route.query_params,
|
|
286
|
+
header_params: route.header_params,
|
|
287
|
+
input_fields: route.input_fields,
|
|
288
|
+
output_fields: route.output_fields,
|
|
289
|
+
auth_hint: route.auth_hint,
|
|
290
|
+
entity_id: route.handler_hint === "sign_in_account" ? "entity_account" : inferApiEntityIdFromPath(route.path),
|
|
291
|
+
track: "api"
|
|
292
|
+
})));
|
|
293
|
+
candidates.routes.push(...routes.map((route) => ({
|
|
294
|
+
path: route.path,
|
|
295
|
+
method: route.method,
|
|
296
|
+
confidence: "high",
|
|
297
|
+
source_kind: "route_code",
|
|
298
|
+
provenance: route.provenance
|
|
299
|
+
})));
|
|
300
|
+
candidates.capabilities = dedupeCandidateRecords(candidates.capabilities, (record) => record.id_hint);
|
|
301
|
+
return { findings, candidates };
|
|
302
|
+
}
|
|
303
|
+
};
|