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,156 @@
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 Composed extends Type {
8
+ name;
9
+ schema;
10
+ consolidated;
11
+ get id() {
12
+ return `comp:${this.name}`;
13
+ }
14
+ constructor(parent, name, schema, consolidated = false) {
15
+ super(parent, name);
16
+ this.name = name;
17
+ this.schema = schema;
18
+ this.consolidated = consolidated;
19
+ }
20
+ forPrompt(_context) {
21
+ return `${Naming.getRefName(this.name)} (Comp)`;
22
+ }
23
+ visit(context) {
24
+ if (this.visited) {
25
+ return;
26
+ }
27
+ context.enter(this);
28
+ trace(context, '-> [composed:visit]', 'in: ' + (this.name == null ? '[object]' : this.name));
29
+ // If not in the context of a Composed or Param, log the composed schema.
30
+ if (!context.inContextOf('Composed', this) && !context.inContextOf('Param', this)) {
31
+ trace(context, '[comp]', 'In composed schema: ' + this.name);
32
+ }
33
+ const composedSchema = this.schema;
34
+ // this will be a type declaration
35
+ if (composedSchema.allOf != null) {
36
+ this.visitAllOfNode(context, composedSchema);
37
+ }
38
+ // represents a Union type
39
+ else if (composedSchema.oneOf != null) {
40
+ this.visitOneOfNode(context, composedSchema);
41
+ }
42
+ // can't hand this yet
43
+ else {
44
+ throw new Error('Composed.visit: unsupported composed schema: ' + this.schema);
45
+ }
46
+ this.visited = true;
47
+ trace(context, '<- [composed:visit]', 'out: ' + this.name);
48
+ context.leave(this);
49
+ }
50
+ generate(context, writer, selection) {
51
+ context.enter(this);
52
+ trace(context, '-> [comp::generate]', `-> in: ${this.name}`);
53
+ const composedSchema = this.schema;
54
+ if (composedSchema.oneOf != null) {
55
+ if (this.children.length > 0) {
56
+ this.children[0].generate(context, writer, selection);
57
+ }
58
+ }
59
+ else if (composedSchema.allOf != null) {
60
+ const selected = this.selectedProps(selection);
61
+ if (selected.length > 0) {
62
+ writer.write('type ');
63
+ writer.write(Naming.getRefName(this.name));
64
+ writer.write(' {\n');
65
+ for (const prop of selected) {
66
+ trace(context, ' [comp::generate]', `-> property: ${prop.name} (parent: ${prop.parent.name})`);
67
+ prop.generate(context, writer, selection);
68
+ }
69
+ writer.write('}\n\n');
70
+ }
71
+ }
72
+ trace(context, '<- [comp::generate]', `-> out: ${this.name}`);
73
+ context.leave(this);
74
+ }
75
+ select(context, writer, selection) {
76
+ trace(context, '-> [comp::select]', `-> in: ${this.name}`);
77
+ if (!this.consolidated) {
78
+ this.consolidate(selection);
79
+ }
80
+ const composedSchema = this.schema;
81
+ if (composedSchema.allOf != null) {
82
+ const selected = this.selectedProps(selection);
83
+ for (const prop of selected) {
84
+ prop.select(context, writer, selection);
85
+ }
86
+ }
87
+ else if (composedSchema.oneOf != null) {
88
+ if (this.children.length === 1) {
89
+ this.children[0].select(context, writer, selection);
90
+ }
91
+ else {
92
+ throw new Error('Expected exactly one child for a oneOf schema');
93
+ }
94
+ }
95
+ trace(context, '<- [comp::select]', `-> out: ${this.name}`);
96
+ }
97
+ consolidate(selection) {
98
+ const ids = new Set();
99
+ let props = new Map();
100
+ const queue = Array.from(this.children.values()).filter((child) => !(child instanceof Prop));
101
+ while (queue.length > 0) {
102
+ const node = queue.shift();
103
+ ids.add(node instanceof Ref ? node.refType.id : node.id);
104
+ if (selection.length > 0) {
105
+ node.props.forEach((prop) => {
106
+ if (selection.find((s) => s.startsWith(prop.path()))) {
107
+ props.set(prop.name, prop);
108
+ }
109
+ });
110
+ }
111
+ else {
112
+ node.props.forEach((prop) => props.set(prop.name, prop));
113
+ }
114
+ // sort props
115
+ props = new Map([...props.entries()].sort());
116
+ const children = Array.from(node.children.values()).filter((child) => !(child instanceof Prop));
117
+ queue.push(...children);
118
+ }
119
+ // copy all collected props from children into this node
120
+ props.forEach((prop, name) => this.props.set(name, prop));
121
+ this.consolidated = true;
122
+ // and return the types.ts we've used
123
+ return ids;
124
+ }
125
+ visitAllOfNode(context, schema) {
126
+ const allOfs = schema.allOf || [];
127
+ const refs = allOfs.map((s) => s.$ref);
128
+ trace(context, '-> [composed::all-of]', `in: '${this.name}' of: ${allOfs.length} - refs: ${refs}`);
129
+ for (let i = 0; i < allOfs.length; i++) {
130
+ const allOfItemSchema = allOfs[i];
131
+ const type = Factory.fromSchema(this, allOfItemSchema);
132
+ trace(context, ' [composed::all-of]', 'allOf type: ' + type);
133
+ if (type) {
134
+ type.visit(context);
135
+ }
136
+ }
137
+ trace(context, '-> [composed]', 'storing: ' + this.name + ' with: ' + this);
138
+ context.store(this.name, this);
139
+ trace(context, '<- [composed::all-of]', `out: '${this.name}' of: ${allOfs.length} - refs: ${refs}`);
140
+ }
141
+ visitOneOfNode(context, schema) {
142
+ const oneOfs = schema.oneOf || [];
143
+ trace(context, '-> [composed::one-of]', `in: OneOf ${this.name} with size: ${oneOfs.length}`);
144
+ const result = Factory.fromUnion(context, this, oneOfs);
145
+ if (!result) {
146
+ throw new Error('Failed to create union type');
147
+ }
148
+ result.visit(context);
149
+ trace(context, '-> [composed::one-of]', `storing: ${this.name} with: ${this}`);
150
+ if (this.name != null) {
151
+ context.store(this.name, this);
152
+ }
153
+ trace(context, '<- [composed::one-of]', `out: OneOf ${this.name} with size: ${oneOfs.length}`);
154
+ }
155
+ }
156
+ //# sourceMappingURL=comp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"comp.js","sourceRoot":"","sources":["../../../src/oas/nodes/comp.ts"],"names":[],"mappings":"AAEA,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;AAGxC,MAAM,OAAO,QAAS,SAAQ,IAAI;IAMvB;IACA;IACA;IAPT,IAAI,EAAE;QACJ,OAAO,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;IAC7B,CAAC;IACD,YACE,MAAyB,EAClB,IAAY,EACZ,MAAoB,EACpB,eAAwB,KAAK;QAEpC,KAAK,C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,CAAC,CAAC;IACjG,CAAC;CACF","sourcesContent":["import { SchemaObject } from 'oas/types';\n\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';\nimport { type ReferenceObject } from './props/referenceObject.js';\n\nexport class Composed extends Type {\n get id(): string {\n return `comp:${this.name}`;\n }\n constructor(\n parent: IType | undefined,\n public name: string,\n public schema: SchemaObject,\n public consolidated: boolean = false,\n ) {\n super(parent, name);\n }\n\n public forPrompt(_context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Comp)`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [composed:visit]', 'in: ' + (this.name == null ? '[object]' : this.name));\n\n // If not in the context of a Composed or Param, log the composed schema.\n if (!context.inContextOf('Composed', this) && !context.inContextOf('Param', this)) {\n trace(context, '[comp]', 'In composed schema: ' + this.name);\n }\n\n const composedSchema = this.schema;\n // this will be a type declaration\n if (composedSchema.allOf != null) {\n this.visitAllOfNode(context, composedSchema);\n }\n // represents a Union type\n else if (composedSchema.oneOf != null) {\n this.visitOneOfNode(context, composedSchema);\n }\n // can't hand this yet\n else {\n throw new Error('Composed.visit: unsupported composed schema: ' + this.schema);\n }\n\n this.visited = true;\n trace(context, '<- [composed:visit]', 'out: ' + this.name);\n context.leave(this);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n trace(context, '-> [comp::generate]', `-> in: ${this.name}`);\n\n const composedSchema = this.schema;\n if (composedSchema.oneOf != null) {\n if (this.children.length > 0) {\n this.children[0].generate(context, writer, selection);\n }\n } else if (composedSchema.allOf != null) {\n const selected = this.selectedProps(selection);\n\n if (selected.length > 0) {\n writer.write('type ');\n writer.write(Naming.getRefName(this.name));\n writer.write(' {\\n');\n\n for (const prop of selected) {\n trace(context, ' [comp::generate]', `-> property: ${prop.name} (parent: ${prop.parent!.name})`);\n prop.generate(context, writer, selection);\n }\n\n writer.write('}\\n\\n');\n }\n }\n\n trace(context, '<- [comp::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [comp::select]', `-> in: ${this.name}`);\n if (!this.consolidated) {\n this.consolidate(selection);\n }\n\n const composedSchema = this.schema;\n if (composedSchema.allOf != null) {\n const selected = this.selectedProps(selection);\n\n for (const prop of selected) {\n prop.select(context, writer, selection);\n }\n } else if (composedSchema.oneOf != null) {\n if (this.children.length === 1) {\n this.children[0].select(context, writer, selection);\n } else {\n throw new Error('Expected exactly one child for a oneOf schema');\n }\n }\n\n trace(context, '<- [comp::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, name) => this.props.set(name, prop));\n\n this.consolidated = true;\n\n // and return the types.ts we've used\n return ids;\n }\n\n private visitAllOfNode(context: OasContext, schema: SchemaObject): void {\n const allOfs = schema.allOf || [];\n const refs = allOfs.map((s) => (s as ReferenceObject).$ref);\n\n trace(context, '-> [composed::all-of]', `in: '${this.name}' of: ${allOfs.length} - refs: ${refs}`);\n\n for (let i = 0; i < allOfs.length; i++) {\n const allOfItemSchema = allOfs[i];\n\n const type = Factory.fromSchema(this, allOfItemSchema as SchemaObject);\n trace(context, ' [composed::all-of]', 'allOf type: ' + type);\n\n if (type) {\n type.visit(context);\n }\n }\n\n trace(context, '-> [composed]', 'storing: ' + this.name + ' with: ' + this);\n context.store(this.name, this);\n trace(context, '<- [composed::all-of]', `out: '${this.name}' of: ${allOfs.length} - refs: ${refs}`);\n }\n\n private visitOneOfNode(context: OasContext, schema: SchemaObject): void {\n const oneOfs = schema.oneOf || [];\n trace(context, '-> [composed::one-of]', `in: OneOf ${this.name} with size: ${oneOfs.length}`);\n\n const result = Factory.fromUnion(context, this, oneOfs as SchemaObject[]);\n if (!result) {\n throw new Error('Failed to create union type');\n }\n\n result.visit(context);\n\n trace(context, '-> [composed::one-of]', `storing: ${this.name} with: ${this}`);\n if (this.name != null) {\n context.store(this.name, this);\n }\n\n trace(context, '<- [composed::one-of]', `out: OneOf ${this.name} with size: ${oneOfs.length}`);\n }\n}\n"]}
@@ -0,0 +1,14 @@
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 En extends Type {
6
+ schema: SchemaObject;
7
+ items: string[];
8
+ constructor(parent: IType, schema: SchemaObject, items?: string[]);
9
+ get id(): string;
10
+ visit(context: OasContext): void;
11
+ forPrompt(_context: OasContext): string;
12
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
13
+ select(context: OasContext, writer: Writer, selection: string[]): void;
14
+ }
@@ -0,0 +1,57 @@
1
+ import { trace } from '../log/trace.js';
2
+ import { GqlUtils } from '../utils/gql.js';
3
+ import { Naming } from '../utils/naming.js';
4
+ import { Type } from './type.js';
5
+ export class En extends Type {
6
+ schema;
7
+ items;
8
+ constructor(parent, schema, items = []) {
9
+ super(parent, 'enum');
10
+ this.schema = schema;
11
+ this.items = items;
12
+ }
13
+ get id() {
14
+ return 'enum:' + this.name;
15
+ }
16
+ visit(context) {
17
+ if (this.visited) {
18
+ return;
19
+ }
20
+ context.enter(this);
21
+ trace(context, '-> [enum:visit]', 'in: ' + this.items.toString());
22
+ if (!context.inContextOf('Param', this)) {
23
+ context.store(this.name, this);
24
+ }
25
+ this.visited = true;
26
+ trace(context, '<- [enum:visit]', 'out: ' + this.items.toString());
27
+ context.leave(this);
28
+ }
29
+ forPrompt(_context) {
30
+ return `${Naming.getRefName(this.name)} (Enum): ${this.items.join(', ')}`;
31
+ }
32
+ generate(context, writer, selection) {
33
+ context.enter(this);
34
+ trace(context, '-> [enum::generate]', `-> in: ${this.name}`);
35
+ if (!context.inContextOf('Param', this)) {
36
+ const builder = 'enum ' + Naming.getRefName(this.name) + ' {\n' + this.items.map((s) => ' ' + s).join(',\n') + '\n}\n\n';
37
+ writer.write(builder);
38
+ }
39
+ // this covers the case where a union combines a scalar with an enum.
40
+ else if (!context.inContextOf('Union', this)) {
41
+ writer.write(GqlUtils.getGQLScalarType(this.schema));
42
+ }
43
+ // Otherwise, do nothing.
44
+ trace(context, '<- [enum::generate]', `-> out: ${this.name}`);
45
+ context.leave(this);
46
+ }
47
+ select(context, writer, selection) {
48
+ trace(context, '-> [enum::select]', `-> in: ${this.name}`);
49
+ // do nothing?
50
+ /*const dependencies = this.dependencies(context);
51
+ dependencies.forEach((dependency) => {
52
+ dependency.select(context, writer);
53
+ });*/
54
+ trace(context, '<- [enum::select]', `-> out: ${this.name}`);
55
+ }
56
+ }
57
+ //# sourceMappingURL=en.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"en.js","sourceRoot":"","sources":["../../../src/oas/nodes/en.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAS,IAAI,EAAE,MAAM,WAAW,CAAC;AAExC,MAA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AAC,IAAI,EAAE,CAAC,CAAC;QAE7D,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC;YACxC,MAAM,OAAO,GACX,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;YAC3G,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;QACD,qEAAqE;aAChE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC;YAC7C,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;QACvD,CAAC;QAED,yBAAyB;QACzB,KAAK,CAAC,OAAO,EAAE,qBAAqB,EAAE,WAAW,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9D,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAEM,MAAM,CAAC,OAAmB,EAAE,MAAc,EAAE,SAAmB;QACpE,KAAK,CAAC,OAAO,EAAE,mBAAmB,EAAE,UAAU,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAE3D,cAAc;QACd;;;aAGK;QAEL,KAAK,CAAC,OAAO,EAAE,mBAAmB,EAAE,WAAW,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9D,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 { GqlUtils } from '../utils/gql.js';\nimport { Naming } from '../utils/naming.js';\nimport { IType, Type } from './type.js';\n\nexport class En extends Type {\n constructor(\n parent: IType,\n public schema: SchemaObject,\n public items: string[] = [],\n ) {\n super(parent, 'enum');\n }\n\n get id(): string {\n return 'enum:' + this.name;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [enum:visit]', 'in: ' + this.items.toString());\n\n if (!context.inContextOf('Param', this)) {\n context.store(this.name, this);\n }\n\n this.visited = true;\n\n trace(context, '<- [enum:visit]', 'out: ' + this.items.toString());\n context.leave(this);\n }\n\n public forPrompt(_context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Enum): ${this.items.join(', ')}`;\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n trace(context, '-> [enum::generate]', `-> in: ${this.name}`);\n\n if (!context.inContextOf('Param', this)) {\n const builder =\n 'enum ' + Naming.getRefName(this.name) + ' {\\n' + this.items.map((s) => ' ' + s).join(',\\n') + '\\n}\\n\\n';\n writer.write(builder);\n }\n // this covers the case where a union combines a scalar with an enum.\n else if (!context.inContextOf('Union', this)) {\n writer.write(GqlUtils.getGQLScalarType(this.schema));\n }\n\n // Otherwise, do nothing.\n trace(context, '<- [enum::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [enum::select]', `-> in: ${this.name}`);\n\n // do nothing?\n /*const dependencies = this.dependencies(context);\n dependencies.forEach((dependency) => {\n dependency.select(context, writer);\n });*/\n\n trace(context, '<- [enum::select]', `-> out: ${this.name}`);\n }\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import { Operation } from 'oas/operation';
2
+ import { ParameterObject, SchemaObject } from 'oas/types';
3
+ import { IType } from './type.js';
4
+ import { OasContext } from '../oasContext.js';
5
+ import { Get } from './get.js';
6
+ import { Param } from './param/param.js';
7
+ import { Prop } from './props/prop.js';
8
+ import { ReferenceObject } from './props/referenceObject.js';
9
+ export declare class Factory {
10
+ static createGet(name: string, op: Operation): Get;
11
+ static fromSchema(parent: IType, schema: SchemaObject): IType;
12
+ static fromProp(context: OasContext, parent: IType, propertyName: string, schema: SchemaObject): Prop;
13
+ static fromResponse(_context: OasContext, parent: IType, mediaSchema: SchemaObject): IType;
14
+ static fromParam(context: OasContext, parent: IType, p: ParameterObject | ReferenceObject): Param;
15
+ static fromCircularRef(parent: IType, child: IType): IType;
16
+ static fromUnion(_context: OasContext, parent: IType, oneOfs: SchemaObject[]): IType;
17
+ }
@@ -0,0 +1,174 @@
1
+ import _ from 'lodash';
2
+ import { trace, warn } from '../log/trace.js';
3
+ import { Naming } from '../utils/naming.js';
4
+ import { GqlUtils } from '../utils/gql.js';
5
+ import { Arr } from './arr.js';
6
+ import { CircularRef } from './circularRef.js';
7
+ import { Composed } from './comp.js';
8
+ import { En } from './en.js';
9
+ import { Get } from './get.js';
10
+ import { Obj } from './obj.js';
11
+ import { Param } from './param/param.js';
12
+ import { PropArray } from './props/propArray.js';
13
+ import { PropObj } from './props/propObj.js';
14
+ import { PropRef } from './props/propRef.js';
15
+ import { PropScalar } from './props/propScalar.js';
16
+ import { Ref } from './ref.js';
17
+ import { Response } from './response.js';
18
+ import { Union } from './union.js';
19
+ import { Scalar } from './scalar.js';
20
+ export class Factory {
21
+ static createGet(name, op) {
22
+ return new Get(name, op);
23
+ }
24
+ static fromSchema(parent, schema) {
25
+ let result = null;
26
+ if ('$ref' in schema) {
27
+ result = new Ref(parent, schema.$ref, schema);
28
+ }
29
+ // array case
30
+ else if (schema.type === 'array' && schema.items) {
31
+ // Array schema case.
32
+ let parentName = parent.name;
33
+ if (parent instanceof Response) {
34
+ // Assume parent.parent is a GetOp.
35
+ const get = parent.parent;
36
+ parentName = _.upperFirst(get.getGqlOpName());
37
+ }
38
+ else {
39
+ trace(null, '[factory]', 'Factory.fromSchema >>> HERE');
40
+ }
41
+ result = new Arr(parent, parentName, schema.items);
42
+ result.itemsType = Factory.fromSchema(result, schema.items);
43
+ }
44
+ // array case
45
+ else if (schema.type === 'object') {
46
+ if (schema.allOf || schema.oneOf) {
47
+ result = new Composed(parent, _.get(schema, 'name') || parent.name, schema);
48
+ }
49
+ else {
50
+ if (!schema.properties) {
51
+ warn(null, '[factory]', 'Object has no properties: ' + JSON.stringify(schema, null, 2) + ' in: ' + parent.pathToRoot());
52
+ }
53
+ result = new Obj(parent, _.get(schema, 'name') || null, schema);
54
+ }
55
+ }
56
+ // Composed schema case.
57
+ else if (schema.allOf || schema.oneOf) {
58
+ result = new Composed(parent, _.get(schema, 'name') || parent.name, schema);
59
+ }
60
+ // scalar
61
+ else {
62
+ const typeStr = schema.type;
63
+ if (typeStr != null) {
64
+ if (typeStr === 'array') {
65
+ throw new Error(`Should have been handled already? ${typeStr}, schema: ${JSON.stringify(schema)}`);
66
+ }
67
+ else if (schema.enum != null) {
68
+ result = new En(parent, schema, schema.enum);
69
+ }
70
+ // scalar case
71
+ else if (GqlUtils.gqlScalar(typeStr)) {
72
+ const scalarType = GqlUtils.getGQLScalarType(schema);
73
+ result = new Scalar(parent, scalarType, schema);
74
+ }
75
+ else {
76
+ throw new Error(`Cannot handle property type ${typeStr}, schema: ${JSON.stringify(schema)}`);
77
+ }
78
+ }
79
+ else if (schema.enum != null) {
80
+ result = new En(parent, schema, _.get(schema, 'enum'));
81
+ }
82
+ else {
83
+ throw new Error(`Cannot handle schema ${parent.pathToRoot()}, schema: ${JSON.stringify(schema)}`);
84
+ }
85
+ }
86
+ if (result != null) {
87
+ parent.add(result);
88
+ }
89
+ else {
90
+ throw new Error(`Not yet implemented for ${JSON.stringify(schema)}`);
91
+ }
92
+ return result;
93
+ }
94
+ static fromProp(context, parent, propertyName, schema) {
95
+ if (!schema) {
96
+ throw new Error(`Should have a schema defined for property '${propertyName}' (parent: '${parent.name}')`);
97
+ }
98
+ const type = schema.type;
99
+ let prop;
100
+ if (!type && '$ref' in schema) {
101
+ prop = new PropRef(parent, propertyName, schema, schema.$ref);
102
+ }
103
+ // uses the type of the schema to find out what kind of property it is
104
+ else if (type) {
105
+ // 1st case is if the type is an array
106
+ if (type === 'array') {
107
+ const array = new PropArray(parent, propertyName, schema);
108
+ const itemsName = Naming.genArrayItems(propertyName);
109
+ const itemsType = Factory.fromProp(context, array, itemsName, schema.items);
110
+ array.setItems(itemsType);
111
+ prop = array;
112
+ }
113
+ // 2nd checks for obj property
114
+ else if (type === 'object') {
115
+ // the type of the property will be an object, which needs to be added as a child
116
+ const propType = new Obj(parent, propertyName, schema);
117
+ prop = new PropObj(parent, propertyName, schema, propType);
118
+ }
119
+ // 3rd tries for scalar
120
+ else if (GqlUtils.gqlScalar(type)) {
121
+ const scalar = GqlUtils.gqlScalar(type);
122
+ prop = new PropScalar(parent, propertyName, scalar, schema);
123
+ }
124
+ // or we don't know how to handle this
125
+ else {
126
+ throw new Error('Cannot handle property type ' + type);
127
+ }
128
+ }
129
+ // otherwise let's use the properties instead and assume an Obj
130
+ else if (schema.properties != null) {
131
+ const propType = new Obj(parent, propertyName, schema);
132
+ prop = new PropObj(parent, propertyName, schema, propType);
133
+ }
134
+ // default case, we don't know what to do so we'll create a scalar of type JSON
135
+ else {
136
+ prop = new PropScalar(parent, propertyName, 'JSON', schema);
137
+ }
138
+ if (parent.ancestors().includes(prop)) {
139
+ console.warn('[factory] Recursion detected! Ancestors already contain this type: \n' + prop.pathToRoot());
140
+ }
141
+ return prop;
142
+ }
143
+ static fromResponse(_context, parent, mediaSchema) {
144
+ // const content = Factory.fromSchema(response, mediaSchema);
145
+ // response.response = content;
146
+ return new Response(parent, 'r', mediaSchema);
147
+ }
148
+ static fromParam(context, parent, p) {
149
+ let param;
150
+ if ('$ref' in p) {
151
+ const ref = p;
152
+ const schema = context.lookupParam(ref.$ref);
153
+ if (!schema) {
154
+ throw new Error('Schema not found for ref: ' + ref.$ref);
155
+ }
156
+ param = schema;
157
+ }
158
+ else {
159
+ param = p;
160
+ }
161
+ const schema = param.schema;
162
+ const required = param.required === true;
163
+ return new Param(parent, param.name, schema, required, schema.default, param);
164
+ }
165
+ static fromCircularRef(parent, child) {
166
+ return new CircularRef(parent, child);
167
+ }
168
+ static fromUnion(_context, parent, oneOfs) {
169
+ const union = new Union(parent, parent.name, oneOfs);
170
+ parent.add(union);
171
+ return union;
172
+ }
173
+ }
174
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/oas/nodes/factory.ts"],"names":[],"mappings":"AAIA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,OAAO,OAAO;IACX,M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M,MAAM,GAAG,KAAK,CAAC,MAAsB,CAAC;QAC5C,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,KAAK,IAAI,CAAC;QAEzC,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAChF,CAAC;IAEM,MAAM,CAAC,eAAe,CAAC,MAAa,EAAE,KAAY;QACvD,OAAO,IAAI,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAEM,MAAM,CAAC,SAAS,CAAC,QAAoB,EAAE,MAAa,EAAE,MAAsB;QACjF,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACrD,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import { Operation } from 'oas/operation';\nimport { ParameterObject, SchemaObject } from 'oas/types';\nimport { OpenAPIV3 } from 'openapi-types';\nimport ArraySchemaObject = OpenAPIV3.ArraySchemaObject;\nimport _ from 'lodash';\nimport { IType } from './type.js';\nimport { trace, warn } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Naming } from '../utils/naming.js';\nimport { GqlUtils } from '../utils/gql.js';\nimport { Arr } from './arr.js';\nimport { CircularRef } from './circularRef.js';\nimport { Composed } from './comp.js';\nimport { En } from './en.js';\nimport { Get } from './get.js';\nimport { Obj } from './obj.js';\nimport { Param } from './param/param.js';\nimport { Prop } from './props/prop.js';\nimport { PropArray } from './props/propArray.js';\nimport { PropObj } from './props/propObj.js';\nimport { PropRef } from './props/propRef.js';\nimport { PropScalar } from './props/propScalar.js';\nimport { Ref } from './ref.js';\nimport { Response } from './response.js';\nimport { Union } from './union.js';\nimport { Scalar } from './scalar.js';\nimport { ReferenceObject } from './props/referenceObject.js';\n\nexport class Factory {\n public static createGet(name: string, op: Operation): Get {\n return new Get(name, op);\n }\n\n public static fromSchema(parent: IType, schema: SchemaObject): IType {\n let result: IType | null = null;\n\n if ('$ref' in schema) {\n result = new Ref(parent, schema.$ref as string, schema as ReferenceObject);\n }\n // array case\n else if (schema.type === 'array' && schema.items) {\n // Array schema case.\n let parentName = parent.name;\n if (parent instanceof Response) {\n // Assume parent.parent is a GetOp.\n const get = parent.parent as Get;\n parentName = _.upperFirst(get.getGqlOpName());\n } else {\n trace(null, '[factory]', 'Factory.fromSchema >>> HERE');\n }\n result = new Arr(parent, parentName, schema.items as ArraySchemaObject);\n (result as Arr).itemsType = Factory.fromSchema(result, schema.items as ArraySchemaObject);\n }\n // array case\n else if (schema.type === 'object') {\n if (schema.allOf || schema.oneOf) {\n result = new Composed(parent, _.get(schema, 'name') || parent.name, schema);\n } else {\n if (!schema.properties) {\n warn(\n null,\n '[factory]',\n 'Object has no properties: ' + JSON.stringify(schema, null, 2) + ' in: ' + parent.pathToRoot(),\n );\n }\n\n result = new Obj(parent, _.get(schema, 'name') || null, schema);\n }\n }\n // Composed schema case.\n else if (schema.allOf || schema.oneOf) {\n result = new Composed(parent, _.get(schema, 'name') || parent.name, schema);\n }\n // scalar\n else {\n const typeStr = schema.type;\n if (typeStr != null) {\n if (typeStr === 'array') {\n throw new Error(`Should have been handled already? ${typeStr}, schema: ${JSON.stringify(schema)}`);\n } else if (schema.enum != null) {\n result = new En(parent, schema, schema.enum! as string[]);\n }\n // scalar case\n else if (GqlUtils.gqlScalar(typeStr as string)) {\n const scalarType = GqlUtils.getGQLScalarType(schema);\n result = new Scalar(parent, scalarType, schema);\n } else {\n throw new Error(`Cannot handle property type ${typeStr}, schema: ${JSON.stringify(schema)}`);\n }\n } else if (schema.enum != null) {\n result = new En(parent, schema, _.get(schema, 'enum') as string[]);\n } else {\n throw new Error(`Cannot handle schema ${parent.pathToRoot()}, schema: ${JSON.stringify(schema)}`);\n }\n }\n\n if (result != null) {\n parent.add(result);\n } else {\n throw new Error(`Not yet implemented for ${JSON.stringify(schema)}`);\n }\n\n return result;\n }\n\n public static fromProp(context: OasContext, parent: IType, propertyName: string, schema: SchemaObject): Prop {\n if (!schema) {\n throw new Error(`Should have a schema defined for property '${propertyName}' (parent: '${parent.name}')`);\n }\n\n const type = schema.type;\n let prop: Prop;\n\n if (!type && '$ref' in schema) {\n prop = new PropRef(parent, propertyName, schema, (schema as ReferenceObject).$ref);\n }\n // uses the type of the schema to find out what kind of property it is\n else if (type) {\n // 1st case is if the type is an array\n if (type === 'array') {\n const array = new PropArray(parent, propertyName, schema);\n\n const itemsName = Naming.genArrayItems(propertyName);\n const itemsType = Factory.fromProp(context, array, itemsName, schema.items as ArraySchemaObject);\n\n array.setItems(itemsType);\n\n prop = array;\n }\n // 2nd checks for obj property\n else if (type === 'object') {\n // the type of the property will be an object, which needs to be added as a child\n const propType: IType = new Obj(parent, propertyName, schema);\n prop = new PropObj(parent, propertyName, schema, propType);\n }\n // 3rd tries for scalar\n else if (GqlUtils.gqlScalar(type as string)) {\n const scalar = GqlUtils.gqlScalar(type as string);\n prop = new PropScalar(parent, propertyName, scalar as string, schema);\n }\n // or we don't know how to handle this\n else {\n throw new Error('Cannot handle property type ' + type);\n }\n }\n // otherwise let's use the properties instead and assume an Obj\n else if (schema.properties != null) {\n const propType: IType = new Obj(parent, propertyName, schema);\n prop = new PropObj(parent, propertyName, schema, propType);\n }\n // default case, we don't know what to do so we'll create a scalar of type JSON\n else {\n prop = new PropScalar(parent, propertyName, 'JSON', schema);\n }\n\n if (parent.ancestors().includes(prop)) {\n console.warn('[factory] Recursion detected! Ancestors already contain this type: \\n' + prop.pathToRoot());\n }\n\n return prop;\n }\n\n public static fromResponse(_context: OasContext, parent: IType, mediaSchema: SchemaObject): IType {\n // const content = Factory.fromSchema(response, mediaSchema);\n // response.response = content;\n return new Response(parent, 'r', mediaSchema);\n }\n\n public static fromParam(context: OasContext, parent: IType, p: ParameterObject | ReferenceObject): Param {\n let param: ParameterObject;\n\n if ('$ref' in p) {\n const ref: ReferenceObject = p as ReferenceObject;\n const schema = context.lookupParam(ref.$ref);\n\n if (!schema) {\n throw new Error('Schema not found for ref: ' + ref.$ref);\n }\n param = schema as ParameterObject;\n } else {\n param = p as ParameterObject;\n }\n\n const schema = param.schema as SchemaObject;\n const required = param.required === true;\n\n return new Param(parent, param.name, schema, required, schema.default, param);\n }\n\n public static fromCircularRef(parent: IType, child: IType): IType {\n return new CircularRef(parent, child);\n }\n\n public static fromUnion(_context: OasContext, parent: IType, oneOfs: SchemaObject[]): IType {\n const union = new Union(parent, parent.name, oneOfs);\n parent.add(union);\n return union;\n }\n}\n"]}
@@ -0,0 +1,24 @@
1
+ import { Operation } from 'oas/operation';
2
+ import { OasContext } from '../oasContext.js';
3
+ import { Writer } from '../io/writer.js';
4
+ import { Param } from './param/param.js';
5
+ import { IType, Type } from './type.js';
6
+ export declare class Get extends Type {
7
+ operation: Operation;
8
+ resultType?: IType;
9
+ params: Param[];
10
+ constructor(name: string, operation: Operation);
11
+ get id(): string;
12
+ visit(context: OasContext): void;
13
+ forPrompt(_context: OasContext): string;
14
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
15
+ select(_context: OasContext, _writer: Writer, _selection: string[]): void;
16
+ getGqlOpName(): string;
17
+ private visitParameters;
18
+ private visitResponses;
19
+ private visitResponse;
20
+ private visitResponseContent;
21
+ private visitResponseRef;
22
+ private generateParameters;
23
+ private visitParameter;
24
+ }