@azure-tools/rlc-common 1.0.0-beta.1

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 (209) hide show
  1. package/.rush/temp/package-deps_build.json +45 -0
  2. package/.rush/temp/shrinkwrap-deps.json +72 -0
  3. package/CHANGELOG.md +3 -0
  4. package/dist/buildClient.js +198 -0
  5. package/dist/buildClient.js.map +1 -0
  6. package/dist/buildClientDefinitions.js +160 -0
  7. package/dist/buildClientDefinitions.js.map +1 -0
  8. package/dist/buildIndexFile.js +150 -0
  9. package/dist/buildIndexFile.js.map +1 -0
  10. package/dist/buildIsUnexpectedHelper.js +219 -0
  11. package/dist/buildIsUnexpectedHelper.js.map +1 -0
  12. package/dist/buildMethodShortcuts.js +50 -0
  13. package/dist/buildMethodShortcuts.js.map +1 -0
  14. package/dist/buildObjectTypes.js +250 -0
  15. package/dist/buildObjectTypes.js.map +1 -0
  16. package/dist/buildPaginateHelper.js +30 -0
  17. package/dist/buildPaginateHelper.js.map +1 -0
  18. package/dist/buildParameterTypes.js +287 -0
  19. package/dist/buildParameterTypes.js.map +1 -0
  20. package/dist/buildPollingHelper.js +21 -0
  21. package/dist/buildPollingHelper.js.map +1 -0
  22. package/dist/buildResponseTypes.js +122 -0
  23. package/dist/buildResponseTypes.js.map +1 -0
  24. package/dist/buildSchemaType.js +44 -0
  25. package/dist/buildSchemaType.js.map +1 -0
  26. package/dist/buildTopLevelIndexFile.js +45 -0
  27. package/dist/buildTopLevelIndexFile.js.map +1 -0
  28. package/dist/helpers/nameConstructors.js +41 -0
  29. package/dist/helpers/nameConstructors.js.map +1 -0
  30. package/dist/helpers/nameUtils.js +196 -0
  31. package/dist/helpers/nameUtils.js.map +1 -0
  32. package/dist/helpers/operationHelpers.js +83 -0
  33. package/dist/helpers/operationHelpers.js.map +1 -0
  34. package/dist/helpers/schemaHelpers.js +27 -0
  35. package/dist/helpers/schemaHelpers.js.map +1 -0
  36. package/dist/helpers/shortcutMethods.js +46 -0
  37. package/dist/helpers/shortcutMethods.js.map +1 -0
  38. package/dist/index.js +44 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/interfaces.js +18 -0
  41. package/dist/interfaces.js.map +1 -0
  42. package/dist/metadata/buildApiExtractorConfig.js +56 -0
  43. package/dist/metadata/buildApiExtractorConfig.js.map +1 -0
  44. package/dist/metadata/buildESLintConfig.js +33 -0
  45. package/dist/metadata/buildESLintConfig.js.map +1 -0
  46. package/dist/metadata/buildLicenseFile.js +41 -0
  47. package/dist/metadata/buildLicenseFile.js.map +1 -0
  48. package/dist/metadata/buildPackageFile.js +232 -0
  49. package/dist/metadata/buildPackageFile.js.map +1 -0
  50. package/dist/metadata/buildReadmeFile.js +170 -0
  51. package/dist/metadata/buildReadmeFile.js.map +1 -0
  52. package/dist/metadata/buildRollupConfig.js +144 -0
  53. package/dist/metadata/buildRollupConfig.js.map +1 -0
  54. package/dist/metadata/buildTsConfig.js +72 -0
  55. package/dist/metadata/buildTsConfig.js.map +1 -0
  56. package/dist/package.json +1 -0
  57. package/dist/static/paginateContent.js +214 -0
  58. package/dist/static/paginateContent.js.map +1 -0
  59. package/dist/static/pollingContent.js +78 -0
  60. package/dist/static/pollingContent.js.map +1 -0
  61. package/dist/test/buildEnvFile.js +31 -0
  62. package/dist/test/buildEnvFile.js.map +1 -0
  63. package/dist/test/buildKarmaConfig.js +19 -0
  64. package/dist/test/buildKarmaConfig.js.map +1 -0
  65. package/dist/test/buildRecordedClient.js +22 -0
  66. package/dist/test/buildRecordedClient.js.map +1 -0
  67. package/dist/test/buildSampleTest.js +19 -0
  68. package/dist/test/buildSampleTest.js.map +1 -0
  69. package/dist/test/template.js +192 -0
  70. package/dist/test/template.js.map +1 -0
  71. package/dist-esm/buildClient.js +191 -0
  72. package/dist-esm/buildClient.js.map +1 -0
  73. package/dist-esm/buildClientDefinitions.js +155 -0
  74. package/dist-esm/buildClientDefinitions.js.map +1 -0
  75. package/dist-esm/buildIndexFile.js +145 -0
  76. package/dist-esm/buildIndexFile.js.map +1 -0
  77. package/dist-esm/buildIsUnexpectedHelper.js +215 -0
  78. package/dist-esm/buildIsUnexpectedHelper.js.map +1 -0
  79. package/dist-esm/buildMethodShortcuts.js +46 -0
  80. package/dist-esm/buildMethodShortcuts.js.map +1 -0
  81. package/dist-esm/buildObjectTypes.js +249 -0
  82. package/dist-esm/buildObjectTypes.js.map +1 -0
  83. package/dist-esm/buildPaginateHelper.js +26 -0
  84. package/dist-esm/buildPaginateHelper.js.map +1 -0
  85. package/dist-esm/buildParameterTypes.js +288 -0
  86. package/dist-esm/buildParameterTypes.js.map +1 -0
  87. package/dist-esm/buildPollingHelper.js +17 -0
  88. package/dist-esm/buildPollingHelper.js.map +1 -0
  89. package/dist-esm/buildResponseTypes.js +127 -0
  90. package/dist-esm/buildResponseTypes.js.map +1 -0
  91. package/dist-esm/buildSchemaType.js +39 -0
  92. package/dist-esm/buildSchemaType.js.map +1 -0
  93. package/dist-esm/buildTopLevelIndexFile.js +41 -0
  94. package/dist-esm/buildTopLevelIndexFile.js.map +1 -0
  95. package/dist-esm/helpers/nameConstructors.js +34 -0
  96. package/dist-esm/helpers/nameConstructors.js.map +1 -0
  97. package/dist-esm/helpers/nameUtils.js +187 -0
  98. package/dist-esm/helpers/nameUtils.js.map +1 -0
  99. package/dist-esm/helpers/operationHelpers.js +72 -0
  100. package/dist-esm/helpers/operationHelpers.js.map +1 -0
  101. package/dist-esm/helpers/schemaHelpers.js +21 -0
  102. package/dist-esm/helpers/schemaHelpers.js.map +1 -0
  103. package/dist-esm/helpers/shortcutMethods.js +42 -0
  104. package/dist-esm/helpers/shortcutMethods.js.map +1 -0
  105. package/dist-esm/index.js +28 -0
  106. package/dist-esm/index.js.map +1 -0
  107. package/dist-esm/interfaces.js +15 -0
  108. package/dist-esm/interfaces.js.map +1 -0
  109. package/dist-esm/metadata/buildApiExtractorConfig.js +51 -0
  110. package/dist-esm/metadata/buildApiExtractorConfig.js.map +1 -0
  111. package/dist-esm/metadata/buildESLintConfig.js +28 -0
  112. package/dist-esm/metadata/buildESLintConfig.js.map +1 -0
  113. package/dist-esm/metadata/buildLicenseFile.js +36 -0
  114. package/dist-esm/metadata/buildLicenseFile.js.map +1 -0
  115. package/dist-esm/metadata/buildPackageFile.js +234 -0
  116. package/dist-esm/metadata/buildPackageFile.js.map +1 -0
  117. package/dist-esm/metadata/buildReadmeFile.js +167 -0
  118. package/dist-esm/metadata/buildReadmeFile.js.map +1 -0
  119. package/dist-esm/metadata/buildRollupConfig.js +139 -0
  120. package/dist-esm/metadata/buildRollupConfig.js.map +1 -0
  121. package/dist-esm/metadata/buildTsConfig.js +67 -0
  122. package/dist-esm/metadata/buildTsConfig.js.map +1 -0
  123. package/dist-esm/package.json +1 -0
  124. package/dist-esm/static/paginateContent.js +211 -0
  125. package/dist-esm/static/paginateContent.js.map +1 -0
  126. package/dist-esm/static/pollingContent.js +75 -0
  127. package/dist-esm/static/pollingContent.js.map +1 -0
  128. package/dist-esm/test/buildEnvFile.js +24 -0
  129. package/dist-esm/test/buildEnvFile.js.map +1 -0
  130. package/dist-esm/test/buildKarmaConfig.js +14 -0
  131. package/dist-esm/test/buildKarmaConfig.js.map +1 -0
  132. package/dist-esm/test/buildRecordedClient.js +17 -0
  133. package/dist-esm/test/buildRecordedClient.js.map +1 -0
  134. package/dist-esm/test/buildSampleTest.js +14 -0
  135. package/dist-esm/test/buildSampleTest.js.map +1 -0
  136. package/dist-esm/test/template.js +189 -0
  137. package/dist-esm/test/template.js.map +1 -0
  138. package/package.json +40 -0
  139. package/publishPackage.js +11 -0
  140. package/rlc-common.build.log +2 -0
  141. package/src/buildClient.ts +251 -0
  142. package/src/buildClientDefinitions.ts +231 -0
  143. package/src/buildIndexFile.ts +172 -0
  144. package/src/buildIsUnexpectedHelper.ts +240 -0
  145. package/src/buildMethodShortcuts.ts +75 -0
  146. package/src/buildObjectTypes.ts +393 -0
  147. package/src/buildPaginateHelper.ts +33 -0
  148. package/src/buildParameterTypes.ts +435 -0
  149. package/src/buildPollingHelper.ts +18 -0
  150. package/src/buildResponseTypes.ts +169 -0
  151. package/src/buildSchemaType.ts +56 -0
  152. package/src/buildTopLevelIndexFile.ts +46 -0
  153. package/src/helpers/nameConstructors.ts +93 -0
  154. package/src/helpers/nameUtils.ts +227 -0
  155. package/src/helpers/operationHelpers.ts +103 -0
  156. package/src/helpers/schemaHelpers.ts +25 -0
  157. package/src/helpers/shortcutMethods.ts +60 -0
  158. package/src/index.ts +28 -0
  159. package/src/interfaces.ts +212 -0
  160. package/src/metadata/buildApiExtractorConfig.ts +59 -0
  161. package/src/metadata/buildESLintConfig.ts +34 -0
  162. package/src/metadata/buildLicenseFile.ts +39 -0
  163. package/src/metadata/buildPackageFile.ts +271 -0
  164. package/src/metadata/buildReadmeFile.ts +231 -0
  165. package/src/metadata/buildRollupConfig.ts +147 -0
  166. package/src/metadata/buildTsConfig.ts +79 -0
  167. package/src/static/paginateContent.ts +210 -0
  168. package/src/static/pollingContent.ts +74 -0
  169. package/src/test/buildEnvFile.ts +26 -0
  170. package/src/test/buildKarmaConfig.ts +15 -0
  171. package/src/test/buildRecordedClient.ts +18 -0
  172. package/src/test/buildSampleTest.ts +15 -0
  173. package/src/test/template.ts +192 -0
  174. package/tsconfig-cjs.json +9 -0
  175. package/tsconfig-common.json +13 -0
  176. package/tsconfig.json +13 -0
  177. package/types/buildClient.d.ts +2 -0
  178. package/types/buildClientDefinitions.d.ts +5 -0
  179. package/types/buildIndexFile.d.ts +5 -0
  180. package/types/buildIsUnexpectedHelper.d.ts +5 -0
  181. package/types/buildMethodShortcuts.d.ts +4 -0
  182. package/types/buildObjectTypes.d.ts +14 -0
  183. package/types/buildPaginateHelper.d.ts +5 -0
  184. package/types/buildParameterTypes.d.ts +5 -0
  185. package/types/buildPollingHelper.d.ts +5 -0
  186. package/types/buildResponseTypes.d.ts +5 -0
  187. package/types/buildSchemaType.d.ts +19 -0
  188. package/types/buildTopLevelIndexFile.d.ts +5 -0
  189. package/types/helpers/nameConstructors.d.ts +28 -0
  190. package/types/helpers/nameUtils.d.ts +25 -0
  191. package/types/helpers/operationHelpers.d.ts +9 -0
  192. package/types/helpers/schemaHelpers.d.ts +4 -0
  193. package/types/helpers/shortcutMethods.d.ts +3 -0
  194. package/types/index.d.ts +25 -0
  195. package/types/interfaces.d.ts +186 -0
  196. package/types/metadata/buildApiExtractorConfig.d.ts +5 -0
  197. package/types/metadata/buildESLintConfig.d.ts +5 -0
  198. package/types/metadata/buildLicenseFile.d.ts +5 -0
  199. package/types/metadata/buildPackageFile.d.ts +5 -0
  200. package/types/metadata/buildReadmeFile.d.ts +5 -0
  201. package/types/metadata/buildRollupConfig.d.ts +5 -0
  202. package/types/metadata/buildTsConfig.d.ts +5 -0
  203. package/types/static/paginateContent.d.ts +1 -0
  204. package/types/static/pollingContent.d.ts +1 -0
  205. package/types/test/buildEnvFile.d.ts +9 -0
  206. package/types/test/buildKarmaConfig.d.ts +5 -0
  207. package/types/test/buildRecordedClient.d.ts +5 -0
  208. package/types/test/buildSampleTest.d.ts +5 -0
  209. package/types/test/template.d.ts +5 -0
