|
1
|
import { |
|
2
|
CustomFieldModel, |
|
3
|
isValidCustomFieldModel, |
|
4
|
} from "@medusajs/admin-shared" |
|
5
|
import { |
|
6
|
ExportDefaultDeclaration, |
|
7
|
isCallExpression, |
|
8
|
isIdentifier, |
|
9
|
isObjectExpression, |
|
10
|
isObjectProperty, |
|
11
|
isStringLiteral, |
|
12
|
isTemplateLiteral, |
|
13
|
NodePath, |
|
14
|
ObjectExpression, |
|
15
|
ObjectProperty, |
|
16
|
} from "../babel" |
|
17
|
import { logger } from "../logger" |
|
18
|
|
|
19
|
export function getModel( |
|
20
|
path: NodePath<ExportDefaultDeclaration>, |
|
21
|
file: string |
|
22
|
): CustomFieldModel | null { |
|
23
|
const configArgument = getConfigArgument(path) |
|
24
|
|
|
25
|
if (!configArgument) { |
|
26
|
return null |
|
27
|
} |
|
28
|
|
|
29
|
const modelProperty = configArgument.properties.find( |
|
30
|
(p) => isObjectProperty(p) && isIdentifier(p.key, { name: "model" }) |
|
31
|
) as ObjectProperty | undefined |
|
32
|
|
|
33
|
if (!modelProperty) { |
|
34
|
return null |
|
35
|
} |
|
36
|
|
|
37
|
if (isTemplateLiteral(modelProperty.value)) { |
|
38
|
logger.warn( |
|
39
|
`'model' property cannot be a template literal (e.g. \`product\`).`, |
|
40
|
{ file } |
|
41
|
) |
|
42
|
return null |
|
43
|
} |
|
44
|
|
|
45
|
if (!isStringLiteral(modelProperty.value)) { |
|
46
|
logger.warn( |
|
47
|
`'model' is invalid. The 'model' property must be a string literal, e.g. 'product' or 'customer'.`, |
|
48
|
{ file } |
|
49
|
) |
|
50
|
return null |
|
51
|
} |
|
52
|
|
|
53
|
const model = modelProperty.value.value.trim() |
|
54
|
|
|
55
|
if (!isValidCustomFieldModel(model)) { |
|
56
|
logger.warn( |
|
57
|
`'model' is invalid, received: ${model}. The 'model' property must be set to a valid model, e.g. 'product' or 'customer'.`, |
|
58
|
{ file } |
|
59
|
) |
|
60
|
return null |
|
61
|
} |
|
62
|
|
|
63
|
return model |
|
64
|
} |
|
65
|
|
|
66
|
export function getConfigArgument( |
|
67
|
path: NodePath<ExportDefaultDeclaration> |
|
68
|
): ObjectExpression | null { |
|
69
|
if (!isCallExpression(path.node.declaration)) { |
|
70
|
return null |
|
71
|
} |
|
72
|
|
|
73
|
if ( |
|
74
|
!isIdentifier(path.node.declaration.callee, { |
|
75
|
name: "unstable_defineCustomFieldsConfig", |
|
76
|
}) |
|
77
|
) { |
|
78
|
return null |
|
79
|
} |
|
80
|
|
|
81
|
const configArgument = path.node.declaration.arguments[0] |
|
82
|
|
|
83
|
if (!isObjectExpression(configArgument)) { |
|
84
|
return null |
|
85
|
} |
|
86
|
|
|
87
|
return configArgument |
|
88
|
} |
|
89
|
|
|
90
|
/** |
|
91
|
* Validates that the 'link' property is present in the custom field config. |
|
92
|
* @param path - The NodePath to the export default declaration. |
|
93
|
* @param file - The file path. |
|
94
|
* @returns - True if the 'link' property is present, false otherwise. |
|
95
|
*/ |
|
96
|
export function validateLink( |
|
97
|
path: NodePath<ExportDefaultDeclaration>, |
|
98
|
file: string |
|
99
|
): boolean { |
|
100
|
const configArgument = getConfigArgument(path) |
|
101
|
|
|
102
|
if (!configArgument) { |
|
103
|
return false |
|
104
|
} |
|
105
|
|
|
106
|
const linkProperty = configArgument.properties.find( |
|
107
|
(p) => isObjectProperty(p) && isIdentifier(p.key, { name: "link" }) |
|
108
|
) as ObjectProperty | undefined |
|
109
|
|
|
110
|
if (!linkProperty) { |
|
111
|
logger.warn(`'link' property is missing.`, { file }) |
|
112
|
return false |
|
113
|
} |
|
114
|
|
|
115
|
return true |
|
116
|
} |