@colyseus/schema 3.0.0-alpha.37 → 3.0.0-alpha.39

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 (45) hide show
  1. package/build/cjs/index.js +142 -32
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +142 -33
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +142 -32
  6. package/lib/Metadata.d.ts +3 -2
  7. package/lib/Metadata.js +57 -15
  8. package/lib/Metadata.js.map +1 -1
  9. package/lib/Reflection.d.ts +4 -3
  10. package/lib/Reflection.js +36 -11
  11. package/lib/Reflection.js.map +1 -1
  12. package/lib/Schema.d.ts +2 -2
  13. package/lib/Schema.js.map +1 -1
  14. package/lib/annotations.d.ts +29 -12
  15. package/lib/annotations.js +36 -3
  16. package/lib/annotations.js.map +1 -1
  17. package/lib/codegen/parser.js +83 -0
  18. package/lib/codegen/parser.js.map +1 -1
  19. package/lib/codegen/types.js +3 -0
  20. package/lib/codegen/types.js.map +1 -1
  21. package/lib/decoder/DecodeOperation.js +4 -0
  22. package/lib/decoder/DecodeOperation.js.map +1 -1
  23. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  24. package/lib/encoder/EncodeOperation.js +2 -2
  25. package/lib/encoder/EncodeOperation.js.map +1 -1
  26. package/lib/index.d.ts +1 -1
  27. package/lib/index.js +2 -1
  28. package/lib/index.js.map +1 -1
  29. package/lib/types/HelperTypes.d.ts +34 -2
  30. package/lib/types/HelperTypes.js.map +1 -1
  31. package/lib/types/TypeContext.js +9 -1
  32. package/lib/types/TypeContext.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/Metadata.ts +62 -16
  35. package/src/Reflection.ts +43 -16
  36. package/src/Schema.ts +2 -3
  37. package/src/annotations.ts +68 -18
  38. package/src/codegen/parser.ts +107 -0
  39. package/src/codegen/types.ts +1 -0
  40. package/src/decoder/DecodeOperation.ts +4 -0
  41. package/src/decoder/strategy/StateCallbacks.ts +1 -1
  42. package/src/encoder/EncodeOperation.ts +4 -2
  43. package/src/index.ts +1 -1
  44. package/src/types/HelperTypes.ts +54 -2
  45. package/src/types/TypeContext.ts +11 -1
package/src/index.ts CHANGED
@@ -41,7 +41,7 @@ export {
41
41
 
42
42
  // Annotations, Metadata and TypeContext
43
43
  export { Metadata } from "./Metadata";
44
- export { type, deprecated, defineTypes, view, } from "./annotations";
44
+ export { type, deprecated, defineTypes, view, schema, type SchemaWithExtends, } from "./annotations";
45
45
  export { TypeContext } from "./types/TypeContext";
46
46
 
47
47
  // Annotation types
@@ -1,5 +1,11 @@
1
- import { ArraySchema } from "./custom/ArraySchema";
2
- import { MapSchema } from "./custom/MapSchema";
1
+ import type { Definition, DefinitionType, PrimitiveType, RawPrimitiveType } from "../annotations";
2
+ import type { Schema } from "../Schema";
3
+ import type { ArraySchema } from "./custom/ArraySchema";
4
+ import type { CollectionSchema } from "./custom/CollectionSchema";
5
+ import type { MapSchema } from "./custom/MapSchema";
6
+ import type { SetSchema } from "./custom/SetSchema";
7
+
8
+ export type Constructor<T = {}> = new (...args: any[]) => T;
3
9
 
4
10
  export interface Collection<K = any, V = any, IT = V> {
5
11
  [Symbol.iterator](): IterableIterator<IT>;
@@ -7,6 +13,52 @@ export interface Collection<K = any, V = any, IT = V> {
7
13
  entries(): IterableIterator<[K, V]>;
8
14
  }
9
15
 
16
+ export type InferValueType<T extends DefinitionType> =
17
+ T extends "string" ? string
18
+ : T extends "number" ? number
19
+ : T extends "int8" ? number
20
+ : T extends "uint8" ? number
21
+ : T extends "int16" ? number
22
+ : T extends "uint16" ? number
23
+ : T extends "int32" ? number
24
+ : T extends "uint32" ? number
25
+ : T extends "int64" ? number
26
+ : T extends "uint64" ? number
27
+ : T extends "float32" ? number
28
+ : T extends "float64" ? number
29
+ : T extends "boolean" ? boolean
30
+
31
+ : T extends { type: infer ChildType extends Constructor } ? InstanceType<ChildType>
32
+ : T extends { type: infer ChildType extends PrimitiveType } ? ChildType
33
+
34
+ : T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[]
35
+ : T extends Array<infer ChildType extends RawPrimitiveType> ? ChildType[]
36
+
37
+ : T extends { array: infer ChildType extends Constructor } ? InstanceType<ChildType>[]
38
+ : T extends { array: infer ChildType extends PrimitiveType } ? ChildType[]
39
+
40
+ : T extends { map: infer ChildType extends Constructor } ? MapSchema<InstanceType<ChildType>>
41
+ : T extends { map: infer ChildType extends PrimitiveType } ? MapSchema<ChildType>
42
+
43
+ : T extends { set: infer ChildType extends Constructor } ? SetSchema<InstanceType<ChildType>>
44
+ : T extends { set: infer ChildType extends PrimitiveType } ? SetSchema<ChildType>
45
+
46
+ : T extends { collection: infer ChildType extends Constructor } ? CollectionSchema<InstanceType<ChildType>>
47
+ : T extends { collection: infer ChildType extends PrimitiveType } ? CollectionSchema<ChildType>
48
+
49
+ : T extends Constructor ? InstanceType<T>
50
+ : T extends PrimitiveType ? T
51
+
52
+ : never;
53
+
54
+ export type InferSchemaInstanceType<T extends Definition> = {
55
+ [K in keyof T]: InferValueType<T[K]>
56
+ } & Schema;
57
+
58
+ export type DefinedSchemaType<T extends Definition, P extends typeof Schema> = {
59
+ new (): InferSchemaInstanceType<T> & InstanceType<P>;
60
+ } & typeof Schema;
61
+
10
62
  export type NonFunctionProps<T> = Omit<T, {
11
63
  [K in keyof T]: T[K] extends Function ? K : never;
12
64
  }[keyof T]>;
@@ -53,7 +53,7 @@ export class TypeContext {
53
53
  // Workaround to allow using an empty Schema (with no `@type()` fields)
54
54
  //
55
55
  if (schema[Symbol.metadata] === undefined) {
56
- Metadata.init(schema);
56
+ Metadata.initialize(schema);
57
57
  }
58
58
 
59
59
  this.schemas.set(schema, typeid);
@@ -74,6 +74,16 @@ export class TypeContext {
74
74
  this.discoverTypes(child, parentIndex, parentFieldViewTag);
75
75
  });
76
76
 
77
+ // add parent classes
78
+ let parent: any = klass;
79
+ while (
80
+ (parent = Object.getPrototypeOf(parent)) &&
81
+ parent !== Schema && // stop at root (Schema)
82
+ parent !== Function.prototype // stop at root (non-Schema)
83
+ ) {
84
+ this.discoverTypes(parent);
85
+ }
86
+
77
87
  const metadata: Metadata = (klass[Symbol.metadata] ??= {});
78
88
 
79
89
  // if any schema/field has filters, mark "context" as having filters.