@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,231 @@
1
+ import { RLCModel } from "../interfaces.js";
2
+ // @ts-ignore: to fix the handlebars issue
3
+ import hbs from "handlebars";
4
+ import { NameType, normalizeName } from "../helpers/nameUtils.js";
5
+
6
+ const readmeTemplate = `# {{ clientDescriptiveName }} library for JavaScript
7
+
8
+ {{ description }}
9
+
10
+ {{#if azureArm}}
11
+ **If you are not familiar with our REST client, please spend 5 minutes to take a look at {{#if serviceDocURL}}[the service's documentation]({{ serviceDocURL }}) and {{/if}}our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library, the REST client provides a light-weighted & developer friendly way to call azure rest api
12
+ {{else}}
13
+ **Please rely heavily on {{#if serviceDocURL}}[the service's documentation]({{ serviceDocURL }}) and {{/if}}our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library**
14
+ {{/if}}
15
+
16
+ Key links:
17
+
18
+ {{#if packageSourceURL}}
19
+ - [Source code]({{ packageSourceURL }})
20
+ {{/if}}
21
+ {{#if packageNPMURL}}
22
+ - [Package (NPM)]({{ packageNPMURL }})
23
+ {{/if}}
24
+ {{#if apiRefURL}}
25
+ - [API reference documentation]({{ apiRefURL }})
26
+ {{/if}}
27
+ {{#if serviceDocURL}}
28
+ - [Product documentation]({{ serviceDocURL }})
29
+ {{/if}}
30
+ {{#if samplesURL}}
31
+ - [Samples]({{ samplesURL }})
32
+ {{/if}}
33
+
34
+ ## Getting started
35
+
36
+ ### Currently supported environments
37
+
38
+ - Node.js version 14.x.x or higher
39
+
40
+ ### Prerequisites
41
+
42
+ - You must have an [Azure subscription](https://azure.microsoft.com/free/){{#if dependencyLink}} and follow [these]({{ dependencyLink }}) instructions{{/if}} to use this package.
43
+
44
+ ### Install the \`{{ clientPackageName }}\` package
45
+
46
+ Install the {{ clientDescriptiveName }} REST client library for JavaScript with \`npm\`:
47
+
48
+ \`\`\`bash
49
+ npm install {{ clientPackageName }}
50
+ \`\`\`
51
+
52
+ ### Create and authenticate a \`{{ clientClassName }}\`
53
+
54
+ To use an [Azure Active Directory (AAD) token credential](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token),
55
+ provide an instance of the desired credential type obtained from the
56
+ [@azure/identity](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) library.
57
+
58
+ To authenticate with AAD, you must first \`npm\` install [\`@azure/identity\`](https://www.npmjs.com/package/@azure/identity) {{#if dependencyLink}}and
59
+ [{{dependencyDescription }}]({{ dependencyLink }}){{/if}}
60
+
61
+ After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from \`@azure/identity\` to use.
62
+ As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential)
63
+ can be used to authenticate the client.
64
+
65
+ Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
66
+ AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
67
+
68
+ ## Troubleshooting
69
+
70
+ ### Logging
71
+
72
+ Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the \`AZURE_LOG_LEVEL\` environment variable to \`info\`. Alternatively, logging can be enabled at runtime by calling \`setLogLevel\` in the \`@azure/logger\`:
73
+
74
+ \`\`\`javascript
75
+ import { setLogLevel } from "@azure/logger";
76
+
77
+ setLogLevel("info");
78
+ \`\`\`
79
+
80
+ For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
81
+ `;
82
+
83
+ /**
84
+ * Meta data information about the service, the package, and the client.
85
+ */
86
+ interface Metadata {
87
+ /** The name of the service */
88
+ serviceName: string;
89
+ /** The name of the package */
90
+ clientPackageName: string;
91
+ /** The name of the client class */
92
+ clientClassName: string;
93
+ /** The URL of the repository the package lives in */
94
+ repoURL?: string;
95
+ /** The URL to the package directory in the repository */
96
+ packageSourceURL?: string;
97
+ /** The URL to the package's samples */
98
+ samplesURL?: string;
99
+ /** A descriptive name for the client extracted from the swagger */
100
+ clientDescriptiveName?: string;
101
+ /** A description for the service extracted from the swagger */
102
+ description?: string;
103
+ /** The URL to the package on npmjs.org */
104
+ packageNPMURL?: string;
105
+ /** The name of the project that lives in the repository */
106
+ projectName?: string;
107
+ /** whether the client accepts standard credentials */
108
+ addCredentials?: boolean;
109
+ /** The link to the identity package in the repository */
110
+ identityPackageURL?: string;
111
+ /** The URL for the service document */
112
+ serviceDocURL?: string;
113
+ /** The dependency info for this service */
114
+ dependencyDescription?: string;
115
+ dependencyLink?: string;
116
+ /** Indicates if the package is a multi-client */
117
+ hasMultiClients?: boolean;
118
+ /** The URL to the API reference */
119
+ apiRefURL?: string;
120
+ /** Check if the rp is management plane */
121
+ azureArm?: boolean;
122
+ }
123
+
124
+ export function buildReadmeFile(model: RLCModel) {
125
+ const generateMetadata = Boolean(model.options?.generateMetadata);
126
+ if (!generateMetadata) {
127
+ return;
128
+ }
129
+
130
+ const metadata = createMetadata(model) ?? {};
131
+ const readmeFileContents = hbs.compile(readmeTemplate, { noEscape: true });
132
+ return {
133
+ path: "README.md",
134
+ content: readmeFileContents(metadata)
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Returns meta data information about the service, the package, and the client.
140
+ * @param codeModel - include the client details
141
+ * @returns inferred metadata about the service, the package, and the client
142
+ */
143
+ function createMetadata(model: RLCModel): Metadata | undefined {
144
+ if (!model.options || !model.options.packageDetails) {
145
+ return;
146
+ }
147
+ // const packageDetails = model.options.packageDetails;
148
+ const {
149
+ packageDetails,
150
+ azureOutputDirectory,
151
+ productDocLink,
152
+ dependencyInfo,
153
+ multiClient,
154
+ batch,
155
+ serviceInfo
156
+ } = model.options;
157
+
158
+ const azureHuh =
159
+ packageDetails?.scopeName === "azure" ||
160
+ packageDetails?.scopeName === "azure-rest";
161
+ const repoURL = "https://github.com/Azure/azure-sdk-for-js";
162
+ const relativePackageSourcePath = azureOutputDirectory;
163
+ const packageSourceURL =
164
+ relativePackageSourcePath &&
165
+ repoURL &&
166
+ `${repoURL}/tree/main/${relativePackageSourcePath}`;
167
+
168
+ const clientPackageName = packageDetails?.name;
169
+ const clientClassName = getClientName(model);
170
+ const serviceName = getServiceName(model);
171
+ var apiRefUrlQueryParameter: string = "";
172
+ if (packageDetails?.version.includes("beta")) {
173
+ apiRefUrlQueryParameter = "?view=azure-node-preview";
174
+ }
175
+
176
+ return {
177
+ serviceName,
178
+ clientClassName,
179
+ clientPackageName: clientPackageName,
180
+ clientDescriptiveName: `${serviceName} REST client`,
181
+ description: serviceInfo?.description ?? packageDetails.description,
182
+ serviceDocURL: productDocLink,
183
+ packageSourceURL: packageSourceURL,
184
+ packageNPMURL: `https://www.npmjs.com/package/${clientPackageName}`,
185
+ samplesURL: packageSourceURL && `${packageSourceURL}/samples`,
186
+ apiRefURL: azureHuh
187
+ ? `https://docs.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
188
+ : undefined,
189
+ dependencyDescription: dependencyInfo?.description,
190
+ dependencyLink: dependencyInfo?.link,
191
+ hasMultiClients: multiClient && batch && batch.length > 1,
192
+ azureArm: Boolean(model.options.azureArm)
193
+ };
194
+ }
195
+
196
+ function getServiceName(model: RLCModel) {
197
+ const azureHuh =
198
+ model?.options?.packageDetails?.scopeName === "azure" ||
199
+ model?.options?.packageDetails?.scopeName === "azure-rest";
200
+ const libraryName = model.libraryName;
201
+ const serviceTitle = model.options?.serviceInfo?.title ?? model.libraryName;
202
+ const batch = model?.options?.batch,
203
+ packageDetails = model?.options?.packageDetails!;
204
+ let simpleServiceName =
205
+ batch && batch.length > 1
206
+ ? normalizeName(packageDetails.nameWithoutScope || "", NameType.Class)
207
+ : normalizeName(serviceTitle, NameType.Class);
208
+ simpleServiceName =
209
+ /**
210
+ * It is a required convention in Azure swaggers for their titles to end with
211
+ * "Client".
212
+ */
213
+ serviceTitle.match(/(.*) Client/)?.[1] ??
214
+ serviceTitle.match(/(.*)Client/)?.[1] ??
215
+ libraryName.match(/(.*)Client/)?.[1] ??
216
+ serviceTitle.match(/(.*) Service/)?.[1] ??
217
+ simpleServiceName;
218
+
219
+ return azureHuh
220
+ ? simpleServiceName.startsWith("Azure")
221
+ ? simpleServiceName
222
+ : `Azure ${simpleServiceName}`
223
+ : simpleServiceName;
224
+ }
225
+
226
+ function getClientName(model: RLCModel) {
227
+ const clientName = model.libraryName;
228
+ return clientName.endsWith("Client")
229
+ ? `${clientName}`
230
+ : `${clientName}Client`;
231
+ }
@@ -0,0 +1,147 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ import { Project } from "ts-morph";
5
+ import { RLCModel } from "../interfaces.js";
6
+
7
+ export function buildRollupConfig(model: RLCModel) {
8
+ const generateMetadata = Boolean(model.options?.generateMetadata),
9
+ azureSdkForJs = Boolean(model.options?.azureSdkForJs);
10
+ // when it's generating rlc codes, only generate rollup config in codegen test
11
+ if (!generateMetadata || azureSdkForJs) {
12
+ return;
13
+ }
14
+
15
+ const project = new Project();
16
+ const filePath = "rollup.config.js";
17
+ const rollupFile = project.createSourceFile(filePath, undefined, {
18
+ overwrite: true
19
+ });
20
+
21
+ rollupFile.addStatements(
22
+ `import nodeResolve from "@rollup/plugin-node-resolve";
23
+ import cjs from "@rollup/plugin-commonjs";
24
+ import sourcemaps from "rollup-plugin-sourcemaps";
25
+ import multiEntry from "@rollup/plugin-multi-entry";
26
+ import json from "@rollup/plugin-json";
27
+
28
+ import nodeBuiltins from "builtin-modules";
29
+
30
+ // #region Warning Handler
31
+
32
+ /**
33
+ * A function that can determine whether a rollup warning should be ignored. If
34
+ * the function returns \`true\`, then the warning will not be displayed.
35
+ */
36
+
37
+ function ignoreNiseSinonEval(warning) {
38
+ return (
39
+ warning.code === "EVAL" &&
40
+ (warning.id && ((warning.id.includes("node_modules/nise")) ||
41
+ warning.id.includes("node_modules/sinon")) === true)
42
+ );
43
+ }
44
+
45
+ function ignoreChaiCircularDependency(warning) {
46
+ return (
47
+ warning.code === "CIRCULAR_DEPENDENCY" &&
48
+ (warning.importer && warning.importer.includes("node_modules/chai") === true)
49
+ );
50
+ }
51
+
52
+ const warningInhibitors = [
53
+ ignoreChaiCircularDependency,
54
+ ignoreNiseSinonEval
55
+ ];
56
+
57
+ /**
58
+ * Construct a warning handler for the shared rollup configuration
59
+ * that ignores certain warnings that are not relevant to testing.
60
+ */
61
+ function makeOnWarnForTesting() {
62
+ return (warning, warn) => {
63
+ // If every inhibitor returns false (i.e. no inhibitors), then show the warning
64
+ if (warningInhibitors.every(inhib => !inhib(warning))) {
65
+ warn(warning);
66
+ }
67
+ };
68
+ }
69
+
70
+ // #endregion
71
+
72
+ function makeBrowserTestConfig() {
73
+ const config = {
74
+ input: {
75
+ include: ["dist-esm/test/**/*.spec.js"],
76
+ exclude: ["dist-esm/test/**/node/**"]
77
+ },
78
+ output: {
79
+ file: \`dist-test/index.browser.js\`,
80
+ format: "umd",
81
+ sourcemap: true
82
+ },
83
+ preserveSymlinks: false,
84
+ plugins: [
85
+ multiEntry({ exports: false }),
86
+ nodeResolve({
87
+ mainFields: ["module", "browser"]
88
+ }),
89
+ cjs(),
90
+ json(),
91
+ sourcemaps()
92
+ //viz({ filename: "dist-test/browser-stats.html", sourcemap: true })
93
+ ],
94
+ onwarn: makeOnWarnForTesting(),
95
+ // Disable tree-shaking of test code. In rollup-plugin-node-resolve@5.0.0,
96
+ // rollup started respecting the "sideEffects" field in package.json. Since
97
+ // our package.json sets "sideEffects=false", this also applies to test
98
+ // code, which causes all tests to be removed by tree-shaking.
99
+ treeshake: false
100
+ };
101
+
102
+ return config;
103
+ }
104
+
105
+ const defaultConfigurationOptions = {
106
+ disableBrowserBundle: false
107
+ };
108
+
109
+ export function makeConfig(
110
+ pkg,
111
+ options
112
+ ) {
113
+ options = {
114
+ ...defaultConfigurationOptions,
115
+ ...(options || {})
116
+ };
117
+
118
+ const baseConfig = {
119
+ // Use the package's module field if it has one
120
+ input: pkg["module"] || "dist-esm/src/index.js",
121
+ external: [
122
+ ...nodeBuiltins,
123
+ ...Object.keys(pkg.dependencies),
124
+ ...Object.keys(pkg.devDependencies)
125
+ ],
126
+ output: { file: "dist/index.js", format: "cjs", sourcemap: true },
127
+ preserveSymlinks: false,
128
+ plugins: [sourcemaps(), nodeResolve()]
129
+ };
130
+
131
+ const config = [baseConfig];
132
+
133
+ if (!options.disableBrowserBundle) {
134
+ config.push(makeBrowserTestConfig());
135
+ }
136
+
137
+ return config;
138
+ }
139
+
140
+ export default makeConfig(require("./package.json"));`
141
+ );
142
+
143
+ return {
144
+ path: filePath,
145
+ content: rollupFile.getFullText()
146
+ };
147
+ }
@@ -0,0 +1,79 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ import { Project } from "ts-morph";
5
+ import { RLCModel } from "../interfaces.js";
6
+
7
+ const restLevelTsConfigInAzureSdkForJs: Record<string, any> = {
8
+ extends: "../../../tsconfig.package",
9
+ compilerOptions: {
10
+ outDir: "./dist-esm",
11
+ declarationDir: "./types"
12
+ },
13
+ include: ["src/**/*.ts"]
14
+ };
15
+
16
+ const restLevelTsConfigNotInAzureSdkForJs: Record<string, any> = {
17
+ compilerOptions: {
18
+ target: "ES2017",
19
+ module: "es6",
20
+ lib: [],
21
+ declaration: true,
22
+ declarationMap: true,
23
+ inlineSources: true,
24
+ sourceMap: true,
25
+ importHelpers: true,
26
+ strict: true,
27
+ alwaysStrict: true,
28
+ noUnusedLocals: true,
29
+ noUnusedParameters: true,
30
+ noImplicitReturns: true,
31
+ noFallthroughCasesInSwitch: true,
32
+ forceConsistentCasingInFileNames: true,
33
+ moduleResolution: "node",
34
+ allowSyntheticDefaultImports: true,
35
+ esModuleInterop: true,
36
+ outDir: "./dist-esm",
37
+ declarationDir: "./types"
38
+ },
39
+ include: ["./src/**/*.ts"]
40
+ };
41
+
42
+ export function buildTsConfig(model: RLCModel, hasSamplesGenerated = false) {
43
+ const generateMetadata = Boolean(model.options?.generateMetadata);
44
+ if (!generateMetadata) {
45
+ return;
46
+ }
47
+ const { generateTest, packageDetails, generateSample, azureSdkForJs } =
48
+ model.options || {};
49
+ const clientPackageName = packageDetails?.name ?? "";
50
+ const project = new Project();
51
+
52
+ const restLevelTsConfig = azureSdkForJs
53
+ ? restLevelTsConfigInAzureSdkForJs
54
+ : restLevelTsConfigNotInAzureSdkForJs;
55
+
56
+ if (generateTest) {
57
+ restLevelTsConfig.include.push("./test/**/*.ts");
58
+ }
59
+ if (generateSample && hasSamplesGenerated) {
60
+ restLevelTsConfig.include.push("samples-dev/**/*.ts");
61
+ restLevelTsConfig.compilerOptions["paths"] = {};
62
+ restLevelTsConfig.compilerOptions["paths"][clientPackageName] = [
63
+ "./src/index"
64
+ ];
65
+ }
66
+
67
+ const filePath = "tsconfig.json";
68
+ const configFile = project.createSourceFile(
69
+ filePath,
70
+ JSON.stringify(restLevelTsConfig),
71
+ {
72
+ overwrite: true
73
+ }
74
+ );
75
+ return {
76
+ path: filePath,
77
+ content: configFile.getFullText()
78
+ };
79
+ }
@@ -0,0 +1,210 @@
1
+ export const paginateContent = `
2
+ import {
3
+ getPagedAsyncIterator,
4
+ PagedAsyncIterableIterator,
5
+ PagedResult
6
+ } from "@azure/core-paging";
7
+ import {
8
+ Client,
9
+ createRestError,
10
+ PathUncheckedResponse
11
+ } from "@azure-rest/core-client";
12
+
13
+ /**
14
+ * Helper type to extract the type of an array
15
+ */
16
+ export type GetArrayType<T> = T extends Array<infer TData> ? TData : never;
17
+
18
+ /**
19
+ * The type of a custom function that defines how to get a page and a link to the next one if any.
20
+ */
21
+ export type GetPage<TPage> = (
22
+ pageLink: string,
23
+ maxPageSize?: number
24
+ ) => Promise<{
25
+ page: TPage;
26
+ nextPageLink?: string;
27
+ }>;
28
+
29
+ /**
30
+ * Options for the paging helper
31
+ */
32
+ export interface PagingOptions<TResponse> {
33
+ /**
34
+ * Custom function to extract pagination details for crating the PagedAsyncIterableIterator
35
+ */
36
+ customGetPage?: GetPage<PaginateReturn<TResponse>[]>
37
+ }
38
+
39
+ /**
40
+ * Helper type to infer the Type of the paged elements from the response type
41
+ * This type is generated based on the swagger information for x-ms-pageable
42
+ * specifically on the itemName property which indicates the property of the response
43
+ * where the page items are found. The default value is \`value\`.
44
+ * This type will allow us to provide strongly typed Iterator based on the response we get as second parameter
45
+ */
46
+ export type PaginateReturn<TResult> = TResult extends {{#each itemNames}}
47
+ {
48
+
49
+ body: { {{this}}?: infer TPage }
50
+
51
+ } {{#if @last }}{{else}} | {{/if}}
52
+ {{/each}}
53
+ ? GetArrayType<TPage>
54
+ : Array<unknown>;
55
+
56
+ /**
57
+ * Helper to paginate results from an initial response that follows the specification of Autorest \`x-ms-pageable\` extension
58
+ * @param client - Client to use for sending the next page requests
59
+ * @param initialResponse - Initial response containing the nextLink and current page of elements
60
+ * @param customGetPage - Optional - Function to define how to extract the page and next link to be used to paginate the results
61
+ * @returns - PagedAsyncIterableIterator to iterate the elements
62
+ */
63
+ export function paginate<TResponse extends PathUncheckedResponse>(
64
+ client: Client,
65
+ initialResponse: TResponse,
66
+ options: PagingOptions<TResponse> = {}
67
+ ): PagedAsyncIterableIterator<PaginateReturn<TResponse>> {
68
+ // Extract element type from initial response
69
+ type TElement = PaginateReturn<TResponse>;
70
+ let firstRun = true;
71
+ {{#if isComplexPaging}}
72
+ // We need to check the response for success before trying to inspect it looking for
73
+ // the properties to use for nextLink and itemName
74
+ checkPagingRequest(initialResponse);
75
+ const { itemName, nextLinkName } = getPaginationProperties(initialResponse);
76
+ {{else}}
77
+ const itemName = {{ quoteWrap itemNames }};
78
+ const nextLinkName = {{quoteWrap nextLinkNames}};
79
+ {{/if}}
80
+ const { customGetPage } = options;
81
+ const pagedResult: PagedResult<TElement[]> = {
82
+ firstPageLink: "",
83
+ getPage:
84
+ typeof customGetPage === "function"
85
+ ? customGetPage
86
+ : async (pageLink: string) => {
87
+ const result = firstRun
88
+ ? initialResponse
89
+ : await client.pathUnchecked(pageLink).get();
90
+ firstRun = false;
91
+ checkPagingRequest(result);
92
+ const nextLink = getNextLink(result.body, nextLinkName);
93
+ const values = getElements<TElement>(result.body, itemName);
94
+ return {
95
+ page: values,
96
+ nextPageLink: nextLink
97
+ };
98
+ }
99
+ };
100
+
101
+ return getPagedAsyncIterator(pagedResult);
102
+ }
103
+
104
+ /**
105
+ * Gets for the value of nextLink in the body
106
+ */
107
+ function getNextLink(body: unknown, nextLinkName?: string): string | undefined {
108
+ if (!nextLinkName) {
109
+ return undefined;
110
+ }
111
+
112
+ const nextLink = (body as Record<string, unknown>)[nextLinkName];
113
+
114
+ if (typeof nextLink !== "string" && typeof nextLink !== "undefined") {
115
+ throw new Error(
116
+ \`Body Property \${nextLinkName} should be a string or undefined\`
117
+ );
118
+ }
119
+
120
+ return nextLink;
121
+ }
122
+
123
+ /**
124
+ * Gets the elements of the current request in the body.
125
+ */
126
+ function getElements<T = unknown>(body: unknown, itemName: string): T[] {
127
+ const value = (body as Record<string, unknown>)[itemName] as T[];
128
+
129
+ // value has to be an array according to the x-ms-pageable extension.
130
+ // The fact that this must be an array is used above to calculate the
131
+ // type of elements in the page in PaginateReturn
132
+ if (!Array.isArray(value)) {
133
+ throw new Error(
134
+ \`Couldn't paginate response\\n Body doesn't contain an array property with name: \${itemName}\`
135
+ );
136
+ }
137
+
138
+ return value ?? [];
139
+ }
140
+
141
+ /**
142
+ * Checks if a request failed
143
+ */
144
+ function checkPagingRequest(response: PathUncheckedResponse): void {
145
+ const Http2xxStatusCodes = [
146
+ "200",
147
+ "201",
148
+ "202",
149
+ "203",
150
+ "204",
151
+ "205",
152
+ "206",
153
+ "207",
154
+ "208",
155
+ "226"
156
+ ];
157
+ if (!Http2xxStatusCodes.includes(response.status)) {
158
+ throw createRestError(
159
+ \`Pagination failed with unexpected statusCode \${response.status\}\`,
160
+ response
161
+ );
162
+ }
163
+ }
164
+
165
+ {{#if isComplexPaging}}
166
+ /**
167
+ * Extracts the itemName and nextLinkName from the initial response to use them for pagination
168
+ */
169
+ function getPaginationProperties(initialResponse: PathUncheckedResponse) {
170
+ // Build a set with the passed custom nextLinkNames
171
+ const nextLinkNames = new Set([{{ quoteWrap nextLinkNames }}]);
172
+
173
+ // Build a set with the passed custom set of itemNames
174
+ const itemNames = new Set([{{ quoteWrap itemNames }}]);
175
+
176
+ let nextLinkName: string | undefined;
177
+ let itemName: string | undefined;
178
+
179
+ for (const name of nextLinkNames) {
180
+ const nextLink = (initialResponse.body as Record<string, unknown>)[
181
+ name
182
+ ] as string;
183
+ if (nextLink) {
184
+ nextLinkName = name;
185
+ break;
186
+ }
187
+ }
188
+
189
+ for (const name of itemNames) {
190
+ const item = (initialResponse.body as Record<string, unknown>)[
191
+ name
192
+ ] as string;
193
+ if (item) {
194
+ itemName = name;
195
+ break;
196
+ }
197
+ }
198
+
199
+ if (!itemName) {
200
+ throw new Error(
201
+ \`Couldn't paginate response\\n Body doesn't contain an array property with name: \${[
202
+ ...itemNames
203
+ ].join(" OR ")}\`
204
+ );
205
+ }
206
+
207
+ return { itemName, nextLinkName };
208
+ }
209
+ {{/if}}
210
+ `;