@takeshape/schema 9.80.3 → 9.81.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/api-version.js +0 -9
  2. package/dist/builtin-schema.js +0 -5
  3. package/dist/content-schema-transform.js +4 -52
  4. package/dist/create-input-schema.js +7 -19
  5. package/dist/enum.js +0 -3
  6. package/dist/flatten-templates.js +0 -11
  7. package/dist/get-is-leaf.js +8 -18
  8. package/dist/index.js +0 -58
  9. package/dist/interfaces.js +2 -26
  10. package/dist/layers/layers.js +23 -103
  11. package/dist/layers/refs.js +26 -69
  12. package/dist/layers/type-utils.js +11 -43
  13. package/dist/layers/visitor.js +0 -6
  14. package/dist/migration/index.js +0 -51
  15. package/dist/migration/to/v3.0.0.js +31 -45
  16. package/dist/migration/to/v3.1.0.js +15 -41
  17. package/dist/migration/to/v3.10.0.js +6 -20
  18. package/dist/migration/to/v3.11.0.js +8 -20
  19. package/dist/migration/to/v3.12.0.js +2 -3
  20. package/dist/migration/to/v3.12.1.js +2 -3
  21. package/dist/migration/to/v3.12.2.js +2 -3
  22. package/dist/migration/to/v3.12.3.js +16 -21
  23. package/dist/migration/to/v3.13.0.js +2 -7
  24. package/dist/migration/to/v3.14.0.js +2 -3
  25. package/dist/migration/to/v3.15.0.js +2 -3
  26. package/dist/migration/to/v3.16.0.js +2 -3
  27. package/dist/migration/to/v3.17.0.js +4 -9
  28. package/dist/migration/to/v3.17.1.js +2 -3
  29. package/dist/migration/to/v3.18.0.js +2 -15
  30. package/dist/migration/to/v3.18.1.js +2 -7
  31. package/dist/migration/to/v3.18.2.js +8 -10
  32. package/dist/migration/to/v3.19.0.js +2 -3
  33. package/dist/migration/to/v3.2.0.js +2 -3
  34. package/dist/migration/to/v3.20.0.js +10 -11
  35. package/dist/migration/to/v3.21.0.js +2 -3
  36. package/dist/migration/to/v3.22.0.js +4 -9
  37. package/dist/migration/to/v3.23.0.js +2 -3
  38. package/dist/migration/to/v3.24.0.js +2 -3
  39. package/dist/migration/to/v3.25.0.js +2 -3
  40. package/dist/migration/to/v3.26.0.js +2 -3
  41. package/dist/migration/to/v3.27.0.js +2 -3
  42. package/dist/migration/to/v3.28.0.js +2 -3
  43. package/dist/migration/to/v3.29.0.js +2 -3
  44. package/dist/migration/to/v3.3.0.js +3 -4
  45. package/dist/migration/to/v3.30.0.js +2 -3
  46. package/dist/migration/to/v3.31.0.js +2 -12
  47. package/dist/migration/to/v3.32.0.js +12 -22
  48. package/dist/migration/to/v3.33.0.js +2 -3
  49. package/dist/migration/to/v3.34.0.js +14 -43
  50. package/dist/migration/to/v3.35.0.js +2 -3
  51. package/dist/migration/to/v3.36.0.js +6 -10
  52. package/dist/migration/to/v3.37.0.js +2 -3
  53. package/dist/migration/to/v3.38.0.js +2 -3
  54. package/dist/migration/to/v3.39.0.js +6 -13
  55. package/dist/migration/to/v3.4.0.js +2 -3
  56. package/dist/migration/to/v3.5.0.js +2 -3
  57. package/dist/migration/to/v3.5.1.js +2 -3
  58. package/dist/migration/to/v3.6.0.js +2 -3
  59. package/dist/migration/to/v3.7.0.js +2 -3
  60. package/dist/migration/to/v3.8.0.js +2 -3
  61. package/dist/migration/to/v3.9.0.js +8 -24
  62. package/dist/migration/utils.js +0 -2
  63. package/dist/mocks.js +9 -12
  64. package/dist/models/project-schema.js +0 -34
  65. package/dist/models/query.js +0 -9
  66. package/dist/models/service.js +0 -8
  67. package/dist/models/shape.js +0 -29
  68. package/dist/project-schema/index.js +1 -105
  69. package/dist/project-schema/migrate.js +1 -54
  70. package/dist/refs.js +39 -159
  71. package/dist/relationships.js +13 -57
  72. package/dist/scalars.js +0 -1
  73. package/dist/schema-transform.js +0 -11
  74. package/dist/schema-util.js +75 -271
  75. package/dist/schemas/index.js +0 -64
  76. package/dist/services.js +24 -44
  77. package/dist/taxonomies.js +0 -13
  78. package/dist/template-shapes/index.js +1 -8
  79. package/dist/template-shapes/templates.js +9 -40
  80. package/dist/template-shapes/types.js +4 -5
  81. package/dist/template-shapes/where.js +14 -110
  82. package/dist/types/index.js +0 -4
  83. package/dist/types/types.js +41 -13
  84. package/dist/types/utils.js +15 -89
  85. package/dist/unions.js +8 -38
  86. package/dist/util/api-indexing.js +2 -19
  87. package/dist/util/detect-cycles.js +2 -16
  88. package/dist/util/find-shape-at-path.js +1 -15
  89. package/dist/util/form-config.js +6 -23
  90. package/dist/util/get-conflicting-properties.js +7 -17
  91. package/dist/util/get-return-shape.js +0 -11
  92. package/dist/util/has-arg.js +0 -6
  93. package/dist/util/index.js +0 -18
  94. package/dist/util/merge.js +28 -74
  95. package/dist/util/patch-schema.js +5 -13
  96. package/dist/validate.js +15 -234
  97. package/dist/versions.js +0 -2
  98. package/dist/workflows.js +0 -15
  99. package/es/api-version.js +0 -4
  100. package/es/content-schema-transform.js +4 -38
  101. package/es/create-input-schema.js +7 -14
  102. package/es/enum.js +0 -2
  103. package/es/flatten-templates.js +0 -4
  104. package/es/get-is-leaf.js +8 -13
  105. package/es/index.js +1 -0
  106. package/es/interfaces.js +2 -14
  107. package/es/layers/layers.js +23 -90
  108. package/es/layers/refs.js +22 -45
  109. package/es/layers/type-utils.js +11 -16
  110. package/es/layers/visitor.js +0 -3
  111. package/es/migration/to/v3.0.0.js +31 -38
  112. package/es/migration/to/v3.1.0.js +16 -34
  113. package/es/migration/to/v3.10.0.js +6 -17
  114. package/es/migration/to/v3.11.0.js +8 -18
  115. package/es/migration/to/v3.12.0.js +2 -2
  116. package/es/migration/to/v3.12.1.js +2 -2
  117. package/es/migration/to/v3.12.2.js +2 -2
  118. package/es/migration/to/v3.12.3.js +16 -19
  119. package/es/migration/to/v3.13.0.js +2 -4
  120. package/es/migration/to/v3.14.0.js +2 -2
  121. package/es/migration/to/v3.15.0.js +2 -2
  122. package/es/migration/to/v3.16.0.js +2 -2
  123. package/es/migration/to/v3.17.0.js +4 -4
  124. package/es/migration/to/v3.17.1.js +2 -2
  125. package/es/migration/to/v3.18.0.js +2 -10
  126. package/es/migration/to/v3.18.1.js +2 -3
  127. package/es/migration/to/v3.18.2.js +8 -8
  128. package/es/migration/to/v3.19.0.js +2 -2
  129. package/es/migration/to/v3.2.0.js +2 -2
  130. package/es/migration/to/v3.20.0.js +10 -9
  131. package/es/migration/to/v3.21.0.js +2 -2
  132. package/es/migration/to/v3.22.0.js +4 -6
  133. package/es/migration/to/v3.23.0.js +2 -2
  134. package/es/migration/to/v3.24.0.js +2 -2
  135. package/es/migration/to/v3.25.0.js +2 -2
  136. package/es/migration/to/v3.26.0.js +2 -2
  137. package/es/migration/to/v3.27.0.js +2 -2
  138. package/es/migration/to/v3.28.0.js +2 -2
  139. package/es/migration/to/v3.29.0.js +2 -2
  140. package/es/migration/to/v3.3.0.js +3 -3
  141. package/es/migration/to/v3.30.0.js +2 -2
  142. package/es/migration/to/v3.31.0.js +2 -5
  143. package/es/migration/to/v3.32.0.js +12 -21
  144. package/es/migration/to/v3.33.0.js +2 -2
  145. package/es/migration/to/v3.34.0.js +16 -32
  146. package/es/migration/to/v3.35.0.js +2 -2
  147. package/es/migration/to/v3.36.0.js +6 -8
  148. package/es/migration/to/v3.37.0.js +2 -2
  149. package/es/migration/to/v3.38.0.js +2 -2
  150. package/es/migration/to/v3.39.0.js +6 -12
  151. package/es/migration/to/v3.4.0.js +2 -2
  152. package/es/migration/to/v3.5.0.js +2 -2
  153. package/es/migration/to/v3.5.1.js +2 -2
  154. package/es/migration/to/v3.6.0.js +2 -2
  155. package/es/migration/to/v3.7.0.js +2 -2
  156. package/es/migration/to/v3.8.0.js +2 -2
  157. package/es/migration/to/v3.9.0.js +8 -20
  158. package/es/mocks.js +10 -6
  159. package/es/models/project-schema.js +0 -26
  160. package/es/models/query.js +0 -7
  161. package/es/models/service.js +0 -6
  162. package/es/models/shape.js +1 -25
  163. package/es/project-schema/index.js +0 -5
  164. package/es/project-schema/migrate.js +3 -51
  165. package/es/refs.js +40 -96
  166. package/es/relationships.js +14 -42
  167. package/es/schema-transform.js +0 -7
  168. package/es/schema-util.js +75 -180
  169. package/es/services.js +18 -27
  170. package/es/taxonomies.js +0 -9
  171. package/es/template-shapes/index.js +1 -4
  172. package/es/template-shapes/templates.js +9 -18
  173. package/es/template-shapes/types.js +3 -4
  174. package/es/template-shapes/where.js +14 -94
  175. package/es/types/types.js +43 -10
  176. package/es/types/utils.js +16 -24
  177. package/es/unions.js +8 -22
  178. package/es/util/api-indexing.js +2 -12
  179. package/es/util/detect-cycles.js +2 -14
  180. package/es/util/find-shape-at-path.js +2 -12
  181. package/es/util/form-config.js +7 -19
  182. package/es/util/get-conflicting-properties.js +8 -14
  183. package/es/util/get-return-shape.js +0 -8
  184. package/es/util/has-arg.js +0 -3
  185. package/es/util/merge.js +28 -55
  186. package/es/util/patch-schema.js +5 -7
  187. package/es/validate.js +15 -194
  188. package/es/workflows.js +0 -6
  189. package/package.json +5 -5
