Skip to content

Commit 3cdf341

Browse files
committed
Merge branch 'release'
2 parents 0c852cf + 723b2a2 commit 3cdf341

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2348
-265
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ temp
55
coverage
66
.idea
77
.DS_store
8-
export
98
dist/
109
*.iml
1110
test/versions/

package-lock.json

Lines changed: 48 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netcracker/qubership-apihub-api-processor",
3-
"version": "4.0.1",
3+
"version": "4.1.0",
44
"description": "",
55
"license": "Apache-2.0",
66
"module": "dist/esm/src/index.js",
@@ -31,10 +31,10 @@
3131
"update-lock-file": "update-lock-file @netcracker"
3232
},
3333
"dependencies": {
34-
"@netcracker/qubership-apihub-api-diff": "2.0.1",
35-
"@netcracker/qubership-apihub-api-unifier": "2.0.0",
36-
"@netcracker/qubership-apihub-json-crawl": "1.0.4",
34+
"@netcracker/qubership-apihub-api-diff": "2.0.2",
35+
"@netcracker/qubership-apihub-api-unifier": "2.1.0",
3736
"@netcracker/qubership-apihub-graphapi": "1.0.8",
37+
"@netcracker/qubership-apihub-json-crawl": "1.0.4",
3838
"adm-zip": "0.5.10",
3939
"ajv": "^8.12.0",
4040
"ajv-formats": "^2.1.1",
@@ -53,6 +53,7 @@
5353
"@types/adm-zip": "0.5.7",
5454
"@types/jest": "^29.5.12",
5555
"@types/js-yaml": "^4.0.5",
56+
"@types/mime-types": "^3.0.0",
5657
"@types/node": "^18.0.0",
5758
"@types/object-hash": "^3.0.2",
5859
"@types/slug": "^5.0.3",
@@ -64,6 +65,7 @@
6465
"eslint-plugin-sort-exports": "^0.8.0",
6566
"jest": "^29.7.0",
6667
"jest-extended": "^4.0.2",
68+
"mime-types": "^3.0.1",
6769
"openapi-types": "^12.1.0",
6870
"ts-jest": "29.1.2",
6971
"ts-node": "^10.9.1",
@@ -80,4 +82,4 @@
8082
"engines": {
8183
"node": ">=18.0.0"
8284
}
83-
}
85+
}

src/apitypes/rest/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { OpenAPIV3 } from 'openapi-types'
1818

19-
import { buildRestDocument, dumpRestDocument } from './rest.document'
19+
import { buildRestDocument, createRestExportDocument, dumpRestDocument } from './rest.document'
2020
import { REST_API_TYPE, REST_DOCUMENT_TYPE } from './rest.consts'
2121
import { compareRestOperationsData } from './rest.changes'
2222
import { buildRestOperations, createNormalizedOperationId } from './rest.operations'
@@ -35,4 +35,5 @@ export const restApiBuilder: ApiBuilder<OpenAPIV3.Document> = {
3535
dumpDocument: dumpRestDocument,
3636
compareOperationsData: compareRestOperationsData,
3737
createNormalizedOperationId: createNormalizedOperationId,
38+
createExportDocument: createRestExportDocument,
3839
}

src/apitypes/rest/rest.consts.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import { FILE_FORMAT_JSON, FILE_FORMAT_YAML, FILE_FORMAT_YML } from '../../consts'
18+
import { KeyOfConstType, ResolvedVersionDocument, ZippableDocument } from '../../types'
19+
import { TEXT_DOCUMENT_TYPE, TextDocumentType } from '../text'
1820

1921
export const REST_API_TYPE = 'rest' as const
2022

@@ -33,12 +35,22 @@ export const REST_DOCUMENT_TYPE = {
3335
SWAGGER: 'openapi-2-0',
3436
} as const
3537

38+
export type RestDocumentType = KeyOfConstType<typeof REST_DOCUMENT_TYPE>
39+
3640
export const REST_FILE_FORMAT = {
3741
YAML: FILE_FORMAT_YAML,
3842
YML: FILE_FORMAT_YML,
3943
JSON: FILE_FORMAT_JSON,
40-
}
44+
} as const
4145

