Update generated ARM clients to latest version (#807)

This commit is contained in:
Steve Faulkner
2021-05-20 20:34:29 -05:00
committed by GitHub
parent e6b3f01f16
commit f4b0ea7d69
73 changed files with 1228 additions and 347 deletions

View File

@@ -15,12 +15,14 @@ But it does work well enough to generate a fully typed tree-shakeable client for
Results of this file should be checked into the repo.
*/
// CHANGE THESE VALUES TO GENERATE NEW CLIENTS
const version = "2021-04-15";
const resourceName = "cosmosNotebooks";
const schemaURL = `https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/${version}/notebook.json`;
const outputDir = path.join(__dirname, `../../src/Utils/arm/generatedClients/${resourceName}/${version}`);
// Array of strings to use for eventual output
const outputTypes: string[] = [""];
const version = "2020-04-01";
const schemaURL = `https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/cosmos-db/resource-manager/Microsoft.DocumentDB/stable/${version}/cosmos-db.json`;
const outputDir = path.join(__dirname, `../../src/Utils/arm/generatedClients/${version}`);
mkdirp.sync(outputDir);
// Buckets for grouping operations based on their name
@@ -33,7 +35,7 @@ const clients: { [key: string]: Client } = {};
// Mapping for OpenAPI types to TypeScript types
const propertyMap: { [key: string]: string } = {
integer: "number"
integer: "number",
};
// Converts a Open API reference: "#/definitions/Foo" to a type name: Foo
@@ -84,6 +86,7 @@ function responseType(operation: Operation, namespace: string) {
return refToType(operation.responses[responseCode].schema.$ref, namespace);
})
.filter((value, index, array) => array.indexOf(value) === index)
.filter((value) => value !== "unknown")
.join(" | ");
}
return "unknown";
@@ -98,6 +101,7 @@ interface Property {
items?: {
$ref: string;
};
enum?: string[];
allOf?: {
$ref: string;
}[];
@@ -129,6 +133,13 @@ const propertyToType = (property: Property, prop: string, required: boolean) =>
/* ${property.description || "undocumented"} */
${property.readOnly ? "readonly " : ""}${prop}${required ? "" : "?"}: ${type}
`);
} else if (property.enum) {
outputTypes.push(`
/* ${property.description || "undocumented"} */
${property.readOnly ? "readonly " : ""}${prop}${required ? "" : "?"}: ${property.enum
.map((v: string) => `"${v}"`)
.join(" | ")}
`);
} else {
if (property.type === undefined) {
console.log(`UHANDLED TYPE: ${prop}. Falling back to unknown`);
@@ -162,9 +173,6 @@ async function main() {
} else {
outputTypes.push(`export interface ${definition} {`);
}
if (definition === "SqlDatabaseGetProperties") {
console.log(schema.definitions[definition]);
}
for (const prop in schema.definitions[definition].properties) {
const property = schema.definitions[definition].properties[prop];
propertyToType(property, prop, schema.definitions[definition].required?.includes(prop));
@@ -225,9 +233,9 @@ async function main() {
// Write all grouped fetch functions to objects
for (const clientName in clients) {
const outputClient: string[] = [""];
outputClient.push(`import { armRequest } from "../../request"\n`);
outputClient.push(`import { armRequest } from "../../../request"\n`);
outputClient.push(`import * as Types from "./types"\n`);
outputClient.push(`import { configContext } from "../../../../ConfigContext";\n`);
outputClient.push(`import { configContext } from "../../../../../ConfigContext";\n`);
outputClient.push(`const apiVersion = "${version}"\n\n`);
for (const path of clients[clientName].paths) {
for (const method in schema.paths[path]) {
@@ -240,7 +248,7 @@ async function main() {
/* ${operation.description || "undocumented"} */
export async function ${sanitize(camelize(methodName))} (
${parametersFromPath(path)
.map(p => `${p}: string`)
.map((p) => `${p}: string`)
.join(",\n")}
${bodyParam(bodyParameter, "Types")}
) : Promise<${responseType(operation, "Types")}> {
@@ -268,13 +276,15 @@ function sanitize(name: string) {
function writeOutputFile(outputPath: string, components: string[]) {
components.unshift(`/*
AUTOGENERATED FILE
Do not manually edit
Run "npm run generateARMClients" to regenerate
Edting this file directly should be done with extreme caution as not to diverge from ARM REST specs
Generated from: ${schemaURL}
*/\n\n`);
writeFileSync(path.join(outputDir, outputPath), components.join(""));
}
main().catch(e => {
main().catch((e) => {
console.error(e);
process.exit(1);
});