1 /**
2 * Render mode mapping utilities for column generation.
3 * Maps field names and GraphQL types to appropriate render modes.
4 */
5 import { GraphQLNamedType, isEnumType } from "@medusajs/framework/utils"
6
7 /**
8 * Common render mode type that can be extended by plugins.
9 */
10 export type RenderMode =
11 | "text"
12 | "number"
13 | "currency"
14 | "date"
15 | "datetime"
16 | "boolean"
17 | "status"
18 | "badges"
19 | "count"
20 | "id"
21 | "display_id"
22 | "email"
23 | "phone"
24 | "url"
25 | "image"
26 | "json"
27 | "country_code"
28 | "address"
29 | "name"
30 | "product_info"
31 | (string & {})
32
33 /**
34 * Known filter operators. Mirrors the operators the generator emits per data
35 * type in {@link ./filter-rules.ts}.
36 */
37 export type FilterOperator =
38 | "eq"
39 | "ne"
40 | "gt"
41 | "gte"
42 | "lt"
43 | "lte"
44 | "contains"
45 | "startsWith"
46 | "endsWith"
47 | "in"
48 | "nin"
49 | (string & {})
50
51 /**
52 * Known computed column categories used for grouping in the UI.
53 */
54 export type ColumnCategory =
55 | "relationship"
56 | "metadata"
57 | "computed"
58 | "metric"
59 | (string & {})
60
61 /**
62 * Maps GraphQL scalar types to render modes.
63 */
64 const TYPE_TO_RENDER_MODE: Record<string, RenderMode> = {
65 // Primitive types
66 boolean: "boolean",
67 text: "text",
68 number: "number",
69
70 // Medusa scalar types
71 dateTime: "datetime",
72 json: "json",
73 id: "id",
74
75 // GraphQL scalars
76 String: "text",
77 Int: "number",
78 Float: "number",
79 Boolean: "boolean",
80 DateTime: "datetime",
81 JSON: "json",
82 ID: "id",
83 }
84
85 /**
86 * Field name patterns that override the default render mode.
87 */
88 interface FieldPatternOverride {
89 pattern: RegExp
90 renderMode: RenderMode
91 semanticType: string
92 }
93
94 const FIELD_PATTERN_OVERRIDES: FieldPatternOverride[] = [
95 // Timestamps
96 { pattern: /_at$/, renderMode: "datetime", semanticType: "timestamp" },
97 {
98 pattern: /^created_at$/,
99 renderMode: "datetime",
100 semanticType: "timestamp",
101 },
102 {
103 pattern: /^updated_at$/,
104 renderMode: "datetime",
105 semanticType: "timestamp",
106 },
107 {
108 pattern: /^deleted_at$/,
109 renderMode: "datetime",
110 semanticType: "timestamp",
111 },
112 { pattern: /_date$/, renderMode: "date", semanticType: "date" },
113
114 // Currency/amounts
115 { pattern: /total$/, renderMode: "currency", semanticType: "currency" },
116 { pattern: /amount$/, renderMode: "currency", semanticType: "currency" },
117 { pattern: /price$/, renderMode: "currency", semanticType: "currency" },
118 { pattern: /^subtotal$/, renderMode: "currency", semanticType: "currency" },
119 { pattern: /^tax_total$/, renderMode: "currency", semanticType: "currency" },
120 {
121 pattern: /^shipping_total$/,
122 renderMode: "currency",
123 semanticType: "currency",
124 },
125 {
126 pattern: /^discount_total$/,
127 renderMode: "currency",
128 semanticType: "currency",
129 },
130
131 // Status fields
132 { pattern: /status$/, renderMode: "status", semanticType: "status" },
133 { pattern: /^state$/, renderMode: "status", semanticType: "status" },
134 {
135 pattern: /^payment_status$/,
136 renderMode: "status",
137 semanticType: "status",
138 },
139 {
140 pattern: /^fulfillment_status$/,
141 renderMode: "status",
142 semanticType: "status",
143 },
144
145 // Contact info
146 { pattern: /^email$/, renderMode: "email", semanticType: "contact" },
147 { pattern: /_email$/, renderMode: "email", semanticType: "contact" },
148 { pattern: /^phone$/, renderMode: "phone", semanticType: "contact" },
149 { pattern: /_phone$/, renderMode: "phone", semanticType: "contact" },
150
151 // Location
152 {
153 pattern: /country_code$/,
154 renderMode: "country_code",
155 semanticType: "location",
156 },
157 {
158 pattern: /^currency_code$/,
159 renderMode: "text",
160 semanticType: "currency_code",
161 },
162
163 // URLs and images
164 { pattern: /^url$/, renderMode: "url", semanticType: "url" },
165 { pattern: /_url$/, renderMode: "url", semanticType: "url" },
166 { pattern: /^thumbnail$/, renderMode: "image", semanticType: "image" },
167 { pattern: /^avatar$/, renderMode: "image", semanticType: "image" },
168 { pattern: /_image$/, renderMode: "image", semanticType: "image" },
169
170 // Identifiers
171 { pattern: /^id$/, renderMode: "id", semanticType: "identifier" },
172 {
173 pattern: /^display_id$/,
174 renderMode: "display_id",
175 semanticType: "identifier",
176 },
177 {
178 pattern: /^custom_display_id$/,
179 renderMode: "display_id",
180 semanticType: "identifier",
181 },
182 { pattern: /_id$/, renderMode: "id", semanticType: "identifier" },
183 { pattern: /^handle$/, renderMode: "text", semanticType: "identifier" },
184 { pattern: /^code$/, renderMode: "text", semanticType: "identifier" },
185
186 // Counts
187 {
188 pattern: /^(count|.*_count)$/,
189 renderMode: "number",
190 semanticType: "count",
191 },
192 { pattern: /quantity$/, renderMode: "number", semanticType: "count" },
193
194 // Booleans
195 { pattern: /^is_/, renderMode: "boolean", semanticType: "boolean" },
196 { pattern: /^has_/, renderMode: "boolean", semanticType: "boolean" },
197 { pattern: /^can_/, renderMode: "boolean", semanticType: "boolean" },
198
199 // Metadata
200 { pattern: /^metadata$/, renderMode: "json", semanticType: "metadata" },
201 ]
202
203 /**
204 * Infer the render mode from a field name and GraphQL type.
205 */
206 export function inferRenderMode(
207 fieldName: string,
208 graphqlType: GraphQLNamedType
209 ): { renderMode: RenderMode; semanticType: string } {
210 // Check field name patterns first (more specific)
211 for (const override of FIELD_PATTERN_OVERRIDES) {
212 if (override.pattern.test(fieldName)) {
213 return {
214 renderMode: override.renderMode,
215 semanticType: override.semanticType,
216 }
217 }
218 }
219
220 if (isEnumType(graphqlType)) {
221 return {
222 renderMode: "status",
223 semanticType: "enum",
224 }
225 }
226
227 const graphqlTypeName = graphqlType.name
228
229 // Fall back to type mapping
230 if (graphqlTypeName && TYPE_TO_RENDER_MODE[graphqlTypeName]) {
231 return {
232 renderMode: TYPE_TO_RENDER_MODE[graphqlTypeName],
233 semanticType: graphqlTypeName.toLowerCase(),
234 }
235 }
236
237 // Default to text
238 return {
239 renderMode: "text",
240 semanticType: "string",
241 }
242 }
243
244 /**
245 * Column data type as used in AdminColumn.
246 */
247 export type ColumnDataType =
248 | "string"
249 | "number"
250 | "boolean"
251 | "date"
252 | "currency"
253 | "enum"
254 | "object"
255 | (string & {})
256 /**
257 * Map a data type to its default render mode.
258 */
259 export function dataTypeToRenderMode(dataType: ColumnDataType): RenderMode {
260 switch (dataType) {
261 case "string":
262 return "text"
263 case "number":
264 return "number"
265 case "boolean":
266 return "boolean"
267 case "date":
268 return "datetime"
269 case "currency":
270 return "currency"
271 case "enum":
272 return "status"
273 case "object":
274 return "json"
275 default:
276 return "text"
277 }
278 }
279
280 /**
281 * Infer the data type from a GraphQL scalar type name.
282 */
283 export function inferDataType(
284 graphqlType: GraphQLNamedType,
285 fieldName: string
286 ): ColumnDataType {
287 const graphqlTypeName = graphqlType.name
288
289 // Check field patterns first
290 if (
291 /_at$/.test(fieldName) ||
292 /^created_at$|^updated_at$|^deleted_at$/.test(fieldName)
293 ) {
294 return "date"
295 }
296 if (/total$|amount$|price$/.test(fieldName)) {
297 return "currency"
298 }
299 if (isEnumType(graphqlType)) {
300 return "enum"
301 }
302 if (/^is_|^has_|^can_/.test(fieldName)) {
303 return "boolean"
304 }
305 if (/^metadata$/.test(fieldName)) {
306 return "object"
307 }
308
309 // Map GraphQL types
310 switch (graphqlTypeName) {
311 case "String":
312 case "ID":
313 return "string"
314 case "Int":
315 case "Float":
316 return "number"
317 case "Boolean":
318 return "boolean"
319 case "DateTime":
320 return "date"
321 case "JSON":
322 return "object"
323 default:
324 return "string"
325 }
326 }