4246
export const REST_KIND_KEY = 'x-api-kind'
4347

4448
export const DEPRECATED_META_KEY = 'x-deprecated-meta'
49+
50+
export function isRestDocument(document: ZippableDocument | ResolvedVersionDocument): boolean {
51+
return Object.values(REST_DOCUMENT_TYPE).includes(document.type as RestDocumentType)
52+
}
53+
54+
export function isTextDocument(document: ResolvedVersionDocument): boolean {
55+
return Object.values(TEXT_DOCUMENT_TYPE).includes(document.type as TextDocumentType)
56+
}

src/apitypes/rest/rest.document.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,24 @@ import { convertObj } from 'swagger2openapi'
2020
import { REST_DOCUMENT_TYPE, REST_KIND_KEY } from './rest.consts'
2121
import type { RestDocumentInfo } from './rest.types'
2222

23-
import { DocumentBuilder, DocumentDumper, YAML_EXPORT_GROUP_FORMAT } from '../../types'
24-
import { FILE_FORMAT } from '../../consts'
25-
import { createBundlingErrorHandler, getBundledFileDataWithDependencies, getDocumentTitle } from '../../utils'
26-
import YAML from 'js-yaml'
23+
import {
24+
_TemplateResolver,
25+
DocumentBuilder,
26+
DocumentDumper,
27+
ExportDocument,
28+
ExportFormat,
29+
} from '../../types'
30+
import { FILE_FORMAT, FILE_FORMAT_HTML, FILE_FORMAT_JSON } from '../../consts'
31+
import {
32+
createBundlingErrorHandler,
33+
EXPORT_FORMAT_TO_FILE_FORMAT,
34+
getBundledFileDataWithDependencies,
35+
getDocumentTitle,
36+
} from '../../utils'
37+
import { dump } from './rest.utils'
38+
import { generateHtmlPage } from '../../utils/export'
39+
import { removeOasExtensions } from '../../utils/removeOasExtensions'
40+
import { OpenApiExtensionKey } from '@netcracker/qubership-apihub-api-unifier'
2741

