@fibery/schema 11.3.0 → 12.0.1

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.
package/lib/brands.d.ts CHANGED
@@ -1,13 +1,26 @@
1
- import { HolderTODO, KindTODO } from "@fibery/helpers/utils/types";
1
+ import { Holder, Kind } from "@fibery/helpers/utils/types";
2
2
  //#region src/brands.d.ts
3
- type Id = string & KindTODO<"id">;
4
- type PublicId = string & KindTODO<"publicId">;
5
- type Name = string & KindTODO<"name">;
6
- type TypeId = Id & HolderTODO<"type">;
7
- type FieldId = Id & HolderTODO<"field">;
8
- type EntityId = Id & HolderTODO<"entity">;
9
- type TypeName = Name & HolderTODO<"type">;
10
- type FieldName = Name & HolderTODO<"field">;
11
- type SpaceName = Name & HolderTODO<"space">;
3
+ type Id = string & Kind<"id">;
4
+ type PublicId = string & Kind<"publicId">;
5
+ type Name = string & Kind<"name">;
6
+ type TypeId = string & Kind<"id"> & Holder<"type">;
7
+ type FieldId = string & Kind<"id"> & Holder<"field">;
8
+ type EntityId = string & Kind<"id"> & Holder<"entity">;
9
+ type SpaceId = string & Kind<"id"> & Holder<"space">;
10
+ type TypeName = string & Kind<"name"> & Holder<"type">;
11
+ type FieldName = string & Kind<"name"> & Holder<"field">;
12
+ type SpaceName = string & Kind<"name"> & Holder<"space">;
13
+ /**
14
+ * A brand with its tags made optional, for lookup parameters.
15
+ *
16
+ * `Partial` turns the required `Kind`/`Holder` into optional properties, so a
17
+ * plain string or literal passes. Sibling brands still carry a *conflicting*
18
+ * tag, so `getTypeObjectByName(fieldName)` remains an error.
19
+ *
20
+ * The leading `string &` is load-bearing: `Partial` also makes
21
+ * `String.prototype` optional, and the getters index a lookup map with the
22
+ * parameter.
23
+ */
24
+ type Permissive<B extends string> = string & Partial<B>;
12
25
  //#endregion
13
- export { EntityId, FieldId, FieldName, Id, Name, PublicId, SpaceName, TypeId, TypeName };
26
+ export { EntityId, FieldId, FieldName, Id, Name, Permissive, PublicId, SpaceId, SpaceName, TypeId, TypeName };
package/lib/errors.d.ts CHANGED
@@ -1,5 +1,26 @@
1
1
  //#region src/errors.d.ts
2
2
  declare class NotDefinedGetError extends Error {}
3
- declare class NotFoundError extends Error {}
3
+ type NotFoundNames = {
4
+ typeId?: string;
5
+ typeName?: string;
6
+ fieldId?: string;
7
+ fieldName?: string;
8
+ };
9
+ /**
10
+ * `message` says only what kind of thing was missing, so it is a safe Sentry title and a
11
+ * bounded grouping key. The names are own enumerable properties, which is what
12
+ * `extraErrorDataIntegration` collects into the event, and what devtools shows on expand.
13
+ *
14
+ * Throw sites fill in both the id and the name wherever they can resolve one: a bare uuid is
15
+ * unactionable in an issue, and a name alone will not survive a rename.
16
+ */
17
+ declare class NotFoundError extends Error {
18
+ readonly what: "type" | "field";
19
+ readonly typeId: string | undefined;
20
+ readonly typeName: string | undefined;
21
+ readonly fieldId: string | undefined;
22
+ readonly fieldName: string | undefined;
23
+ constructor(what: "type" | "field", { typeId, typeName, fieldId, fieldName }: NotFoundNames);
24
+ }
4
25
  //#endregion
5
- export { NotDefinedGetError, NotFoundError };
26
+ export { NotDefinedGetError, NotFoundError, NotFoundNames };
package/lib/errors.js CHANGED
@@ -1,5 +1,28 @@
1
1
  //#region src/errors.ts
2
2
  var NotDefinedGetError = class extends Error {};
