1 import {
2 RemoteQueryObjectConfig,
3 RemoteQueryObjectFromStringResult,
4 } from "@medusajs/types"
5 import { isObject } from "./is-object"
6
7 /**
8 * Convert a string fields array to a remote query object
9 * @param config - The configuration object
10 *
11 * @example
12 * const fields = [
13 * "id",
14 * "created_at",
15 * "updated_at",
16 * "deleted_at",
17 * "url",
18 * "metadata",
19 * "tags.id",
20 * "tags.created_at",
21 * "tags.updated_at",
22 * "tags.deleted_at",
23 * "tags.value",
24 * "options.id",
25 * "options.created_at",
26 * "options.updated_at",
27 * "options.deleted_at",
28 * "options.title",
29 * "options.product_id",
30 * "options.metadata",
31 * "options.values.id",
32 * "options.values.created_at",
33 * "options.values.updated_at",
34 * "options.values.deleted_at",
35 * "options.values.value",
36 * "options.values.option_id",
37 * "options.values.variant_id",
38 * "options.values.metadata",
39 * ]
40 *
41 * const remoteQueryObject = remoteQueryObjectFromString({
42 * entryPoint: "product",
43 * variables: {},
44 * fields,
45 * })
46 *
47 * console.log(remoteQueryObject)
48 * // {
49 * // product: {
50 * // __args: {},
51 * // fields: [
52 * // "id",
53 * // "created_at",
54 * // "updated_at",
55 * // "deleted_at",
56 * // "url",
57 * // "metadata",
58 * // ],
59 * //
60 * // tags: {
61 * // fields: ["id", "created_at", "updated_at", "deleted_at", "value"],
62 * // },
63 * //
64 * // options: {
65 * // fields: [
66 * // "id",
67 * // "created_at",
68 * // "updated_at",
69 * // "deleted_at",
70 * // "title",
71 * // "product_id",
72 * // "metadata",
73 * // ],
74 * // values: {
75 * // fields: [
76 * // "id",
77 * // "created_at",
78 * // "updated_at",
79 * // "deleted_at",
80 * // "value",
81 * // "option_id",
82 * // "variant_id",
83 * // "metadata",
84 * // ],
85 * // },
86 * // },
87 * // },
88 * // }
89 */
90 export function remoteQueryObjectFromString<
91 const TEntry extends string,
92 const TConfig extends RemoteQueryObjectConfig<TEntry>
93 >(
94 config: TConfig | RemoteQueryObjectConfig<TEntry>
95 ): RemoteQueryObjectFromStringResult<TConfig> {
96 const {
97 entryPoint,
98 service,
99 variables = {},
100 fields = [],
101 } = {
102 ...config,
103 entryPoint: "entryPoint" in config ? config.entryPoint : undefined,
104 service: "service" in config ? config.service : undefined,
105 }
106
107 const entryKey = (entryPoint ?? service) as string
108
109 const remoteJoinerConfig: object = {
110 [entryKey]: {
111 fields: [],
112 isServiceAccess: !!service, // specifies if the entry point is a service
113 },
114 }
115
116 const usedVariables = new Set()
117
118 for (const field of fields) {
119 const fieldAsString = field as string
120 if (!fieldAsString.includes(".")) {
121 remoteJoinerConfig[entryKey]["fields"].push(field)
122 continue
123 }
124
125 const fieldSegments = fieldAsString.split(".")
126 const fieldProperty = fieldSegments.pop()
127
128 let combinedPath = ""
129
130 const deepConfigRef = fieldSegments.reduce((acc, curr) => {
131 combinedPath = combinedPath ? combinedPath + "." + curr : curr
132
133 if (isObject(variables) && combinedPath in variables) {
134 acc[curr] ??= {}
135 acc[curr]["__args"] = variables[combinedPath]
136 usedVariables.add(combinedPath)
137 } else {
138 acc[curr] ??= {}
139 }
140
141 return acc[curr]
142 }, remoteJoinerConfig[entryKey])
143
144 deepConfigRef["fields"] ??= []
145 deepConfigRef["fields"].push(fieldProperty)
146 }
147
148 const topLevelArgs = {}
149 for (const key of Object.keys(variables ?? {})) {
150 if (!usedVariables.has(key)) {
151 topLevelArgs[key] = variables[key]
152 }
153 }
154
155 remoteJoinerConfig[entryKey]["__args"] = topLevelArgs ?? {}
156
157 return {
158 __value: remoteJoinerConfig,
159 } as RemoteQueryObjectFromStringResult<TConfig>
160 }