2842
const openApiDocumentMeta = (data: OpenAPIV3.Document): RestDocumentInfo => {
2943
if (typeof data !== 'object' || !data) {
@@ -99,8 +113,41 @@ export const buildRestDocument: DocumentBuilder<OpenAPIV3.Document> = async (par
99113
}
100114

101115
export const dumpRestDocument: DocumentDumper<OpenAPIV3.Document> = (document, format) => {
102-
if (format === YAML_EXPORT_GROUP_FORMAT) {
103-
return new Blob([YAML.dump(document.data)], { type: 'application/yaml' })
116+
return new Blob(...dump(document.data, format ?? FILE_FORMAT_JSON))
117+
}
118+
119+
export async function createRestExportDocument(
120+
filename: string,
121+
data: string,
122+
format: ExportFormat,
123+
packageName: string,
124+
version: string,
125+
templateResolver: _TemplateResolver,
126+
allowedOasExtensions?: OpenApiExtensionKey[],
127+
generatedHtmlExportDocuments?: ExportDocument[],
128+
addBackLink?: boolean,
129+
): Promise<ExportDocument> {
130+
const exportFilename = `${getDocumentTitle(filename)}.${format}`
131+
const [[document], blobProperties] = dump(removeOasExtensions(JSON.parse(data), allowedOasExtensions), EXPORT_FORMAT_TO_FILE_FORMAT.get(format)!)
132+
133+
if (format === FILE_FORMAT_HTML) {
134+
const htmlExportDocument = {
135+
data: await generateHtmlPage(
136+
document,
137+
getDocumentTitle(filename),
138+
packageName,
139+
version,
140+
templateResolver,
141+
addBackLink,
142+
),
143+
filename: exportFilename,
144+
}
145+
generatedHtmlExportDocuments?.push(htmlExportDocument)
146+
return htmlExportDocument
147+
}
148+
149+
return {
150+
data: new Blob([document], blobProperties),
151+
filename: exportFilename,
104152
}
105-
return new Blob([JSON.stringify(document.data, undefined, 2)], { type: 'application/json' })
106153
}

src/apitypes/rest/rest.parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const parseRestFile = async (fileId: string, source: Blob): Promise<TextF
5656
kind: FILE_KIND.TEXT,
5757
}
5858
}
59-
} else if ([REST_FILE_FORMAT.YAML, REST_FILE_FORMAT.YML].includes(extension) || !extension) {
59+
} else if (([REST_FILE_FORMAT.YAML, REST_FILE_FORMAT.YML] as string[]).includes(extension) || !extension) {
6060
if (/\s*?'?"?openapi'?"?\s*?:\s*?\|?\s*'?"?3\.[01]\..+?'?"?/g.test(sourceString)) {
6161
const data = YAML.load(sourceString) as OpenAPIV3.Document
6262

src/apitypes/rest/rest.utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { OpenAPIV3 } from 'openapi-types'
1818
import { CustomTags } from './rest.types'
1919
import { API_AUDIENCE_EXTERNAL, API_AUDIENCE_INTERNAL, API_AUDIENCE_UNKNOWN, ApiAudience } from '../../types'
2020
import { isObject } from '@netcracker/qubership-apihub-json-crawl'
21-
import { CUSTOM_PARAMETER_API_AUDIENCE } from '../../consts'
21+
import { CUSTOM_PARAMETER_API_AUDIENCE, FILE_FORMAT_JSON, FILE_FORMAT_YAML } from '../../consts'
22+
import YAML from 'js-yaml'
2223

2324
export const getOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): string => {
2425
if (!Array.isArray(servers) || !servers.length) { return '' }
@@ -65,3 +66,15 @@ export const resolveApiAudience = (info: unknown): ApiAudience => {
6566
apiAudience = [API_AUDIENCE_INTERNAL, API_AUDIENCE_EXTERNAL].includes(apiAudience) ? apiAudience : API_AUDIENCE_UNKNOWN
6667
return apiAudience
6768
}
69+
70+
type TextBlobConstructorParameters = [[string], BlobPropertyBag]
71+
72+
export const dump = (value: unknown, format: typeof FILE_FORMAT_YAML | typeof FILE_FORMAT_JSON): TextBlobConstructorParameters => {
73+
if (format === FILE_FORMAT_YAML) {
74+
return [[YAML.dump(value)], { type: 'application/yaml' }]
75+
}
76+
if (format === FILE_FORMAT_JSON) {
77+
return [[JSON.stringify(value, undefined, 2)], { type: 'application/json' }]
78+
}
79+
throw new Error(`Unsupported format: ${format}`)
80+
}

src/apitypes/text/text.consts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { KeyOfConstType } from '../../types'
1617

1718
export const TEXT_API_TYPE = 'text' as const
1819

1920
export const TEXT_DOCUMENT_TYPE = {
2021
MARKDOWN: 'markdown',
2122
} as const
2223

24+
export type TextDocumentType = KeyOfConstType<typeof TEXT_DOCUMENT_TYPE>
25+
2326
export const TEXT_FILE_FORMAT = {
2427
MD: 'md',
2528
} as const

src/apitypes/unknown/unknown.document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { BuildConfigFile, DocumentBuilder, DocumentDumper, File, VersionDocument } from '../../types'
17+
import { BuildConfigFile, DocumentBuilder, DocumentDumper, SourceFile, VersionDocument } from '../../types'
1818
import { createBundlingErrorHandler, getBundledFileDataWithDependencies, getDocumentTitle } from '../../utils'
1919
import { FILE_FORMAT } from '../../consts'
2020

@@ -57,7 +57,7 @@ export const buildUnknownDocument: DocumentBuilder<string> = async (parsedFile,
5757
}
5858
}
5959

60-
export const buildBinaryDocument: (parsedFile: File, file: BuildConfigFile) => Promise<VersionDocument> = async (parsedFile, file) => {
60+
export const buildBinaryDocument: (parsedFile: SourceFile, file: BuildConfigFile) => Promise<VersionDocument> = async (parsedFile, file) => {
6161
const { fileId, slug = '', publish, ...metadata } = file
6262
const { type, format, source } = parsedFile
6363

0 commit comments

Comments
 (0)