-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6690287
commit c44e270
Showing
36 changed files
with
994 additions
and
1,482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Group } from "./dependencyManager/types"; | ||
import { LanguagePlugin } from "./languages/types"; | ||
|
||
class AnnotationManager { | ||
private nanoapiRegex: RegExp; | ||
private commentPrefix: string; | ||
path: string; | ||
method?: string; | ||
group?: string; | ||
|
||
constructor(comment: string, languagePlugin: LanguagePlugin) { | ||
this.nanoapiRegex = languagePlugin.annotationRegex; | ||
this.commentPrefix = languagePlugin.commentPrefix; | ||
const { path, method, group } = this.#parsetext(comment); | ||
this.path = path; | ||
this.method = method; | ||
this.group = group; | ||
} | ||
|
||
#parsetext(text: string) { | ||
const matches = text.match(this.nanoapiRegex); | ||
|
||
if (!matches) { | ||
throw new Error("Could not parse annotation"); | ||
} | ||
|
||
const methodRegex = /method:([^ ]+)/; | ||
const pathRegex = /path:([^ ]+)/; | ||
const groupRegex = /group:([^ ]+)/; | ||
|
||
const pathMatches = text.match(pathRegex); | ||
const methodMatches = text.match(methodRegex); | ||
const groupMatches = text.match(groupRegex); | ||
|
||
if (!pathMatches) { | ||
throw new Error("Could not parse path from annotation"); | ||
} | ||
|
||
const path = pathMatches[1]; | ||
const method = methodMatches ? methodMatches[1] : undefined; | ||
const group = groupMatches ? groupMatches[1] : undefined; | ||
|
||
return { path, method, group }; | ||
} | ||
|
||
matchesEndpoint(path: string, method: string | undefined) { | ||
return this.path === path && this.method === method; | ||
} | ||
|
||
isInGroup(group: Group) { | ||
// check if annotation has a method (actual endpoint) | ||
if (this.method) { | ||
const endpoint = group.endpoints.find( | ||
(endpoint) => | ||
endpoint.method === this.method && endpoint.path === this.path, | ||
); | ||
|
||
if (endpoint) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// if annotation has no method, we treat it as a module that contains other endpoints | ||
const endpoints = group.endpoints.filter((endpoint) => | ||
endpoint.path.startsWith(this.path), | ||
); | ||
|
||
if (endpoints.length > 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
stringify() { | ||
let annotation = `${this.commentPrefix} @nanoapi`; | ||
if (this.method) { | ||
annotation += ` method:${this.method}`; | ||
} | ||
if (this.path) { | ||
annotation += ` path:${this.path}`; | ||
} | ||
if (this.group) { | ||
annotation += ` group:${this.group}`; | ||
} | ||
|
||
return annotation; | ||
} | ||
} | ||
|
||
export default AnnotationManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { z } from "zod"; | ||
import DependencyTreeManager from "../helper/dependencyTree"; | ||
import DependencyTreeManager from "../dependencyManager/dependencyManager"; | ||
import { scanSchema } from "./helpers/validation"; | ||
|
||
export function scan(payload: z.infer<typeof scanSchema>) { | ||
const dependencyTreeManager = new DependencyTreeManager( | ||
payload.entrypointPath, | ||
); | ||
const endpoints = dependencyTreeManager.getEndponts(); | ||
|
||
return { endpoints }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.