@takeshape/schema 11.89.0 → 11.91.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.
@@ -11,7 +11,7 @@ export function flattenTemplates(projectSchema) {
11
11
  const newSchema = deepClone(projectSchema);
12
12
  for (const ref of templateRefs) {
13
13
  if (ref.template && isValidTemplate(ref.template)) {
14
- const { shapeName, dependencies } = resolveTemplate(newSchema, ref.template, refItemToNamespacedShapeName(ref));
14
+ const { shapeName, dependencies } = resolveTemplate(newSchema, ref.template, refItemToNamespacedShapeName(newSchema, ref));
15
15
  set(newSchema, ref.path, ref.path[ref.path.length - 1] === '@ref' ? `local:${shapeName}` : shapeName);
16
16
  Object.assign(newSchema.shapes, dependencies);
17
17
  }
package/dist/index.d.ts CHANGED
@@ -43,6 +43,7 @@ export * from './util/form-config.ts';
43
43
  export * from './util/get-conflicting-properties.ts';
44
44
  export * from './util/get-return-shape.ts';
45
45
  export * from './util/has-arg.ts';
46
+ export * from './util/is-asset-property.ts';
46
47
  export * from './util/merge.ts';
47
48
  export * from './util/patch-schema.ts';
48
49
  export * from './util/shapes.ts';
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ export * from "./util/form-config.js";
41
41
  export * from "./util/get-conflicting-properties.js";
42
42
  export * from "./util/get-return-shape.js";
43
43
  export * from "./util/has-arg.js";
44
+ export * from "./util/is-asset-property.js";
44
45
  export * from "./util/merge.js";
45
46
  export * from "./util/patch-schema.js";
46
47
  export * from "./util/shapes.js";
@@ -1,6 +1,8 @@
1
1
  import type { OneOfSchema, ProjectSchemaJSON, PropertySchema, ShapeJSON, ShapeMap } from './project-schema/index.ts';
2
- import type { RefItem, ServicesShapesContext } from './refs.ts';
3
- export declare function isInterfaceShape(shape?: ShapeJSON): boolean;
2
+ import { type RefItem, type ServicesShapesContext } from './refs.ts';
3
+ export declare function isInterfaceShape(shape?: ShapeJSON): shape is ShapeJSON & {
4
+ type: 'interface';
5
+ };
4
6
  export type ImplementationShapeMap = Record<string, Set<ShapeJSON>>;
5
7
  export type ImplementationShapeNameMap = Record<string, Set<string>>;
6
8
  export declare function isInterfaceRef(context: ServicesShapesContext, propertySchema: PropertySchema): boolean;
@@ -1,6 +1,6 @@
1
1
  import { isDefined } from '@takeshape/util';
2
2
  import mapValues from 'lodash/mapValues.js';
3
- import { getRef, normalizeRefExpression, refExpressionToRefItem, refItemToNamespacedShapeName, refItemToShape, shapeToRefItem } from "./refs.js";
3
+ import { getRef, getShapeByRef, normalizeRefExpression, parseRef, refItemToNamespacedShapeName, refItemToShape, shapeToRefItem } from "./refs.js";
4
4
  import { isUnionSchema } from "./unions.js";
5
5
  export function isInterfaceShape(shape) {
6
6
  return shape?.type === 'interface';
@@ -36,7 +36,8 @@ export function pruneUnusedInterfaces(projectSchema, shapeMap) {
36
36
  return mapValues(shapeMap, (shape) => {
37
37
  const { interfaces } = shape;
38
38
  if (interfaces?.length) {
39
- const filteredInterfaces = interfaces.filter((refExpression) => shapeMap[refItemToNamespacedShapeName(refExpressionToRefItem(projectSchema, refExpression))]);
39
+ const context = { ...projectSchema, shapes: shapeMap };
40
+ const filteredInterfaces = interfaces.filter((refExpression) => getShapeByRef(context, refExpression));
40
41
  if (interfaces.length !== filteredInterfaces.length) {
41
42
  return { ...shape, interfaces: filteredInterfaces };
42
43
  }
@@ -45,13 +46,13 @@ export function pruneUnusedInterfaces(projectSchema, shapeMap) {
45
46
  });
46
47
  }
47
48
  function compareRefItems(refItem1, refItem2) {
48
- return refItem1.typeName === refItem2.typeName && refItem1.serviceKey === refItem2.serviceKey;
49
+ return refItem1.shapeName === refItem2.shapeName && refItem1.layerId === refItem2.layerId;
49
50
  }
50
51
  export function getImplementingShapes(context, interfaceRef) {
51
52
  const shapeMap = context.shapes;
52
53
  const implementingShapes = [];
53
54
  for (const shape of Object.values(shapeMap)) {
54
- if (shape.interfaces?.find((interfaceRefExpression) => compareRefItems(refExpressionToRefItem(context, interfaceRefExpression), interfaceRef))) {
55
+ if (shape.interfaces?.find((interfaceRefExpression) => compareRefItems(parseRef(context, interfaceRefExpression), interfaceRef))) {
55
56
  implementingShapes.push(shape);
56
57
  }
57
58
  }
@@ -74,7 +75,7 @@ export function getPossibleShapeNames(projectSchema, shape) {
74
75
  names = shape.schema.oneOf
75
76
  .map((member) => {
76
77
  const ref = getRef(projectSchema, member);
77
- return ref && refItemToNamespacedShapeName(ref);
78
+ return ref && refItemToNamespacedShapeName(projectSchema, ref);
78
79
  })
79
80
  .filter(isDefined);
80
81
  }
@@ -3,15 +3,10 @@ import set from 'lodash/fp/set.js';
3
3
  import unset from 'lodash/fp/unset.js';
4
4
  import upperFirst from 'lodash/upperFirst.js';
5
5
  import { SERVICE_OBJECT_PATTERN_NAME } from "../../constants.js";
6
- import { getRefOrItemsRef } from "../../refs.js";
6
+ import { getRefOrItemsRef, refItemToNamespacedShapeName } from "../../refs.js";
7
7
  import { createSchemaPropertyAccessor } from "../../schema-util.js";
8
8
  import { CURRENT_SCHEMA_VERSION } from "../../schemas/index.js";
9
9
  import { getServiceObjectFields } from "../../services/services.js";
10
- // Inline utilities for posterity
11
- function refItemToShapeName(refItem) {
12
- const { serviceNamespace, typeName } = refItem;
13
- return serviceNamespace ? `${serviceNamespace}_${typeName}` : typeName;
14
- }
15
10
  // Map of form studio widgets and their replacements in the shape editor
16
11
  const widgetMigrations = {
17
12
  object: 'shapeObject',
@@ -39,7 +34,7 @@ export function normalizeForms(projectSchema) {
39
34
  if (propSchema) {
40
35
  const ref = getRefOrItemsRef(projectSchema, propSchema);
41
36
  if (ref && propConfig.properties && propConfig.widget !== 'shopify') {
42
- const nestedShapeName = refItemToShapeName(ref);
37
+ const nestedShapeName = refItemToNamespacedShapeName(projectSchema, ref);
43
38
  normalizedForms[nestedShapeName] = { default: deepClone(propConfig) };
44
39
  storeSourceForm(shapeName, formConfig, propName);
45
40
  normalizeNested(nestedShapeName, propConfig);
@@ -1,4 +1,4 @@
1
- import { dereferenceSchema, getQuery, normalizePropertyRef, normalizeRefExpression, parsePropertyRef, refExpressionToRefItem, refItemToNamespacedShapeName } from "../refs.js";
1
+ import { dereferenceSchema, getQuery, normalizePropertyRef, normalizeRefExpression, parsePropertyRef, parseRef, refItemToNamespacedShapeName } from "../refs.js";
2
2
  import { buildRuntimeSchema } from "../runtime-schema.js";
3
3
  import { getShape } from "../util/shapes.js";
4
4
  import { Query } from './query.js';
@@ -18,16 +18,16 @@ class ProjectSchemaRefResolver {
18
18
  const shapeRef = normalizeRefExpression(this.#runtimeSchemaJson, rawShapeRef);
19
19
  let shape = this.#shapeCache.get(shapeRef);
20
20
  if (!shape) {
21
- const refItem = refExpressionToRefItem(this.#runtimeSchemaJson, rawShapeRef);
21
+ const refItem = parseRef(this.#runtimeSchemaJson, rawShapeRef);
22
22
  if (refItem) {
23
- const namespacedName = refItemToNamespacedShapeName(refItem);
23
+ const namespacedName = refItemToNamespacedShapeName(this.#runtimeSchemaJson, refItem);
24
24
  const shapeJson = getShape(this.#runtimeSchemaJson, namespacedName);
25
25
  if (shapeJson) {
26
26
  shape = new Shape(this, refItem, shapeJson);
27
27
  this.#shapeCache.set(shapeRef, shape);
28
28
  }
29
- else if (refItem.serviceKey !== 'local') {
30
- const layerSchema = this.#layers[refItem.serviceKey]?.schema;
29
+ else if (refItem.layerId !== 'local') {
30
+ const layerSchema = this.#layers[refItem.layerId]?.schema;
31
31
  if (layerSchema) {
32
32
  const shapeJson = getShape(layerSchema, namespacedName);
33
33
  if (shapeJson) {
@@ -51,8 +51,8 @@ class ProjectSchemaRefResolver {
51
51
  }
52
52
  else {
53
53
  const parsedRef = parsePropertyRef(queryRef);
54
- if (parsedRef && parsedRef.serviceId !== 'local') {
55
- const layerSchema = this.#layers[parsedRef.serviceId]?.schema;
54
+ if (parsedRef && parsedRef.layerId !== 'local') {
55
+ const layerSchema = this.#layers[parsedRef.layerId]?.schema;
56
56
  if (layerSchema) {
57
57
  const queryEntry = getQuery(layerSchema, parsedRef.propertyName);
58
58
  if (queryEntry) {
@@ -7,5 +7,6 @@ export declare class Shape {
7
7
  get name(): string;
8
8
  get ref(): string;
9
9
  get refItem(): RefItem;
10
+ get json(): ShapeJSON;
10
11
  getObjectSchema(): import("../project-schema/latest.ts").ObjectSchema | undefined;
11
12
  }
@@ -1,4 +1,4 @@
1
- import { refItemToAtRef } from "../refs.js";
1
+ import { serializeRef } from "../refs.js";
2
2
  import { isObjectSchema } from "../types/utils.js";
3
3
  export class Shape {
4
4
  #refResolver;
@@ -14,11 +14,14 @@ export class Shape {
14
14
  return this.#shape.name;
15
15
  }
16
16
  get ref() {
17
- return refItemToAtRef(this.#ref);
17
+ return serializeRef(this.#ref);
18
18
  }
19
19
  get refItem() {
20
20
  return this.#ref;
21
21
  }
22
+ get json() {
23
+ return this.#shape;
24
+ }
22
25
  getObjectSchema() {
23
26
  if (!this.#dereferencedSchema) {
24
27
  this.#dereferencedSchema = this.#refResolver.dereferenceSchema(this.#shape.schema);
package/dist/refs.d.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  import { type Maybe } from '@takeshape/util';
2
2
  import type { AIToolConfig, Args, ObjectSchema, ProjectSchemaJSON, PropertySchema, QueryJSON, Ref, ReturnShape, ShapeJSON, ShapeSchema } from './project-schema/index.ts';
3
- import type { SchemaPath, SchemaWithRef } from './types/index.ts';
3
+ import type { SchemaPath } from './types/index.ts';
4
4
  export type ServicesContext = Pick<ProjectSchemaJSON, 'services'>;
5
5
  export type ServicesShapesContext = Pick<ProjectSchemaJSON, 'services' | 'shapes'>;
6
+ export declare const LOCAL_LAYER_ID = "local";
6
7
  /**
7
8
  * This interface describes a parsed `@ref` or `$ref`.
8
9
  * The intended usage is to convert a schema with a ref to a RefItem which can then
9
10
  * be passed to the various rich utilities in this file.
10
11
  */
11
12
  export type RefItem = {
12
- typeName: string;
13
- serviceKey: string;
14
- serviceNamespace?: string;
13
+ shapeName: string;
14
+ layerId: string;
15
15
  template?: string;
16
- isForeign: boolean;
17
- isValidService: boolean;
18
- isInterfaceRef?: boolean;
16
+ };
17
+ export type PropertyRefItem = RefItem & {
18
+ propertyName: string;
19
19
  };
20
20
  /**
21
21
  * Adds a schema path, needed when visiting a whole schema to report errors.
@@ -23,10 +23,6 @@ export type RefItem = {
23
23
  export type RefItemWithPath = {
24
24
  path: SchemaPath;
25
25
  } & RefItem;
26
- /**
27
- * Guard for RefItemWithPath. Tests for presence of all required props.
28
- */
29
- export declare function isRefItemWithPath(ref: Record<string, unknown>): ref is RefItemWithPath;
30
26
  /**
31
27
  * Parse a template like `PaginatedList<Post>` and return both the template and the shape name.
32
28
  */
@@ -40,7 +36,7 @@ export type ParsedReturnShape = {
40
36
  shapeName: string;
41
37
  ref?: RefItem;
42
38
  };
43
- export declare function parseReturnShape(projectSchema: ServicesContext, shape: ReturnShape): ParsedReturnShape;
39
+ export declare function parseReturnShape(context: ServicesContext, shape: ReturnShape): ParsedReturnShape;
44
40
  /**
45
41
  * Convert Query.shape to a PropertySchema
46
42
  */
@@ -55,8 +51,7 @@ export declare function createTemplateShapeName(template: string, shapeName: str
55
51
  */
56
52
  export declare const untemplate: (input: string) => string;
57
53
  export declare function getFlattenedTemplateShapeName(shapeName: string, template?: string): string;
58
- export declare function $refToAtRef($ref: string, service: string, namespace?: string): string;
59
- export declare function refSchemaToPath(context: ServicesContext, refSchema: SchemaWithRef): string[];
54
+ export declare function refSchemaToPath(context: ServicesContext, refSchema: PropertySchema): string[];
60
55
  /**
61
56
  * Safely turn a "template" shape reference (`@args`, `args`, `shape`) into a `RefItem`
62
57
  *
@@ -65,7 +60,8 @@ export declare function refSchemaToPath(context: ServicesContext, refSchema: Sch
65
60
  * A ref expression might include "template" name and refer to a local or remote
66
61
  * shape, `Foo` or `CreateArgs<Foo>` or `CreateArgs<remote:Foo>`.*
67
62
  */
68
- export declare function refExpressionToRefItem(context: ServicesContext, refExpression: string): RefItem;
63
+ export declare function parseRef(context: ServicesContext, refExpression: string): RefItem;
64
+ export declare function isRefEqual(context: ServicesContext, a: string | RefItem, b: string | RefItem): boolean;
69
65
  /**
70
66
  * Converts a list of ref expressions into a list of ref items.
71
67
  */
@@ -79,24 +75,12 @@ export declare function refExpressionListToRefItemList(context: ServicesContext,
79
75
  * A ref expression might include "template" name and refer to a local or remote
80
76
  * shape, `Foo` or `CreateArgs<Foo>` or `CreateArgs<remote:Foo>`.
81
77
  */
82
- export declare function refExpressionToShape(context: ServicesShapesContext, refExpression: string): Maybe<ShapeJSON>;
83
- /**
84
- * Provided a service name and a referenced type. Supports 2 or 3 part
85
- * references, e.g., `shopify:my-store:ProductInput` or `my-store:ProductInput`.
86
- *
87
- * @returns
88
- * A 2 member array, where 0 is the service name, 1 is the referenced type.
89
- */
90
- export declare function splitAtRef(atRef: string): [string, string];
91
- /**
92
- * Parses and returns a typeName, serviceKey, and potentially a namespace for
93
- * the type.
94
- */
95
- export declare function atRefToRefItem(context: ServicesContext, atRef: string, template?: string): RefItem;
78
+ export declare function getShapeByRef(context: ServicesShapesContext, refExpression: string): Maybe<ShapeJSON>;
79
+ export declare function isValidServiceId(context: ServicesContext, serviceId: string): boolean;
80
+ export declare function isValidRefItem(context: ServicesContext): (refItem: RefItem) => boolean;
96
81
  export declare function refToRefItem(context: ServicesContext, $ref: string, template?: string): RefItem;
97
82
  export type ParsedName = {
98
- namespace?: string;
99
- serviceId: string;
83
+ layerId: string;
100
84
  name: string;
101
85
  };
102
86
  /**
@@ -109,14 +93,14 @@ export declare function shapeToRefString(context: ServicesContext, shape: ShapeJ
109
93
  * Given a property schema and project schema, finds a `@ref` or `$ref` and
110
94
  * returns a `RefItem` suitable for loading a referenced schema...
111
95
  */
112
- export declare function getRef(context: ServicesContext, refSchema: SchemaWithRef): Maybe<RefItem>;
113
- export declare function getRefWithPath(context: ServicesContext, refSchema: SchemaWithRef, schemaPath?: SchemaPath, isInterfaceRef?: boolean): Maybe<RefItemWithPath>;
96
+ export declare function getRef(context: ServicesContext, refSchema: PropertySchema): Maybe<RefItem>;
97
+ export declare function getRefWithPath(context: ServicesContext, refSchema: PropertySchema, schemaPath?: SchemaPath): Maybe<RefItemWithPath>;
114
98
  /**
115
99
  * Tests for a `@ref` or `$ref` property directly on the passed schema.
116
100
  *
117
101
  * @param schema Any schema that might could have a ref property.
118
102
  */
119
- export declare function hasRefProperty(schema: SchemaWithRef): boolean;
103
+ export declare function hasRefProperty(schema: PropertySchema): boolean;
120
104
  /**
121
105
  * From a property that has oneOf or items with oneOf, get all refs
122
106
  */
@@ -125,19 +109,20 @@ export declare function getOneOfRefs(context: ServicesContext, propertySchema: P
125
109
  * From a schema with a ref, will return the top-level ref, and potentially
126
110
  * a ref item from a nested `items` property.
127
111
  */
128
- export declare function getRefOrItemsRef(context: ServicesContext, refSchema: SchemaWithRef, schemaPath?: SchemaPath): Maybe<RefItemWithPath>;
112
+ export declare function getRefOrItemsRef(context: ServicesContext, refSchema: PropertySchema, schemaPath?: SchemaPath): Maybe<RefItemWithPath>;
129
113
  /**
130
114
  * Turns a `RefItem` into an `@ref`.
131
115
  */
132
- export declare function refItemToAtRef(refItem: RefItem): string;
116
+ export declare function serializeRef(refItem: RefItem): string;
133
117
  /**
134
118
  * Turns a `RefItem` into a standard shape name, namespacing if present.
135
119
  */
136
- export declare function refItemToNamespacedShapeName(refItem: RefItem): string;
120
+ export declare function refItemToNamespacedShapeName(context: ServicesContext, refItem: RefItem): string;
121
+ export declare function refExpressionToNamespacedName(context: ServicesContext, refExpression: string): string;
137
122
  /**
138
123
  * Turns a `RefItem` into a path suitable for `lodash.get`.
139
124
  */
140
- export declare function refItemToShapePath(refItem: RefItem): string[];
125
+ export declare function refItemToShapePath(context: ServicesContext, refItem: RefItem): string[];
141
126
  /**
142
127
  * Get a Shape referenced by a `RefItem`.
143
128
  */
@@ -160,11 +145,11 @@ export declare function refItemToShapeSchema(context: ServicesShapesContext, ref
160
145
  /**
161
146
  * Sugar, for when you really just need a string...
162
147
  */
163
- export declare function getRefShapeName(context: ServicesContext, refSchema: SchemaWithRef): Maybe<string>;
148
+ export declare function getRefShapeName(context: ServicesContext, refSchema: PropertySchema): Maybe<string>;
164
149
  /**
165
150
  * Sugar, for when you really just need a path...
166
151
  */
167
- export declare function anyRefToShapeSchemaPath(context: ServicesContext, refSchema: SchemaWithRef): Maybe<string[]>;
152
+ export declare function refSchemaToShapeSchemaPath(context: ServicesContext, refSchema: PropertySchema): Maybe<string[]>;
168
153
  /**
169
154
  * Helper fn to omit `ref` props from the target schema, and then extend it with the source schema.
170
155
  */
@@ -192,23 +177,16 @@ export declare function hasResolvableRef(context: ServicesShapesContext, schema:
192
177
  */
193
178
  export declare function dereferenceSchema(context: ServicesShapesContext, shapeOrFieldSchema: PropertySchema, schemaPath?: SchemaPath): PropertySchema;
194
179
  export declare function dereferenceObjectSchema(context: ServicesShapesContext, shapeOrFieldSchema: PropertySchema, schemaPath?: SchemaPath): ObjectSchema;
195
- export type GetNamespace = (serviceId: string) => Maybe<string>;
180
+ export type GetNamespace = (layerId: string) => Maybe<string>;
196
181
  export declare function createGetNamespace(context: ServicesContext): GetNamespace;
197
- export type PropertyRefItem = {
198
- serviceId: string;
199
- shapeName: string;
200
- propertyName: string;
201
- };
202
182
  export declare function parsePropertyRef(refStr: string): Maybe<PropertyRefItem>;
203
- export declare function serializePropertyRef({ shapeName, propertyName, serviceId }: PropertyRefItem): string;
183
+ export declare function serializePropertyRef({ shapeName, propertyName, layerId }: PropertyRefItem): string;
204
184
  export declare function normalizePropertyRef(refStr: string): string;
205
185
  export declare function propertyRefToShapeRef(propertyRef: string): string;
206
- export declare function formatShapeRef(serviceId: string, shapeName: string): string;
186
+ export declare function formatShapeRef(layerId: string, shapeName: string): string;
207
187
  export declare function applyNamespace(namespace: string | undefined, name: string): string;
208
188
  export declare function propertyRefItemToResolverPath(getNamespace: GetNamespace, item: PropertyRefItem): string[];
209
189
  export declare function propertyRefItemToArgsPath(getNamespace: GetNamespace, item: PropertyRefItem): string[];
210
- export declare function propertyRefItemToLocalName(getNamespace: GetNamespace, item: PropertyRefItem): string;
211
- export declare function queryRefToLocalName(context: ServicesContext, queryRef: string): string | undefined;
212
190
  export declare function propertyRefItemToPath(getNamespace: GetNamespace, item: PropertyRefItem): string[];
213
191
  export declare function propertyRefToArgs(projectSchema: ProjectSchemaJSON, ref: string | PropertyRefItem): Maybe<Args>;
214
192
  /**
@@ -224,7 +202,8 @@ export type QueryEntry = {
224
202
  };
225
203
  export declare function getQuery(projectSchema: Pick<ProjectSchemaJSON, 'services' | 'shapes' | 'queries' | 'mutations'>, queryRef: string): Maybe<QueryEntry>;
226
204
  export declare function ensureQuery(projectSchema: ProjectSchemaJSON, queryRef: string): QueryEntry;
227
- export declare function normalizeRefs(projectSchema: ProjectSchemaJSON, property: PropertySchema): PropertySchema;
205
+ export declare function normalizeRefs(projectSchema: ProjectSchemaJSON): ProjectSchemaJSON;
206
+ export declare function normalizeRefs(projectSchema: ProjectSchemaJSON, property?: PropertySchema): PropertySchema;
228
207
  /**
229
208
  * Get a ShapeJSON from a RefItem, with the schema dereferenced.
230
209
  */