-
Notifications
You must be signed in to change notification settings - Fork 2
feat: added document filtering during export depending on shareability status #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
df13724
fedd3ec
7c5a2bd
1b2575f
dc0ea44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,17 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { BuilderStrategy, BuildResult, BuildTypeContexts, ExportDocument, ExportVersionBuildConfig } from '../types' | ||
| import { getDocumentTitle, getSplittedVersionKey } from '../utils' | ||
| import { | ||
| BuilderStrategy, | ||
| BuildResult, | ||
| BuildTypeContexts, | ||
| ExportDocument, | ||
| ExportVersionBuildConfig, | ||
| ResolvedVersionDocument, | ||
| SHAREABILITY_STATUS_UNKNOWN, | ||
| ShareabilityStatus, | ||
| } from '../types' | ||
| import { getDocumentTitle, getFileExtension, getSplittedVersionKey } from '../utils' | ||
| import { | ||
| createCommonStaticExportDocuments, | ||
| createSingleFileExportName, | ||
|
|
@@ -27,44 +36,65 @@ import { FILE_FORMAT_HTML, FILE_FORMAT_JSON } from '../consts' | |
|
|
||
| export class ExportVersionStrategy implements BuilderStrategy { | ||
| async execute(config: ExportVersionBuildConfig, buildResult: BuildResult, contexts: BuildTypeContexts): Promise<BuildResult> { | ||
| const { versionDocumentsResolver } = contexts.builderContext(config) | ||
| const { packageId, version: versionWithRevision } = config | ||
| const { documents } = await versionDocumentsResolver(versionWithRevision, packageId) ?? { documents: [] } | ||
|
|
||
| const documentsToExport = filterDocumentsByShareabilityStatus(documents, config) | ||
|
|
||
| const isSingleNonRestDocument = documentsToExport.length === 1 && !isRestDocument(documentsToExport[0]) | ||
|
|
||
| switch (config.format) { | ||
| case FILE_FORMAT_HTML: | ||
| await exportToHTML(config, buildResult, contexts) | ||
| await exportToHTML(config, buildResult, contexts, documentsToExport) | ||
| break | ||
| default: | ||
| await defaultExport(config, buildResult, contexts) | ||
| await defaultExport(config, buildResult, contexts, documentsToExport) | ||
| break | ||
| } | ||
|
|
||
| const { packageId, version: versionWithRevision, format = FILE_FORMAT_JSON } = config | ||
| const { format = FILE_FORMAT_JSON } = config | ||
| const [version] = getSplittedVersionKey(versionWithRevision) | ||
| if (buildResult.exportDocuments.length > 1) { | ||
| buildResult.exportFileName = `${packageId}_${version}.zip` | ||
| return buildResult | ||
| } | ||
|
|
||
| buildResult.exportFileName = createSingleFileExportName(packageId, version, getDocumentTitle(buildResult.exportDocuments[0].filename), format) | ||
| const singleExportDocument = buildResult.exportDocuments[0] | ||
| const singleExportDocumentExtension = isSingleNonRestDocument | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add tests to this logic? |
||
| ? getFileExtension(singleExportDocument.filename) | ||
| : format | ||
|
|
||
| buildResult.exportFileName = createSingleFileExportName( | ||
| packageId, | ||
| version, | ||
| getDocumentTitle(singleExportDocument.filename), | ||
| singleExportDocumentExtension, | ||
| ) | ||
| return buildResult | ||
| } | ||
| } | ||
|
|
||
| async function exportToHTML(config: ExportVersionBuildConfig, buildResult: BuildResult, contexts: BuildTypeContexts): Promise<void> { | ||
| async function exportToHTML( | ||
| config: ExportVersionBuildConfig, | ||
| buildResult: BuildResult, | ||
| contexts: BuildTypeContexts, | ||
| documentsToExport: ReadonlyArray<ResolvedVersionDocument>, | ||
| ): Promise<void> { | ||
| const { | ||
| versionDocumentsResolver, | ||
| rawDocumentResolver, | ||
| templateResolver, | ||
| packageResolver, | ||
| apiBuilders, | ||
| packageResolver, | ||
| } = contexts.builderContext(config) | ||
| const { packageId, version: versionWithRevision, format, allowedOasExtensions } = config | ||
| const [version] = getSplittedVersionKey(versionWithRevision) | ||
| const { name: packageName } = await packageResolver(packageId) | ||
| const { documents } = await versionDocumentsResolver(versionWithRevision, packageId) ?? { documents: [] } | ||
|
|
||
| const generatedHtmlExportDocuments: ExportDocument[] = [] | ||
| const restDocuments = documents.filter(isRestDocument) | ||
| const restDocuments = documentsToExport.filter(isRestDocument) | ||
| const shouldAddIndexPage = restDocuments.length > 0 | ||
| const transformedDocuments = await Promise.all(documents.map(async document => { | ||
| const transformedDocuments = await Promise.all(documentsToExport.map(async document => { | ||
| const { createExportDocument } = apiBuilders.find(({ types }) => types.includes(document.type)) || unknownApiBuilder | ||
| const file = await rawDocumentResolver(versionWithRevision, packageId, document.slug) | ||
| return await createExportDocument?.(file.name, await file.text(), format, packageName, version, templateResolver, allowedOasExtensions, generatedHtmlExportDocuments, shouldAddIndexPage) ?? createUnknownExportDocument(file.name, file) | ||
|
|
@@ -81,9 +111,13 @@ async function exportToHTML(config: ExportVersionBuildConfig, buildResult: Build | |
| } | ||
| } | ||
|
|
||
| async function defaultExport(config: ExportVersionBuildConfig, buildResult: BuildResult, contexts: BuildTypeContexts): Promise<void> { | ||
| async function defaultExport( | ||
| config: ExportVersionBuildConfig, | ||
| buildResult: BuildResult, | ||
| contexts: BuildTypeContexts, | ||
| documentsToExport: ReadonlyArray<ResolvedVersionDocument>, | ||
| ): Promise<void> { | ||
| const { | ||
| versionDocumentsResolver, | ||
| rawDocumentResolver, | ||
| templateResolver, | ||
| packageResolver, | ||
|
|
@@ -92,13 +126,26 @@ async function defaultExport(config: ExportVersionBuildConfig, buildResult: Buil | |
| const { packageId, version: versionWithRevision, format, allowedOasExtensions } = config | ||
| const [version] = getSplittedVersionKey(versionWithRevision) | ||
| const { name: packageName } = await packageResolver(packageId) | ||
| const { documents } = await versionDocumentsResolver(versionWithRevision, packageId) ?? { documents: [] } | ||
|
|
||
| const transformedDocuments = await Promise.all(documents.map(async document => { | ||
| const transformedDocuments = await Promise.all(documentsToExport.map(async document => { | ||
| const { createExportDocument } = apiBuilders.find(({ types }) => types.includes(document.type)) || unknownApiBuilder | ||
| const file = await rawDocumentResolver(versionWithRevision, packageId, document.slug) | ||
| return await createExportDocument?.(file.name, await file.text(), format, packageName, version, templateResolver, allowedOasExtensions) ?? createUnknownExportDocument(file.name, file) | ||
| })) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. callback on lines 97-101 and 130-134 seems the same |
||
|
|
||
| buildResult.exportDocuments.push(...transformedDocuments) | ||
| } | ||
|
|
||
| function filterDocumentsByShareabilityStatus( | ||
| documents: ReadonlyArray<ResolvedVersionDocument>, | ||
| config: ExportVersionBuildConfig, | ||
| ): ReadonlyArray<ResolvedVersionDocument> { | ||
| const { allowedShareabilityStatuses } = config | ||
|
|
||
| if (!allowedShareabilityStatuses || allowedShareabilityStatuses.length === 0) { | ||
| return documents | ||
| } | ||
| const allowedSet = new Set<ShareabilityStatus>(allowedShareabilityStatuses) | ||
|
|
||
| return documents.filter(doc => allowedSet.has(doc.shareabilityStatus ?? SHAREABILITY_STATUS_UNKNOWN)) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this flag used once, move it to line 63