Skip to content

Commit

Permalink
Dedupe reported endpoints and endpoint params
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Oct 10, 2024
1 parent e30ff9f commit b29e2ee
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/lib/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,39 +226,52 @@ function processNamespace(namespace: Namespace, report: Report): void {

function processEndpoint(endpoint: Endpoint, report: Report): void {
if (endpoint.isUndocumented) {
report.undocumented.endpoints.push({
addUniqueEndpoint(report.undocumented.endpoints, {
name: endpoint.path,
reason: endpoint.undocumentedMessage ?? defaultUndocumentedMessage,
})
}

if (endpoint.description == null || endpoint.description.trim() === '') {
report.noDescription.endpoints.push({ name: endpoint.path })
addUniqueEndpoint(report.noDescription.endpoints, { name: endpoint.path })
}

if (endpoint.isDeprecated) {
report.deprecated.endpoints.push({
addUniqueEndpoint(report.deprecated.endpoints, {
name: endpoint.path,
reason: endpoint.deprecationMessage ?? defaultDeprecatedMessage,
})
}

if (endpoint.isDraft) {
report.draft.endpoints.push({
addUniqueEndpoint(report.draft.endpoints, {
name: endpoint.path,
reason: endpoint.draftMessage ?? defaultDraftMessage,
})
}

if (endpoint.codeSamples.length === 0) {
report.endpointsWithoutCodeSamples.push(endpoint.path)
if (!report.endpointsWithoutCodeSamples.includes(endpoint.path)) {
report.endpointsWithoutCodeSamples.push(endpoint.path)
}
}

processResponseKeys(endpoint, report)

processParameters(endpoint.path, endpoint.request.parameters, report)
}

function addUniqueEndpoint(
reportedEndpoints: ReportItem[],
newEndpoint: ReportItem,
): void {
if (
!reportedEndpoints.some((endpoint) => endpoint.name === newEndpoint.name)
) {
reportedEndpoints.push(newEndpoint)
}
}

function processResponseKeys(endpoint: Endpoint, report: Report): void {
if (!('responseKey' in endpoint.response)) return

Expand Down Expand Up @@ -339,27 +352,38 @@ function processParameters(
)

if (categorizedParams.undocumented.length > 0) {
report.undocumented.parameters.push({
addUniqueParameters(report.undocumented.parameters, {
path,
params: categorizedParams.undocumented,
})
}
if (categorizedParams.noDescription.length > 0) {
report.noDescription.parameters.push({
addUniqueParameters(report.noDescription.parameters, {
path,
params: categorizedParams.noDescription,
})
}
if (categorizedParams.deprecated.length > 0) {
report.deprecated.parameters.push({
addUniqueParameters(report.deprecated.parameters, {
path,
params: categorizedParams.deprecated,
})
}
if (categorizedParams.draft.length > 0) {
report.draft.parameters.push({
addUniqueParameters(report.draft.parameters, {
path,
params: categorizedParams.draft,
})
}
}

const addUniqueParameters = (
reportedParams: ParameterReportItem[],
newParam: ParameterReportItem,
): void => {
if (reportedParams.some((param) => param.path === newParam.path)) {
return
}

reportedParams.push(newParam)
}

0 comments on commit b29e2ee

Please sign in to comment.