1 import type { CustomFieldModelFormMap } from "@medusajs/admin-shared"
2 import { z, ZodFirstPartySchemaTypes } from "zod"
3 import {
4 CustomFieldConfig,
5 CustomFormField,
6 LayoutConfig,
7 RouteConfig,
8 WidgetConfig,
9 } from "./types"
10
11 function createConfigHelper<TConfig>(config: TConfig): TConfig {
12 return {
13 ...config,
14 /**
15 * This property is required to allow the config to be exported,
16 * while still allowing HMR to work correctly.
17 *
18 * It tricks Fast Refresh into thinking that the config is a React component,
19 * which allows it to be updated without a full page reload.
20 */
21 $$typeof: Symbol.for("react.memo"),
22 }
23 }
24
25 /**
26 * Define a widget configuration.
27 *
28 * @param config The widget configuration.
29 * @returns The widget configuration.
30 */
31 export function defineWidgetConfig(config: WidgetConfig) {
32 return createConfigHelper(config)
33 }
34
35 /**
36 * Define a route configuration.
37 *
38 * @param config The route configuration.
39 * @returns The route configuration.
40 */
41 export function defineRouteConfig(config: RouteConfig) {
42 return createConfigHelper(config)
43 }
44
45 /**
46 * Define a custom layout configuration.
47 *
48 * @param config The layout configuration.
49 * @returns The layout configuration.
50 */
51 export function defineLayoutConfig(config: LayoutConfig) {
52 return createConfigHelper(config)
53 }
54
55 /**
56 * Define a custom fields configuration.
57 *
58 * @param config The custom fields configuration.
59 * @returns The custom fields configuration.
60 *
61 * @experimental This API is experimental and may change in the future.
62 */
63 export function unstable_defineCustomFieldsConfig<
64 TModel extends keyof CustomFieldModelFormMap
65 >(config: CustomFieldConfig<TModel>) {
66 return createConfigHelper(config)
67 }
68
69 /**
70 * Creates a type-safe form builder.
71 *
72 * @returns The form helper.
73 *
74 * @example
75 * ```ts
76 * import { unstable_createFormHelper, unstable_defineCustomFieldsConfig } from "@medusajs/admin-sdk"
77 * import type { HttpTypes } from "@medusajs/types"
78 * import type { Brand } from "../../types/brand"
79 *
80 * type ExtendedProduct = HttpTypes.Product & {
81 * brand: Brand | null
82 * }
83 *
84 * const form = unstable_createFormHelper<ExtendedProduct>()
85 *
86 * export default unstable_defineCustomFieldsConfig({
87 * entryPoint: "product",
88 * link: "brand",
89 * forms: [{
90 * form: "create",
91 * fields: {
92 * brand_id: form.define({
93 * rules: form.string().nullish(),
94 * defaultValue: "",
95 * }),
96 * }
97 * }]
98 * })
99 * ```
100 *
101 * @experimental This API is experimental and may change in the future.
102 */
103 export function unstable_createFormHelper<TData>() {
104 return {
105 /**
106 * Define a custom form field.
107 *
108 * @param field The field to define.
109 * @returns The field.
110 */
111 define: <T extends ZodFirstPartySchemaTypes>(
112 field: Omit<CustomFormField<TData, T>, "validation"> & { validation: T }
113 ): CustomFormField<TData, T> => {
114 return field as CustomFormField<TData, T>
115 },
116 string: () => z.string(),
117 number: () => z.number(),
118 boolean: () => z.boolean(),
119 date: () => z.date(),
120 array: z.array,
121 object: z.object,
122 null: () => z.null(),
123 nullable: z.nullable,
124 undefined: () => z.undefined(),
125 coerce: z.coerce,
126 }
127 }