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

panates / opra / 29494915195

16 Jul 2026 11:33AM UTC coverage: 83.024% (+0.4%) from 82.653%
29494915195

push

github

erayhanoglu
1.29.1

4118 of 5238 branches covered (78.62%)

Branch coverage included in aggregate %.

34348 of 41093 relevant lines covered (83.59%)

239.47 hits per line

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

87.66
/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';
14,829✔
7
import type { DataType } from '../data-type/data-type.js';
1✔
8
import type { EnumType } from '../data-type/enum-type.js';
12,097✔
9
import type { MappedType } from '../data-type/mapped-type.js';
12,097✔
10
import type { MixinType } from '../data-type/mixin-type.js';
12,097!
11
import type { SimpleType } from '../data-type/simple-type.js';
×
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';
19,322✔
15

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

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

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

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

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

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

1✔
88
  getDataTypeNameWithNs(dataType: DataType): string | undefined {
1✔
89
    if (!dataType.name) return;
1✔
90
    const ns = this.getDocument().getDataTypeNs(dataType);
1✔
91
    return ns ? ns + ':' + dataType.name : dataType.name;
62✔
92
  }
62✔
93

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

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

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

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

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

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

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

© 2026 Coveralls, Inc