@takeshape/schema 11.88.2 → 11.88.4

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.
@@ -1,9 +1,9 @@
1
1
  import type { ObjectSchema } from '../project-schema/latest.ts';
2
2
  import { type QueryEntry } from '../refs.ts';
3
- import type { IRuntimeSchema } from './types.ts';
3
+ import type { ReferenceResolver } from './types.ts';
4
4
  export declare class Query {
5
5
  #private;
6
- constructor(parent: IRuntimeSchema, entry: QueryEntry);
6
+ constructor(parent: ReferenceResolver, entry: QueryEntry);
7
7
  get ref(): string;
8
8
  get argsSchema(): ObjectSchema | undefined;
9
9
  }
@@ -1,11 +1,11 @@
1
1
  import { serializePropertyRef } from "../refs.js";
2
2
  export class Query {
3
- #parent;
3
+ #refResolver;
4
4
  #name;
5
5
  #query;
6
6
  #ref;
7
7
  constructor(parent, entry) {
8
- this.#parent = parent;
8
+ this.#refResolver = parent;
9
9
  this.#name = entry.name;
10
10
  this.#query = entry.query;
11
11
  this.#ref = entry.ref;
@@ -19,7 +19,7 @@ export class Query {
19
19
  if (typeof args === 'object') {
20
20
  return args;
21
21
  }
22
- return this.#parent.getShape(args)?.getObjectSchema();
22
+ return this.#refResolver.getShape(args)?.getObjectSchema();
23
23
  }
24
24
  return undefined;
25
25
  }
@@ -1,13 +1,13 @@
1
- import type { ProjectSchemaJSON, PropertySchema } from '../project-schema/index.ts';
1
+ import type { ProjectSchemaJSON } from '../project-schema/index.ts';
2
2
  import type { ServiceLayers } from '../types/types.ts';
3
3
  import { Query } from './query.js';
4
4
  import { Shape } from './shape.js';
5
- import type { IRuntimeSchema } from './types.ts';
6
- export declare class RuntimeSchema implements IRuntimeSchema {
5
+ export declare class RuntimeSchema {
7
6
  #private;
8
- constructor(userSchema: ProjectSchemaJSON, layers: ServiceLayers);
9
- get runtimeSchemaJson(): ProjectSchemaJSON;
10
- getShape(rawShapeRef: string): Shape | undefined;
11
- getQuery(rawQueryRef: string): Query | undefined;
12
- dereferenceSchema(propertySchema: PropertySchema): PropertySchema;
7
+ private constructor();
8
+ static buildSchema(userSchema: ProjectSchemaJSON, layers: ServiceLayers): RuntimeSchema;
9
+ static fromJson(runtimeSchema: ProjectSchemaJSON, layers: ServiceLayers): RuntimeSchema;
10
+ getQuery(ref: string): Query | undefined;
11
+ getShape(ref: string): Shape | undefined;
12
+ get json(): ProjectSchemaJSON;
13
13
  }
@@ -3,33 +3,25 @@ import { buildRuntimeSchema } from "../runtime-schema.js";
3
3
  import { getShape } from "../util/shapes.js";
4
4
  import { Query } from './query.js';
5
5
  import { Shape } from './shape.js';
6
- export class RuntimeSchema {
7
- #userSchema;
8
- #runtimeSchema;
6
+ class ProjectSchemaRefResolver {
7
+ #runtimeSchemaJson;
9
8
  #layers;
10
9
  #shapeCache;
11
10
  #queryCache;
12
- constructor(userSchema, layers) {
13
- this.#userSchema = userSchema;
11
+ constructor(runtimeSchema, layers) {
12
+ this.#runtimeSchemaJson = runtimeSchema;
14
13
  this.#layers = layers;
15
14
  this.#shapeCache = new Map();
16
15
  this.#queryCache = new Map();
17
16
  }
18
- get runtimeSchemaJson() {
19
- if (!this.#runtimeSchema) {
20
- // biome-ignore lint/suspicious/noConsole: ignore
21
- this.#runtimeSchema = buildRuntimeSchema(this.#userSchema, this.#layers, console.log);
22
- }
23
- return this.#runtimeSchema;
24
- }
25
17
  getShape(rawShapeRef) {
26
- const shapeRef = normalizeRefExpression(this.#userSchema, rawShapeRef);
18
+ const shapeRef = normalizeRefExpression(this.#runtimeSchemaJson, rawShapeRef);
27
19
  let shape = this.#shapeCache.get(shapeRef);
28
20
  if (!shape) {
29
- const refItem = refExpressionToRefItem(this.runtimeSchemaJson, rawShapeRef);
21
+ const refItem = refExpressionToRefItem(this.#runtimeSchemaJson, rawShapeRef);
30
22
  if (refItem) {
31
23
  const namespacedName = refItemToNamespacedShapeName(refItem);
32
- const shapeJson = getShape(this.runtimeSchemaJson, namespacedName);
24
+ const shapeJson = getShape(this.#runtimeSchemaJson, namespacedName);
33
25
  if (shapeJson) {
34
26
  shape = new Shape(this, refItem, shapeJson);
35
27
  this.#shapeCache.set(shapeRef, shape);
@@ -52,7 +44,7 @@ export class RuntimeSchema {
52
44
  const queryRef = normalizePropertyRef(rawQueryRef);
53
45
  let query = this.#queryCache.get(queryRef);
54
46
  if (!query) {
55
- const queryEntry = getQuery(this.runtimeSchemaJson, queryRef);
47
+ const queryEntry = getQuery(this.#runtimeSchemaJson, queryRef);
56
48
  if (queryEntry) {
57
49
  query = new Query(this, queryEntry);
58
50
  this.#queryCache.set(queryRef, query);
@@ -74,6 +66,33 @@ export class RuntimeSchema {
74
66
  return query;
75
67
  }
76
68
  dereferenceSchema(propertySchema) {
77
- return dereferenceSchema(this.runtimeSchemaJson, propertySchema);
69
+ return dereferenceSchema(this.#runtimeSchemaJson, propertySchema);
70
+ }
71
+ }
72
+ export class RuntimeSchema {
73
+ #runtimeSchemaJson;
74
+ #layers;
75
+ #refResolver;
76
+ constructor(runtimeSchema, layers) {
77
+ this.#runtimeSchemaJson = runtimeSchema;
78
+ this.#layers = layers;
79
+ this.#refResolver = new ProjectSchemaRefResolver(runtimeSchema, layers);
80
+ }
81
+ static buildSchema(userSchema, layers) {
82
+ // biome-ignore lint/suspicious/noConsole: ignore
83
+ const runtimeSchema = buildRuntimeSchema(userSchema, layers, console.log);
84
+ return new RuntimeSchema(runtimeSchema, layers);
85
+ }
86
+ static fromJson(runtimeSchema, layers) {
87
+ return new RuntimeSchema(runtimeSchema, layers);
88
+ }
89
+ getQuery(ref) {
90
+ return this.#refResolver.getQuery(ref);
91
+ }
92
+ getShape(ref) {
93
+ return this.#refResolver.getShape(ref);
94
+ }
95
+ get json() {
96
+ return this.#runtimeSchemaJson;
78
97
  }
79
98
  }
@@ -1,9 +1,9 @@
1
1
  import type { ShapeJSON } from '../project-schema/latest.ts';
2
2
  import { type RefItem } from '../refs.ts';
3
- import type { IRuntimeSchema } from './types.ts';
3
+ import type { ReferenceResolver } from './types.ts';
4
4
  export declare class Shape {
5
5
  #private;
6
- constructor(parent: IRuntimeSchema, ref: RefItem, shape: ShapeJSON);
6
+ constructor(parent: ReferenceResolver, ref: RefItem, shape: ShapeJSON);
7
7
  get name(): string;
8
8
  get ref(): string;
9
9
  get refItem(): RefItem;
@@ -1,12 +1,12 @@
1
1
  import { refItemToAtRef } from "../refs.js";
2
2
  import { isObjectSchema } from "../types/utils.js";
3
3
  export class Shape {
4
- #parent;
4
+ #refResolver;
5
5
  #ref;
6
6
  #shape;
7
7
  #dereferencedSchema;
8
8
  constructor(parent, ref, shape) {
9
- this.#parent = parent;
9
+ this.#refResolver = parent;
10
10
  this.#ref = ref;
11
11
  this.#shape = shape;
12
12
  }
@@ -21,7 +21,7 @@ export class Shape {
21
21
  }
22
22
  getObjectSchema() {
23
23
  if (!this.#dereferencedSchema) {
24
- this.#dereferencedSchema = this.#parent.dereferenceSchema(this.#shape.schema);
24
+ this.#dereferencedSchema = this.#refResolver.dereferenceSchema(this.#shape.schema);
25
25
  }
26
26
  if (isObjectSchema(this.#dereferencedSchema)) {
27
27
  return this.#dereferencedSchema;
@@ -1,7 +1,7 @@
1
1
  import type { Query } from '@/src/models/query.js';
2
2
  import type { Shape } from '@/src/models/shape.js';
3
3
  import type { PropertySchema } from '../project-schema/index.ts';
4
- export interface IRuntimeSchema {
4
+ export interface ReferenceResolver {
5
5
  getShape(shapeRef: string): Shape | undefined;
6
6
  getQuery(queryRef: string): Query | undefined;
7
7
  dereferenceSchema(propertySchema: PropertySchema): PropertySchema;
package/dist/refs.d.ts CHANGED
@@ -15,7 +15,8 @@ export type RefItem = {
15
15
  template?: string;
16
16
  isForeign: boolean;
17
17
  isValidService: boolean;
18
- } & Record<string, unknown>;
18
+ isInterfaceRef?: boolean;
19
+ };
19
20
  /**
20
21
  * Adds a schema path, needed when visiting a whole schema to report errors.
21
22
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/schema",
3
- "version": "11.88.2",
3
+ "version": "11.88.4",
4
4
  "description": "TakeShape Schema",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -57,9 +57,9 @@
57
57
  "p-reduce": "^2.1.0",
58
58
  "semver": "^7.3.2",
59
59
  "tiny-invariant": "^1.2.0",
60
- "@takeshape/errors": "11.88.2",
61
- "@takeshape/json-schema": "11.88.2",
62
- "@takeshape/util": "11.88.2"
60
+ "@takeshape/errors": "11.88.4",
61
+ "@takeshape/json-schema": "11.88.4",
62
+ "@takeshape/util": "11.88.4"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@takeshape/json-schema-to-typescript": "^11.0.0",
@@ -76,7 +76,7 @@
76
76
  "json-schema-to-ts": "^3.1.1",
77
77
  "meow": "^9.0.0",
78
78
  "shortid": "^2.2.15",
79
- "@takeshape/infra": "11.88.2"
79
+ "@takeshape/infra": "11.88.4"
80
80
  },
81
81
  "engines": {
82
82
  "node": ">=20"