• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oprajs / opra / 19958387866

05 Dec 2025 09:19AM UTC coverage: 80.99% (-0.2%) from 81.23%
19958387866

push

github

erayhanoglu
chore: Updated deps

3534 of 4540 branches covered (77.84%)

Branch coverage included in aggregate %.

31111 of 38237 relevant lines covered (81.36%)

218.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.34
/packages/common/src/document/common/document-node.ts
1
import type { Type } from 'ts-gems';
1✔
2
import { OpraSchema } from '../../schema/index.js';
1✔
3
import type { ApiDocument } from '../api-document.js';
1✔
4
import { DATATYPE_METADATA, kDataTypeMap, kTypeNSMap } from '../constants.js';
1✔
5
import type { ArrayType } from '../data-type/array-type.js';
1✔
6
import type { ComplexType } from '../data-type/complex-type.js';
1✔
7
import type { DataType } from '../data-type/data-type.js';
1✔
8
import type { EnumType } from '../data-type/enum-type.js';
1✔
9
import type { MappedType } from '../data-type/mapped-type.js';
1✔
10
import type { MixinType } from '../data-type/mixin-type.js';
1✔
11
import type { SimpleType } from '../data-type/simple-type.js';
1✔
12
import type { UnionType } from '../data-type/union-type.js';
1✔
13
import type { DataTypeMap } from './data-type-map.js';
1✔
14
import type { DocumentElement } from './document-element.js';
1✔
15

1✔
16
/**
1✔
17
 * @class DocumentNode
1✔
18
 */