package/es/refs.js CHANGED
@@ -10,6 +10,16 @@ import isString from 'lodash/isString';
10
10
  import { isScalar } from './scalars';
11
11
  import { NotFoundError } from '@takeshape/errors';
12
12
 
13
+ /**
14
+ * This interface describes a parsed `@ref` or `$ref`.
15
+ * The intended usage is to convert a schema with a ref to a RefItem which can then
16
+ * be passed to the various rich utilities in this file.
17
+ */
18
+
19
+ /**
20
+ * Adds a schema path, needed when visiting a whole schema to report errors.
21
+ */
22
+
13
23
  /**
14
24
  * Guard for RefItemWithPath. Tests for presence of all required props.
15
25
  */
@@ -20,17 +30,14 @@ const templateShapeRegex = /^(\w+)<([\w:-]+)>$/;
20
30
  /**
21
31
  * Parse a template like `PaginatedList<Post>` and return both the template and the shape name.
22
32
  */
23
-
24
33
  export function parseTemplateShape(shapeExpression) {
25
34
  const matches = templateShapeRegex.exec(shapeExpression);
26
-
27
35
  if (matches) {
28
36
  return {
29
37
  shapeName: matches[2],
30
38
  template: matches[1]
31
39
  };
32
40
  }
33
-
34
41
  return {
35
42
  shapeName: shapeExpression,
36
43
  template: undefined
@@ -40,28 +47,23 @@ export function parseReturnShape(projectSchema, shape) {
40
47
  if (typeof shape === 'object') {
41
48
  if (isRefSchema(shape.items)) {
42
49
  const ref = getRef(projectSchema, shape.items);
43
-
44
50
  if (!ref) {
45
51
  throw new Error(`Could not parse ${JSON.stringify(shape.items)}: invalid ref`);
46
52
  }
47
-
48
53
  return {
49
54
  isArray: true,
50
55
  ref,
51
56
  shapeName: refItemToShapeName(ref)
52
57
  };
53
58
  }
54
-
55
59
  if (typeof shape.items.type !== 'string') {
56
60
  throw new Error(`Could not parse ${JSON.stringify(shape.items)}: invalid type`);
57
61
  }
58
-
59
62
  return {
60
63
  isArray: true,
61
64
  shapeName: shape.items.type
62
65
  };
63
66
  }
64
-
65
67
  const ref = refExpressionToRefItem(projectSchema, shape);
66
68
  const shapeName = refItemToShapeName(ref);
67
69
  return {
@@ -71,10 +73,10 @@ export function parseReturnShape(projectSchema, shape) {
71
73
  template: ref.template
72
74
  };
73
75
  }
76
+
74
77
  /**
75
78
  * Convert Query.shape to a PropertySchema
76
79
  */
77
-
78
80
  export function returnShapeToSchema(projectSchema, returnShape) {
79
81
  if (typeof returnShape === 'string') {
80
82
  if (isScalar(returnShape) || returnShape === 'object') {
@@ -82,59 +84,51 @@ export function returnShapeToSchema(projectSchema, returnShape) {
82
84
  type: returnShape
83
85
  };
84
86
  }
85
-
86
87
  return {
87
88
  '@ref': refItemToAtRef(refExpressionToRefItem(projectSchema, returnShape))
88
89
  };
89
90
  }
90
-
91
91
  return returnShape;
92
92
  }
93
+
93
94
  /**
94
95
  * Create a template string like `PaginatedList<Post>` from a template and shape name.
95
96
  */
96
-
97
97
  export function createTemplateShapeName(template, shapeName) {
98
98
  return `${template}<${shapeName}>`;
99
99
  }
100
+
100
101
  /**
101
102
  * If the string is a template like `PaginatedList<Post>`, return the shape name,
102
103
  * in this case `Post`. Otherwise return the input.
103
104
  */
104
-
105
105
  export const untemplate = input => {
106
106
  return parseTemplateShape(input).shapeName;
107
107
  };
108
108
  export function getFlattenedTemplateShapeName(shapeName, template) {
109
109
  return `${shapeName}${template ?? ''}`;
110
110
  }
111
-
112
111
  function $refToPath(ref) {
113
112
  return ref.substr(2).split('/');
114
113
  }
115
-
116
114
  function $refToShapeName(ref) {
117
115
  return $refToPath(ref)[1];
118
116
  }
119
-
120
117
  export function $refToAtRef($ref, service, namespace) {
121
118
  const shapeName = $refToShapeName($ref);
122
-
123
119
  if (service === 'local') {
124
120
  return `local:${shapeName}`;
125
121
  }
126
-
127
122
  return `${service}:${namespace === undefined ? shapeName : shapeName.replace(`${namespace}_`, '')}`;
128
123
  }
129
124
  export function refSchemaToPath(context, refSchema) {
130
125
  const refItem = getRef(context, refSchema);
131
-
132
126
  if (refItem === undefined) {
133
127
  throw new Error('Invalid ref in refSchemaToPath');
134
128
  }
135
-
136
129
  return refItemToShapePath(refItem);
137
130
  }
131
+
138
132
  /**
139
133
  * Safely turn a "template" shape reference (`@args`, `args`, `shape`) into a `RefItem`
140
134
  *
@@ -143,26 +137,24 @@ export function refSchemaToPath(context, refSchema) {
143
137
  * A ref expression might include "template" name and refer to a local or remote
144
138
  * shape, `Foo` or `CreateArgs<Foo>` or `CreateArgs<remote:Foo>`.*
145
139
  */
146
-
147
140
  export function refExpressionToRefItem(context, refExpression) {
148
141
  const {
149
142
  shapeName,
150
143
  template
151
144
  } = parseTemplateShape(refExpression);
152
-
153
145
  if (shapeName.includes(':')) {
154
146
  return atRefToRefItem(context, shapeName, template);
155
147
  }
156
-
157
148
  return refToRefItem(context, `#/shapes/${shapeName}/schema`, template);
158
149
  }
150
+
159
151
  /**
160
152
  * Converts a list of ref expressions into a list of ref items.
161
153
  */
162
-
163
154
  export function refExpressionListToRefItemList(context, refExpressionList) {
164
155
  return refExpressionList.map(ref => refExpressionToRefItem(context, ref));
165
156
  }
157
+
166
158
  /**
167
159
  * Sugar for converting a `refExpression` directly into a shape, without the
168
160
  * intermediate `refItem`.
@@ -172,10 +164,10 @@ export function refExpressionListToRefItemList(context, refExpressionList) {
172
164
  * A ref expression might include "template" name and refer to a local or remote
173
165
  * shape, `Foo` or `CreateArgs<Foo>` or `CreateArgs<remote:Foo>`.
174
166
  */
175
-
176
167
  export function refExpressionToShape(context, refExpression) {
177
168
  return refItemToShape(context, refExpressionToRefItem(context, refExpression));
178
169
  }
170
+
179
171
  /**
180
172
  * Provided a service name and a referenced type. Supports 2 or 3 part
181
173
  * references, e.g., `shopify:my-store:ProductInput` or `my-store:ProductInput`.
@@ -183,24 +175,22 @@ export function refExpressionToShape(context, refExpression) {
183
175
  * @returns
184
176
  * A 2 member array, where 0 is the service name, 1 is the referenced type.
185
177
  */
186
-
187
178
  export function splitAtRef(atRef) {
188
179
  const refParts = atRef.split(':');
189
180
  const serviceKey = refParts.length === 2 ? refParts[0] : `${refParts[0]}:${refParts[1]}`;
190
181
  const referencedType = refParts.length === 2 ? refParts[1] : refParts[2];
191
182
  return [serviceKey, referencedType];
192
183
  }
184
+
193
185
  /**
194
186
  * Parses and returns a typeName, serviceKey, and potentially a namespace for
195
187
  * the type.
196
188
  */
197
-
198
189
  export function atRefToRefItem(context, atRef, template) {
199
190
  var _context$services;
200
-
201
- const [serviceKey, typeName] = splitAtRef(atRef); // It's possible the service doesn't have a namespace, or is local and has
191
+ const [serviceKey, typeName] = splitAtRef(atRef);
192
+ // It's possible the service doesn't have a namespace, or is local and has
202
193
  // no service config.
203
-
204
194
  const serviceConfig = (_context$services = context.services) === null || _context$services === void 0 ? void 0 : _context$services[serviceKey];
205
195
  const serviceNamespace = serviceConfig === null || serviceConfig === void 0 ? void 0 : serviceConfig.namespace;
206
196
  return {
@@ -214,7 +204,6 @@ export function atRefToRefItem(context, atRef, template) {
214
204
  export function refToRefItem(context, $ref, template) {
215
205
  return shapeNameToRefItem(context, $refToShapeName($ref), template);
216
206
  }
217
-
218
207
  /**
219
208
  * Parse a name (query, shape, etc) that might have a namespace and find the corresponding service layer id
220
209
  */
@@ -222,18 +211,15 @@ export function parseName(context, name) {
222
211
  const shapeNameParts = name.split('_');
223
212
  let serviceId;
224
213
  let namespace;
225
-
226
214
  if (shapeNameParts.length > 1) {
227
215
  const namespaces = getServiceNamespaces(context);
228
216
  const maybeNamespace = shapeNameParts[0];
229
217
  serviceId = namespaces.get(maybeNamespace);
230
-
231
218
  if (serviceId) {
232
219
  name = shapeNameParts.slice(1).join('_');
233
220
  namespace = maybeNamespace;
234
221
  }
235
222
  }
236
-
237
223
  return {
238
224
  name,
239
225
  serviceId: serviceId ?? 'local',
@@ -253,11 +239,11 @@ export function shapeNameToRefItem(context, shapeName, template) {
253
239
  export function shapeToRefItem(context, shape) {
254
240
  return shapeNameToRefItem(context, shape.name);
255
241
  }
242
+
256
243
  /**
257
244
  * Given a property schema and project schema, finds a `@ref` or `$ref` and
258
245
  * returns a `RefItem` suitable for loading a referenced schema...
259
246
  */
260
-
261
247
  export function getRef(context, refSchema) {
262
248
  if (refSchema['@ref']) {
263
249
  // @ref: my-service:ShapeName
@@ -267,7 +253,6 @@ export function getRef(context, refSchema) {
267
253
  } = parseTemplateShape(refSchema['@ref']);
268
254
  return atRefToRefItem(context, shapeName, template);
269
255
  }
270
-
271
256
  if (refSchema.$ref) {
272
257
  // $ref: #/shapes/[ShapeName OR Namespace_TypeName]/schema
273
258
  return refToRefItem(context, refSchema.$ref);
@@ -275,50 +260,48 @@ export function getRef(context, refSchema) {
275
260
  }
276
261
  export function getRefWithPath(context, refSchema, schemaPath = []) {
277
262
  const refItem = getRef(context, refSchema);
278
-
279
263
  if (refItem) {
280
264
  const refType = getRefType(refSchema);
281
- return { ...refItem,
265
+ return {
266
+ ...refItem,
282
267
  path: schemaPath.concat([refType])
283
268
  };
284
269
  }
285
270
  }
271
+
286
272
  /**
287
273
  * Tests for a `@ref` or `$ref` property directly on the passed schema.
288
274
  *
289
275
  * @param schema Any schema that might could have a ref property.
290
276
  */
291
-
292
277
  export function hasRefProperty(schema) {
293
278
  return Boolean(schema['@ref'] ?? schema.$ref);
294
279
  }
280
+
295
281
  /**
296
282
  * From a schema with a ref, will return the top-level ref, and potentially
297
283
  * a ref item from a nested `items` property.
298
284
  */
299
-
300
285
  export function getRefOrItemsRef(context, refSchema, schemaPath = []) {
301
286
  const refItem = getRefWithPath(context, refSchema, schemaPath);
302
-
303
287
  if (refItem) {
304
288
  return refItem;
305
289
  }
306
-
307
290
  if (refSchema.items) {
308
291
  return getRefOrItemsRef(context, refSchema.items, schemaPath.concat(['items']));
309
292
  }
310
293
  }
294
+
311
295
  /**
312
296
  * Turns a `RefItem` into an `@ref`.
313
297
  */
314
-
315
298
  export function refItemToAtRef(refItem) {
316
299
  return `${refItem.serviceKey}:${refItem.typeName}`;
317
300
  }
301
+
318
302
  /**
319
303
  * Turns a `RefItem` into a standard shape name, namespacing if present.
320
304
  */
321
-
322
305
  export function refItemToShapeName(refItem) {
323
306
  const {
324
307
  serviceNamespace,
@@ -326,32 +309,32 @@ export function refItemToShapeName(refItem) {
326
309
  } = refItem;
327
310
  return serviceNamespace ? `${serviceNamespace}_${typeName}` : typeName;
328
311
  }
312
+
329
313
  /**
330
314
  * Turns a `RefItem` into a path suitable for `lodash.get`.
331
315
  */
332
-
333
316
  export function refItemToShapePath(refItem) {
334
317
  return ['shapes', refItemToShapeName(refItem)];
335
318
  }
319
+
336
320
  /**
337
321
  * Get a Shape referenced by a `RefItem`.
338
322
  */
339
-
340
323
  export function refItemToShape(context, refItem) {
341
324
  const shapePath = refItemToShapePath(refItem);
342
325
  return get(context, shapePath);
343
326
  }
327
+
344
328
  /**
345
329
  * Get all shapes referenced by a RefItem array.
346
330
  */
347
-
348
331
  export function refItemListToShapeList(context, refItems) {
349
332
  return refItems.map(ref => refItemToShape(context, ref)).filter(isDefined);
350
333
  }
334
+
351
335
  /**
352
336
  * Get a shape referenced by a `RefItem`, also returning the path to the new shape.
353
337
  */
354
-
355
338
  export function refItemToShapeWithPath(context, refItem) {
356
339
  const shapePath = refItemToShapePath(refItem);
357
340
  const shape = get(context, shapePath);
@@ -360,100 +343,90 @@ export function refItemToShapeWithPath(context, refItem) {
360
343
  path: shapePath
361
344
  } : undefined;
362
345
  }
346
+
363
347
  /**
364
348
  * Get a Shape schema referenced by a `RefItem`.
365
349
  */
366
-
367
350
  export function refItemToShapeSchema(context, refItem) {
368
351
  const shapePath = refItemToShapePath(refItem);
369
352
  return get(context, [...shapePath, 'schema']);
370
353
  }
354
+
371
355
  /**
372
356
  * Sugar, for when you really just need a string...
373
357
  */
374
-
375
358
  export function getRefShapeName(context, refSchema) {
376
359
  const refItem = getRef(context, refSchema);
377
360
  return refItem ? refItemToShapeName(refItem) : undefined;
378
361
  }
362
+
379
363
  /**
380
364
  * Sugar, for when you really just need a path...
381
365
  */
382
-
383
366
  export function anyRefToShapeSchemaPath(context, refSchema) {
384
367
  const refItem = getRef(context, refSchema);
385
368
  return refItem ? [...refItemToShapePath(refItem), 'schema'] : undefined;
386
369
  }
370
+
387
371
  /**
388
372
  * Helper fn to omit `ref` props from the target schema, and then extend it with the source schema.
389
373
  */
390
-
391
374
  export const omitRefAndExtend = (targetSchema, sourceSchema) => assign(omit(['@ref', '$ref'], targetSchema), sourceSchema);
375
+
392
376
  /**
393
377
  * If there is a $ref, this will insert a property schema from the top level
394
378
  * in place of the reference.
395
379
  */
396
-
397
380
  export function followRef(context, schema, ref) {
398
381
  const refItem = ref ?? getRef(context, schema);
399
-
400
382
  if (refItem) {
401
383
  const referencedSchema = refItemToShapeSchema(context, refItem);
402
-
403
384
  if (referencedSchema) {
404
385
  return omitRefAndExtend(schema, referencedSchema);
405
386
  }
406
387
  }
407
-
408
388
  return schema;
409
389
  }
390
+
410
391
  /**
411
392
  * Returns whether a schema has refs.
412
393
  */
413
-
414
394
  export function hasRef(schema) {
415
395
  if (isRefSchema(schema) || isRefSchemaLegacy(schema)) {
416
396
  return true;
417
397
  }
418
-
419
398
  if (isAllOfSchema(schema)) {
420
399
  return Boolean(schema.allOf.find(hasRef));
421
400
  }
422
-
423
401
  if (isOneOfSchema(schema)) {
424
402
  return Boolean(schema.oneOf.find(hasRef));
425
403
  }
426
-
427
404
  return false;
428
405
  }
406
+
429
407
  /**
430
408
  * Returns whether a schema has refs which can be followed.
431
409
  */
432
-
433
410
  export function hasResolvableRef(context, schema) {
434
411
  if (isRefSchema(schema) || isRefSchemaLegacy(schema)) {
435
412
  return Boolean(refItemToShapeSchema(context, getRef(context, schema)));
436
413
  }
437
-
438
414
  if (isAllOfSchema(schema)) {
439
415
  return Boolean(schema.allOf.find(s => hasResolvableRef(context, s)));
440
416
  }
441
-
442
417
  if (isOneOfSchema(schema)) {
443
418
  return Boolean(schema.oneOf.find(s => hasResolvableRef(context, s)));
444
419
  }
445
-
446
420
  return false;
447
421
  }
448
-
449
422
  function isSelfReferential(path, shapeName) {
450
423
  if (path[0] !== 'shapes' || path[1] !== shapeName) {
451
424
  return false;
452
425
  }
453
-
454
426
  const index = path.findIndex(part => part === 'allOf' || part === 'extends');
455
427
  return index !== -1 && isIntegerLike(path[index + 1]);
456
428
  }
429
+
457
430
  /**
458
431
  * Given a schema, will resolve any `allOf` or `ref` objects. This function has
459
432
  * to fully resolve all refs, because attempting to extend an object schema with
@@ -462,28 +435,21 @@ function isSelfReferential(path, shapeName) {
462
435
  *
463
436
  * If it can't create a valid schema it returns the input.
464
437
  */
465
-
466
-
467
438
  export function dereferenceSchema(context, shapeOrFieldSchema, schemaPath = []) {
468
439
  const resolveAll = (propertySchema, resolvePath) => {
469
440
  try {
470
441
  if (isAllOfSchema(propertySchema)) {
471
442
  return merge(propertySchema.allOf, 'allOf', resolvePath);
472
443
  }
473
-
474
444
  if (isExtendsSchema(propertySchema)) {
475
445
  return merge(propertySchema.extends, 'extends', resolvePath);
476
446
  }
477
-
478
447
  if (isRefSchema(propertySchema)) {
479
448
  const refItem = getRefWithPath(context, propertySchema);
480
-
481
449
  if (!refItem) {
482
450
  throw new Error('ref could not be parsed');
483
451
  }
484
-
485
452
  const shapeWithPath = refItemToShapeWithPath(context, refItem);
486
-
487
453
  if (shapeWithPath && !isSelfReferential(resolvePath, shapeWithPath.shape.name)) {
488
454
  /**
489
455
  * Pass the new shape's path rather than the parent's path, which
@@ -491,16 +457,13 @@ export function dereferenceSchema(context, shapeOrFieldSchema, schemaPath = [])
491
457
  */
492
458
  return resolveAll(omitRefAndExtend(propertySchema, shapeWithPath.shape.schema), shapeWithPath.path);
493
459
  }
494
-
495
460
  return;
496
461
  }
497
-
498
462
  return propertySchema;
499
463
  } catch (err) {
500
464
  throw new Error(`error at '${resolvePath.join('.')}', ${err.message}`);
501
465
  }
502
466
  };
503
-
504
467
  const merge = (schemas, propName, path) => {
505
468
  const mergedSchema = schemas.map((schema, index) => {
506
469
  const resolved = resolveAll(schema, [...path, propName, index]);
@@ -511,22 +474,18 @@ export function dereferenceSchema(context, shapeOrFieldSchema, schemaPath = [])
511
474
  });
512
475
  return assign(omit(propName, resolveSchema), mergedSchema);
513
476
  };
514
-
515
477
  return resolveAll(shapeOrFieldSchema, schemaPath) ?? shapeOrFieldSchema;
516
478
  }
517
479
  export function dereferenceObjectSchema(context, shapeOrFieldSchema, schemaPath = []) {
518
480
  const schema = dereferenceSchema(context, shapeOrFieldSchema, schemaPath);
519
-
520
481
  if (!isObjectSchema(schema)) {
521
482
  throw new Error(`provided schema at '${schemaPath.join('.')}' could not be resolved to an object schema`);
522
483
  }
523
-
524
484
  return schema;
525
485
  }
526
486
  export function createGetNamespace(context) {
527
487
  return serviceId => {
528
488
  var _context$services2, _context$services2$se;
529
-
530
489
  return (_context$services2 = context.services) === null || _context$services2 === void 0 ? void 0 : (_context$services2$se = _context$services2[serviceId]) === null || _context$services2$se === void 0 ? void 0 : _context$services2$se.namespace;
531
490
  };
532
491
  }
@@ -535,7 +494,6 @@ export function parsePropertyRef(refStr) {
535
494
  const serviceId = index === -1 ? 'local' : refStr.substring(0, index);
536
495
  const coordinate = index === -1 ? refStr : refStr.substring(index + 1);
537
496
  const coordinateParts = coordinate.split('.');
538
-
539
497
  if (coordinateParts.length === 1) {
540
498
  return {
541
499
  serviceId,
@@ -543,7 +501,6 @@ export function parsePropertyRef(refStr) {
543
501
  propertyName: coordinateParts[0]
544
502
  };
545
503
  }
546
-
547
504
  if (coordinateParts.length === 2) {
548
505
  return {
549
506
  serviceId,
@@ -557,11 +514,9 @@ export function serializePropertyRef(propertyRef) {
557
514
  }
558
515
  export function propertyRefToShapeRef(propertyRef) {
559
516
  const parsed = parsePropertyRef(propertyRef);
560
-
561
517
  if (!parsed) {
562
518
  throw new Error(`Invalid property ref "${propertyRef}`);
563
519
  }
564
-
565
520
  return formatShapeRef(parsed.serviceId, parsed.shapeName);
566
521
  }
567
522
  export function formatShapeRef(serviceId, shapeName) {
@@ -584,11 +539,9 @@ export function propertyRefItemToLocalName(getNamespace, item) {
584
539
  serviceId,
585
540
  propertyName
586
541
  } = item;
587
-
588
542
  if (shapeName === 'Query' || shapeName === 'Mutation') {
589
543
  return applyNamespace(getNamespace(serviceId), propertyName);
590
544
  }
591
-
592
545
  return propertyName;
593
546
  }
594
547
  export function propertyRefItemToPath(getNamespace, item) {
@@ -598,25 +551,22 @@ export function propertyRefItemToPath(getNamespace, item) {
598
551
  propertyName
599
552
  } = item;
600
553
  const namespace = getNamespace(serviceId);
601
-
602
554
  if (shapeName === 'Query' || shapeName === 'Mutation') {
603
555
  return [shapeName === 'Query' ? 'queries' : 'mutations', applyNamespace(namespace, propertyName)];
604
556
  }
605
-
606
557
  return ['shapes', applyNamespace(namespace, shapeName), 'schema', 'properties', propertyName];
607
558
  }
608
559
  export function propertyRefToArgs(projectSchema, ref) {
609
560
  const parsed = typeof ref === 'object' ? ref : parsePropertyRef(ref);
610
-
611
561
  if (parsed) {
612
562
  const path = propertyRefItemToArgsPath(createGetNamespace(projectSchema), parsed);
613
563
  return get(projectSchema, path);
614
564
  }
615
565
  }
566
+
616
567
  /**
617
568
  * Get a ref to the given shape.
618
569
  */
619
-
620
570
  export function shapeNameToRef(context, shapeName) {
621
571
  return refItemToAtRef(shapeNameToRefItem(context, shapeName));
622
572
  }
@@ -627,10 +577,8 @@ export function getAllPropertyRefs(projectSchema) {
627
577
  const result = [];
628
578
  visit(projectSchema, ['to'], (value, path) => {
629
579
  const parent = get(projectSchema, path.slice(0, -1));
630
-
631
580
  if (isString(value) && isRecord(parent) && parent.name === 'delegate') {
632
581
  const parsed = parsePropertyRef(value);
633
-
634
582
  if (parsed) {
635
583
  result.push(parsed);
636
584
  }
@@ -640,11 +588,9 @@ export function getAllPropertyRefs(projectSchema) {
640
588
  }
641
589
  export function getQuery(projectSchema, queryRef) {
642
590
  const propertyRef = parsePropertyRef(queryRef);
643
-
644
591
  if (!propertyRef || propertyRef.shapeName !== 'Query' && propertyRef.shapeName !== 'Mutation') {
645
592
  throw new Error(`Invalid query ref "${queryRef}"`);
646
593
  }
647
-
648
594
  const path = propertyRefItemToPath(createGetNamespace(projectSchema), propertyRef);
649
595
  const query = get(projectSchema, path);
650
596
  return query ? {
@@ -655,10 +601,8 @@ export function getQuery(projectSchema, queryRef) {
655
601
  }
656
602
  export function ensureQuery(projectSchema, queryRef) {
657
603
  const query = getQuery(projectSchema, queryRef);
658
-
659
604
  if (!query) {
660
605
  throw new NotFoundError(`Query "${query}" not found`);
661
606
  }
662
-
663
607
  return query;
664
608
  }