@opra/common 1.14.0 → 1.15.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 (39) hide show
  1. package/browser/index.cjs +5 -5
  2. package/browser/index.mjs +5 -5
  3. package/cjs/document/common/document-node.js +11 -0
  4. package/cjs/document/data-type/complex-type-base.js +1 -1
  5. package/cjs/document/data-type/complex-type.js +4 -0
  6. package/cjs/document/data-type/mapped-type.js +4 -0
  7. package/cjs/document/data-type/mixin-type.js +2 -5
  8. package/cjs/document/data-type/union-type.js +84 -0
  9. package/cjs/document/factory/data-type.factory.js +45 -0
  10. package/cjs/document/index.js +1 -0
  11. package/cjs/schema/data-type/union-type.interface.js +7 -0
  12. package/cjs/schema/opra-schema.js +1 -0
  13. package/cjs/schema/type-guards.js +7 -1
  14. package/esm/document/common/document-node.js +11 -0
  15. package/esm/document/data-type/complex-type-base.js +1 -1
  16. package/esm/document/data-type/complex-type.js +4 -0
  17. package/esm/document/data-type/mapped-type.js +4 -0
  18. package/esm/document/data-type/mixin-type.js +2 -5
  19. package/esm/document/data-type/union-type.js +81 -0
  20. package/esm/document/factory/data-type.factory.js +45 -0
  21. package/esm/document/index.js +1 -0
  22. package/esm/schema/data-type/union-type.interface.js +4 -0
  23. package/esm/schema/opra-schema.js +1 -0
  24. package/esm/schema/type-guards.js +6 -1
  25. package/package.json +7 -4
  26. package/types/document/common/document-node.d.ts +7 -0
  27. package/types/document/data-type/complex-type.d.ts +3 -1
  28. package/types/document/data-type/mapped-type.d.ts +2 -0
  29. package/types/document/data-type/mixin-type.d.ts +22 -12
  30. package/types/document/data-type/union-type.d.ts +73 -0
  31. package/types/document/factory/data-type.factory.d.ts +12 -2
  32. package/types/document/index.d.ts +1 -0
  33. package/types/schema/data-type/complex-type.interface.d.ts +2 -0
  34. package/types/schema/data-type/data-type.interface.d.ts +3 -2
  35. package/types/schema/data-type/field.interface.d.ts +4 -0
  36. package/types/schema/data-type/mapped-type.interface.d.ts +2 -0
  37. package/types/schema/data-type/union-type.interface.d.ts +15 -0
  38. package/types/schema/opra-schema.d.ts +1 -0
  39. package/types/schema/type-guards.d.ts +2 -0
@@ -7,6 +7,7 @@ import type { EnumType } from '../data-type/enum-type.js';
7
7
  import type { MappedType } from '../data-type/mapped-type.js';
8
8
  import type { MixinType } from '../data-type/mixin-type.js';
9
9
  import type { SimpleType } from '../data-type/simple-type.js';
10
+ import type { UnionType } from '../data-type/union-type.js';
10
11
  import type { DataTypeMap } from './data-type-map.js';
11
12
  import type { DocumentElement } from './document-element.js';
12
13
  /**
@@ -56,4 +57,10 @@ export declare class DocumentNode {
56
57
  * Throws error if data type is not a MixinType
57
58
  */
58
59
  getMixinType(nameOrCtor: string | object | any[], scope?: string | '*'): MixinType;
60
+ /**
61
+ * Returns EnumType instance by name or Constructor.
62
+ * Returns undefined if not found
63
+ * Throws error if data type is not a UnionType
64
+ */
65
+ getUnionType(nameOrCtor: string | object | any[], scope?: string | '*'): UnionType;
59
66
  }
@@ -19,7 +19,7 @@ export declare namespace ComplexType {
19
19
  additionalFields?: boolean | string | TypeThunkAsync | ['error'] | ['error', string];
20
20
  }, DataType.Metadata, OpraSchema.ComplexType> {
21
21
  }
