Files
Umbraco-CMS/templates/UmbracoExtension/Client/scripts/generate-openapi.js
Warren Buckley 4852e49041 Update dotnet new umbraco-extension template with newer @hey-api/openapi-ts (#19825)
* Updated hey-api as the client-fetch is bundled as part of @hey-api/openapi-ts in newer versions

* Regenerated a new package-lock.json file

* Fix typescript issue

* Update templates/UmbracoExtension/Client/package.json

Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>

* Updated client dependencies

* Vite, TypeScript, hey-api
* Chalk & Cross-Env for the generate-client script

* Explicitly remove package-lock.json as it will be out of sync due to UMBRACO_VERSION_FROM_TEMPLATE

* Regenerated Hey API client that now ships client rather than dependancy

* Vite and Hey-API were already out of date (updated to the very latest)

---------

Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>
2025-08-20 08:04:17 +02:00

54 lines
2.2 KiB
JavaScript

import fetch from 'node-fetch';
import chalk from 'chalk';
import { createClient, defaultPlugins } from '@hey-api/openapi-ts';
// Start notifying user we are generating the TypeScript client
console.log(chalk.green("Generating OpenAPI client..."));
const swaggerUrl = process.argv[2];
if (swaggerUrl === undefined) {
console.error(chalk.red(`ERROR: Missing URL to OpenAPI spec`));
console.error(`Please provide the URL to the OpenAPI spec as the first argument found in ${chalk.yellow('package.json')}`);
console.error(`Example: node generate-openapi.js ${chalk.yellow('https://localhost:44331/umbraco/swagger/REPLACE_ME/swagger.json')}`);
process.exit();
}
// Needed to ignore self-signed certificates from running Umbraco on https on localhost
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
// Start checking to see if we can connect to the OpenAPI spec
console.log("Ensure your Umbraco instance is running");
console.log(`Fetching OpenAPI definition from ${chalk.yellow(swaggerUrl)}`);
fetch(swaggerUrl).then(async (response) => {
if (!response.ok) {
console.error(chalk.red(`ERROR: OpenAPI spec returned with a non OK (200) response: ${response.status} ${response.statusText}`));
console.error(`The URL to your Umbraco instance may be wrong or the instance is not running`);
console.error(`Please verify or change the URL in the ${chalk.yellow('package.json')} for the script ${chalk.yellow('generate-openapi')}`);
return;
}
console.log(`OpenAPI spec fetched successfully`);
console.log(`Calling ${chalk.yellow('hey-api')} to generate TypeScript client`);
await createClient({
client: '@hey-api/client-fetch',
input: swaggerUrl,
output: 'src/api',
plugins: [
...defaultPlugins,
{
name: '@hey-api/sdk',
asClass: true,
classNameBuilder: '{{name}}Service',
}
],
});
})
.catch(error => {
console.error(`ERROR: Failed to connect to the OpenAPI spec: ${chalk.red(error.message)}`);
console.error(`The URL to your Umbraco instance may be wrong or the instance is not running`);
console.error(`Please verify or change the URL in the ${chalk.yellow('package.json')} for the script ${chalk.yellow('generate-openapi')}`);
});