Skip to content

Commit

Permalink
Fix: show paths supported by a version (#2862)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Nov 3, 2023
1 parent 091578c commit 460c10b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ interface ValidationProviderProps {

export const ValidationProvider = ({ children }: ValidationProviderProps) => {
const { resources } = useAppSelector((state) => state);
const base = getResourcesSupportedByVersion(resources.data.children, GRAPH_API_VERSIONS[0]);
const base = getResourcesSupportedByVersion(resources.data.children!, GRAPH_API_VERSIONS[0]);

const [isValid, setIsValid] = useState<boolean>(false);
const [query, setQuery] = useState<string>('');
const [validationError, setValidationError] = useState<string>('');

const [versionedResources, setVersionedResources] =
useState<IResource[]>(resources.data.children.length > 0 ? base : []);
useState<IResource[]>(resources.data.children!.length > 0 ? base : []);
const [version, setVersion] = useState<string>(GRAPH_API_VERSIONS[0]);

const { queryVersion } = parseSampleUrl(query);

useEffect(() => {
if (resources.data.children.length > 0) {
setVersionedResources(getResourcesSupportedByVersion(resources.data.children, GRAPH_API_VERSIONS[0]));
if (resources.data.children!.length > 0) {
setVersionedResources(getResourcesSupportedByVersion(resources.data.children!, GRAPH_API_VERSIONS[0]));
}
}, [resources])

useEffect(() => {
if (version !== queryVersion && GRAPH_API_VERSIONS.includes(queryVersion) && resources.data.children.length > 0) {
setVersionedResources(getResourcesSupportedByVersion(resources.data.children, queryVersion));
if (version !== queryVersion && GRAPH_API_VERSIONS.includes(queryVersion) && resources.data.children!.length > 0) {
setVersionedResources(getResourcesSupportedByVersion(resources.data.children!, queryVersion));
setVersion(queryVersion);
}
}, [query]);
Expand Down
21 changes: 17 additions & 4 deletions src/app/utils/resources/resources-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ function getResourcesSupportedByVersion(
}

function versionExists(resource: IResource, version: string): boolean {
return resource &&
resource.labels &&
resource.labels.length > 0 &&
resource.labels.some((k) => k.name === version);
if (!resource) {
return false;
}

const hasLabels = resource.labels && resource.labels.length > 0;
const hasChildren = resource.children && resource.children.length > 0;

if (!hasLabels && !hasChildren) {
return false;
}

if (!hasLabels && hasChildren) {
const childLabels = resource.children?.map((child) => child.labels);
return childLabels?.some((child) => child?.some((label) => label.name === version)) || false;
}

return resource.labels.some((k) => k.name === version);
}

function searchResources(haystack: IResource[], needle: string): IResource[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const SuffixRenderer = () => {

const resourceDocumentationUrl = new DocumentationService({
sampleQuery,
source: resources.data.children
source: resources.data.children!
}).getDocumentationLink();

const sampleDocumentationUrl = new DocumentationService({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Tests suffix utilities', () => {
query.sampleUrl = `https://graph.microsoft.com/v1.0/me/messages/${id}`;
const docService = new DocumentationService({
sampleQuery: query,
source: resource.children
source: resource.children!
})
const documentationUrl = docService.getDocumentationLink();
expect(documentationUrl).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Postman collection should', () => {

function setupCollection() {
const version = 'v1.0';
const filtered = createResourcesList(resource.children, version)[0];
const filtered = createResourcesList(resource.children!, version)[0];
const item: any = filtered.links[0];
const paths = getResourcePaths(item, version);
const collection = generatePostmanCollection(paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe('Resource payload should', () => {
});

it('return children with version v1.0', async () => {
const resources = getResourcesSupportedByVersion(resource.children, 'v1.0');
const resources = getResourcesSupportedByVersion(resource.children!, 'v1.0');
expect(resources.length).toBeGreaterThan(0);
});

it('return links with version v1.0', async () => {
const filtered = createResourcesList(resource.children, 'v1.0')[0];
const filtered = createResourcesList(resource.children!, 'v1.0')[0];
expect(filtered.links.length).toBeGreaterThan(0);
});

Expand All @@ -31,15 +31,15 @@ describe('Resource payload should', () => {
const currentTree = getCurrentTree({
paths,
level,
resourceItems: resource.children,
resourceItems: resource.children!,
version
});
expect(currentTree).not.toBeNull();
});

it('return available methods', async () => {
const version = 'v1.0';
const filtered = createResourcesList(resource.children, version)[0];
const filtered = createResourcesList(resource.children!, version)[0];
const resourceLink = filtered.links[0];
const availableMethods = getAvailableMethods(resourceLink.labels, version);
expect(availableMethods).not.toBeNull();
Expand All @@ -58,7 +58,7 @@ describe('Resource payload should', () => {
const currentTree = getCurrentTree({
paths,
level,
resourceItems: resource.children,
resourceItems: resource.children!,
version
});
const link = currentTree.links[0];
Expand All @@ -68,7 +68,7 @@ describe('Resource payload should', () => {

it('return a flattened list of links', async () => {
const version = 'v1.0';
const filtered = createResourcesList(resource.children, version)[0];
const filtered = createResourcesList(resource.children!, version)[0];
const item: any = filtered.links[0];
const paths = getResourcePaths(item, version);
expect(paths.length).toBeGreaterThan(0);
Expand All @@ -84,7 +84,7 @@ describe('Resource payload should', () => {
});

describe('Resource filter should', () => {
const resources = getResourcesSupportedByVersion(resource.children, 'v1.0');
const resources = getResourcesSupportedByVersion(resource.children!, 'v1.0');

const messageId = 'AAMkAGFkNWI1Njg3LWZmNTUtNDZjOS04ZTM2LTc5ZTc5ZjFlNTM4ZgB1SyTR4EQuQIAbWVtP3x1LBwD4_HsJDyJ8QAAA=';

Expand All @@ -109,7 +109,7 @@ describe('Resource filter should', () => {
const setWithId = getMatchingResourceForUrl(requestUrl, resources)!;
const setWithPlaceholder = getMatchingResourceForUrl(baseUrl, resources)!;

expect(setWithId.children.length).toBe(setWithPlaceholder.children.length);
expect(setWithId.children?.length).toBe(setWithPlaceholder.children?.length);
});

});
Expand Down
2 changes: 1 addition & 1 deletion src/modules/suggestions/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Suggestions implements ISuggestions {
return this.createOpenApiResponse(versionedResources, url);
} else {
const matching = getMatchingResourceForUrl(url, versionedResources);
if (matching && matching.children.length > 0) {
if (matching && matching.children && matching.children.length > 0) {
return this.createOpenApiResponse(matching.children, url)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Method = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
export interface IResource {
segment: string;
labels: IResourceLabel[];
children: IResource[];
children?: IResource[];
}

export interface IResourceLabel {
Expand Down

0 comments on commit 460c10b

Please sign in to comment.