Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/actions/portal/toc/new-toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export class PortalNewTocAction {
callbackGroups: new Map()
};

if (!expandEndpoints && !expandModels && !expandWebhooks && !expandCallbacks) {
return defaultComponents;
}

const specDirectory = buildDirectory.join('spec');

if (!(await this.fileService.directoryExists(specDirectory))) {
Expand All @@ -103,12 +99,12 @@ export class PortalNewTocAction {
return defaultComponents;
}

const endpointGroups = expandEndpoints ? extractEndpointGroupsForToc(result.value) : new Map();
const models = expandModels ? extractModelsForToc(result.value) : [];
const webhookGroups = expandWebhooks ? extractWebhooksForToc(result.value) : new Map();
const callbackGroups = expandCallbacks ? extractCallbacksForToc(result.value) : new Map();

return { endpointGroups, models, webhookGroups, callbackGroups };
return {
endpointGroups: extractEndpointGroupsForToc(result.value),
models: extractModelsForToc(result.value),
webhookGroups: extractWebhooksForToc(result.value),
callbackGroups: extractCallbacksForToc(result.value)
};
});
})();
const contentContext = new ContentContext(contentDirectory);
Expand All @@ -123,10 +119,10 @@ export class PortalNewTocAction {
}

const toc = this.tocGenerator.createTocStructure(
sdlTocComponents.endpointGroups,
sdlTocComponents.models,
sdlTocComponents.webhookGroups,
sdlTocComponents.callbackGroups,
{ data: sdlTocComponents.endpointGroups, expand: expandEndpoints },
{ data: sdlTocComponents.models, expand: expandModels },
{ data: sdlTocComponents.webhookGroups, expand: expandWebhooks },
{ data: sdlTocComponents.callbackGroups, expand: expandCallbacks },
contentGroups
);
const yamlString = this.tocGenerator.transformToYaml(toc);
Expand Down
169 changes: 110 additions & 59 deletions src/application/portal/toc/toc-structure-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,36 @@ import {

// TODO: Refactor

type Endpoints = {
data: Map<string, TocEndpoint[]>;
expand: boolean;
};

type Webhooks = {
data: Map<string, TocWebhookPage[]>;
expand: boolean;
};

type Callbacks = {
data: Map<string, TocCallbackPage[]>;
expand: boolean;
};

type Models = {
data: TocModelPage[];
expand: boolean;
};

export class TocStructureGenerator {
createTocStructure(
endpointGroups: Map<string, TocEndpoint[]>,
models: TocModelPage[],
webhookGroups: Map<string, TocWebhookPage[]>,
callbackGroups: Map<string, TocCallbackPage[]>,
endpoints: Endpoints,
models: Models,
webhooks: Webhooks,
callbacks: Callbacks,
contentGroups: TocGroup[] = []
): Toc {
const events = [...this.getCallbacksSection(callbacks), ...this.getWebhooksSection(webhooks)];

return {
toc: [
{
Expand All @@ -32,12 +54,16 @@ export class TocStructureGenerator {
]
},
...contentGroups,
this.getEndpointsSection(endpointGroups),
{
group: 'Events',
items: [this.getCallbacksSection(callbackGroups), this.getWebhooksSection(webhookGroups)]
},
this.getModelsSection(models),
this.getEndpointsSection(endpoints),
...(events.length > 0
? [
{
group: 'Events',
items: events
}
]
: []),
...this.getModelsSection(models),
Comment thread
asadali214 marked this conversation as resolved.
Resolved
{
generate: 'SDK Infrastructure',
from: 'sdk-infra'
Expand All @@ -54,16 +80,16 @@ export class TocStructureGenerator {
});
}

private getEndpointsSection(endpointGroups: Map<string, TocEndpoint[]>): TocGroup | TocGenerated {
if (endpointGroups.size === 0) {
private getEndpointsSection(endpoints: Endpoints): TocGroup | TocGenerated {
if (!endpoints.expand || endpoints.data.size === 0) {
return {
generate: 'API Endpoints',
from: 'endpoints'
};
}
return {
group: 'API Endpoints',
items: Array.from(endpointGroups).map(([groupName, endpoints]) => ({
items: Array.from(endpoints.data).map(([groupName, endpoints]) => ({
group: groupName,
items: [
{
Expand All @@ -77,61 +103,86 @@ export class TocStructureGenerator {
};
}

private getCallbacksSection(callbackGroups: Map<string, TocCallbackPage[]>): TocGroup | TocGenerated {
if (callbackGroups.size === 0) {
return {
generate: null,
from: 'callbacks'
};
private getCallbacksSection(callbacks: Callbacks): (TocGroup | TocGenerated)[] {
if (callbacks.data.size === 0) {
return [];
}
if (callbackGroups.size === 1) {
return {
group: Array.from(callbackGroups.keys())[0],
items: Array.from(callbackGroups.values())[0]
};
if (callbacks.expand === true) {
if (callbacks.data.size === 1) {
return [
{
group: Array.from(callbacks.data.keys())[0],
items: Array.from(callbacks.data.values())[0]
}
];
}
return [
{
group: 'Callbacks',
items: Array.from(callbacks.data).map(([groupName, eventList]) => ({
group: groupName,
items: eventList
}))
}
];
}
return {
group: 'Callbacks',
items: Array.from(callbackGroups).map(([groupName, eventList]) => ({
group: groupName,
items: eventList
}))
};
return [
{
generate: 'Callbacks',
from: 'callbacks'
}
];
}

private getWebhooksSection(webhookGroups: Map<string, TocWebhookPage[]>): TocGroup | TocGenerated {
if (webhookGroups.size === 0) {
return {
generate: null,
from: 'webhooks'
};
private getWebhooksSection(webhooks: Webhooks): (TocGroup | TocGenerated)[] {
if (webhooks.data.size === 0) {
return [];
}
if (webhookGroups.size === 1) {
return {
group: Array.from(webhookGroups.keys())[0],
items: Array.from(webhookGroups.values())[0]
};
if (webhooks.expand === true) {
if (webhooks.data.size === 1) {
return [
{
group: Array.from(webhooks.data.keys())[0],
items: Array.from(webhooks.data.values())[0]
}
];
}
return [
{
group: 'Webhooks',
items: Array.from(webhooks.data).map(([groupName, eventList]) => ({
group: groupName,
items: eventList
}))
}
];
}
return {
group: 'Webhooks',
items: Array.from(webhookGroups).map(([groupName, eventList]) => ({
group: groupName,
items: eventList
}))
};
return [
{
generate: 'Webhooks',
from: 'webhooks'
}
];
}

private getModelsSection(models: TocModelPage[]): TocGroup | TocGenerated {
if (models.length === 0) {
return {
generate: 'Models',
from: 'models'
};
private getModelsSection(models: Models): (TocGroup | TocGenerated)[] {
if (models.data.length === 0) {
return [];
}
return {
group: 'Models',
items: models
};
if (!models.expand) {
return [
{
generate: 'Models',
from: 'models'
}
];
}
return [
{
group: 'Models',
items: models.data
}
];
}

private transformKeys(obj: any): any {
Expand Down