-
Notifications
You must be signed in to change notification settings - Fork 92
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
a44d7c7
commit 83ac7da
Showing
5 changed files
with
110 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { IsNull } from "typeorm"; | ||
import { isSuspectedParamater } from "../../utils"; | ||
import { ApiEndpoint, ApiTrace } from "../../../models"; | ||
import { AppDataSource } from "../../data-source"; | ||
import { RestMethod } from "../../enums"; | ||
|
||
interface GenerateEndpoint { | ||
parameterizedPath: string; | ||
host: string; | ||
method: RestMethod; | ||
traces: ApiTrace[]; | ||
} | ||
|
||
export class EndpointsService { | ||
static async generateEndpointsFromTraces() { | ||
const apiTraceRepository = AppDataSource.getRepository(ApiTrace); | ||
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint); | ||
const regexToTracesMap: Record<string, GenerateEndpoint> = {}; | ||
const traces = await apiTraceRepository.findBy({ apiEndpointUuid: IsNull() }); | ||
if (traces?.length > 0) { | ||
for (let i = 0; i < traces.length; i++) { | ||
const trace = traces[i]; | ||
let found = false; | ||
const regexes = Object.keys(regexToTracesMap); | ||
for (let x = 0; x < regexes.length && !found; x++) { | ||
const regex = regexes[x]; | ||
const curr = regexToTracesMap[regex]; | ||
if (RegExp(regex).test(trace.path) && trace.host === curr.host && trace.method === curr.method) { | ||
found = true; | ||
regexToTracesMap[regex] = { ...regexToTracesMap[regex], traces: [...regexToTracesMap[regex].traces, trace]}; | ||
} | ||
} | ||
if (!found) { | ||
const pathTokens = trace.path.split("/"); | ||
let paramNum = 1; | ||
let parameterizedPath = ""; | ||
let pathRegex = String.raw``; | ||
for (let j = 0; j < pathTokens.length; j++) { | ||
const tokenString = pathTokens[j]; | ||
if (isSuspectedParamater(tokenString)) { | ||
parameterizedPath += `/{param${paramNum}}`; | ||
pathRegex += String.raw`/[^/]+`; | ||
paramNum += 1; | ||
} else { | ||
parameterizedPath += `/${tokenString}`; | ||
pathRegex += String.raw`/${tokenString}`; | ||
} | ||
} | ||
if (pathRegex.length > 0) { | ||
if (regexToTracesMap[pathRegex]) { | ||
regexToTracesMap[pathRegex] = { ...regexToTracesMap[pathRegex], traces: [...regexToTracesMap[pathRegex].traces, trace]}; | ||
} else { | ||
regexToTracesMap[pathRegex] = { parameterizedPath, host: trace.host, method: trace.method, traces: [trace]}; | ||
} | ||
} | ||
} | ||
} | ||
console.log(regexToTracesMap); | ||
Object.entries(regexToTracesMap).map(async ([regex, value], idx) => { | ||
const apiEndpoint = new ApiEndpoint(); | ||
apiEndpoint.path = value.parameterizedPath; | ||
apiEndpoint.host = value.traces[0].host; | ||
apiEndpoint.environment = value.traces[0].environment; | ||
apiEndpoint.totalCalls = value.traces.length; | ||
apiEndpoint.method = value.traces[0].method; | ||
apiEndpoint.owner = value.traces[0].owner; | ||
await apiEndpointRepository.save(apiEndpoint); | ||
// TODO: Do something with setting sensitive data classes during iteration of traces | ||
for (let i = 0; i < value.traces.length; i++) { | ||
const trace = value.traces[i]; | ||
trace.apiEndpoint = apiEndpoint; | ||
await apiTraceRepository.save(trace); | ||
} | ||
}) | ||
} | ||
} | ||
} |
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,11 @@ | ||
import validator from "validator"; | ||
|
||
export const isSuspectedParamater = (value: string) => { | ||
if (!isNaN(Number(value))) { | ||
return true; | ||
} | ||
if (validator.isUUID(value)) { | ||
return true; | ||
} | ||
return false; | ||
} |
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