3
- var NotFoundError = class extends Error {};
3
+ /**
4
+ * `message` says only what kind of thing was missing, so it is a safe Sentry title and a
5
+ * bounded grouping key. The names are own enumerable properties, which is what
6
+ * `extraErrorDataIntegration` collects into the event, and what devtools shows on expand.
7
+ *
8
+ * Throw sites fill in both the id and the name wherever they can resolve one: a bare uuid is
9
+ * unactionable in an issue, and a name alone will not survive a rename.
10
+ */
11
+ var NotFoundError = class extends Error {
12
+ what;
13
+ typeId;
14
+ typeName;
15
+ fieldId;
16
+ fieldName;
17
+ constructor(what, { typeId, typeName, fieldId, fieldName }) {
18
+ super(`${what} not found`);
19
+ this.name = "NotFoundError";
20
+ this.what = what;
21
+ this.typeId = typeId;
22
+ this.typeName = typeName;
23
+ this.fieldId = fieldId;
24
+ this.fieldName = fieldName;
25
+ }
26
+ };
4
27
  //#endregion
5
28
  export { NotDefinedGetError, NotFoundError };
@@ -1,4 +1,4 @@
1
- import { FieldId, FieldName, TypeName } from "./brands.js";
1
+ import { FieldId, FieldName, SpaceName, TypeName } from "./brands.js";
2
2
  import { TypeObject } from "./type-object.js";
3
3
  import { SchemaRef } from "./schema.js";
4
4
  //#region src/field-object.d.ts
@@ -77,12 +77,12 @@ type RawFieldObject = {
77
77
  };
78
78
  type FieldObjectParams = {
79
79
  rawFieldObject: RawFieldObject;
80
- holderType: string;
80
+ holderType: TypeName;
81
81
  schema: SchemaRef;
82
82
  };
