From 19b3fd9b481bf0b7b0b617887f15e062fcfeae22 Mon Sep 17 00:00:00 2001 From: Nikhil Shahi Date: Fri, 5 Aug 2022 16:31:17 -0500 Subject: [PATCH] fix endpoint generate from traces for host and method --- backend/src/services/endpoints/index.ts | 42 ++++++++++++++----------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/backend/src/services/endpoints/index.ts b/backend/src/services/endpoints/index.ts index a9ba8989..10f2de3d 100644 --- a/backend/src/services/endpoints/index.ts +++ b/backend/src/services/endpoints/index.ts @@ -7,6 +7,7 @@ import { RestMethod } from "../../enums"; interface GenerateEndpoint { parameterizedPath: string; host: string; + regex: string; method: RestMethod; traces: ApiTrace[]; } @@ -26,17 +27,15 @@ export class EndpointsService { 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 + RegExp(regex).test(`${trace.host}-${trace.method}-${trace.path}`) ) { found = true; - regexToTracesMap[regex] = { + regexToTracesMap[regex].traces.push(trace); + /*regexToTracesMap[regex] = { ...regexToTracesMap[regex], traces: [...regexToTracesMap[regex].traces, trace], - }; + };*/ } } if (!found) { @@ -46,25 +45,30 @@ export class EndpointsService { 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 (tokenString.length > 0) { + 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] = { + const regexKey = `${trace.host}-${trace.method}-${pathRegex}`; + if (regexToTracesMap[regexKey]) { + regexToTracesMap[regexKey].traces.push(trace); + /*regexToTracesMap[pathRegex] = { ...regexToTracesMap[pathRegex], traces: [...regexToTracesMap[pathRegex].traces, trace], - }; + };*/ } else { - regexToTracesMap[pathRegex] = { + regexToTracesMap[regexKey] = { parameterizedPath, host: trace.host, + regex: pathRegex, method: trace.method, traces: [trace], }; @@ -75,13 +79,13 @@ export class EndpointsService { Object.entries(regexToTracesMap).map(async ([regex, value], idx) => { const apiEndpoint = new ApiEndpoint(); apiEndpoint.path = value.parameterizedPath; - apiEndpoint.pathRegex = regex; + apiEndpoint.pathRegex = value.regex; apiEndpoint.host = value.traces[0].host; 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 + // TODO: Do something with setting sensitive data classes during iteration of traces and add auto generated open api spec for inferred endpoints for (let i = 0; i < value.traces.length; i++) { const trace = value.traces[i]; trace.apiEndpoint = apiEndpoint;