-
-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathComponentProvider.ts
More file actions
99 lines (92 loc) · 3.32 KB
/
Copy pathComponentProvider.ts
File metadata and controls
99 lines (92 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable security/detect-object-injection */
import type { AsyncAPIDocumentInterface } from '@asyncapi/parser'
import { OptimizableComponentGroup, OptimizableComponent } from 'types'
import { JSONPath } from 'jsonpath-plus'
import chain from 'lodash/chain'
export const toLodashPath = (jsonPointer: string): string => {
// Remove leading slash if present
if (jsonPointer.startsWith('/')) {
jsonPointer = jsonPointer.slice(1)
}
// Split the JSON Pointer into tokens
const tokens = jsonPointer.split('/')
// Unescape the special characters and transform into Lodash path
return tokens
.map((token) => {
// Replace tilde representations
token = token.replace(/~1/g, '/')
token = token.replace(/~0/g, '~')
// Check if token can be treated as an array index (non-negative integer)
if (/^\d+$/.test(token)) {
return `[${token}]`
}
// For nested properties, use dot notation
return token
})
.join('.')
}
export const parseComponentsFromPath = (
asyncAPIDocument: AsyncAPIDocumentInterface,
paths: string[]
): OptimizableComponent[] => {
return chain(paths)
.map((path) => {
return JSONPath({
resultType: 'all',
json: asyncAPIDocument.json(),
path,
})
})
.flatten()
.map((component) => ({
path: toLodashPath(component.path),
component: component.value,
}))
.value()
}
export const getOptimizableComponents = (
asyncAPIDocument: AsyncAPIDocumentInterface,
): OptimizableComponentGroup[] => {
const optimizeableComponents: OptimizableComponentGroup[] = []
const getAllComponents = (type: string) => {
// @ts-ignore
if (typeof asyncAPIDocument[type] !== 'function') return []
// @ts-ignore
return asyncAPIDocument[type]().all().concat(asyncAPIDocument.components()[type]().all())
}
const optimizableComponents = {
servers: getAllComponents('servers'),
messages: getAllComponents('messages'),
channels: getAllComponents('channels'),
schemas: getAllComponents('schemas'),
operations: getAllComponents('operations'),
securitySchemes: getAllComponents('securitySchemes'),
serverVariables: getAllComponents('serverVariables'),
parameters: getAllComponents('parameters'),
correlationIds: getAllComponents('correlationIds'),
replies: getAllComponents('replies'),
replyAddresses: getAllComponents('replyAddresses'),
externalDocs: getAllComponents('externalDocs'),
tags: getAllComponents('tags'),
operationTraits: getAllComponents('operationTraits'),
messageTraits: getAllComponents('messageTraits'),
serverBindings: getAllComponents('serverBindings'),
channelBindings: getAllComponents('channelBindings'),
operationBindings: getAllComponents('operationBindings'),
messageBindings: getAllComponents('messageBindings'),
}
for (const [type, components] of Object.entries(optimizableComponents)) {
if (components.length === 0) continue
optimizeableComponents.push({
type,
components: components.map((component: any) => ({
path: toLodashPath(component.jsonPath()),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
component: component.json(),
})),
})
}
return optimizeableComponents
}