83
83
  declare class FieldObject {
84
84
  private readonly raw;
85
- readonly holderType: string;
85
+ readonly holderType: TypeName;
86
86
  private readonly schema;
87
87
  /** Lazily filled; declared here so all instances share one hidden class. */
88
88
  private cache;
@@ -119,7 +119,7 @@ declare class FieldObject {
119
119
  get isDependency(): boolean;
120
120
  get isDependencyStart(): boolean;
121
121
  get nameParts(): Readonly<{
122
- namespace: string;
122
+ namespace: SpaceName;
123
123
  name: string;
124
124
  }>;
125
125
  get title(): string;
@@ -144,8 +144,12 @@ var FieldObject = class {
144
144
  get typeObject() {
145
145
  const typeObject = this.resolveTypeObject();
146
146
  if (!typeObject) {
147
- if (relaxable("fieldObject.typeObject")) return;
148
- throw new NotFoundError(`!schema.typeObjectsByName["${this.type}"]`);
147
+ if (relaxable()) return;
148
+ throw new NotFoundError("type", {
149
+ typeName: this.type,
150
+ fieldId: this.id,
151
+ fieldName: this.name
152
+ });
149
153
  }
150
154
  return typeObject;
151
155
  }
@@ -155,8 +159,12 @@ var FieldObject = class {
155
159
  if (!value) {
156
160
  const holderTypeObject = this.schema.resolveTypeObject(this.holderType);
157
161
  if (!holderTypeObject) {
158
- if (relaxable("fieldObject.holderTypeObject")) return;
159
- throw new NotFoundError(`!schema.typeObjectsByName["${this.holderType}"]`);
162
+ if (relaxable()) return;
163
+ throw new NotFoundError("type", {
164
+ typeName: this.holderType,
165
+ fieldId: this.id,
166
+ fieldName: this.name
167
+ });
160
168
  }
161
169
  value = holderTypeObject;
162
170
  cache.holderTypeObject = value;
package/lib/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { EntityId, FieldId, FieldName, Id, Name, PublicId, SpaceName, TypeId, TypeName } from "./brands.js";
2
- import { NotDefinedGetError, NotFoundError } from "./errors.js";
1
+ import { EntityId, FieldId, FieldName, Id, Name, Permissive, PublicId, SpaceId, SpaceName, TypeId, TypeName } from "./brands.js";
2
+ import { NotDefinedGetError, NotFoundError, NotFoundNames } from "./errors.js";
3
3
  import { RawTypeObject, SyncSource, TypeObject, TypeObjectFactory, TypeObjectParams } from "./type-object.js";
4
4
  import { RawSchema, Schema, SchemaRef } from "./schema.js";
5
5
  import { Cardinality, FieldObject, FieldObjectParams, LinkRule, LinkRuleOperand, RawFieldObject, SyncField, SyncRelation } from "./field-object.js";
6
6
  import { Factory, factory, protectiveFactory } from "./factory.js";
7
7
  import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
8
- export { Cardinality, EntityId, Factory, FieldId, FieldName, FieldObject, FieldObjectParams, Id, LinkRule, LinkRuleOperand, Name, NotDefinedGetError, NotFoundError, PublicId, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SpaceName, SyncField, SyncRelation, SyncSource, TypeId, TypeName, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
8
+ export { Cardinality, EntityId, Factory, FieldId, FieldName, FieldObject, FieldObjectParams, Id, LinkRule, LinkRuleOperand, Name, NotDefinedGetError, NotFoundError, NotFoundNames, Permissive, PublicId, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SpaceId, SpaceName, SyncField, SyncRelation, SyncSource, TypeId, TypeName, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
@@ -1,5 +1,7 @@
1
1
  //#region src/relaxable.d.ts
2
2
  declare const relaxableRealm: () => boolean;
3
- declare function relaxable(method: string): boolean;
3
+ declare let relaxable: () => boolean;
4
+ /** Temporary application policy while strict lookups roll out. Standalone schema stays strict. */
5
+ declare function setRelaxablePredicate(next: () => boolean): () => void;
4
6
  //#endregion
5
- export { relaxable, relaxableRealm };
7
+ export { relaxable, relaxableRealm, setRelaxablePredicate };
package/lib/relaxable.js CHANGED
@@ -1,10 +1,13 @@
1
- import { traceError } from "@fibery/helpers/utils/trace";
2
1
  //#region src/relaxable.ts
3
2
  const relaxableRealm = () => typeof window !== "undefined" && Boolean(window["x-ui-version"]);
4
- function relaxable(method) {
5
- if (!relaxableRealm()) return false;
6
- traceError(/* @__PURE__ */ new Error(`Relaxed ${method}, return undefined`));
7
- return true;
3
+ let relaxable = () => false;
4
+ /** Temporary application policy while strict lookups roll out. Standalone schema stays strict. */
5
+ function setRelaxablePredicate(next) {
6
+ const previous = relaxable;
7
+ relaxable = next;
8
+ return () => {
9
+ relaxable = previous;
10
+ };
8
11
  }
9
12
  //#endregion
10
- export { relaxable, relaxableRealm };
13
+ export { relaxable, relaxableRealm, setRelaxablePredicate };
package/lib/schema.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TypeId, TypeName } from "./brands.js";
1
+ import { Permissive, TypeId, TypeName } from "./brands.js";
2
2
  import { RawTypeObject, TypeObject } from "./type-object.js";
3
3
  import { FieldObject } from "./field-object.js";
4
4
  import { Factory } from "./factory.js";
@@ -14,7 +14,7 @@ type RawSchema = {
14
14
  */
15
15
  type SchemaRef = {
16
16
  readonly version: string;
17
- resolveTypeObject: (type: string) => TypeObject | null;
17
+ resolveTypeObject: (type: Permissive<TypeName>) => TypeObject | null;
18
18
  resolveTypeComponentFieldObject: (type: string) => FieldObject | null;
19
19
  resolveMultiRelatedFieldObjects: (multiRelation: string) => ReadonlyArray<FieldObject>;
20
20
  };
@@ -36,15 +36,15 @@ declare class Schema implements SchemaRef {
36
36
  };
37
37
  };
38
38
  /** @internal resolves a field's `fibery/type` to its type object */
39
- resolveTypeObject(type: TypeName): TypeObject | null;
39
+ resolveTypeObject(type: Permissive<TypeName>): TypeObject | null;
40
40
  /** @internal the type-component field pointing at `type`, if any */
41
41
  resolveTypeComponentFieldObject(type: string): FieldObject | null;
42
42
  /** @internal the non-entity-ref ends of a multi-relation */
43
43
  resolveMultiRelatedFieldObjects(multiRelation: string): ReadonlyArray<FieldObject>;
44
- getTypeObjectById(typeId: TypeId): TypeObject | null;
45
- getTypeObjectByName(typeName: TypeName): TypeObject | null;
46
- getTypeObjectByIdOrThrow(typeId: TypeId): TypeObject;
47
- getTypeObjectByNameOrThrow(typeName: TypeName): TypeObject;
44
+ getTypeObjectById(typeId: Permissive<TypeId>): TypeObject | null;
45
+ getTypeObjectByName(typeName: Permissive<TypeName>): TypeObject | null;
46
+ getTypeObjectByIdOrThrow(typeId: Permissive<TypeId>): TypeObject;
47
+ getTypeObjectByNameOrThrow(typeName: Permissive<TypeName>): TypeObject;
48
48
  }
49
49
  //#endregion
50
50
  export { RawSchema, Schema, SchemaRef };
package/lib/schema.js CHANGED
@@ -4,6 +4,31 @@ import { relaxable } from "./relaxable.js";
4
4
  const EMPTY_FIELD_OBJECTS = Object.freeze([]);
5
5
  const typeComponentFieldObjects = /* @__PURE__ */ new WeakMap();
6
6
  const multiRelatedFieldObjects = /* @__PURE__ */ new WeakMap();
7
+ /**
8
+ * Children's view of their schema. Methods live on the prototype and the Schema itself in a
9
+ * WeakMap, so the only own property is `version` — a deep-equal walking a type object stops
10
+ * here and compares schema versions instead of re-entering the whole graph.
11
+ */
12
+ const backRefSchemas = /* @__PURE__ */ new WeakMap();
13
+ var SchemaBackRef = class {
14
+ version;
15
+ constructor(schema) {
16
+ this.version = schema.version;
17
+ backRefSchemas.set(this, schema);
18
+ }
19
+ get schema() {
20
+ return backRefSchemas.get(this);
21
+ }
22
+ resolveTypeObject(type) {
23
+ return this.schema.resolveTypeObject(type);
24
+ }
25
+ resolveTypeComponentFieldObject(type) {
26
+ return this.schema.resolveTypeComponentFieldObject(type);
27
+ }
28
+ resolveMultiRelatedFieldObjects(multiRelation) {
29
+ return this.schema.resolveMultiRelatedFieldObjects(multiRelation);
30
+ }
31
+ };
7
32
  var Schema = class {
8
33
  typeObjects;
9
34
  typeObjectsById;
@@ -16,9 +41,10 @@ var Schema = class {
16
41
  this.id = id;
17
42
  this.version = version;
18
43
  this.rawTypeObjects = rawTypeObjects;
44
+ const schemaRef = new SchemaBackRef(this);
19
45
  this.typeObjects = rawTypeObjects.map((rawTypeObject) => factory.makeTypeObject({
20
46
  rawTypeObject,
21
- schema: this
47
+ schema: schemaRef
22
48
  }));
23
49
  this.typeObjectsById = factory.makeTypeObjectsById(this.typeObjects);
24
50
  this.typeObjectsByName = factory.makeTypeObjectsByName(this.typeObjects);
@@ -66,16 +92,16 @@ var Schema = class {
66
92
  getTypeObjectByIdOrThrow(typeId) {
67
93
  const typeObject = this.getTypeObjectById(typeId);
68
94
  if (!typeObject) {
69
- if (relaxable("getTypeObjectByIdOrThrow")) return;
70
- throw new NotFoundError(`!schema.typeObjectsById["${typeId}"]`);
95
+ if (relaxable()) return;
96
+ throw new NotFoundError("type", { typeId });
71
97
  }
72
98
  return typeObject;
73
99
  }
74
100
  getTypeObjectByNameOrThrow(typeName) {
75
101
  const typeObject = this.getTypeObjectByName(typeName);
76
102
  if (!typeObject) {
77
- if (relaxable("getTypeObjectByNameOrThrow")) return;
78
- throw new NotFoundError(`!schema.typeObjectsByName["${typeName}"]`);
103
+ if (relaxable()) return;
104
+ throw new NotFoundError("type", { typeName });
79
105
  }
80
106
  return typeObject;
81
107
  }
@@ -1,4 +1,4 @@
1
- import { FieldId, FieldName, TypeId, TypeName } from "./brands.js";
1
+ import { FieldId, FieldName, Permissive, SpaceName, TypeId, TypeName } from "./brands.js";
2
2
  import { SchemaRef } from "./schema.js";
3
3
  import { FieldObject, FieldObjectParams, RawFieldObject } from "./field-object.js";
4
4
  //#region src/type-object.d.ts
@@ -58,10 +58,10 @@ declare class TypeObject {
58
58
  private cache;
59
59
  constructor({ rawTypeObject, schema, factory }: TypeObjectParams);
60
60
  toJSON(): string | RawTypeObject;
61
- getFieldObjectById(fieldId: FieldId): FieldObject | null;
62
- getFieldObjectByName(fieldName: FieldName): FieldObject | null;
63
- getFieldObjectByIdOrThrow(fieldId: FieldId): FieldObject;
64
- getFieldObjectByNameOrThrow(fieldName: FieldName): FieldObject;
61
+ getFieldObjectById(fieldId: Permissive<FieldId>): FieldObject | null;
62
+ getFieldObjectByName(fieldName: Permissive<FieldName>): FieldObject | null;
63
+ getFieldObjectByIdOrThrow(fieldId: Permissive<FieldId>): FieldObject;
64
+ getFieldObjectByNameOrThrow(fieldName: Permissive<FieldName>): FieldObject;
65
65
  get rawMeta(): Readonly<RawTypeObject["fibery/meta"]>;
66
66
  get name(): TypeName;
67
67
  get description(): string | null;
@@ -81,7 +81,7 @@ declare class TypeObject {
81
81
  get units(): unknown[] | null;
82
82
  get syncSource(): SyncSource | null;
83
83
  get nameParts(): Readonly<{
84
- namespace: string;
84
+ namespace: SpaceName;
85
85
  name: string;
86
86
  }>;
87
87
  get installedMixins(): ReadonlySet<string>;
@@ -33,16 +33,24 @@ var TypeObject = class {
33
33
  getFieldObjectByIdOrThrow(fieldId) {
34
34
  const fieldObject = this.getFieldObjectById(fieldId);
35
35
  if (!fieldObject) {
36
- if (relaxable("getFieldObjectByIdOrThrow")) return;
37
- throw new NotFoundError(`!schema.typeObjectsByName["${this.name}"].fieldObjectsById["${fieldId}"]`);
36
+ if (relaxable()) return;
37
+ throw new NotFoundError("field", {
38
+ typeId: this.id,
39
+ typeName: this.name,
40
+ fieldId
41
+ });
38
42
  }
39
43
  return fieldObject;
40
44
  }
41
45
  getFieldObjectByNameOrThrow(fieldName) {
42
46
  const fieldObject = this.getFieldObjectByName(fieldName);
43
47
  if (!fieldObject) {
44
- if (relaxable("getFieldObjectByNameOrThrow")) return;
45
- throw new NotFoundError(`!schema.typeObjectsByName["${this.name}"].fieldObjectsByName["${fieldName}"]`);
48
+ if (relaxable()) return;
49
+ throw new NotFoundError("field", {
50
+ typeId: this.id,
51
+ typeName: this.name,
52
+ fieldName
53
+ });
46
54
  }
47
55
  return fieldObject;
48
56
  }
package/lib/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { SpaceName, TypeName } from "./brands.js";
1
2
  import { RawTypeObject, TypeObject } from "./type-object.js";
2
3
  import { RawSchema } from "./schema.js";
3
4
  //#region src/utils.d.ts
@@ -9,13 +10,13 @@ declare const toPascalCase: (lispCasedString?: string) => string;
9
10
  declare const toNonEnumTitle: (strName: string) => string;
10
11
  declare const parseEnumType: (type: string) => {
11
12
  templateName: string;
12
- templateNamespace: string;
13
+ templateNamespace: SpaceName;
13
14
  targetName: string;
14
- targetNamespace: string;
15
+ targetNamespace: SpaceName;
15
16
  };
16
17
  declare const toTitle: (typeOrAttr: string) => string;
17
18
  declare const parseType: (type: string) => {
18
- namespace: string;
19
+ namespace: SpaceName;
19
20
  name: string;
20
21
  };
21
22
  declare const makeEnumTypeNamePart: ({ holderType, enumName }: {
@@ -25,7 +26,7 @@ declare const makeEnumTypeNamePart: ({ holderType, enumName }: {
25
26
  declare const attachEnumNamespace: ({ enumNamespace, typeNamePart }: {
26
27
  enumNamespace: string;
27
28
  typeNamePart: string;
28
- }) => string;
29
+ }) => TypeName;
29
30
  declare const removeDeletedTypesAndFields: (rawSchema: RawSchema) => {
30
31
  "fibery/types": RawTypeObject[];
31
32
  "fibery/id": string;
package/lib/utils.js CHANGED
@@ -104,6 +104,7 @@ const attachEnumNamespace = ({ enumNamespace, typeNamePart }) => {
104
104
  const reportedStrippedFields = /* @__PURE__ */ new Set();
105
105
  const reportStrippedField = (holderTypeName, rawFieldObject, isTypeSoftDeleted) => {
106
106
  if (!relaxableRealm()) return;
107
+ if (process.env.NODE_ENV === "production") return;
107
108
  const key = `${holderTypeName} ${rawFieldObject["fibery/name"]} ${rawFieldObject["fibery/type"]}`;
108
109
  if (reportedStrippedFields.has(key)) return;
109
110
  reportedStrippedFields.add(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/schema",
3
- "version": "11.3.0",
3
+ "version": "12.0.1",
4
4
  "description": "Fibery schema utils",
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "lodash": "4.17.21",
25
- "@fibery/helpers": "3.4.0",
25
+ "@fibery/helpers": "4.1.0",
26
26
  "@fibery/pluralize": "1.2.1"
27
27
  }
28
28
  }