2024-11-08 06:55:43 +00:00
|
|
|
import fetch from 'node-fetch';
|
|
|
|
|
import chalk from 'chalk';
|
2025-03-11 17:14:47 +01:00
|
|
|
import { createClient, defaultPlugins } from '@hey-api/openapi-ts';
|
2024-11-08 06:55:43 +00:00
|
|
|
|
|
|
|
|
// 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)}`);
|
|
|
|
|
|
2025-03-11 17:14:47 +01:00
|
|
|
fetch(swaggerUrl).then(async (response) => {
|
2024-11-08 06:55:43 +00:00
|
|
|
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`);
|
|
|
|
|
|
2025-03-11 17:14:47 +01:00
|
|
|
await createClient({
|
2024-11-08 06:55:43 +00:00
|
|
|
input: swaggerUrl,
|
|
|
|
|
output: 'src/api',
|
2025-03-11 17:14:47 +01:00
|
|
|
plugins: [
|
|
|
|
|
...defaultPlugins,
|
2025-08-26 13:40:06 +02:00
|
|
|
'@hey-api/client-fetch',
|
2025-03-11 17:14:47 +01:00
|
|
|
{
|
|
|
|
|
name: '@hey-api/sdk',
|
2025-08-20 07:04:17 +01:00
|
|
|
asClass: true,
|
|
|
|
|
classNameBuilder: '{{name}}Service',
|
2025-03-11 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
],
|
2024-11-08 06:55:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.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')}`);
|
|
|
|
|
});
|