diff --git a/src/actions/portal/toc/new-toc.ts b/src/actions/portal/toc/new-toc.ts index cae2d3543..dfe01f8d7 100644 --- a/src/actions/portal/toc/new-toc.ts +++ b/src/actions/portal/toc/new-toc.ts @@ -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))) { @@ -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); @@ -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); diff --git a/src/application/portal/toc/toc-structure-generator.ts b/src/application/portal/toc/toc-structure-generator.ts index 954b09d5b..cc497eecb 100644 --- a/src/application/portal/toc/toc-structure-generator.ts +++ b/src/application/portal/toc/toc-structure-generator.ts @@ -12,14 +12,36 @@ import { // TODO: Refactor +type Endpoints = { + data: Map; + expand: boolean; +}; + +type Webhooks = { + data: Map; + expand: boolean; +}; + +type Callbacks = { + data: Map; + expand: boolean; +}; + +type Models = { + data: TocModelPage[]; + expand: boolean; +}; + export class TocStructureGenerator { createTocStructure( - endpointGroups: Map, - models: TocModelPage[], - webhookGroups: Map, - callbackGroups: Map, + endpoints: Endpoints, + models: Models, + webhooks: Webhooks, + callbacks: Callbacks, contentGroups: TocGroup[] = [] ): Toc { + const events = [...this.getCallbacksSection(callbacks), ...this.getWebhooksSection(webhooks)]; + return { toc: [ { @@ -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), { generate: 'SDK Infrastructure', from: 'sdk-infra' @@ -54,8 +80,8 @@ export class TocStructureGenerator { }); } - private getEndpointsSection(endpointGroups: Map): 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' @@ -63,7 +89,7 @@ export class TocStructureGenerator { } return { group: 'API Endpoints', - items: Array.from(endpointGroups).map(([groupName, endpoints]) => ({ + items: Array.from(endpoints.data).map(([groupName, endpoints]) => ({ group: groupName, items: [ { @@ -77,61 +103,86 @@ export class TocStructureGenerator { }; } - private getCallbacksSection(callbackGroups: Map): 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): 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 {