@@ -0,0 +1,393 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ import {
5
+ InterfaceDeclarationStructure,
6
+ PropertySignatureStructure,
7
+ StructureKind,
8
+ TypeAliasDeclarationStructure
9
+ } from "ts-morph";
10
+ import { NameType, normalizeName } from "./helpers/nameUtils.js";
11
+ import { isDictionarySchema, isObjectSchema } from "./helpers/schemaHelpers.js";
12
+ import {
13
+ ObjectSchema,
14
+ Parameter,
15
+ Property,
16
+ RLCModel,
17
+ SchemaContext
18
+ } from "./interfaces.js";
19
+
20
+ /**
21
+ * Generates interfaces for ObjectSchemas
22
+ */
23
+ export function buildObjectInterfaces(
24
+ model: RLCModel,
25
+ importedModels: Set<string>,
26
+ schemaUsage: SchemaContext[]
27
+ ): InterfaceDeclarationStructure[] {
28
+ const objectSchemas: ObjectSchema[] = (model.schemas ?? []).filter(
29
+ (o) =>
30
+ isObjectSchema(o) &&
31
+ (o as ObjectSchema).usage?.some((u) => schemaUsage.includes(u))
32
+ );
33
+ const objectInterfaces: InterfaceDeclarationStructure[] = [];
34
+
35
+ for (const objectSchema of objectSchemas) {
36
+ const baseName = getObjectBaseName(objectSchema, schemaUsage);
37
+ const interfaceDeclaration = getObjectInterfaceDeclaration(
38
+ baseName,
39
+ objectSchema,
40
+ schemaUsage,
41
+ importedModels
42
+ );
43
+
44
+ objectInterfaces.push(interfaceDeclaration);
45
+ }
46
+ return objectInterfaces;
47
+ }
48
+
49
+ export function buildPolymorphicAliases(
50
+ model: RLCModel,
51
+ schemaUsage: SchemaContext[]
52
+ ) {
53
+ // We'll add aliases for polymorphic objects
54
+ const objectAliases: TypeAliasDeclarationStructure[] = [];
55
+ const objectSchemas: ObjectSchema[] = (model.schemas ?? []).filter(
56
+ (o) =>
57
+ isObjectSchema(o) &&
58
+ (o as ObjectSchema).usage?.some((u) => schemaUsage.includes(u))
59
+ );
60
+ for (const objectSchema of objectSchemas) {
61
+ const baseName = getObjectBaseName(objectSchema, schemaUsage);
62
+ const typeAlias = getPolymorphicTypeAlias(
63
+ baseName,
64
+ objectSchema,
65
+ schemaUsage
66
+ );
67
+ if (typeAlias) {
68
+ objectAliases.push(typeAlias);
69
+ }
70
+ }
71
+
72
+ return objectAliases;
73
+ }
74
+
75
+ /**
76
+ * Gets a base name for an object schema this is tipically used with suffixes when building interface or type names
77
+ */
78
+ function getObjectBaseName(
79
+ objectSchema: ObjectSchema,
80
+ schemaUsage: SchemaContext[]
81
+ ) {
82
+ const nameSuffix = schemaUsage.includes(SchemaContext.Output) ? "Output" : "";
83
+ const name = normalizeName(
84
+ objectSchema.name,
85
+ NameType.Interface,
86
+ true /** guard name */
87
+ );
88
+
89
+ return `${name}${nameSuffix}`;
90
+ }
91
+
92
+ /**
93
+ * If the current object is a Polymorphic parent, we need to create
94
+ * a type alias with the union of its children to enable polymorphism
95
+ */
96
+ function getPolymorphicTypeAlias(
97
+ baseName: string,
98
+ objectSchema: ObjectSchema,
99
+ schemaUsage: SchemaContext[]
100
+ ): TypeAliasDeclarationStructure | undefined {
101
+ if (!isPolymorphicParent(objectSchema)) {
102
+ return undefined;
103
+ }
104
+
105
+ const unionTypes: string[] = [];
106
+
107
+ // If the object itself has a discriminatorValue add its base to the union
108
+ if (objectSchema.discriminatorValue) {
109
+ unionTypes.push(`${baseName}Parent`);
110
+ }
111
+
112
+ for (const child of objectSchema.children?.all ?? []) {
113
+ const nameSuffix = schemaUsage.includes(SchemaContext.Output)
114
+ ? "Output"
115
+ : "";
116
+ const name = normalizeName(
117
+ child.name,
118
+ NameType.Interface,
119
+ true /** shouldGuard */
120
+ );
121
+
122
+ unionTypes.push(`${name}${nameSuffix}`);
123
+ }
124
+
125
+ const description = objectSchema.description;
126
+
127
+ return {
128
+ kind: StructureKind.TypeAlias,
129
+ ...(description && { docs: [{ description }] }),
130
+ name: `${baseName}`,
131
+ type: unionTypes.join(" | "),
132
+ isExported: true
133
+ };
134
+ }
135
+
136
+ /**
137
+ * Builds the interface for the current object schema. If it is a polymorphic
138
+ * root node it will suffix it with Base.
139
+ */
140
+ function getObjectInterfaceDeclaration(
141
+ baseName: string,
142
+ objectSchema: ObjectSchema,
143
+ schemaUsage: SchemaContext[],
144
+ importedModels: Set<string>
145
+ ): InterfaceDeclarationStructure {
146
+ let interfaceName = `${baseName}`;
147
+ if (isPolymorphicParent(objectSchema)) {
148
+ interfaceName = `${baseName}Parent`;
149
+ }
150
+
151
+ const properties = objectSchema.properties ?? {};
152
+
153
+ let propertySignatures = getPropertySignatures(
154
+ properties,
155
+ schemaUsage,
156
+ importedModels
157
+ );
158
+
159
+ // Add the polymorphic property if exists
160
+ propertySignatures = addDiscriminatorProperty(
161
+ objectSchema,
162
+ propertySignatures
163
+ );
164
+
165
+ // Calculate the parents of the current object
166
+ const extendFrom = getImmediateParentsNames(objectSchema, schemaUsage);
167
+
168
+ const description = objectSchema.description;
169
+ return {
170
+ kind: StructureKind.Interface,
171
+ ...(description && { docs: [{ description }] }),
172
+ name: interfaceName,
173
+ isExported: true,
174
+ properties: propertySignatures,
175
+ ...(extendFrom && { extends: extendFrom })
176
+ };
177
+ }
178
+
179
+ function isPolymorphicParent(objectSchema: ObjectSchema) {
180
+ return objectSchema.isPolyParent ? true : false;
181
+ }
182
+
183
+ function addDiscriminatorProperty(
184
+ objectSchema: ObjectSchema,
185
+ properties: PropertySignatureStructure[]
186
+ ): PropertySignatureStructure[] {
187
+ const polymorphicProperty = getDiscriminatorProperty(objectSchema);
188
+
189
+ if (polymorphicProperty) {
190
+ // It is possible that the polymorphic property needs to override an existing property.
191
+ // This is usually the case on the top level parent where the property already has a type of string
192
+ // we need to replace it with the polymorphic values of its children
193
+ const filteredProperties = properties.filter(
194
+ (p) => p.name !== polymorphicProperty.name
195
+ );
196
+ return [...filteredProperties, polymorphicProperty];
197
+ }
198
+
199
+ return properties;
200
+ }
201
+
202
+ /**
203
+ * Finds the name of the property used as discriminator and the discriminator value.
204
+ */
205
+ function getDiscriminatorProperty(
206
+ objectSchema: ObjectSchema
207
+ ): PropertySignatureStructure | undefined {
208
+ const discriminatorValue = objectSchema.discriminatorValue;
209
+ if (!discriminatorValue && !objectSchema.discriminator) {
210
+ return undefined;
211
+ }
212
+
213
+ const discriminators = getDiscriminatorValue(objectSchema);
214
+ const discriminatorPropertyName = getDiscriminatorPropertyName(objectSchema);
215
+
216
+ if (discriminators) {
217
+ if (discriminatorPropertyName === undefined) {
218
+ throw new Error(
219
+ `getDiscriminatorProperty: Expected object ${objectSchema.name} to have a discriminator in its hierarchy but found none`
220
+ );
221
+ }
222
+
223
+ return {
224
+ kind: StructureKind.PropertySignature,
225
+ name: `"${discriminatorPropertyName}"`,
226
+ type: discriminators
227
+ };
228
+ }
229
+
230
+ return undefined;
231
+ }
232
+
233
+ /**
234
+ * Finds the closest discriminator property
235
+ */
236
+ function getDiscriminatorPropertyName(objectSchema: ObjectSchema) {
237
+ if (objectSchema.discriminator !== undefined) {
238
+ return objectSchema.discriminator.name;
239
+ }
240
+
241
+ const allParents = objectSchema.parents?.all ?? [];
242
+
243
+ for (const parent of allParents) {
244
+ if (isObjectSchema(parent) && parent.discriminator) {
245
+ return parent.discriminator.name;
246
+ }
247
+ }
248
+ return undefined;
249
+ }
250
+
251
+ /**
252
+ * Calculates the discriminator values that a given object needs
253
+ */
254
+ function getDiscriminatorValue(objectSchema: ObjectSchema): string | undefined {
255
+ const discriminatorValue = objectSchema.discriminatorValue
256
+ ? objectSchema.discriminatorValue
257
+ : objectSchema.discriminator
258
+ ? objectSchema.name
259
+ : undefined;
260
+ const children = objectSchema.children?.immediate ?? [];
261
+
262
+ // If the current object has a discriminatorValue but doesn't have any children
263
+ // it is a leaf node and the only discriminator value needed is itself
264
+ if (discriminatorValue && !children.length) {
265
+ return `"${discriminatorValue}"`;
266
+ }
267
+
268
+ // when the current object has both discriminator and discriminatorValue
269
+ if (children) {
270
+ const discriminatorProperty = objectSchema.discriminator;
271
+ // Even when there are children, if no discriminatorProperty is present this is a leaf in the polymorphism tree
272
+ if (!discriminatorProperty) {
273
+ return `"${discriminatorValue}"`;
274
+ }
275
+
276
+ // the current object has discriminated children we need to find all the discriminatorValues for each of its children
277
+ const allChildren = objectSchema.children?.all ?? [];
278
+
279
+ // Top level parents may not have a discriminator of their own.
280
+ const selfDiscriminator = discriminatorValue
281
+ ? [`"${discriminatorValue}"`]
282
+ : [];
283
+
284
+ const childValues = getChildDiscriminatorValues(allChildren).map(
285
+ (v) => `"${v}"`
286
+ );
287
+
288
+ return [...selfDiscriminator, ...childValues].join(" | ");
289
+ }
290
+
291
+ return undefined;
292
+ }
293
+
294
+ /**
295
+ * Looks into the children and grabs all possible discriminatorValues
296
+ */
297
+ function getChildDiscriminatorValues(children: ObjectSchema[]): string[] {
298
+ const discriminatorValues = new Set<string>();
299
+ for (const child of children) {
300
+ if (isObjectSchema(child) && child.discriminatorValue) {
301
+ discriminatorValues.add(child.discriminatorValue);
302
+ }
303
+ }
304
+
305
+ return [...discriminatorValues];
306
+ }
307
+
308
+ /**
309
+ * Gets a list of types a given object may extend from
310
+ */
311
+ function getImmediateParentsNames(
312
+ objectSchema: ObjectSchema,
313
+ schemaUsage: SchemaContext[]
314
+ ): string[] {
315
+ if (!objectSchema.parents?.immediate) {
316
+ return [];
317
+ }
318
+
319
+ let extendFrom: string[] = [];
320
+
321
+ // If an immediate parent is a DictionarySchema, that means that the object has been marked
322
+ // with additional properties. We need to add Record<string, unknown> to the extend list and
323
+ if (objectSchema.parents.immediate.find(isDictionarySchema)) {
324
+ extendFrom.push("Record<string, unknown>");
325
+ }
326
+
327
+ // Get the rest of the parents excluding any DictionarySchemas
328
+ const parents = objectSchema.parents.immediate
329
+ .filter((p) => !isDictionarySchema(p))
330
+ .map((parent) => {
331
+ const nameSuffix = schemaUsage.includes(SchemaContext.Output)
332
+ ? "Output"
333
+ : "";
334
+ const name = `${normalizeName(
335
+ parent.name,
336
+ NameType.Interface,
337
+ true /** shouldGuard */
338
+ )}${nameSuffix}`;
339
+
340
+ return isObjectSchema(parent) && isPolymorphicParent(parent)
341
+ ? `${name}Parent`
342
+ : name;
343
+ });
344
+
345
+ return [...parents, ...extendFrom];
346
+ }
347
+
348
+ function getPropertySignatures(
349
+ properties: { [key: string]: Property },
350
+ schemaUsage: SchemaContext[],
351
+ importedModels: Set<string>
352
+ ) {
353
+ return Object.keys(properties).map((p) =>
354
+ getPropertySignature(
355
+ { ...properties[p], name: p },
356
+ schemaUsage,
357
+ importedModels
358
+ )
359
+ );
360
+ }
361
+
362
+ /**
363
+ * Builds a Typescript property or parameter signature
364
+ * @param property - Property or parameter to get the Typescript signature for
365
+ * @param importedModels - Set to track the models that need to be imported
366
+ * @returns a PropertySignatureStructure for the property.
367
+ */
368
+ export function getPropertySignature(
369
+ property: Property | Parameter,
370
+ schemaUsage: SchemaContext[],
371
+ importedModels = new Set<string>()
372
+ ): PropertySignatureStructure {
373
+ const propertyName = property.name;
374
+
375
+ const description = property.description;
376
+ const type =
377
+ ((schemaUsage.includes(SchemaContext.Output) &&
378
+ property.usage?.includes(SchemaContext.Output)) ||
379
+ (schemaUsage.includes(SchemaContext.Exception) &&
380
+ property.usage?.includes(SchemaContext.Exception))) &&
381
+ property.outputTypeName
382
+ ? property.outputTypeName
383
+ : property.typeName
384
+ ? property.typeName
385
+ : property.type;
386
+ return {
387
+ name: propertyName,
388
+ ...(description && { docs: [{ description }] }),
389
+ hasQuestionToken: !property.required,
390
+ type,
391
+ kind: StructureKind.PropertySignature
392
+ };
393
+ }
@@ -0,0 +1,33 @@
1
+ import { RLCModel } from "./interfaces.js";
2
+ import * as path from "path";
3
+ // @ts-ignore: to fix the handlebars issue
4
+ import hbs from "handlebars";
5
+ import { paginateContent } from "./static/paginateContent.js";
6
+
7
+ export function buildPaginateHelper(model: RLCModel) {
8
+ const pagingInfo = model.annotations;
9
+ // return directly if no paging info
10
+ if (!pagingInfo || pagingInfo.hasPaging !== true || !pagingInfo.pageDetails) {
11
+ return;
12
+ }
13
+
14
+ hbs.registerHelper(
15
+ "quoteWrap",
16
+ function (value: string | number | boolean | string[]) {
17
+ if (Array.isArray(value)) {
18
+ return value.map((element) => `"${element}"`).join();
19
+ }
20
+
21
+ return `"${value}"`;
22
+ }
23
+ );
24
+
25
+ const { srcPath } = model;
26
+ const paginateHelperContents = hbs.compile(paginateContent, {
27
+ noEscape: true
28
+ });
29
+ return {
30
+ path: path.join(srcPath, "paginateHelper.ts"),
31
+ content: paginateHelperContents(pagingInfo.pageDetails)
32
+ };
33
+ }