1✔
19
export class DocumentNode {
1✔
20
  protected [kDataTypeMap]?: DataTypeMap;
1✔
21
  protected _document?: ApiDocument;
1✔
22
  readonly parent?: DocumentNode;
1✔
23
  readonly element: DocumentElement;
1✔
24

1✔
25
  constructor(element: DocumentElement, parent?: DocumentNode) {
1✔
26
    this.element = element;
14,250✔
27
    this.parent = parent;
14,250✔
28
  }
14,250✔
29

1✔
30
  getDocument(): ApiDocument {
1✔
31
    if (this._document) return this._document;
12,723✔
32
    if (this.element[kTypeNSMap]) return this.element as unknown as ApiDocument;
12,723✔
33
    if (this.parent) return (this._document = this.parent.getDocument());
5,504!
34
    // istanbul ignore next
×
35
    throw new Error('ApiDocument not found in document tree');
×
36
  }
×
37

1✔
38
  hasDataType(
1✔
39
    nameOrCtor: string | Type | Function | object | any[],
×
40
    scope?: string | '*',
×
41
  ): boolean {
×
42
    return !!this.findDataType(nameOrCtor, scope);
×
43
  }
×
44

1✔
45
  findDataType(
1✔
46
    nameOrCtor: string | Type | Function | object | any[],
19,010✔
47
    scope?: string | '*',
19,010✔
48
  ): DataType | undefined {
19,010✔
49
    const dt = this[kDataTypeMap]?.get(nameOrCtor);
19,010✔
50
    if (dt && dt.inScope(scope)) return dt;
19,010✔
51
    return this.parent
19,003✔
52
      ? this.parent.findDataType(nameOrCtor, scope)
19,003!
53
      : undefined;
×
54
  }
19,010✔
55

1✔
56
  /**
1✔
57
   * Returns DataType instance by name or Constructor. Returns undefined if not found
1✔
58
   */
1✔
59
  getDataType(
1✔
60
    nameOrCtor: string | Type | Function | object | any[],
2,085✔
61
    scope?: string | '*',
2,085✔
62
  ): DataType {
2,085✔
63
    const dt = this.findDataType(nameOrCtor, scope);
2,085✔
64
    if (dt) return dt;
2,085✔
65
    let name = '';
1✔
66
    if (typeof nameOrCtor === 'function') {
2,085!
67
      const metadata = Reflect.getMetadata(DATATYPE_METADATA, nameOrCtor);
×
68
      name = metadata.name;
×
69
    } else if (typeof nameOrCtor === 'object') {
2,085!
70
      const metadata = nameOrCtor[DATATYPE_METADATA];
×
71
      name = metadata?.name;
×
72
    }
×
73
    if (!name) {
1✔
74
      if (nameOrCtor && typeof nameOrCtor === 'string') name = nameOrCtor;
1!
75
      else if (typeof nameOrCtor === 'function') name = nameOrCtor.name;
×
76
    }
1✔
77
    if (dt)
1✔
78
      throw new TypeError(
2,085!
79
        `Data type${name ? ' (' + name + ')' : ''} is not in requested scope (${scope})`,
✔
80
      );
1✔
81
    throw new TypeError(`Unknown data type${name ? ' (' + name + ')' : ''}`);
2,085!
82
  }
2,085✔
83

1✔
84
  getDataTypeNameWithNs(dataType: DataType): string | undefined {
1✔
85
    if (!dataType.name) return;
6,000✔
86
    const ns = this.getDocument().getDataTypeNs(dataType);
5,115✔
87
    return ns ? ns + ':' + dataType.name : dataType.name;
6,000✔
88
  }
6,000✔
89

1✔
90
  /**
1✔
91
   * Returns EnumType instance by name or Constructor.
1✔
92
   * Returns undefined if not found
1✔
93
   * Throws error if data type is not a UnionType
1✔
94
   */
1✔
95
  getArrayType(
1✔
96
    nameOrCtor: string | object | any[],
3✔
97
    scope?: string | '*',
3✔
98
  ): ArrayType {
3✔
99
    const t = this.getDataType(nameOrCtor, scope);
3✔
100
    if (t.kind === OpraSchema.ArrayType.Kind) return t as ArrayType;
3!
101
    throw new TypeError(`Data type "${t.name || t}" is not a MixinType`);
3✔
102
  }
3✔
103

1✔
104
  /**
1✔
105
   * Returns ComplexType instance by name or Constructor.
1✔
106
   * Returns undefined if not found
1✔
107
   * Throws error if data type is not a ComplexType
1✔
108
   */
1✔
109
  getComplexType(
1✔
110
    nameOrCtor: string | Type | Function,
1,424✔
111
    scope?: string | '*',
1,424✔
112
  ): ComplexType {
1,424✔
113
    const t = this.getDataType(nameOrCtor, scope);
1,424✔
114
    if (t.kind === OpraSchema.ComplexType.Kind) return t as ComplexType;
1,424✔
115
    throw new TypeError(`Data type "${t.name}" is not a ComplexType`);
2✔
116
  }
2✔
117

1✔
118
  /**
1✔
119
   * Returns SimpleType instance by name or Constructor.
1✔
120
   * Returns undefined if not found
1✔
121
   * Throws error if data type is not a SimpleType
1✔
122
   */
1✔
123
  getSimpleType(nameOrCtor: string | Type, scope?: string | '*'): SimpleType {
1✔
124
    const t = this.getDataType(nameOrCtor, scope);
64✔
125
    if (t.kind === OpraSchema.SimpleType.Kind) return t as SimpleType;
64✔
126
    throw new TypeError(`Data type "${t.name || t}" is not a SimpleType`);
64!
127
  }
64✔
128

1✔
129
  /**
1✔
130
   * Returns EnumType instance by name or Constructor.
1✔
131
   * Returns undefined if not found
1✔
132
   * Throws error if data type is not a EnumType
1✔
133
   */
1✔
134
  getEnumType(
1✔
135
    nameOrCtor: string | object | any[],
10✔
136
    scope?: string | '*',
10✔
137
  ): EnumType {
10✔
138
    const t = this.getDataType(nameOrCtor, scope);
10✔
139
    if (t.kind === OpraSchema.EnumType.Kind) return t as EnumType;
10!
140
    throw new TypeError(`Data type "${t.name || t}" is not a EnumType`);
10✔
141
  }
10✔
142

1✔
143
  /**
1✔
144
   * Returns EnumType instance by name or Constructor.
1✔
145
   * Returns undefined if not found
1✔
146
   * Throws error if data type is not a MappedType
1✔
147
   */
1✔
148
  getMappedType(
1✔
149
    nameOrCtor: string | object | any[],
8✔
150
    scope?: string | '*',
8✔
151
  ): MappedType {
8✔
152
    const t = this.getDataType(nameOrCtor, scope);
8✔
153
    if (t.kind === OpraSchema.MappedType.Kind) return t as MappedType;
8!
154
    throw new TypeError(`Data type "${t.name || t}" is not a MappedType`);
8✔
155
  }
8✔
156

1✔
157
  /**
1✔
158
   * Returns EnumType instance by name or Constructor.
1✔
159
   * Returns undefined if not found
1✔
160
   * Throws error if data type is not a MixinType
1✔
161
   */
1✔
162
  getMixinType(
1✔
163
    nameOrCtor: string | object | any[],
3✔
164
    scope?: string | '*',
3✔
165
  ): MixinType {
3✔
166
    const t = this.getDataType(nameOrCtor, scope);
3✔
167
    if (t.kind === OpraSchema.MixinType.Kind) return t as MixinType;
3!
168
    throw new TypeError(`Data type "${t.name || t}" is not a MixinType`);
3✔
169
  }
3✔
170

1✔
171
  /**
1✔
172
   * Returns EnumType instance by name or Constructor.
1✔
173
   * Returns undefined if not found
1✔
174
   * Throws error if data type is not a UnionType
1✔
175
   */
1✔
176
  getUnionType(
1✔
177
    nameOrCtor: string | object | any[],
2✔
178
    scope?: string | '*',
2✔
179
  ): UnionType {
2✔
180
    const t = this.getDataType(nameOrCtor, scope);
2✔
181
    if (t.kind === OpraSchema.UnionType.Kind) return t as UnionType;
2!
182
    throw new TypeError(`Data type "${t.name || t}" is not a UnionType`);
2✔
183
  }
2✔
184
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc