apollo-conn-gen 0.0.2

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 (146) hide show
  1. package/dist/cli/json.d.ts +1 -0
  2. package/dist/cli/json.js +70 -0
  3. package/dist/cli/json.js.map +1 -0
  4. package/dist/cli/oas-helpers/index.d.ts +4 -0
  5. package/dist/cli/oas-helpers/index.js +54 -0
  6. package/dist/cli/oas-helpers/index.js.map +1 -0
  7. package/dist/cli/oas.d.ts +1 -0
  8. package/dist/cli/oas.js +49 -0
  9. package/dist/cli/oas.js.map +1 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +4 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/json/index.d.ts +4 -0
  14. package/dist/json/index.js +5 -0
  15. package/dist/json/index.js.map +1 -0
  16. package/dist/json/io/index.d.ts +1 -0
  17. package/dist/json/io/index.js +2 -0
  18. package/dist/json/io/index.js.map +1 -0
  19. package/dist/json/io/writer.d.ts +15 -0
  20. package/dist/json/io/writer.js +45 -0
  21. package/dist/json/io/writer.js.map +1 -0
  22. package/dist/json/walker/index.d.ts +2 -0
  23. package/dist/json/walker/index.js +3 -0
  24. package/dist/json/walker/index.js.map +1 -0
  25. package/dist/json/walker/jsonContext.d.ts +12 -0
  26. package/dist/json/walker/jsonContext.js +42 -0
  27. package/dist/json/walker/jsonContext.js.map +1 -0
  28. package/dist/json/walker/jsonGen.d.ts +19 -0
  29. package/dist/json/walker/jsonGen.js +209 -0
  30. package/dist/json/walker/jsonGen.js.map +1 -0
  31. package/dist/json/walker/log/trace.d.ts +3 -0
  32. package/dist/json/walker/log/trace.js +11 -0
  33. package/dist/json/walker/log/trace.js.map +1 -0
  34. package/dist/json/walker/naming.d.ts +8 -0
  35. package/dist/json/walker/naming.js +66 -0
  36. package/dist/json/walker/naming.js.map +1 -0
  37. package/dist/json/walker/types/index.d.ts +4 -0
  38. package/dist/json/walker/types/index.js +5 -0
  39. package/dist/json/walker/types/index.js.map +1 -0
  40. package/dist/json/walker/types/jsonArray.d.ts +13 -0
  41. package/dist/json/walker/types/jsonArray.js +70 -0
  42. package/dist/json/walker/types/jsonArray.js.map +1 -0
  43. package/dist/json/walker/types/jsonObj.d.ts +17 -0
  44. package/dist/json/walker/types/jsonObj.js +76 -0
  45. package/dist/json/walker/types/jsonObj.js.map +1 -0
  46. package/dist/json/walker/types/jsonScalar.d.ts +12 -0
  47. package/dist/json/walker/types/jsonScalar.js +39 -0
  48. package/dist/json/walker/types/jsonScalar.js.map +1 -0
  49. package/dist/json/walker/types/jsonType.d.ts +16 -0
  50. package/dist/json/walker/types/jsonType.js +30 -0
  51. package/dist/json/walker/types/jsonType.js.map +1 -0
  52. package/dist/oas/index.d.ts +14 -0
  53. package/dist/oas/index.js +15 -0
  54. package/dist/oas/index.js.map +1 -0
  55. package/dist/oas/io/writer.d.ts +25 -0
  56. package/dist/oas/io/writer.js +313 -0
  57. package/dist/oas/io/writer.js.map +1 -0
  58. package/dist/oas/log/trace.d.ts +3 -0
  59. package/dist/oas/log/trace.js +7 -0
  60. package/dist/oas/log/trace.js.map +1 -0
  61. package/dist/oas/nodes/arr.d.ts +15 -0
  62. package/dist/oas/nodes/arr.js +47 -0
  63. package/dist/oas/nodes/arr.js.map +1 -0
  64. package/dist/oas/nodes/circularRef.d.ts +13 -0
  65. package/dist/oas/nodes/circularRef.js +39 -0
  66. package/dist/oas/nodes/circularRef.js.map +1 -0
  67. package/dist/oas/nodes/comp.d.ts +18 -0
  68. package/dist/oas/nodes/comp.js +156 -0
  69. package/dist/oas/nodes/comp.js.map +1 -0
  70. package/dist/oas/nodes/en.d.ts +14 -0
  71. package/dist/oas/nodes/en.js +57 -0
  72. package/dist/oas/nodes/en.js.map +1 -0
  73. package/dist/oas/nodes/factory.d.ts +17 -0
  74. package/dist/oas/nodes/factory.js +174 -0
  75. package/dist/oas/nodes/factory.js.map +1 -0
  76. package/dist/oas/nodes/get.d.ts +24 -0
  77. package/dist/oas/nodes/get.js +167 -0
  78. package/dist/oas/nodes/get.js.map +1 -0
  79. package/dist/oas/nodes/obj.d.ts +15 -0
  80. package/dist/oas/nodes/obj.js +177 -0
  81. package/dist/oas/nodes/obj.js.map +1 -0
  82. package/dist/oas/nodes/param/param.d.ts +17 -0
  83. package/dist/oas/nodes/param/param.js +64 -0
  84. package/dist/oas/nodes/param/param.js.map +1 -0
  85. package/dist/oas/nodes/props/prop.d.ts +12 -0
  86. package/dist/oas/nodes/props/prop.js +33 -0
  87. package/dist/oas/nodes/props/prop.js.map +1 -0
  88. package/dist/oas/nodes/props/propArray.d.ts +15 -0
  89. package/dist/oas/nodes/props/propArray.js +75 -0
  90. package/dist/oas/nodes/props/propArray.js.map +1 -0
  91. package/dist/oas/nodes/props/propObj.d.ts +16 -0
  92. package/dist/oas/nodes/props/propObj.js +70 -0
  93. package/dist/oas/nodes/props/propObj.js.map +1 -0
  94. package/dist/oas/nodes/props/propRef.d.ts +19 -0
  95. package/dist/oas/nodes/props/propRef.js +108 -0
  96. package/dist/oas/nodes/props/propRef.js.map +1 -0
  97. package/dist/oas/nodes/props/propScalar.d.ts +16 -0
  98. package/dist/oas/nodes/props/propScalar.js +47 -0
  99. package/dist/oas/nodes/props/propScalar.js.map +1 -0
  100. package/dist/oas/nodes/props/referenceObject.d.ts +3 -0
  101. package/dist/oas/nodes/props/referenceObject.js +2 -0
  102. package/dist/oas/nodes/props/referenceObject.js.map +1 -0
  103. package/dist/oas/nodes/ref.d.ts +17 -0
  104. package/dist/oas/nodes/ref.js +70 -0
  105. package/dist/oas/nodes/ref.js.map +1 -0
  106. package/dist/oas/nodes/response.d.ts +14 -0
  107. package/dist/oas/nodes/response.js +48 -0
  108. package/dist/oas/nodes/response.js.map +1 -0
  109. package/dist/oas/nodes/scalar.d.ts +12 -0
  110. package/dist/oas/nodes/scalar.js +26 -0
  111. package/dist/oas/nodes/scalar.js.map +1 -0
  112. package/dist/oas/nodes/type.d.ts +43 -0
  113. package/dist/oas/nodes/type.js +107 -0
  114. package/dist/oas/nodes/type.js.map +1 -0
  115. package/dist/oas/nodes/union.d.ts +16 -0
  116. package/dist/oas/nodes/union.js +133 -0
  117. package/dist/oas/nodes/union.js.map +1 -0
  118. package/dist/oas/oasContext.d.ts +23 -0
  119. package/dist/oas/oasContext.js +73 -0
  120. package/dist/oas/oasContext.js.map +1 -0
  121. package/dist/oas/oasGen.d.ts +28 -0
  122. package/dist/oas/oasGen.js +166 -0
  123. package/dist/oas/oasGen.js.map +1 -0
  124. package/dist/oas/prompts/base/theme.d.ts +1 -0
  125. package/dist/oas/prompts/base/theme.js +50 -0
  126. package/dist/oas/prompts/base/theme.js.map +1 -0
  127. package/dist/oas/prompts/base/utils.d.ts +3 -0
  128. package/dist/oas/prompts/base/utils.js +8 -0
  129. package/dist/oas/prompts/base/utils.js.map +1 -0
  130. package/dist/oas/prompts/prompt.d.ts +19 -0
  131. package/dist/oas/prompts/prompt.js +176 -0
  132. package/dist/oas/prompts/prompt.js.map +1 -0
  133. package/dist/oas/prompts/theme.d.ts +126 -0
  134. package/dist/oas/prompts/theme.js +2 -0
  135. package/dist/oas/prompts/theme.js.map +1 -0
  136. package/dist/oas/utils/gql.d.ts +5 -0
  137. package/dist/oas/utils/gql.js +35 -0
  138. package/dist/oas/utils/gql.js.map +1 -0
  139. package/dist/oas/utils/naming.d.ts +16 -0
  140. package/dist/oas/utils/naming.js +143 -0
  141. package/dist/oas/utils/naming.js.map +1 -0
  142. package/dist/oas/utils/typeUtils.d.ts +6 -0
  143. package/dist/oas/utils/typeUtils.js +25 -0
  144. package/dist/oas/utils/typeUtils.js.map +1 -0
  145. package/package.json +77 -0
  146. package/readme.md +383 -0
@@ -0,0 +1,26 @@
1
+ import { trace } from '../log/trace.js';
2
+ import { Type } from './type.js';
3
+ export class Scalar extends Type {
4
+ schema;
5
+ constructor(parent, name, schema) {
6
+ super(parent, name);
7
+ this.schema = schema;
8
+ }
9
+ visit(_context) {
10
+ this.visited = true;
11
+ }
12
+ forPrompt(_context) {
13
+ return `Scalar Node - Name: ${this.name}, Value: ${this.schema}`;
14
+ }
15
+ generate(context, writer, selection) {
16
+ context.enter(this);
17
+ trace(context, '-> [scalar::generate]', `-> in: ${this.name}`);
18
+ writer.write(this.name);
19
+ trace(context, '<- [scalar::generate]', `-> out: ${this.name}`);
20
+ context.leave(this);
21
+ }
22
+ select(context, writer, selection) {
23
+ // Scalars do not need to be selected.
24
+ }
25
+ }
26
+ //# sourceMappingURL=scalar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scalar.js","sourceRoot":"","sources":["../../../src/oas/nodes/scalar.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAS,IAAI,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,OAAO,MAAO,SAAQ,IAAI;IAIrB;IAHT,YACE,MAAyB,EACzB,IAAY,EACL,MAAoB;QAE3B,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAFb,WAAM,GAAN,MAAM,CAAc;IAG7B,CAAC;IACM,KAAK,CAAC,QAAoB;QAC/B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACtB,CAAC;IAEM,SAAS,CAAC,QAAoB;QACnC,OAAO,uBAAuB,IAAI,CAAC,IAAI,YAAY,IAAI,CAAC,MAAM,EAAE,CAAC;IACnE,CAAC;IAEM,QAAQ,CAAC,OAAmB,EAAE,MAAc,EAAE,SAAmB;QACtE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpB,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,UAAU,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/D,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,WAAW,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAChE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAEM,MAAM,CAAC,OAAmB,EAAE,MAAc,EAAE,SAAmB;QACpE,sCAAsC;IACxC,CAAC;CACF","sourcesContent":["import { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { IType, Type } from './type.js';\n\nexport class Scalar extends Type {\n constructor(\n parent: IType | undefined,\n name: string,\n public schema: SchemaObject,\n ) {\n super(parent, name);\n }\n public visit(_context: OasContext): void {\n this.visited = true;\n }\n\n public forPrompt(_context: OasContext): string {\n return `Scalar Node - Name: ${this.name}, Value: ${this.schema}`;\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n trace(context, '-> [scalar::generate]', `-> in: ${this.name}`);\n writer.write(this.name);\n trace(context, '<- [scalar::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n // Scalars do not need to be selected.\n }\n}\n"]}
@@ -0,0 +1,43 @@
1
+ import { OasContext } from '../oasContext.js';
2
+ import { Writer } from '../io/writer.js';
3
+ import { Prop } from './props/prop.js';
4
+ export interface IType {
5
+ name: string;
6
+ parent?: IType;
7
+ children: IType[];
8
+ circularRef?: IType;
9
+ props: Map<string, Prop>;
10
+ id: string;
11
+ forPrompt(context: OasContext): string;
12
+ add(child: IType): void;
13
+ ancestors(): IType[];
14
+ visit(context: OasContext): void;
15
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
16
+ pathToRoot(): string;
17
+ path(): string;
18
+ expand(context: OasContext): IType[];
19
+ find(path: string, collection: IType[]): IType | boolean;
20
+ select(context: OasContext, writer: Writer, selection: string[]): void;
21
+ }
22
+ export declare abstract class Type implements IType {
23
+ parent?: IType;
24
+ name: string;
25
+ children: IType[];
26
+ circularRef?: IType;
27
+ visited: boolean;
28
+ private readonly _props;
29
+ protected constructor(parent: IType | undefined, name: string);
30
+ abstract visit(context: OasContext): void;
31
+ abstract forPrompt(context: OasContext): string;
32
+ abstract select(context: OasContext, writer: Writer, selection: string[]): void;
33
+ find(path: string, collection: IType[]): IType | boolean;
34
+ expand(context: OasContext): IType[];
35
+ abstract generate(context: OasContext, writer: Writer, selection: string[]): void;
36
+ get id(): string;
37
+ get props(): Map<string, Prop>;
38
+ ancestors(): IType[];
39
+ path(): string;
40
+ pathToRoot(): string;
41
+ add(child: IType): void;
42
+ selectedProps(selection: string[]): Prop[];
43
+ }
@@ -0,0 +1,107 @@
1
+ import { trace } from '../log/trace.js';
2
+ import { Factory } from './factory.js';
3
+ export class Type {
4
+ parent;
5
+ name;
6
+ children;
7
+ circularRef;
8
+ visited;
9
+ _props;
10
+ constructor(parent, name) {
11
+ this.parent = parent;
12
+ this.name = name;
13
+ this.children = [];
14
+ this.visited = false;
15
+ this._props = new Map();
16
+ }
17
+ find(path, collection) {
18
+ const parts = path.split('>');
19
+ let current;
20
+ let i = 0;
21
+ do {
22
+ const part = parts[i];
23
+ current = collection.find((t) => t.id === part);
24
+ if (!current) {
25
+ return false;
26
+ }
27
+ collection = Array.from(current.children.values()) || Array.from(current.props.values()) || [];
28
+ // console.log("found", current);
29
+ i++;
30
+ } while (i < parts.length);
31
+ return current || false;
32
+ }
33
+ expand(context) {
34
+ trace(context, '-> [expand]', `in: path: ${this.path()}`);
35
+ if (!this.visited) {
36
+ this.visit(context);
37
+ }
38
+ trace(context, '<- [expand]', `out: path: ${this.path()}`);
39
+ // TODO:
40
+ // if ((type instanceof Composed || type instanceof Union) && !type.getProps().isEmpty()) {
41
+ // return type.props?.values() || [];
42
+ // }
43
+ // else {
44
+ return this.children;
45
+ // }
46
+ }
47
+ get id() {
48
+ return this.name;
49
+ }
50
+ get props() {
51
+ return this._props;
52
+ }
53
+ ancestors() {
54
+ return this.parent ? [...this.parent.ancestors(), this] : [this];
55
+ }
56
+ path() {
57
+ const ancestors = this.ancestors();
58
+ return ancestors
59
+ .map((t) => t.id)
60
+ .join('>')
61
+ .replace(/#\/components\/schemas/g, '#/c/s');
62
+ }
63
+ pathToRoot() {
64
+ let builder = '';
65
+ let indent = 0;
66
+ const ancestors = this.ancestors();
67
+ for (let i = 0; i < ancestors.length; i++) {
68
+ builder += ' <- ' + ' '.repeat(indent++) + ancestors[i].id + ' (' + ancestors[i].constructor.name + ')\n';
69
+ }
70
+ return builder;
71
+ }
72
+ add(child) {
73
+ const paths = this.ancestors();
74
+ const contains = paths.map((p) => p.id).includes(child.id);
75
+ if (contains) {
76
+ trace(null, '-> [type:add]', 'already contains child: ' + child.id);
77
+ const ancestor = paths[paths.map((p) => p.id).indexOf(child.id)];
78
+ const wrapper = Factory.fromCircularRef(this, ancestor);
79
+ this.children.push(wrapper);
80
+ }
81
+ else {
82
+ this.children.push(child);
83
+ }
84
+ }
85
+ /*
86
+ remove = (value: IType): void => {
87
+ const index = this.children.findIndex(child => child === value);
88
+ if (index !== -1) {
89
+ this.children.splice(index, 1);
90
+ }
91
+ };
92
+
93
+ */
94
+ /*traverseB(callback: (node: IType) => void): void {
95
+ const queue: IType[] = [this];
96
+
97
+ while (queue.length > 0) {
98
+ const node = queue.shift()!;
99
+ callback(node);
100
+ queue.push(...node.children);
101
+ }
102
+ }*/
103
+ selectedProps(selection) {
104
+ return Array.from(this.props.values()).filter((prop) => selection.find((s) => s.startsWith(prop.path())));
105
+ }
106
+ }
107
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../../src/oas/nodes/type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAuBvC,MAAM,OAAgB,IAAI;IACjB,MAAM,CAAS;IACf,IAAI,CAAS;IACb,QAAQ,CAAU;IAClB,WAAW,CAAS;IACpB,OAAO,CAAU;IAEP,MAAM,CAAoB;IAE3C,YAAsB,MAAyB,EAAE,IAAY;QA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IAEM,GAAG,CAAC,KAAY;QACrB,MAAM,KAAK,GAAY,IAAI,CAAC,SAAS,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAY,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAEpE,IAAI,QAAQ,EAAE,CAAC;YACb,KAAK,CAAC,IAAI,EAAE,eAAe,EAAE,0BAA0B,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC;YACpE,MAAM,QAAQ,GAAU,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;YACxE,MAAM,OAAO,GAAG,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACxD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;IAED;;;;;;;;IAQA;IAEA;;;;;;;;OAQG;IAEI,aAAa,CAAC,SAAmB;QACtC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5G,CAAC;CACF","sourcesContent":["import { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Factory } from './factory.js';\nimport { Prop } from './props/prop.js';\n\nexport interface IType {\n name: string;\n parent?: IType;\n children: IType[];\n circularRef?: IType;\n props: Map<string, Prop>;\n id: string;\n\n forPrompt(context: OasContext): string;\n add(child: IType): void;\n ancestors(): IType[];\n visit(context: OasContext): void;\n generate(context: OasContext, writer: Writer, selection: string[]): void;\n pathToRoot(): string;\n path(): string;\n expand(context: OasContext): IType[];\n find(path: string, collection: IType[]): IType | boolean;\n select(context: OasContext, writer: Writer, selection: string[]): void;\n}\n\nexport abstract class Type implements IType {\n public parent?: IType;\n public name: string;\n public children: IType[];\n public circularRef?: IType;\n public visited: boolean;\n\n private readonly _props: Map<string, Prop>;\n\n protected constructor(parent: IType | undefined, name: string) {\n this.parent = parent;\n this.name = name;\n this.children = [];\n this.visited = false;\n this._props = new Map<string, Prop>();\n }\n\n public abstract visit(context: OasContext): void;\n\n public abstract forPrompt(context: OasContext): string;\n\n public abstract select(context: OasContext, writer: Writer, selection: string[]): void;\n\n public find(path: string, collection: IType[]): IType | boolean {\n const parts = path.split('>');\n let current: IType | undefined;\n\n let i = 0;\n do {\n const part = parts[i];\n\n current = collection.find((t) => t.id === part);\n if (!current) {\n return false;\n }\n\n collection = Array.from(current!.children.values()) || Array.from(current!.props.values()) || [];\n // console.log(\"found\", current);\n\n i++;\n } while (i < parts.length);\n\n return current || false;\n }\n\n public expand(context: OasContext): IType[] {\n trace(context, '-> [expand]', `in: path: ${this.path()}`);\n if (!this.visited) {\n this.visit(context);\n }\n\n trace(context, '<- [expand]', `out: path: ${this.path()}`);\n\n // TODO:\n // if ((type instanceof Composed || type instanceof Union) && !type.getProps().isEmpty()) {\n // return type.props?.values() || [];\n // }\n // else {\n return this.children;\n // }\n }\n\n public abstract generate(context: OasContext, writer: Writer, selection: string[]): void;\n\n get id() {\n return this.name;\n }\n get props() {\n return this._props;\n }\n\n public ancestors(): IType[] {\n return this.parent ? [...this.parent.ancestors(), this] : [this];\n }\n\n public path(): string {\n const ancestors = this.ancestors();\n return ancestors\n .map((t) => t.id)\n .join('>')\n .replace(/#\\/components\\/schemas/g, '#/c/s');\n }\n\n public pathToRoot(): string {\n let builder = '';\n let indent = 0;\n\n const ancestors = this.ancestors();\n for (let i = 0; i < ancestors.length; i++) {\n builder += ' <- ' + ' '.repeat(indent++) + ancestors[i].id + ' (' + ancestors[i].constructor.name + ')\\n';\n }\n\n return builder;\n }\n\n public add(child: IType): void {\n const paths: IType[] = this.ancestors();\n const contains: boolean = paths.map((p) => p.id).includes(child.id);\n\n if (contains) {\n trace(null, '-> [type:add]', 'already contains child: ' + child.id);\n const ancestor: IType = paths[paths.map((p) => p.id).indexOf(child.id)];\n const wrapper = Factory.fromCircularRef(this, ancestor);\n this.children.push(wrapper);\n } else {\n this.children.push(child);\n }\n }\n\n /*\n remove = (value: IType): void => {\n const index = this.children.findIndex(child => child === value);\n if (index !== -1) {\n this.children.splice(index, 1);\n }\n };\n\n*/\n\n /*traverseB(callback: (node: IType) => void): void {\n const queue: IType[] = [this];\n\n while (queue.length > 0) {\n const node = queue.shift()!;\n callback(node);\n queue.push(...node.children);\n }\n }*/\n\n public selectedProps(selection: string[]) {\n return Array.from(this.props.values()).filter((prop) => selection.find((s) => s.startsWith(prop.path())));\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { SchemaObject } from 'oas/types';
2
+ import { OasContext } from '../oasContext.js';
3
+ import { Writer } from '../io/writer.js';
4
+ import { IType, Type } from './type.js';
5
+ export declare class Union extends Type {
6
+ consolidated: boolean;
7
+ get id(): string;
8
+ schemas: SchemaObject[];
9
+ constructor(parent: IType, name: string, schemas: SchemaObject[], consolidated?: boolean);
10
+ forPrompt(_context: OasContext): string;
11
+ visit(context: OasContext): void;
12
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
13
+ select(context: OasContext, writer: Writer, selection: string[]): void;
14
+ consolidate(selection: string[]): Set<string>;
15
+ private visitProperties;
16
+ }
@@ -0,0 +1,133 @@
1
+ import { trace } from '../log/trace.js';
2
+ import { Naming } from '../utils/naming.js';
3
+ import { Factory } from './factory.js';
4
+ import { Prop } from './props/prop.js';
5
+ import { Ref } from './ref.js';
6
+ import { Type } from './type.js';
7
+ export class Union extends Type {
8
+ consolidated;
9
+ get id() {
10
+ return `union:${this.name}`;
11
+ }
12
+ schemas;
13
+ constructor(parent, name, schemas, consolidated = false) {
14
+ super(parent, name);
15
+ this.consolidated = consolidated;
16
+ this.schemas = schemas;
17
+ }
18
+ forPrompt(_context) {
19
+ return `${Naming.getRefName(this.name)} (Union)`;
20
+ }
21
+ visit(context) {
22
+ if (this.visited) {
23
+ return;
24
+ }
25
+ const schemas = this.schemas.map((s) => s.type);
26
+ context.enter(this);
27
+ trace(context, '-> [union:visit]', 'in: ' + schemas);
28
+ if (!context.inContextOf('Composed', this)) {
29
+ trace(context, '[union]', 'In union: ' + this.parent?.name);
30
+ }
31
+ for (const refSchema of this.schemas) {
32
+ const type = Factory.fromSchema(this, refSchema);
33
+ trace(context, ' [union:visit]', 'of type: ' + type);
34
+ type.visit(context);
35
+ }
36
+ if (!context.inContextOf('Param', this)) {
37
+ this.visitProperties(context);
38
+ }
39
+ if (this.name != null) {
40
+ context.store(this.name, this);
41
+ }
42
+ this.visited = true;
43
+ trace(context, '<- [union:visit]', 'out: ' + schemas);
44
+ context.leave(this);
45
+ }
46
+ generate(context, writer, selection) {
47
+ context.enter(this);
48
+ const schemas = this.schemas.map((s) => s.type);
49
+ trace(context, '-> [union::generate]', 'in: ' + schemas);
50
+ /* params with Unions are weird, but here's an example:
51
+ * id: oneOf [string, Enum {me}] */
52
+ if (context.inContextOf('Param', this)) {
53
+ for (const child of this.children) {
54
+ child.generate(context, writer, selection);
55
+ }
56
+ }
57
+ else {
58
+ // When generating this union in GQL it might look like:
59
+ // union MyUnion = Type1 | Type2 | Type3
60
+ writer
61
+ .append('#### NOT SUPPORTED YET BY CONNECTORS!!! union ')
62
+ .append(Naming.getRefName(this.name))
63
+ .append(' = ');
64
+ const childrenNames = this.children.map((child) => Naming.getRefName(child.name)).join('# | ');
65
+ writer.append(childrenNames).append('#\n\n');
66
+ trace(context, ' [union::generate]', `[union] -> object: ${this.name}`);
67
+ writer
68
+ .append('type ')
69
+ .append(Naming.getRefName(this.name))
70
+ .append(' { #### replacement for Union ')
71
+ .append(this.name)
72
+ .append('\n');
73
+ const selected = this.selectedProps(selection);
74
+ for (const prop of selected) {
75
+ trace(context, ' [union::generate]', `-> property: ${prop.name} (parent: ${prop.parent.name})`);
76
+ prop.generate(context, writer, selection);
77
+ }
78
+ writer.append('} \n### End replacement for ').append(this.name).append('\n\n');
79
+ }
80
+ trace(context, '<- [union::generate]', 'out: ' + schemas);
81
+ context.leave(this);
82
+ }
83
+ select(context, writer, selection) {
84
+ trace(context, '-> [union::select]', `-> in: ${this.name}`);
85
+ if (!this.consolidated) {
86
+ this.consolidate(selection);
87
+ }
88
+ const selected = this.selectedProps(selection);
89
+ for (const prop of selected) {
90
+ prop.select(context, writer, selection);
91
+ }
92
+ trace(context, '<- [union::select]', `-> out: ${this.name}`);
93
+ }
94
+ consolidate(selection) {
95
+ const ids = new Set();
96
+ let props = new Map();
97
+ const queue = Array.from(this.children.values()).filter((child) => !(child instanceof Prop));
98
+ while (queue.length > 0) {
99
+ const node = queue.shift();
100
+ ids.add(node instanceof Ref ? node.refType.id : node.id);
101
+ if (selection.length > 0) {
102
+ node.props.forEach((prop) => {
103
+ if (selection.find((s) => s.startsWith(prop.path()))) {
104
+ props.set(prop.name, prop);
105
+ }
106
+ });
107
+ }
108
+ else {
109
+ node.props.forEach((prop) => props.set(prop.name, prop));
110
+ }
111
+ // sort props
112
+ props = new Map([...props.entries()].sort());
113
+ const children = Array.from(node.children.values()).filter((child) => !(child instanceof Prop));
114
+ queue.push(...children);
115
+ }
116
+ // copy all collected props from children into this node
117
+ props.forEach((prop) => this.props.set(prop.name, prop));
118
+ this.consolidated = true;
119
+ // and return the types.ts we've used
120
+ return ids;
121
+ }
122
+ visitProperties(_context) {
123
+ // do nothing?
124
+ // for (const [_, prop] of collected.entries()) {
125
+ // trace(context, ' [union]', 'prop: ' + prop);
126
+ // this.props.set(prop.name, prop);
127
+ //
128
+ // if (!this.children.includes(prop))
129
+ // this.add(prop);
130
+ // }
131
+ }
132
+ }
133
+ //# sourceMappingURL=union.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union.js","sourceRoot":"","sources":["../../../src/oas/nodes/union.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAS,IAAI,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,OAAO,KAAM,SAAQ,IAAI;IAUpB;IATT,IAAI,EAAE;QACJ,OAAO,SAAS,IAAI,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IACM,OAAO,CAAiB;IAE/B,YACE,MAAa,EACb,IAAY,EACZ,OAAuB,EAChB,eAAwB,KAAK;QAEpC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAFb,iBAAY,GAAZ,YAAY,CAAiB;QAGpC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAEM,SAAS,CAAC,QAAoB;QACnC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;IACnD,CAAC;IAEM,KAAK,CAAC,OAAmB;QAC9B,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAEhD,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,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GAAG,QAAQ,CAAC,CAAC;QAC1B,CAAC;QAED,wDAAwD;QACxD,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QAEzD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QAEzB,qCAAqC;QACrC,OAAO,GAAG,CAAC;IACb,CAAC;IAEO,eAAe,CAAC,QAAoB;QAC1C,cAAc;QACd,iDAAiD;QACjD,mDAAmD;QACnD,qCAAqC;QACrC,EAAE;QACF,uCAAuC;QACvC,sBAAsB;QACtB,IAAI;IACN,CAAC;CACF","sourcesContent":["import { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\nimport { Factory } from './factory.js';\nimport { Prop } from './props/prop.js';\nimport { Ref } from './ref.js';\nimport { IType, Type } from './type.js';\n\nexport class Union extends Type {\n get id(): string {\n return `union:${this.name}`;\n }\n public schemas: SchemaObject[];\n\n constructor(\n parent: IType,\n name: string,\n schemas: SchemaObject[],\n public consolidated: boolean = false,\n ) {\n super(parent, name);\n this.schemas = schemas;\n }\n\n public forPrompt(_context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Union)`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n const schemas = this.schemas.map((s) => s.type);\n\n context.enter(this);\n trace(context, '-> [union:visit]', 'in: ' + schemas);\n\n if (!context.inContextOf('Composed', this)) {\n trace(context, '[union]', 'In union: ' + this.parent?.name);\n }\n\n for (const refSchema of this.schemas) {\n const type = Factory.fromSchema(this, refSchema);\n trace(context, ' [union:visit]', 'of type: ' + type);\n\n type.visit(context);\n }\n\n if (!context.inContextOf('Param', this)) {\n this.visitProperties(context);\n }\n\n if (this.name != null) {\n context.store(this.name, this);\n }\n\n this.visited = true;\n trace(context, '<- [union:visit]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n const schemas = this.schemas.map((s) => s.type);\n trace(context, '-> [union::generate]', 'in: ' + schemas);\n\n /* params with Unions are weird, but here's an example:\n * id: oneOf [string, Enum {me}] */\n if (context.inContextOf('Param', this)) {\n for (const child of this.children) {\n child.generate(context, writer, selection);\n }\n } else {\n // When generating this union in GQL it might look like:\n // union MyUnion = Type1 | Type2 | Type3\n writer\n .append('#### NOT SUPPORTED YET BY CONNECTORS!!! union ')\n .append(Naming.getRefName(this.name))\n .append(' = ');\n\n const childrenNames = this.children.map((child) => Naming.getRefName(child.name)).join('# | ');\n\n writer.append(childrenNames).append('#\\n\\n');\n\n trace(context, ' [union::generate]', `[union] -> object: ${this.name}`);\n\n writer\n .append('type ')\n .append(Naming.getRefName(this.name))\n .append(' { #### replacement for Union ')\n .append(this.name)\n .append('\\n');\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n trace(context, ' [union::generate]', `-> property: ${prop.name} (parent: ${prop.parent!.name})`);\n prop.generate(context, writer, selection);\n }\n\n writer.append('} \\n### End replacement for ').append(this.name).append('\\n\\n');\n }\n\n trace(context, '<- [union::generate]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]): void {\n trace(context, '-> [union::select]', `-> in: ${this.name}`);\n if (!this.consolidated) {\n this.consolidate(selection);\n }\n\n const selected = this.selectedProps(selection);\n\n for (const prop of selected) {\n prop.select(context, writer, selection);\n }\n\n trace(context, '<- [union::select]', `-> out: ${this.name}`);\n }\n\n public consolidate(selection: string[]): Set<string> {\n const ids: Set<string> = new Set();\n let props: Map<string, Prop> = new Map();\n\n const queue: IType[] = Array.from(this.children.values()).filter((child) => !(child instanceof Prop));\n\n while (queue.length > 0) {\n const node = queue.shift()!;\n\n ids.add(node instanceof Ref ? (node as Ref).refType!.id : node.id);\n\n if (selection.length > 0) {\n node.props.forEach((prop) => {\n if (selection.find((s) => s.startsWith(prop.path()))) {\n props.set(prop.name, prop);\n }\n });\n } else {\n node.props.forEach((prop) => props.set(prop.name, prop));\n }\n\n // sort props\n props = new Map([...props.entries()].sort());\n\n const children = Array.from(node.children.values()).filter((child) => !(child instanceof Prop));\n\n queue.push(...children);\n }\n\n // copy all collected props from children into this node\n props.forEach((prop) => this.props.set(prop.name, prop));\n\n this.consolidated = true;\n\n // and return the types.ts we've used\n return ids;\n }\n\n private visitProperties(_context: OasContext): void {\n // do nothing?\n // for (const [_, prop] of collected.entries()) {\n // trace(context, ' [union]', 'prop: ' + prop);\n // this.props.set(prop.name, prop);\n //\n // if (!this.children.includes(prop))\n // this.add(prop);\n // }\n }\n}\n"]}
@@ -0,0 +1,23 @@
1
+ import Oas from 'oas';
2
+ import { ParameterObject, ResponseObject, SchemaObject } from 'oas/types';
3
+ import { IType } from './nodes/type.js';
4
+ import { ReferenceObject } from './nodes/props/referenceObject.js';
5
+ export declare class OasContext {
6
+ static readonly COMPONENTS_SCHEMAS: string;
7
+ static readonly COMPONENTS_RESPONSES: string;
8
+ static readonly PARAMETER_SCHEMAS: string;
9
+ generatedSet: Set<string>;
10
+ indent: number;
11
+ stack: IType[];
12
+ types: Map<string, IType>;
13
+ private parser;
14
+ constructor(parser: Oas);
15
+ enter(type: IType): void;
16
+ leave(type: IType): void;
17
+ size(): number;
18
+ store(name: string, type: IType): void;
19
+ lookupResponse(ref: string): ResponseObject | ReferenceObject | null;
20
+ lookupRef(ref: string | null): SchemaObject | null;
21
+ lookupParam(ref: string): ParameterObject | boolean;
22
+ inContextOf(type: string, node: IType): boolean;
23
+ }
@@ -0,0 +1,73 @@
1
+ import { trace } from './log/trace.js';
2
+ import { Naming } from './utils/naming.js';
3
+ export class OasContext {
4
+ static COMPONENTS_SCHEMAS = '#/components/schemas/';
5
+ static COMPONENTS_RESPONSES = '#/components/responses/';
6
+ static PARAMETER_SCHEMAS = '#/components/parameters/';
7
+ // private prompt: Prompt;
8
+ generatedSet = new Set();
9
+ indent;
10
+ stack = new Array();
11
+ types = new Map();
12
+ parser;
13
+ constructor(parser) {
14
+ this.parser = parser;
15
+ // this.prompt = prompt;
16
+ this.indent = 0;
17
+ }
18
+ enter(type) {
19
+ this.stack.push(type);
20
+ trace(this, '-> [context::enter]', type.id);
21
+ }
22
+ leave(type) {
23
+ this.stack.pop();
24
+ trace(this, '<- [context::leave]', type.id);
25
+ }
26
+ size() {
27
+ return this.stack.length;
28
+ }
29
+ store(name, type) {
30
+ trace(this, '[context::store]', 'store ' + type.id);
31
+ this.types.set(name, type);
32
+ }
33
+ lookupResponse(ref) {
34
+ if (ref && ref.startsWith(OasContext.COMPONENTS_RESPONSES)) {
35
+ const definition = this.parser.getDefinition();
36
+ const responses = definition.components?.responses ?? {};
37
+ // get the response schema
38
+ return responses[Naming.getRefName(ref)] ?? null;
39
+ }
40
+ return null;
41
+ }
42
+ lookupRef(ref) {
43
+ if (ref && ref.startsWith(OasContext.COMPONENTS_SCHEMAS)) {
44
+ const definition = this.parser.getDefinition();
45
+ const schemas = definition.components?.schemas ?? {};
46
+ return schemas ? schemas[Naming.getRefName(ref)] : null;
47
+ }
48
+ return null;
49
+ }
50
+ lookupParam(ref) {
51
+ if (ref && ref.startsWith(OasContext.PARAMETER_SCHEMAS)) {
52
+ const definition = this.parser.getDefinition();
53
+ const parameters = definition.components?.parameters ?? {};
54
+ // get the parameter schema
55
+ const name = Naming.getRefName(ref);
56
+ return parameters[name] ?? false;
57
+ }
58
+ return false;
59
+ }
60
+ inContextOf(type, node) {
61
+ // console
62
+ for (let i = this.stack.length - 1; i >= 0; i--) {
63
+ if (this.stack[i] === node) {
64
+ continue;
65
+ }
66
+ if (this.stack[i].constructor.name === type) {
67
+ return true;
68
+ }
69
+ }
70
+ return false;
71
+ }
72
+ }
73
+ //# sourceMappingURL=oasContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oasContext.js","sourceRoot":"","sources":["../../src/oas/oasContext.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AEM,WAAW,CAAC,IAAY,EAAE,IAAW;QAC1C,UAAU;QACV,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAChD,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;gBAC3B,SAAS;YACX,CAAC;YAED,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;gBAC5C,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC","sourcesContent":["import Oas from 'oas';\nimport { ParameterObject, ResponseObject, SchemaObject } from 'oas/types';\nimport { trace } from './log/trace.js';\nimport { IType } from './nodes/type.js';\nimport { ReferenceObject } from './nodes/props/referenceObject.js';\nimport { Naming } from './utils/naming.js';\n\nexport class OasContext {\n public static readonly COMPONENTS_SCHEMAS: string = '#/components/schemas/';\n public static readonly COMPONENTS_RESPONSES: string = '#/components/responses/';\n public static readonly PARAMETER_SCHEMAS: string = '#/components/parameters/';\n // private prompt: Prompt;\n public generatedSet: Set<string> = new Set();\n public indent: number;\n\n public stack: IType[] = new Array<IType>();\n public types: Map<string, IType> = new Map();\n\n private parser: Oas;\n\n constructor(parser: Oas) {\n this.parser = parser;\n // this.prompt = prompt;\n this.indent = 0;\n }\n\n public enter(type: IType): void {\n this.stack.push(type);\n trace(this, '-> [context::enter]', type.id);\n }\n\n public leave(type: IType): void {\n this.stack.pop();\n trace(this, '<- [context::leave]', type.id);\n }\n\n public size() {\n return this.stack.length;\n }\n\n public store(name: string, type: IType): void {\n trace(this, '[context::store]', 'store ' + type.id);\n this.types.set(name, type);\n }\n\n public lookupResponse(ref: string): ResponseObject | ReferenceObject | null {\n if (ref && ref.startsWith(OasContext.COMPONENTS_RESPONSES)) {\n const definition = this.parser.getDefinition();\n const responses = definition.components?.responses ?? {};\n\n // get the response schema\n return responses[Naming.getRefName(ref)!] ?? null;\n }\n\n return null;\n }\n\n public lookupRef(ref: string | null): SchemaObject | null {\n if (ref && ref.startsWith(OasContext.COMPONENTS_SCHEMAS)) {\n const definition = this.parser.getDefinition();\n const schemas = definition.components?.schemas ?? {};\n\n return schemas ? schemas[Naming.getRefName(ref)!] : null;\n }\n return null;\n }\n\n public lookupParam(ref: string): ParameterObject | boolean {\n if (ref && ref.startsWith(OasContext.PARAMETER_SCHEMAS)) {\n const definition = this.parser.getDefinition();\n const parameters = definition.components?.parameters ?? {};\n\n // get the parameter schema\n const name = Naming.getRefName(ref)!;\n return (parameters[name] as ParameterObject) ?? false;\n }\n\n return false;\n }\n\n public inContextOf(type: string, node: IType): boolean {\n // console\n for (let i = this.stack.length - 1; i >= 0; i--) {\n if (this.stack[i] === node) {\n continue;\n }\n\n if (this.stack[i].constructor.name === type) {\n return true;\n }\n }\n\n return false;\n }\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import Oas from 'oas';
2
+ import { OasContext } from './oasContext.js';
3
+ import { IType } from './nodes/type.js';
4
+ import { Writer } from './io/writer.js';
5
+ interface IGenOptions {
6
+ skipValidation: boolean;
7
+ }
8
+ export declare class OasGen {
9
+ static fromData(data: ArrayBuffer, options?: IGenOptions): Promise<OasGen>;
10
+ static fromFile(sourceFile: string, options?: IGenOptions): Promise<OasGen>;
11
+ parser: Oas;
12
+ context?: OasContext;
13
+ paths: Map<string, IType>;
14
+ constructor(parser: Oas);
15
+ title(): string;
16
+ version(): string;
17
+ generateSchema(paths: string[]): string;
18
+ visit(): Promise<void>;
19
+ getContext(): OasContext;
20
+ expand(type: IType): IType[];
21
+ find(path: string): IType | boolean;
22
+ findPath(path: string): IType | boolean;
23
+ writer(): Writer;
24
+ private visitGet;
25
+ private printRefs;
26
+ private visitPath;
27
+ }
28
+ export {};