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,167 @@
1
+ import { trace, warn } from '../log/trace.js';
2
+ import { Naming } from '../utils/naming.js';
3
+ import { Factory } from './factory.js';
4
+ import { Type } from './type.js';
5
+ export class Get extends Type {
6
+ operation;
7
+ resultType;
8
+ params = [];
9
+ constructor(name, operation) {
10
+ super(undefined, name);
11
+ this.operation = operation;
12
+ }
13
+ get id() {
14
+ return `get:${this.name}`;
15
+ }
16
+ visit(context) {
17
+ if (this.visited) {
18
+ trace(context, '-> [get:visit]', this.name + ' already visited.');
19
+ return;
20
+ }
21
+ context.enter(this);
22
+ trace(context, '-> [get:visit]', 'in ' + this.name);
23
+ // 1. Visit params.
24
+ this.visitParameters(context);
25
+ // 2. Visit responses
26
+ this.visitResponses(context);
27
+ this.visited = true;
28
+ trace(context, '<- [get:visit]', 'out ' + this.name);
29
+ context.leave(this);
30
+ }
31
+ forPrompt(_context) {
32
+ return `[GET] ${this.name}`;
33
+ }
34
+ generate(context, writer, selection) {
35
+ context.enter(this);
36
+ trace(context, '-> [get::generate]', `-> in: ${this.name}`);
37
+ const summary = this.operation.getSummary();
38
+ const originalPath = this.operation.path;
39
+ if (summary || originalPath) {
40
+ writer.append(' """\n').append(' ');
41
+ if (summary) {
42
+ writer.append(summary).append(' ');
43
+ }
44
+ if (originalPath) {
45
+ writer.append('(').append(originalPath).append(')');
46
+ }
47
+ writer.append('\n """\n');
48
+ }
49
+ writer.append(' ').append(this.getGqlOpName());
50
+ this.generateParameters(context, writer, selection);
51
+ if (this.resultType) {
52
+ writer.append(': ');
53
+ this.resultType.generate(context, writer, selection);
54
+ }
55
+ writer.append('\n');
56
+ trace(context, '<- [get::generate]', `-> out: ${this.name}`);
57
+ context.leave(this);
58
+ }
59
+ select(_context, _writer, _selection) {
60
+ // do nothing
61
+ }
62
+ getGqlOpName() {
63
+ return Naming.genOperationName(this.operation.path, this.operation);
64
+ }
65
+ visitParameters(context) {
66
+ trace(context, '-> [get::params]', 'in: ' + this.name);
67
+ const parameters = this.operation.getParameters();
68
+ if (parameters && parameters.length > 0) {
69
+ this.params = parameters
70
+ .filter((p) => !p.in || (p.in && p.in.toLowerCase() !== 'header'))
71
+ .map((p) => this.visitParameter(context, this, p));
72
+ }
73
+ else {
74
+ this.params = [];
75
+ }
76
+ trace(context, '<- [get::params]', 'out: ' + this.name);
77
+ }
78
+ visitResponses = (context) => {
79
+ const statusCodes = this.operation.getResponseStatusCodes();
80
+ if (!statusCodes.includes('200') && !statusCodes.includes('default')) {
81
+ throw new Error('Could not find a valid 200 response');
82
+ }
83
+ const responses = this.operation.schema.responses;
84
+ if (responses['200']) {
85
+ this.visitResponse(context, '200', responses['200']);
86
+ }
87
+ else if (responses.default) {
88
+ this.visitResponse(context, 'default', responses.default);
89
+ }
90
+ else {
91
+ throw new Error("Could not find a '200' or 'default' response");
92
+ }
93
+ };
94
+ visitResponse(context, code, response) {
95
+ const content = response.content;
96
+ if ('$ref' in response) {
97
+ this.visitResponseRef(context, response);
98
+ }
99
+ // If the response has a content property, we need to find the JSON content.
100
+ else if (content) {
101
+ const json = response.content['application/json'];
102
+ if (!json) {
103
+ warn(context, ` [${code}]`, 'no entry found for content application/json!');
104
+ }
105
+ else {
106
+ this.visitResponseContent(context, code, json);
107
+ }
108
+ }
109
+ else if (code === 'default') {
110
+ // there is no response for this operation
111
+ // TODO: should we synthesize one?
112
+ }
113
+ else {
114
+ throw new Error('Not yet implemented for: ' + JSON.stringify(response));
115
+ }
116
+ }
117
+ visitResponseContent(context, _code, media) {
118
+ trace(context, '-> [get::responses::content]', 'in ' + this.name);
119
+ const schema = media.schema;
120
+ if (!schema) {
121
+ throw new Error('No schema content found!');
122
+ }
123
+ this.resultType = Factory.fromResponse(context, this, schema);
124
+ // PENDING: do not visit anymore
125
+ // if (this.resultType) {
126
+ // this.resultType.visit(context);
127
+ // }
128
+ if (this.resultType && !this.children.includes(this.resultType)) {
129
+ this.add(this.resultType);
130
+ }
131
+ trace(context, '<- [get::responses::content]', 'out ' + this.name);
132
+ }
133
+ visitResponseRef(context, ref) {
134
+ trace(context, '-> [get::responses::ref]', `in: ${this.name}, ref: ${ref.$ref}`);
135
+ const lookup = context.lookupResponse(ref.$ref);
136
+ if (!lookup) {
137
+ throw new Error('Could not find a response with ref: ' + ref.$ref);
138
+ }
139
+ if ('$ref' in lookup) {
140
+ throw new Error('Not yet implemented for nested refs');
141
+ }
142
+ this.visitResponse(context, ref.$ref, lookup);
143
+ trace(context, '<- [get::responses::ref]', `out: ${this.name}, ref: ${ref.$ref}`);
144
+ }
145
+ generateParameters(context, writer, selection) {
146
+ const sorted = this.params.sort((a, b) => (b.required ? 1 : 0) - (a.required ? 1 : 0));
147
+ if (sorted.length === 0) {
148
+ return;
149
+ }
150
+ writer.append('(');
151
+ sorted.forEach((parameter, index) => {
152
+ if (index > 0) {
153
+ writer.append(', ');
154
+ }
155
+ parameter.generate(context, writer, selection);
156
+ });
157
+ writer.append(')');
158
+ }
159
+ visitParameter(context, parent, p) {
160
+ trace(context, '->[visitParameter]', 'begin: ' + p.name);
161
+ const param = Factory.fromParam(context, parent, p);
162
+ param.visit(context);
163
+ trace(context, '<-[visitParameter]', 'end: ' + p.name);
164
+ return param;
165
+ }
166
+ }
167
+ //# sourceMappingURL=get.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get.js","sourceRoot":"","sources":["../../../src/oas/nodes/get.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAG9C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAS,IAAI,EAAE,MAAM,WAAW,CAAC;AAIxC,MAAM,OAAO,GAAI,SAAQ,IAAI;IAMlB;IALF,UAAU,CAAS;IACnB,MAAM,GAAY,EAAE,CAAC;IAE5B,YACE,IAAY,EACL,SAAoB;QAE3B,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAFhB,cAAS,GAAT,SAAS,CAAW;IAG7B,CAAC;IAED,IAAI,EAAE;QACJ,OAAO,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;IAEM,KAAK,CAAC,OAAmB;QAC9B,IAAI,IAAI,CAAC,OAAO,EAAE,C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,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAEvF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAEnB,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,KAAK,EAAE,EAAE;YAClC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACd,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACtB,CAAC;YACD,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACrB,CAAC;IAEO,cAAc,CAAC,OAAmB,EAAE,MAAY,EAAE,CAAkB;QAC1E,KAAK,CAAC,OAAO,EAAE,oBAAoB,EAAE,SAAS,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;QAEzD,MAAM,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;QACpD,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAErB,KAAK,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;QACvD,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import { Operation } from 'oas/operation';\nimport { MediaTypeObject, ParameterObject, ResponseObject, SchemaObject } from 'oas/types';\n\nimport { trace, warn } 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 { Param } from './param/param.js';\nimport { IType, Type } from './type.js';\n\nimport { ReferenceObject } from './props/referenceObject.js';\n\nexport class Get extends Type {\n public resultType?: IType;\n public params: Param[] = [];\n\n constructor(\n name: string,\n public operation: Operation,\n ) {\n super(undefined, name);\n }\n\n get id(): string {\n return `get:${this.name}`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n trace(context, '-> [get:visit]', this.name + ' already visited.');\n return;\n }\n\n context.enter(this);\n trace(context, '-> [get:visit]', 'in ' + this.name);\n\n // 1. Visit params.\n this.visitParameters(context);\n\n // 2. Visit responses\n this.visitResponses(context);\n this.visited = true;\n\n trace(context, '<- [get:visit]', 'out ' + this.name);\n context.leave(this);\n }\n\n public forPrompt(_context: OasContext): string {\n return `[GET] ${this.name}`;\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n trace(context, '-> [get::generate]', `-> in: ${this.name}`);\n\n const summary = this.operation.getSummary();\n const originalPath = this.operation.path;\n\n if (summary || originalPath) {\n writer.append(' \"\"\"\\n').append(' ');\n if (summary) {\n writer.append(summary).append(' ');\n }\n if (originalPath) {\n writer.append('(').append(originalPath).append(')');\n }\n writer.append('\\n \"\"\"\\n');\n }\n\n writer.append(' ').append(this.getGqlOpName());\n this.generateParameters(context, writer, selection);\n\n if (this.resultType) {\n writer.append(': ');\n this.resultType.generate(context, writer, selection);\n }\n\n writer.append('\\n');\n trace(context, '<- [get::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public select(_context: OasContext, _writer: Writer, _selection: string[]) {\n // do nothing\n }\n\n public getGqlOpName(): string {\n return Naming.genOperationName(this.operation.path, this.operation);\n }\n\n private visitParameters(context: OasContext): void {\n trace(context, '-> [get::params]', 'in: ' + this.name);\n\n const parameters = this.operation.getParameters();\n\n if (parameters && parameters.length > 0) {\n this.params = parameters\n .filter((p) => !p.in || (p.in && (p.in as string).toLowerCase() !== 'header'))\n .map((p: ParameterObject) => this.visitParameter(context, this, p));\n } else {\n this.params = [];\n }\n\n trace(context, '<- [get::params]', 'out: ' + this.name);\n }\n\n private visitResponses = (context: OasContext) => {\n const statusCodes = this.operation.getResponseStatusCodes();\n\n if (!statusCodes.includes('200') && !statusCodes.includes('default')) {\n throw new Error('Could not find a valid 200 response');\n }\n\n const responses = this.operation.schema.responses;\n if (responses!['200']) {\n this.visitResponse(context, '200', responses!['200'] as ResponseObject);\n } else if (responses!.default) {\n this.visitResponse(context, 'default', responses!.default as ResponseObject);\n } else {\n throw new Error(\"Could not find a '200' or 'default' response\");\n }\n };\n\n private visitResponse(context: OasContext, code: string, response: ResponseObject): void {\n const content = response.content as MediaTypeObject;\n\n if ('$ref' in response) {\n this.visitResponseRef(context, response as ReferenceObject);\n }\n // If the response has a content property, we need to find the JSON content.\n else if (content) {\n const json = response.content!['application/json']!;\n if (!json) {\n warn(context, ` [${code}]`, 'no entry found for content application/json!');\n } else {\n this.visitResponseContent(context, code, json);\n }\n } else if (code === 'default') {\n // there is no response for this operation\n // TODO: should we synthesize one?\n } else {\n throw new Error('Not yet implemented for: ' + JSON.stringify(response));\n }\n }\n\n private visitResponseContent(context: OasContext, _code: string, media: MediaTypeObject): void {\n trace(context, '-> [get::responses::content]', 'in ' + this.name);\n const schema = media!.schema as SchemaObject;\n\n if (!schema) {\n throw new Error('No schema content found!');\n }\n\n this.resultType = Factory.fromResponse(context, this, schema);\n // PENDING: do not visit anymore\n // if (this.resultType) {\n // this.resultType.visit(context);\n // }\n\n if (this.resultType && !this.children.includes(this.resultType)) {\n this.add(this.resultType);\n }\n\n trace(context, '<- [get::responses::content]', 'out ' + this.name);\n }\n\n private visitResponseRef(context: OasContext, ref: ReferenceObject): void {\n trace(context, '-> [get::responses::ref]', `in: ${this.name}, ref: ${ref.$ref}`);\n\n const lookup = context.lookupResponse(ref.$ref!);\n if (!lookup) {\n throw new Error('Could not find a response with ref: ' + ref.$ref);\n }\n\n if ('$ref' in lookup) {\n throw new Error('Not yet implemented for nested refs');\n }\n\n this.visitResponse(context, ref.$ref!, lookup as ResponseObject);\n trace(context, '<- [get::responses::ref]', `out: ${this.name}, ref: ${ref.$ref}`);\n }\n\n private generateParameters(context: OasContext, writer: Writer, selection: string[]): void {\n const sorted = this.params.sort((a, b) => (b.required ? 1 : 0) - (a.required ? 1 : 0));\n\n if (sorted.length === 0) {\n return;\n }\n\n writer.append('(');\n\n sorted.forEach((parameter, index) => {\n if (index > 0) {\n writer.append(', ');\n }\n parameter.generate(context, writer, selection);\n });\n\n writer.append(')');\n }\n\n private visitParameter(context: OasContext, parent: Type, p: ParameterObject): Param {\n trace(context, '->[visitParameter]', 'begin: ' + p.name);\n\n const param = Factory.fromParam(context, parent, p);\n param.visit(context);\n\n trace(context, '<-[visitParameter]', 'end: ' + p.name);\n return param;\n }\n}\n"]}
@@ -0,0 +1,15 @@
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 Obj extends Type {
6
+ schema: SchemaObject;
7
+ constructor(parent: IType | undefined, name: string, schema: SchemaObject);
8
+ forPrompt(context: OasContext): string;
9
+ get id(): string;
10
+ visit(context: OasContext): void;
11
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
12
+ select(context: OasContext, writer: Writer, selection: string[]): void;
13
+ private updateName;
14
+ private visitProperties;
15
+ }
@@ -0,0 +1,177 @@
1
+ import { trace } from '../log/trace.js';
2
+ import { Naming } from '../utils/naming.js';
3
+ import { Arr } from './arr.js';
4
+ import { Factory } from './factory.js';
5
+ import { PropArray } from './props/propArray.js';
6
+ import { Ref } from './ref.js';
7
+ import { Response } from './response.js';
8
+ import { Type } from './type.js';
9
+ export class Obj extends Type {
10
+ schema;
11
+ constructor(parent, name, schema) {
12
+ super(parent, name);
13
+ this.schema = schema;
14
+ this.updateName();
15
+ }
16
+ forPrompt(context) {
17
+ return `${Naming.getRefName(this.name)} (Obj)`;
18
+ }
19
+ get id() {
20
+ return `obj:${this.name}`;
21
+ }
22
+ visit(context) {
23
+ if (this.visited) {
24
+ return;
25
+ }
26
+ context.enter(this);
27
+ trace(context, '-> [obj:visit]', 'in ' + this.name);
28
+ if (!context.inContextOf('Composed', this)) {
29
+ trace(context, '[obj]', 'In object: ' + (this.name ? this.name : this.parent?.name));
30
+ }
31
+ this.visitProperties(context);
32
+ this.visited = true;
33
+ if (this.name) {
34
+ context.store(this.name, this);
35
+ }
36
+ trace(context, '<- [obj:visit]', 'out ' + this.name);
37
+ context.leave(this);
38
+ }
39
+ generate(context, writer, selection) {
40
+ if (this.props.size === 0) {
41
+ return;
42
+ }
43
+ if (context.inContextOf('Response', this)) {
44
+ writer.append(Naming.genTypeName(this.name));
45
+ return;
46
+ }
47
+ context.enter(this);
48
+ trace(context, '-> [obj::generate]', `-> in: ${this.name}`);
49
+ const sanitised = Naming.genTypeName(this.name);
50
+ const refName = Naming.getRefName(this.name);
51
+ writer
52
+ .append('type ')
53
+ .append(sanitised === refName ? refName : sanitised)
54
+ .append(' {\n');
55
+ const selected = this.selectedProps(selection);
56
+ for (const prop of selected) {
57
+ trace(context, '-> [obj::generate]', `-> property: ${prop.name} (parent: ${prop.parent.name})`);
58
+ prop.generate(context, writer, selection);
59
+ }
60
+ writer.append('}\n\n');
61
+ trace(context, '<- [obj::generate]', `-> out: ${this.name}`);
62
+ context.leave(this);
63
+ }
64
+ select(context, writer, selection) {
65
+ trace(context, '-> [obj::select]', `-> in: ${this.name}`);
66
+ const selected = this.selectedProps(selection);
67
+ for (const prop of selected) {
68
+ prop.select(context, writer, selection);
69
+ }
70
+ trace(context, '<- [obj::select]', `-> out: ${this.name}`);
71
+ }
72
+ updateName() {
73
+ let name = this.name;
74
+ // If we are an inline object named "items", try to create a better name.
75
+ if (!name || name === 'items') {
76
+ const parent = this.parent;
77
+ const parentName = parent.name;
78
+ // if the parent is a reference, we can use the name of the obj itself
79
+ if (parent instanceof Ref) {
80
+ name = parentName.replace('ref:', 'obj:');
81
+ }
82
+ // else is our parent an array?
83
+ else if (parent instanceof Arr || parent instanceof PropArray) {
84
+ // if so, synthesize a name based on the parent name
85
+ name = Naming.genTypeName(Naming.getRefName(parentName) + 'Item');
86
+ }
87
+ // if the parent is a response, we can use the operation name and append "Response"
88
+ else if (parent instanceof Response) {
89
+ const op = parent.parent;
90
+ name = op.getGqlOpName() + 'Response';
91
+ }
92
+ // if the parent is an object then we can use the parent name
93
+ else if (parent instanceof Obj) {
94
+ name = parentName + 'Obj';
95
+ }
96
+ // extreme case -- we synthesize an anonymous name
97
+ else {
98
+ name = `[anonymous:${this.parent.name}]`;
99
+ }
100
+ }
101
+ this.name = name;
102
+ }
103
+ visitProperties(context) {
104
+ if (!this.schema.properties) {
105
+ return;
106
+ }
107
+ const properties = this.schema.properties;
108
+ const propKeys = Object.keys(properties);
109
+ trace(context, '-> [obj::props]', 'in props ' + (propKeys.length === 0 ? '0' : propKeys.length.toString()));
110
+ if (propKeys.length === 0) {
111
+ trace(context, '<- [obj::props]', 'no props ' + this.props.size);
112
+ return;
113
+ }
114
+ const sorted = Object.entries(properties).sort((a, b) => a[0].toLowerCase().localeCompare(b[0].toLowerCase()));
115
+ for (const [key, schemaValue] of sorted) {
116
+ const prop = Factory.fromProp(context, this, key, schemaValue);
117
+ this.props.set(prop.name, prop);
118
+ // TODO: we should not be adding this twice
119
+ if (!this.children.includes(prop)) {
120
+ this.add(prop);
121
+ }
122
+ }
123
+ // const propertiesNames = Array.from(collected.values())
124
+ // .map((p) => p.forPrompt(context))
125
+ // .join(',\n - ');
126
+ //
127
+ // const inCompose = context.inContextOf(Composed, this);
128
+ // const inComposeIdx = Type.findAncestorOf(this, Composed);
129
+ // const inArrayIdx = Type.findAncestorOf(this, PropArray);
130
+ //
131
+ // if (!inCompose || inArrayIdx > inComposeIdx) {
132
+ // console.log('Obj.visitProperties HERE');
133
+ // }
134
+ //
135
+ // trace(
136
+ // context,
137
+ // ' [obj::props]',
138
+ // `${this.getSimpleName()} is within compose context? ${inCompose}`
139
+ // );
140
+ //
141
+ // const addAll =
142
+ // !inCompose || inArrayIdx > inComposeIdx
143
+ // ? await context.prompt.yesNoSelect(
144
+ // this.path(),
145
+ // ` -> Add all properties from [object] ${this.getOwner()}?: \n - ${propertiesNames}\n`
146
+ // )
147
+ // : 'y';
148
+ //
149
+ // if (addAll === 'y' || addAll === 's') {
150
+ // for (const [propertyName, propertySchema] of sorted) {
151
+ // const prop = Factory.fromProperty(
152
+ // context,
153
+ // this,
154
+ // propertyName,
155
+ // propertySchema
156
+ // );
157
+ // if (
158
+ // addAll === 'y' ||
159
+ // (await context.prompt.yesNo(
160
+ // prop.path(),
161
+ // `Add field '${prop.forPrompt(context)}'?`
162
+ // ))
163
+ // ) {
164
+ // trace(context, ' [obj::props]', 'prop: ' + prop);
165
+ // this.props.set(propertyName, prop);
166
+ // if (!this.children.includes(prop)) {
167
+ // this.add(prop);
168
+ // }
169
+ // }
170
+ // }
171
+ // }
172
+ //
173
+ // this.addDependencies(context);
174
+ trace(context, '<- [obj::props]', 'out props ' + this.props.size);
175
+ }
176
+ }
177
+ //# sourceMappingURL=obj.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"obj.js","sourceRoot":"","sources":["../../../src/oas/nodes/obj.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAS,IAAI,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,OAAO,GAAI,SAAQ,IAAI;IAIlB;IAHT,YACE,MAAyB,EACzB,IAAY,EACL,MAAoB;QAE3B,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAFb,WAAM,GAAN,MAAM,CAAc;QAG3B,IAAI,CAAC,UAAU,E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IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAEhC,2CAA2C;YAC3C,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBAClC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;QAED,yDAAyD;QACzD,sCAAsC;QACtC,qBAAqB;QACrB,EAAE;QACF,yDAAyD;QACzD,4DAA4D;QAC5D,2DAA2D;QAC3D,EAAE;QACF,iDAAiD;QACjD,6CAA6C;QAC7C,IAAI;QACJ,EAAE;QACF,SAAS;QACT,aAAa;QACb,uBAAuB;QACvB,sEAAsE;QACtE,KAAK;QACL,EAAE;QACF,iBAAiB;QACjB,4CAA4C;QAC5C,0CAA0C;QAC1C,qBAAqB;QACrB,8FAA8F;QAC9F,QAAQ;QACR,aAAa;QACb,EAAE;QACF,0CAA0C;QAC1C,2DAA2D;QAC3D,yCAAyC;QACzC,iBAAiB;QACjB,cAAc;QACd,sBAAsB;QACtB,uBAAuB;QACvB,SAAS;QACT,WAAW;QACX,0BAA0B;QAC1B,qCAAqC;QACrC,uBAAuB;QACvB,oDAAoD;QACpD,WAAW;QACX,UAAU;QACV,4DAA4D;QAC5D,4CAA4C;QAC5C,6CAA6C;QAC7C,0BAA0B;QAC1B,UAAU;QACV,QAAQ;QACR,MAAM;QACN,IAAI;QACJ,EAAE;QACF,iCAAiC;QACjC,KAAK,CAAC,OAAO,EAAE,iBAAiB,EAAE,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpE,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 { Arr } from './arr.js';\nimport { Factory } from './factory.js';\nimport { Get } from './get.js';\nimport { PropArray } from './props/propArray.js';\nimport { Ref } from './ref.js';\nimport { Response } from './response.js';\nimport { IType, Type } from './type.js';\n\nexport class Obj extends Type {\n constructor(\n parent: IType | undefined,\n name: string,\n public schema: SchemaObject,\n ) {\n super(parent, name);\n this.updateName();\n }\n\n public forPrompt(context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Obj)`;\n }\n\n get id(): string {\n return `obj:${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, '-> [obj:visit]', 'in ' + this.name);\n\n if (!context.inContextOf('Composed', this)) {\n trace(context, '[obj]', 'In object: ' + (this.name ? this.name : this.parent?.name));\n }\n\n this.visitProperties(context);\n this.visited = true;\n\n if (this.name) {\n context.store(this.name, this);\n }\n\n trace(context, '<- [obj:visit]', 'out ' + this.name);\n context.leave(this);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n if (this.props.size === 0) {\n return;\n }\n\n if (context.inContextOf('Response', this)) {\n writer.append(Naming.genTypeName(this.name));\n return;\n }\n\n context.enter(this);\n trace(context, '-> [obj::generate]', `-> in: ${this.name}`);\n\n const sanitised = Naming.genTypeName(this.name);\n const refName = Naming.getRefName(this.name);\n\n writer\n .append('type ')\n .append(sanitised === refName ? refName : sanitised)\n .append(' {\\n');\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n trace(context, '-> [obj::generate]', `-> property: ${prop.name} (parent: ${prop.parent!.name})`);\n prop.generate(context, writer, selection);\n }\n\n writer.append('}\\n\\n');\n\n trace(context, '<- [obj::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [obj::select]', `-> in: ${this.name}`);\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n prop.select(context, writer, selection);\n }\n\n trace(context, '<- [obj::select]', `-> out: ${this.name}`);\n }\n\n private updateName(): void {\n let name = this.name;\n // If we are an inline object named \"items\", try to create a better name.\n if (!name || name === 'items') {\n const parent = this.parent;\n const parentName = parent!.name;\n\n // if the parent is a reference, we can use the name of the obj itself\n if (parent instanceof Ref) {\n name = parentName.replace('ref:', 'obj:');\n }\n // else is our parent an array?\n else if (parent instanceof Arr || parent instanceof PropArray) {\n // if so, synthesize a name based on the parent name\n name = Naming.genTypeName(Naming.getRefName(parentName) + 'Item');\n }\n // if the parent is a response, we can use the operation name and append \"Response\"\n else if (parent instanceof Response) {\n const op = parent.parent as Get;\n name = op.getGqlOpName() + 'Response';\n }\n // if the parent is an object then we can use the parent name\n else if (parent instanceof Obj) {\n name = parentName + 'Obj';\n }\n // extreme case -- we synthesize an anonymous name\n else {\n name = `[anonymous:${this.parent!.name}]`;\n }\n }\n\n this.name = name;\n }\n\n private visitProperties(context: OasContext): void {\n if (!this.schema.properties) {\n return;\n }\n\n const properties = this.schema.properties as Record<string, SchemaObject>;\n const propKeys = Object.keys(properties);\n trace(context, '-> [obj::props]', 'in props ' + (propKeys.length === 0 ? '0' : propKeys.length.toString()));\n\n if (propKeys.length === 0) {\n trace(context, '<- [obj::props]', 'no props ' + this.props.size);\n return;\n }\n\n const sorted = Object.entries(properties).sort((a, b) => a[0].toLowerCase().localeCompare(b[0].toLowerCase()));\n\n for (const [key, schemaValue] of sorted) {\n const prop = Factory.fromProp(context, this, key, schemaValue);\n this.props.set(prop.name, prop);\n\n // TODO: we should not be adding this twice\n if (!this.children.includes(prop)) {\n this.add(prop);\n }\n }\n\n // const propertiesNames = Array.from(collected.values())\n // .map((p) => p.forPrompt(context))\n // .join(',\\n - ');\n //\n // const inCompose = context.inContextOf(Composed, this);\n // const inComposeIdx = Type.findAncestorOf(this, Composed);\n // const inArrayIdx = Type.findAncestorOf(this, PropArray);\n //\n // if (!inCompose || inArrayIdx > inComposeIdx) {\n // console.log('Obj.visitProperties HERE');\n // }\n //\n // trace(\n // context,\n // ' [obj::props]',\n // `${this.getSimpleName()} is within compose context? ${inCompose}`\n // );\n //\n // const addAll =\n // !inCompose || inArrayIdx > inComposeIdx\n // ? await context.prompt.yesNoSelect(\n // this.path(),\n // ` -> Add all properties from [object] ${this.getOwner()}?: \\n - ${propertiesNames}\\n`\n // )\n // : 'y';\n //\n // if (addAll === 'y' || addAll === 's') {\n // for (const [propertyName, propertySchema] of sorted) {\n // const prop = Factory.fromProperty(\n // context,\n // this,\n // propertyName,\n // propertySchema\n // );\n // if (\n // addAll === 'y' ||\n // (await context.prompt.yesNo(\n // prop.path(),\n // `Add field '${prop.forPrompt(context)}'?`\n // ))\n // ) {\n // trace(context, ' [obj::props]', 'prop: ' + prop);\n // this.props.set(propertyName, prop);\n // if (!this.children.includes(prop)) {\n // this.add(prop);\n // }\n // }\n // }\n // }\n //\n // this.addDependencies(context);\n trace(context, '<- [obj::props]', 'out props ' + this.props.size);\n }\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import { ParameterObject, 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 Param extends Type {
6
+ schema: SchemaObject;
7
+ required: boolean;
8
+ defaultValue: unknown;
9
+ parameter: ParameterObject;
10
+ resultType: IType;
11
+ constructor(parent: IType, name: string, schema: SchemaObject, required: boolean, defaultValue: unknown, parameter: ParameterObject);
12
+ visit(context: OasContext): void;
13
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
14
+ forPrompt(context: OasContext): string;
15
+ select(context: OasContext, writer: Writer, selection: string[]): void;
16
+ private writeDefaultValue;
17
+ }
@@ -0,0 +1,64 @@
1
+ import { trace } from '../../log/trace.js';
2
+ import { Naming } from '../../utils/naming.js';
3
+ import { Factory } from '../factory.js';
4
+ import { Type } from '../type.js';
5
+ export class Param extends Type {
6
+ schema;
7
+ required;
8
+ defaultValue;
9
+ parameter;
10
+ resultType;
11
+ constructor(parent, name, schema, required, defaultValue, parameter) {
12
+ super(parent, name);
13
+ this.schema = schema;
14
+ this.required = required;
15
+ this.defaultValue = defaultValue;
16
+ this.parameter = parameter;
17
+ }
18
+ visit(context) {
19
+ if (this.visited) {
20
+ return;
21
+ }
22
+ context.enter(this);
23
+ trace(context, '-> [param:visit]', 'in: ' + this.name);
24
+ this.resultType = Factory.fromSchema(this, this.schema);
25
+ trace(context, ' [param:visit]', 'type: ' + this.resultType);
26
+ this.resultType.visit(context);
27
+ trace(context, '<- [param:visit]', 'out: ' + this.name);
28
+ context.leave(this);
29
+ }
30
+ generate(context, writer, selection) {
31
+ context.enter(this);
32
+ trace(context, '-> [param::generate]', `-> in: ${this.name}`);
33
+ writer.write(Naming.genParamName(this.name));
34
+ writer.write(': ');
35
+ this.resultType.generate(context, writer, selection);
36
+ if (this.required) {
37
+ writer.write('!');
38
+ }
39
+ if (this.defaultValue !== null && this.defaultValue !== undefined) {
40
+ this.writeDefaultValue(writer);
41
+ }
42
+ trace(context, '<- [param::generate]', `-> out: ${this.name}`);
43
+ context.leave(this);
44
+ }
45
+ forPrompt(context) {
46
+ return `Param{ name=${this.name}, required=${this.required}, defaultValue=${this.defaultValue}, props=${this.props}, resultType=${this.resultType} }`;
47
+ }
48
+ select(context, writer, selection) {
49
+ // do nothing
50
+ }
51
+ writeDefaultValue(writer) {
52
+ writer.write(' = ');
53
+ const value = this.defaultValue;
54
+ if (typeof value === 'number') {
55
+ writer.write(value.toString());
56
+ }
57
+ else if (typeof value === 'string') {
58
+ writer.write('"');
59
+ writer.write(String(value));
60
+ writer.write('"');
61
+ }
62
+ }
63
+ }
64
+ //# sourceMappingURL=param.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"param.js","sourceRoot":"","sources":["../../../../src/oas/nodes/param/param.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAG3C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAS,IAAI,EAAE,MAAM,YAAY,CA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YAC5B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;CACF","sourcesContent":["import { ParameterObject, 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 { IType, Type } from '../type.js';\n\nexport class Param extends Type {\n public resultType!: IType;\n\n constructor(\n parent: IType,\n name: string,\n public schema: SchemaObject,\n public required: boolean,\n public defaultValue: unknown,\n public parameter: ParameterObject,\n ) {\n super(parent, name);\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [param:visit]', 'in: ' + this.name);\n\n this.resultType = Factory.fromSchema(this, this.schema);\n trace(context, ' [param:visit]', 'type: ' + this.resultType);\n this.resultType.visit(context);\n\n trace(context, '<- [param: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, '-> [param::generate]', `-> in: ${this.name}`);\n\n writer.write(Naming.genParamName(this.name));\n writer.write(': ');\n\n this.resultType.generate(context, writer, selection);\n\n if (this.required) {\n writer.write('!');\n }\n\n if (this.defaultValue !== null && this.defaultValue !== undefined) {\n this.writeDefaultValue(writer);\n }\n\n trace(context, '<- [param::generate]', `-> out: ${this.name}`);\n context.leave(this);\n }\n\n public forPrompt(context: OasContext): string {\n return `Param{ name=${this.name}, required=${this.required}, defaultValue=${this.defaultValue}, props=${this.props}, resultType=${this.resultType} }`;\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n // do nothing\n }\n\n private writeDefaultValue(writer: Writer): void {\n writer.write(' = ');\n const value = this.defaultValue;\n\n if (typeof value === 'number') {\n writer.write(value.toString());\n } else if (typeof value === 'string') {\n writer.write('\"');\n writer.write(String(value));\n writer.write('\"');\n }\n }\n}\n"]}
@@ -0,0 +1,12 @@
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 abstract class Prop extends Type {
6
+ schema: SchemaObject;
7
+ required: boolean;
8
+ constructor(parent: IType | undefined, name: string, schema: SchemaObject);
9
+ generate(context: OasContext, writer: Writer, selection: string[]): void;
10
+ abstract getValue(context: OasContext): string;
11
+ protected generateValue(context: OasContext, writer: Writer): void;
12
+ }
@@ -0,0 +1,33 @@
1
+ import { Naming } from '../../utils/naming.js';
2
+ import { Type } from '../type.js';
3
+ export class Prop extends Type {
4
+ schema;
5
+ required = false;
6
+ constructor(parent, name, schema) {
7
+ super(parent, name);
8
+ this.schema = schema;
9
+ }
10
+ generate(context, writer, selection) {
11
+ const description = this.schema.description;
12
+ if (description != null) {
13
+ if (description.includes('\n') || description.includes('\r') || description.includes('"')) {
14
+ writer.append(' """\n').append(' ').append(description).append('\n """\n');
15
+ }
16
+ else {
17
+ writer.append(' "').append(description).append('"\n');
18
+ }
19
+ }
20
+ writer.append(' ').append(Naming.sanitiseField(this.name)).append(': ');
21
+ this.generateValue(context, writer);
22
+ if (this.required) {
23
+ writer.append('!');
24
+ }
25
+ // TODO: source
26
+ // writer.append(" # ").append(this.parent);
27
+ writer.append('\n');
28
+ }
29
+ generateValue(context, writer) {
30
+ writer.append(this.getValue(context));
31
+ }
32
+ }
33
+ //# sourceMappingURL=prop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prop.js","sourceRoot":"","sources":["../../../../src/oas/nodes/props/prop.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAS,IAAI,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,OAAgB,IAAK,SAAQ,IAAI;IAM5B;IALF,QAAQ,GAAY,KAAK,CAAC;IAEjC,YACE,MAAyB,EACzB,IAAY,EACL,MAAoB;QAE3B,KAAK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Object } from 'oas/types';\nimport { OasContext } from '../../oasContext.js';\nimport { Writer } from '../../io/writer.js';\nimport { Naming } from '../../utils/naming.js';\nimport { IType, Type } from '../type.js';\n\nexport abstract class Prop extends Type {\n public required: boolean = false;\n\n constructor(\n parent: IType | undefined,\n name: string,\n public schema: SchemaObject,\n ) {\n super(parent, name);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n const description = this.schema.description;\n if (description != null) {\n if (description.includes('\\n') || description.includes('\\r') || description.includes('\"')) {\n writer.append(' \"\"\"\\n').append(' ').append(description).append('\\n \"\"\"\\n');\n } else {\n writer.append(' \"').append(description).append('\"\\n');\n }\n }\n\n writer.append(' ').append(Naming.sanitiseField(this.name)).append(': ');\n\n this.generateValue(context, writer);\n\n if (this.required) {\n writer.append('!');\n }\n\n // TODO: source\n // writer.append(\" # \").append(this.parent);\n\n writer.append('\\n');\n }\n\n public abstract getValue(context: OasContext): string;\n\n protected generateValue(context: OasContext, writer: Writer): void {\n writer.append(this.getValue(context));\n }\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import { OasContext } from '../../oasContext.js';
2
+ import { Writer } from '../../io/writer.js';
3
+ import { IType } from '../type.js';
4
+ import { Prop } from '../props/prop.js';
5
+ export declare class PropArray extends Prop {
6
+ items?: Prop;
7
+ get id(): string;
8
+ visit(context: OasContext): void;
9
+ setItems(items: Prop): void;
10
+ getValue(context: OasContext): string;
11
+ add(child: IType): void;
12
+ forPrompt(context: OasContext): string;
13
+ select(context: OasContext, writer: Writer, selection: string[]): void;
14
+ needsBrackets(child: IType): boolean;
15
+ }