22
- interface Options extends Combine<Pick<Metadata, 'additionalFields' | 'keyField'>, DataType.Options, Pick<OpraSchema.ComplexType, 'abstract'>> {
22
+ interface Options extends Combine<Pick<Metadata, 'additionalFields' | 'keyField' | 'discriminatorField' | 'discriminatorValue'>, DataType.Options, Pick<OpraSchema.ComplexType, 'abstract'>> {
23
23
  }
24
24
  interface InitArguments extends Combine<{
25
25
  kind: OpraSchema.ComplexType.Kind;
@@ -72,6 +72,8 @@ declare abstract class ComplexTypeClass extends ComplexTypeBase {
72
72
  readonly kind: OpraSchema.ComplexType.Kind;
73
73
  readonly base?: ComplexType | MappedType | MixinType;
74
74
  readonly ctor?: Type;
75
+ discriminatorField?: string;
76
+ discriminatorValue?: string;
75
77
  extendsFrom(baseType: DataType | string | Type | object): boolean;
76
78
  toJSON(options?: ApiDocument.ExportOptions): OpraSchema.ComplexType;
77
79
  protected _locateBase(callback: (base: ComplexTypeBase) => boolean): ComplexTypeBase | undefined;
@@ -68,6 +68,8 @@ declare class MappedTypeClass extends ComplexTypeBase {
68
68
  readonly pick?: Field.Name[];
69
69
  readonly partial?: Field.Name[] | boolean;
70
70
  readonly required?: Field.Name[] | boolean;
71
+ discriminatorField?: string;
72
+ discriminatorValue?: string;
71
73
  extendsFrom(baseType: DataType | string | Type | object): boolean;
72
74
  toJSON(options?: ApiDocument.ExportOptions): OpraSchema.MappedType;
73
75
  protected _locateBase(callback: (base: ComplexTypeBase) => boolean): ComplexTypeBase | undefined;
@@ -39,25 +39,35 @@ export interface MixinTypeStatic {
39
39
  new (owner: DocumentElement, args: MixinType.InitArguments): MixinType;
40
40
  /**
41
41
  * Create a new mixin type from given two types
42
- * @param c1
43
- * @param c2
42
+ * @param types
44
43
  * @param options
45
- */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2>(c1: Class<A1, I1, S1>, c2: Class<A2, I2, S2>, options?: DataType.Options): Class<any[], I1 & I2, S1 & S2>;
44
+ */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2>(types: [Class<A1, I1, S1>, Class<A2, I2, S2>], options?: DataType.Options): Class<any[], I1 & I2, S1 & S2>;
46
45
  /**
47
46
  * Helper method that mixes given types
48
- * @param c1
49
- * @param c2
50
- * @param c3
47
+ * @param types
51
48
  * @param options
52
- */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2, A3 extends any[], I3, S3>(c1: Class<A1, I1, S1>, c2: Class<A2, I2, S2>, c3?: Class<A3, I3, S3>, options?: DataType.Options): Class<any[], I1 & I2 & I3, S1 & S2 & S3>;
49
+ */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2, A3 extends any[], I3, S3>(types: [Class<A1, I1, S1>, Class<A2, I2, S2>, Class<A3, I3, S3>], options?: DataType.Options): Class<any[], I1 & I2 & I3, S1 & S2 & S3>;
53
50
  /**
54
51
  * Helper method that mixes given types
55
- * @param c1
56
- * @param c2
57
- * @param c3
58
- * @param c4
52
+ * @param types
59
53
  * @param options
60
- */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2, A3 extends any[], I3, S3, A4 extends any[], I4, S4>(c1: Class<A1, I1, S1>, c2: Class<A2, I2, S2>, c3?: Class<A3, I3, S3>, c4?: Class<A4, I4, S4>, options?: DataType.Options): Class<any[], I1 & I2 & I3 & I4, S1 & S2 & S3 & S4>;
54
+ */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2, A3 extends any[], I3, S3, A4 extends any[], I4, S4>(types: [
55
+ Class<A1, I1, S1>,
56
+ Class<A2, I2, S2>,
57
+ Class<A3, I3, S3>,
58
+ Class<A4, I4, S4>
59
+ ], options?: DataType.Options): Class<any[], I1 & I2 & I3 & I4, S1 & S2 & S3 & S4>;
60
+ /**
61
+ * Helper method that mixes given types
62
+ * @param types
63
+ * @param options
64
+ */ <A1 extends any[], I1, S1, A2 extends any[], I2, S2, A3 extends any[], I3, S3, A4 extends any[], I4, S4, A5 extends any[], I5, S5>(types: [
65
+ Class<A1, I1, S1>,
66
+ Class<A2, I2, S2>,
67
+ Class<A3, I3, S3>,
68
+ Class<A4, I4, S4>,
69
+ Class<A5, I5, S5>
70
+ ], options?: DataType.Options): Class<any[], I1 & I2 & I3 & I4 & I5, S1 & S2 & S3 & S4 & S5>;
61
71
  prototype: MixinType;
62
72
  }
63
73
  /**
@@ -0,0 +1,73 @@
1
+ import 'reflect-metadata';
2
+ import type { Combine, Type } from 'ts-gems';
3
+ import { Validator } from 'valgen';
4
+ import { OpraSchema } from '../../schema/index.js';
5
+ import type { ApiDocument } from '../api-document.js';
6
+ import type { DocumentElement } from '../common/document-element.js';
7
+ import type { ComplexType } from './complex-type.js';
8
+ import { DataType } from './data-type.js';
9
+ import type { MappedType } from './mapped-type.js';
10
+ import type { MixinType } from './mixin-type.js';
11
+ import type { SimpleType } from './simple-type.js';
12
+ /**
13
+ * @namespace UnionType
14
+ */
15
+ export declare namespace UnionType {
16
+ interface Metadata extends Combine<{
17
+ kind: OpraSchema.UnionType.Kind;
18
+ name?: string;
19
+ types: Type[];
20
+ }, DataType.Metadata, OpraSchema.UnionType> {
21
+ }
22
+ interface InitArguments extends Combine<{
23
+ kind: OpraSchema.UnionType.Kind;
24
+ types: (ComplexType | MappedType | MixinType | SimpleType)[];
25
+ ctor?: Type;
26
+ }, DataType.InitArguments, UnionType.Metadata> {
27
+ }
28
+ interface Options extends Combine<Pick<OpraSchema.UnionType, 'discriminator'>, DataType.Options> {
29
+ }
30
+ }
31
+ /**
32
+ * Type definition for UnionType
33
+ * @class UnionType
34
+ */
35
+ export interface UnionTypeStatic {
36
+ /**
37
+ * Class constructor of UnionType
38
+ *
39
+ * @param owner
40
+ * @param args
41
+ * @constructor
42
+ */
43
+ new (owner: DocumentElement, args: UnionType.InitArguments): UnionType;
44
+ /**
45
+ * Create a new mixin type from given two types
46
+ */
47
+ (types: (string | Type)[], options?: UnionType.Options): Type;
48
+ prototype: UnionType;
49
+ }
50
+ /**
51
+ * Type definition of UnionType prototype
52
+ * @interface
53
+ */
54
+ export interface UnionType extends UnionTypeClass {
55
+ }
56
+ /**
57
+ * @class UnionType
58
+ */
59
+ export declare const UnionType: UnionTypeStatic;
60
+ /**
61
+ *
62
+ * @class UnionType
63
+ */
64
+ declare class UnionTypeClass extends DataType {
65
+ readonly kind: OpraSchema.UnionType.Kind;
66
+ readonly types: (ComplexType | MixinType | MappedType | SimpleType)[];
67
+ readonly discriminator?: string;
68
+ toJSON(options?: ApiDocument.ExportOptions): OpraSchema.UnionType;
69
+ generateCodec(codec: 'encode' | 'decode', options?: DataType.GenerateCodecOptions): Validator;
70
+ extendsFrom(): boolean;
71
+ protected _locateBase(): UnionType | undefined;
72
+ }
73
+ export {};
@@ -10,6 +10,7 @@ import { EnumType } from '../data-type/enum-type.js';
10
10
  import { MappedType } from '../data-type/mapped-type.js';
11
11
  import { MixinType } from '../data-type/mixin-type.js';
12
12
  import { SimpleType } from '../data-type/simple-type.js';
13
+ import { UnionType } from '../data-type/union-type.js';
13
14
  /**
14
15
  *
15
16
  * @namespace DataTypeFactory
@@ -44,11 +45,16 @@ export declare namespace DataTypeFactory {
44
45
  base?: string | SimpleTypeInit;
45
46
  }, SimpleType.InitArguments> {
46
47
  }
48
+ interface UnionTypeInit extends Combine<{
49
+ _instance?: MixinType;
50
+ types: (string | ComplexTypeInit | MappedTypeInit | MixinTypeInit)[];
51
+ }, UnionType.InitArguments> {
52
+ }
47
53
  interface ApiFieldInit extends Combine<{
48
54
  type: string | DataTypeInitArguments;
49
55
  }, ApiField.InitArguments> {
50
56
  }
51
- type DataTypeInitArguments = ComplexTypeInit | EnumTypeInit | MappedTypeInit | MixinTypeInit | SimpleTypeInit;
57
+ type DataTypeInitArguments = ComplexTypeInit | EnumTypeInit | MappedTypeInit | MixinTypeInit | SimpleTypeInit | UnionTypeInit;
52
58
  interface Context extends DocumentInitContext {
53
59
  importQueue?: ResponsiveMap<any>;
54
60
  initArgsMap?: ResponsiveMap<DataTypeInitArguments>;
@@ -98,12 +104,16 @@ export declare class DataTypeFactory {
98
104
  protected static _prepareMixinTypeArgs(context: DataTypeFactory.Context, owner: DocumentElement, initArgs: DataTypeFactory.MixinTypeInit, metadata: (MixinType.Metadata | OpraSchema.MixinType) & {
99
105
  ctor?: Type;
100
106
  }): Promise<void>;
107
+ protected static _prepareUnionTypeArgs(context: DataTypeFactory.Context, owner: DocumentElement, initArgs: DataTypeFactory.UnionTypeInit, metadata: (UnionType.Metadata | OpraSchema.UnionType) & {
108
+ ctor?: Type;
109
+ }): Promise<void>;
101
110
  protected static _createDataType(context: DocumentInitContext & {
102
111
  initArgsMap?: ResponsiveMap<DataTypeFactory.DataTypeInitArguments>;
103
- }, owner: DocumentElement, args: DataTypeFactory.DataTypeInitArguments | string): ComplexType | EnumType | MappedType | MixinType | SimpleType | undefined;
112
+ }, owner: DocumentElement, args: DataTypeFactory.DataTypeInitArguments | string): ComplexType | EnumType | MappedType | MixinType | SimpleType | UnionType | undefined;
104
113
  protected static _createComplexType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.ComplexTypeInit): ComplexType;
105
114
  protected static _createEnumType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.EnumTypeInit): EnumType;
106
115
  protected static _createMappedType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.MappedTypeInit): MappedType;
107
116
  protected static _createMixinType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.MixinTypeInit): MixinType;
108
117
  protected static _createSimpleType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.SimpleTypeInit): SimpleType;
118
+ protected static _createUnionType(context: DocumentInitContext, owner: DocumentElement, args: DataTypeFactory.UnionTypeInit): UnionType;
109
119
  }
@@ -33,6 +33,7 @@ export * from './data-type/pick-type.js';
33
33
  export * from './data-type/primitive-types/index.js';
34
34
  export * from './data-type/required-type.js';
35
35
  export * from './data-type/simple-type.js';
36
+ export * from './data-type/union-type.js';
36
37
  export type { RpcControllerDecorator } from './decorators/rpc-controller.decorator.js';
37
38
  export type { RpcOperationDecorator } from './decorators/rpc-operation.decorator.js';
38
39
  export * from './factory/api-document.factory.js';
@@ -10,6 +10,8 @@ export interface ComplexType extends StrictOmit<DataTypeBase, 'kind'> {
10
10
  fields?: Record<Field.Name, Field | DataType.Name>;
11
11
  additionalFields?: boolean | string | DataType | ['error'] | ['error', string];
12
12
  keyField?: Field.Name;
13
+ discriminatorField?: string;
14
+ discriminatorValue?: string;
13
15
  }
14
16
  export declare namespace ComplexType {
15
17
  const Kind = "ComplexType";
@@ -3,10 +3,11 @@ import type { EnumType } from './enum-type.interface.js';
3
3
  import type { MappedType } from './mapped-type.interface.js';
4
4
  import type { MixinType } from './mixin-type.interface.js';
5
5
  import type { SimpleType } from './simple-type.interface.js';
6
- export type DataType = SimpleType | EnumType | ComplexType | MappedType | MixinType;
6
+ import type { UnionType } from './union-type.interface.js';
7
+ export type DataType = SimpleType | EnumType | ComplexType | MappedType | MixinType | UnionType;
7
8
  export declare namespace DataType {
8
9
  type Name = string;
9
- type Kind = ComplexType.Kind | EnumType.Kind | MappedType.Kind | SimpleType.Kind | MixinType.Kind;
10
+ type Kind = ComplexType.Kind | EnumType.Kind | MappedType.Kind | SimpleType.Kind | MixinType.Kind | UnionType.Kind;
10
11
  }
11
12
  export interface DataTypeBase {
12
13
  kind: DataType.Kind;
@@ -10,6 +10,10 @@ export declare namespace Field {
10
10
  */
11
11
  export type Field = {
12
12
  type?: DataType.Name | DataType;
13
+ /**
14
+ * Defines the label of the field. Mostly used for UI.
15
+ */
16
+ label?: string;
13
17
  /**
14
18
  * Defines the description of the field
15
19
  */
@@ -10,6 +10,8 @@ export interface MappedType extends StrictOmit<DataTypeBase, 'kind'> {
10
10
  pick?: Field.Name[];
11
11
  partial?: Field.Name[] | boolean;
12
12
  required?: Field.Name[] | boolean;
13
+ discriminatorField?: string;
14
+ discriminatorValue?: string;
13
15
  }
14
16
  export declare namespace MappedType {
15
17
  const Kind = "MappedType";
@@ -0,0 +1,15 @@
1
+ import type { StrictOmit } from 'ts-gems';
2
+ import type { ComplexType } from './complex-type.interface.js';
3
+ import type { DataType, DataTypeBase } from './data-type.interface.js';
4
+ import type { MappedType } from './mapped-type.interface.js';
5
+ import type { MixinType } from './mixin-type.interface.js';
6
+ import type { SimpleType } from './simple-type.interface.js';
7
+ export interface UnionType extends StrictOmit<DataTypeBase, 'kind'> {
8
+ kind: UnionType.Kind;
9
+ types: (DataType.Name | ComplexType | MixinType | MappedType | SimpleType)[];
10
+ discriminator?: string;
11
+ }
12
+ export declare namespace UnionType {
13
+ const Kind = "UnionType";
14
+ type Kind = 'UnionType';
15
+ }
@@ -7,6 +7,7 @@ export * from './data-type/field.interface.js';
7
7
  export * from './data-type/mapped-type.interface.js';
8
8
  export * from './data-type/mixin-type.interface.js';
9
9
  export * from './data-type/simple-type.interface.js';
10
+ export * from './data-type/union-type.interface.js';
10
11
  export * from './data-type-container.interface.js';
11
12
  export * from './http/http-controller.interface.js';
12
13
  export * from './http/http-media-type.interface.js';
@@ -4,6 +4,7 @@ import { EnumType } from './data-type/enum-type.interface.js';
4
4
  import { MappedType } from './data-type/mapped-type.interface.js';
5
5
  import { MixinType } from './data-type/mixin-type.interface.js';
6
6
  import { SimpleType } from './data-type/simple-type.interface.js';
7
+ import { UnionType } from './data-type/union-type.interface.js';
7
8
  import { HttpController } from './http/http-controller.interface.js';
8
9
  export declare function isDataType(obj: any): obj is DataType;
9
10
  export declare function isComplexType(obj: any): obj is ComplexType;
@@ -11,4 +12,5 @@ export declare function isSimpleType(obj: any): obj is SimpleType;
11
12
  export declare function isMixinType(obj: any): obj is MixinType;
12
13
  export declare function isMappedType(obj: any): obj is MappedType;
13
14
  export declare function isEnumType(obj: any): obj is EnumType;
15
+ export declare function isUnionType(obj: any): obj is UnionType;
14
16
  export declare function isHttpController(obj: any): obj is HttpController;