Skip to content

Commit 9377ac5

Browse files
committed
wip
1 parent b7f592d commit 9377ac5

File tree

15 files changed

+709
-172
lines changed

15 files changed

+709
-172
lines changed

examples/node/nestjs-typescript/src/comics/comics.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@nestjs/common';
1010
import { ComicsService } from './comics.service';
1111
import { Comic, ComicPartial } from './comics.data';
12-
import { calculateHighFibonacci } from 'src/helpers/fibonacci';
12+
import { calculateHighFibonacci } from '../helpers/fibonacci';
1313

1414
@Controller('comics')
1515
export class ComicsController {

examples/python/flask/api/views/elves.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get(self, *args, **kwargs):
1111
return elves
1212

1313

14-
# @nanoapi path:/api/elves method:GET
14+
# @nanoapi method:GET path:/api/elves group:elf_read
1515
elves_bp.add_url_rule("/", view_func=ElfListView.as_view("elf_list"), methods=["GET"])
1616

1717

@@ -22,7 +22,7 @@ def post(self, *args, **kwargs):
2222
return new_elf
2323

2424

25-
# @nanoapi path:/api/elves method:POST
25+
# @nanoapi method:POST path:/api/elves group:elf_write
2626
elves_bp.add_url_rule(
2727
"/", view_func=ElfCreateView.as_view("elf_create"), methods=["POST"]
2828
)
@@ -34,7 +34,7 @@ def get(self, elf_id, *args, **kwargs):
3434
return elf
3535

3636

37-
# @nanoapi path:/api/elves/<elf_id> method:GET
37+
# @nanoapi method:GET path:/api/elves/<elf_id> group:elf_read
3838
elves_bp.add_url_rule(
3939
"/<int:elf_id>", view_func=ElfDetailView.as_view("elf_detail"), methods=["GET"]
4040
)
@@ -47,7 +47,7 @@ def put(self, elf_id, *args, **kwargs):
4747
return updated_elf
4848

4949

50-
# @nanoapi path:/api/elves/<elf_id> method:PUT
50+
# @nanoapi method:PUT path:/api/elves/<elf_id> group:elf_write
5151
elves_bp.add_url_rule(
5252
"/<int:elf_id>", view_func=ElfUpdateView.as_view("elf_update"), methods=["PUT"]
5353
)
@@ -59,7 +59,7 @@ def delete(self, elf_id, *args, **kwargs):
5959
return None
6060

6161

62-
# @nanoapi path:/api/elves/<elf_id> method:DELETE
62+
# @nanoapi method:DELETE path:/api/elves/<elf_id> group:elf_write
6363
elves_bp.add_url_rule(
6464
"/<int:elf_id>", view_func=ElfDeleteView.as_view("elf_delete"), methods=["DELETE"]
6565
)

examples/python/flask/api/views/hobbits.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get(self, *args, **kwargs):
1111
return hobbits
1212

1313

14-
# @nanoapi path:/api/hobbits method:GET
14+
# @nanoapi method:GET path:/api/hobbits group:hobbit_read
1515
hobbits_bp.add_url_rule(
1616
"/", view_func=HobbitListView.as_view("hobbit_list"), methods=["GET"]
1717
)
@@ -24,7 +24,7 @@ def post(self, *args, **kwargs):
2424
return new_hobbit
2525

2626

27-
# @nanoapi path:/api/hobbits method:POST
27+
# @nanoapi method:POST path:/api/hobbits group:hobbit_write
2828
hobbits_bp.add_url_rule(
2929
"/", view_func=HobbitCreateView.as_view("hobbit_create"), methods=["POST"]
3030
)
@@ -36,7 +36,7 @@ def get(self, hobbit_id, *args, **kwargs):
3636
return hobbit
3737

3838

39-
# @nanoapi path:/api/hobbits/<hobbit_id> method:GET
39+
# @nanoapi method:GET path:/api/hobbits/<hobbit_id> group:hobbit_read
4040
hobbits_bp.add_url_rule(
4141
"/<int:hobbit_id>",
4242
view_func=HobbitDetailView.as_view("hobbit_detail"),
@@ -51,7 +51,7 @@ def put(self, hobbit_id, *args, **kwargs):
5151
return updated_hobbit
5252

5353

54-
# @nanoapi path:/api/hobbits/<hobbit_id> method:PUT
54+
# @nanoapi method:PUT path:/api/hobbits/<hobbit_id> group:hobbit_write
5555
hobbits_bp.add_url_rule(
5656
"/<int:hobbit_id>",
5757
view_func=HobbitUpdateView.as_view("hobbit_update"),
@@ -65,7 +65,7 @@ def delete(self, hobbit_id, *args, **kwargs):
6565
return None
6666

6767

68-
# @nanoapi path:/api/hobbits/<hobbit_id> method:DELETE
68+
# @nanoapi method:DELETE path:/api/hobbits/<hobbit_id> group:hobbit_write
6969
hobbits_bp.add_url_rule(
7070
"/<int:hobbit_id>",
7171
view_func=HobbitDeleteView.as_view("hobbit_delete"),

examples/python/flask/api/wizards/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask.views import MethodView
22
from flask import request, Blueprint
3-
from api.wizards.services import WizardService
3+
from .services import WizardService
44

55
wizards_bp = Blueprint("wizards", __name__)
66

@@ -11,7 +11,7 @@ def get(self, *args, **kwargs):
1111
return wizards
1212

1313

14-
# @nanoapi path:/api/wizards method:GET
14+
# @nanoapi method:GET path:/api/wizards group:wizard_read
1515
wizards_bp.add_url_rule(
1616
"/", view_func=WizardListView.as_view("wizard_list"), methods=["GET"]
1717
)
@@ -24,7 +24,7 @@ def post(self, *args, **kwargs):
2424
return new_wizard
2525

2626

27-
# @nanoapi path:/api/wizards method:POST
27+
# @nanoapi method:POST path:/api/wizards group:wizard_write
2828
wizards_bp.add_url_rule(
2929
"/", view_func=WizardCreateView.as_view("wizard_create"), methods=["POST"]
3030
)
@@ -36,7 +36,7 @@ def get(self, wizard_id, *args, **kwargs):
3636
return wizard
3737

3838

39-
# @nanoapi path:/api/wizards/<wizard_id> method:GET
39+
# @nanoapi method:GET path:/api/wizards/<wizard_id> group:wizard_read
4040
wizards_bp.add_url_rule(
4141
"/<int:wizard_id>",
4242
view_func=WizardDetailView.as_view("wizard_detail"),
@@ -51,7 +51,7 @@ def put(self, wizard_id, *args, **kwargs):
5151
return updated_wizard
5252

5353

54-
# @nanoapi path:/api/wizards/<wizard_id> method:PUT
54+
# @nanoapi method:PUT path:/api/wizards/<wizard_id> group:wizard_write
5555
wizards_bp.add_url_rule(
5656
"/<int:wizard_id>",
5757
view_func=WizardUpdateView.as_view("wizard_update"),
@@ -65,7 +65,7 @@ def delete(self, wizard_id, *args, **kwargs):
6565
return None
6666

6767

68-
# @nanoapi path:/api/wizards/<wizard_id> method:DELETE
68+
# @nanoapi method:DELETE path:/api/wizards/<wizard_id> group:wizard_write
6969
wizards_bp.add_url_rule(
7070
"/<int:wizard_id>",
7171
view_func=WizardDeleteView.as_view("wizard_delete"),

examples/python/flask/app.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
from flask import Flask
2-
from api.wizards.views import wizards_bp
2+
from .api.wizards.views import wizards_bp
33
from api.views.elves import elves_bp
44
from api.views.hobbits import hobbits_bp
55

66
app = Flask(__name__)
77

88

9-
# @nanoapi path:/ method:GET
109
@app.get("/")
1110
def hello_world():
1211
return {"message": "Hello, World!"}
1312

1413

15-
# @nanoapi path:/liveness method:GET
1614
@app.get("/liveness")
1715
def liveness():
1816
return {"status": "ok"}
1917

20-
21-
# @nanoapi path:/readiness method:GET
18+
# @nanoapi path:readiness method:GET
2219
@app.get("/readiness")
2320
def readiness():
2421
return {"status": "ok"}
2522

26-
2723
# @nanoapi path:/api/wizards
2824
app.register_blueprint(wizards_bp, url_prefix="/api/wizards")
2925

packages/cli/src/api/sync.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { syncSchema } from "./helpers/validation";
33
import fs from "fs";
44
import DependencyTreeManager from "../dependencyManager/dependencyManager";
55
import AnnotationManager from "../annotationManager";
6-
import { getLanguagePluginFromFilePath } from "../languagesPlugins";
6+
import { getLanguagePlugin } from "../languagesPlugins";
77
import { replaceIndexesFromSourceCode } from "../helper/file";
88

99
export function sync(payload: z.infer<typeof syncSchema>) {
@@ -29,7 +29,10 @@ export function sync(payload: z.infer<typeof syncSchema>) {
2929
});
3030

3131
updatedEndpoints.forEach((endpoint) => {
32-
const languagePlugin = getLanguagePluginFromFilePath(endpoint.filePath);
32+
const languagePlugin = getLanguagePlugin(
33+
payload.entrypointPath,
34+
endpoint.filePath,
35+
);
3336

3437
const sourceCode = fs.readFileSync(endpoint.filePath, "utf-8");
3538

packages/cli/src/dependencyManager/dependencyManager.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
import fs from "fs";
22

3-
import { resolveFilePath } from "../helper/file";
43
import { DependencyTree, Group, Endpoint } from "./types";
54
import AnnotationManager from "../annotationManager";
6-
import { getLanguagePluginFromFilePath } from "../languagesPlugins";
5+
import { getLanguagePlugin } from "../languagesPlugins";
76

87
class DependencyTreeManager {
8+
entryPointPath: string;
99
dependencyTree: DependencyTree;
1010

11-
constructor(filePath: string) {
12-
const dependencyTree = this.#getDependencyTree(filePath);
11+
constructor(entryPointPath: string) {
12+
this.entryPointPath = entryPointPath;
13+
const dependencyTree = this.#getDependencyTree(
14+
this.entryPointPath,
15+
this.entryPointPath,
16+
);
1317
this.dependencyTree = dependencyTree;
1418
}
1519

16-
#getDependencyTree(filePath: string): DependencyTree {
17-
const sourceCode = fs.readFileSync(filePath, "utf8");
20+
#getDependencyTree(
21+
entryPointPath: string,
22+
currentFilePath: string,
23+
): DependencyTree {
24+
const sourceCode = fs.readFileSync(currentFilePath, "utf8");
1825

1926
const dependencyTree: DependencyTree = {
20-
path: filePath,
27+
path: currentFilePath,
2128
sourceCode,
2229
children: [],
2330
};
2431

25-
const languagePlugin = getLanguagePluginFromFilePath(filePath);
32+
const languagePlugin = getLanguagePlugin(entryPointPath, currentFilePath);
2633

2734
const tree = languagePlugin.parser.parse(sourceCode);
2835

29-
let imports = languagePlugin.getImports(tree.rootNode);
30-
31-
imports = imports.filter((importPath) => importPath.source.startsWith("."));
36+
const imports = languagePlugin.getImports(currentFilePath, tree.rootNode);
3237

33-
imports.forEach((importPath) => {
34-
const resolvedPath = resolveFilePath(importPath.source, filePath);
35-
if (resolvedPath && fs.existsSync(resolvedPath)) {
36-
const childTree = this.#getDependencyTree(resolvedPath);
37-
dependencyTree.children.push(childTree);
38+
imports.forEach((depImport) => {
39+
if (depImport.isExternal || !depImport.source) {
40+
// Ignore external dependencies
41+
return;
3842
}
43+
44+
const childTree = this.#getDependencyTree(
45+
entryPointPath,
46+
depImport.source,
47+
);
48+
dependencyTree.children.push(childTree);
3949
});
4050

4151
return dependencyTree;
@@ -58,7 +68,10 @@ class DependencyTreeManager {
5868
parentFilePaths: string[],
5969
dependencyTree: DependencyTree,
6070
) {
61-
const languagePlugin = getLanguagePluginFromFilePath(dependencyTree.path);
71+
const languagePlugin = getLanguagePlugin(
72+
this.entryPointPath,
73+
dependencyTree.path,
74+
);
6275

6376
const tree = languagePlugin.parser.parse(dependencyTree.sourceCode);
6477

packages/cli/src/helper/file.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from "fs";
2-
import path from "path";
32

43
export function cleanupOutputDir(outputDir: string) {
54
if (fs.existsSync(outputDir)) {
@@ -11,44 +10,6 @@ export function createOutputDir(outputDir: string) {
1110
fs.mkdirSync(outputDir, { recursive: true });
1211
}
1312

14-
export function resolveFilePath(importPath: string, currentFile: string) {
15-
const currentFileExt = path.extname(currentFile);
16-
const importExt = path.extname(importPath);
17-
if (importPath.startsWith(".")) {
18-
if (importExt) {
19-
// If import path has an extension, resolve directly
20-
const resolvedPath = path.resolve(path.dirname(currentFile), importPath);
21-
if (fs.existsSync(resolvedPath)) {
22-
return resolvedPath;
23-
}
24-
}
25-
26-
// If import path does not have an extension, try current file's extension first
27-
const resolvedPathWithCurrentExt = path.resolve(
28-
path.dirname(currentFile),
29-
`${importPath}${currentFileExt}`,
30-
);
31-
if (fs.existsSync(resolvedPathWithCurrentExt)) {
32-
return resolvedPathWithCurrentExt;
33-
}
34-
35-
// try to resolve with any extension
36-
const resolvedPath = path.resolve(path.dirname(currentFile), importPath);
37-
try {
38-
const resolvedPathWithAnyExt = require.resolve(resolvedPath);
39-
if (fs.existsSync(resolvedPathWithAnyExt)) {
40-
return resolvedPathWithAnyExt;
41-
}
42-
} catch {
43-
// cannot resolve the path, return null
44-
return null;
45-
}
46-
}
47-
48-
// Skip external dependencies (e.g., node_modules)
49-
return null;
50-
}
51-
5213
export function removeIndexesFromSourceCode(
5314
sourceCode: string,
5415
indexesToRemove: { startIndex: number; endIndex: number }[],

packages/cli/src/languagesPlugins/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import JavascriptPlugin from "./javascript";
2+
import PythonPlugin from "./python";
23
import { LanguagePlugin } from "./types";
34
import TypescriptPlugin from "./typescript";
45

5-
export function getLanguagePluginFromFilePath(
6+
export function getLanguagePlugin(
7+
entryPointPath: string,
68
filePath: string,
79
): LanguagePlugin {
810
const ext = filePath.split(".").pop();
911

1012
switch (ext) {
1113
case "js":
12-
return new JavascriptPlugin();
14+
return new JavascriptPlugin(entryPointPath);
1315
case "ts":
14-
return new TypescriptPlugin();
16+
return new TypescriptPlugin(entryPointPath);
17+
case "py":
18+
return new PythonPlugin(entryPointPath);
1519
default:
1620
throw new Error(`Unsupported file type: ${ext}`);
1721
}

0 commit comments

Comments
 (0)