Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added proxy support for the generate commands. #1665

Merged
merged 4 commits into from
Feb 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-pandas-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/cli": patch
---

feat: added Proxy support for the generate commands.
8 changes: 6 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -428,7 +428,7 @@ Generates whatever you want using templates compatible with AsyncAPI Generator.
USAGE
$ asyncapi generate fromTemplate ASYNCAPI TEMPLATE [-h] [-d <value>...] [--no-interactive] [-i] [--debug] [-n <value>...]
[-o <value>] [--force-write] [-w] [-p <value>...] [--map-base-url <value>] [--registry-url <value>] [--registry-auth
<value>] [--registry-token <value>] [--use-new-generator]
<value>] [--registry-token <value>] [--proxyHost <value>] [--proxyPort <value>] [--use-new-generator]

ARGUMENTS
ASYNCAPI - Local path, url or context-name pointing to AsyncAPI file
@@ -454,6 +454,8 @@ FLAGS
username and password
--registry-url=<value> [default: https://registry.npmjs.org] Specifies the URL of the private registry for
fetching templates and dependencies
--proxyHost=<value> Name of the ProxyHost
--proxyPort=<value> Port number number for the proxyHost.
--use-new-generator Use v2 generator, for generating from newer templates

DESCRIPTION
@@ -477,7 +479,7 @@ USAGE
[--csharpAutoImplement] [--csharpNewtonsoft] [--csharpArrayType Array|List] [--csharpHashcode] [--csharpEqual]
[--csharpSystemJson] [--javaIncludeComments] [--javaJackson] [--javaConstraints] [--javaArrayType Array|List]
[--pyDantic] [--no-interactive] [--log-diagnostics] [--diagnostics-format
json|stylish|junit|html|text|teamcity|pretty] [--fail-severity error|warn|info|hint]
json|stylish|junit|html|text|teamcity|pretty] [--proxyHost <value>] [--proxyPort <value>] [--fail-severity error|warn|info|hint]

ARGUMENTS
LANGUAGE (typescript|csharp|golang|java|javascript|dart|python|rust|kotlin|php|cplusplus|scala) The language you want
@@ -528,6 +530,8 @@ FLAGS
--tsModuleSystem=<option> [default: ESM] TypeScript specific, define the module system to be used.
<options: ESM|CJS>
--tsRawPropertyNames Typescript specific, generate the models using raw property names.
--proxyHost=<value> Name of the ProxyHost
--proxyPort=<value> Port number number for the proxyHost.

DESCRIPTION
Generates typed models
12 changes: 11 additions & 1 deletion src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import { intro, isCancel, spinner, text } from '@clack/prompts';
import { inverse, yellow, magenta, green, red } from 'picocolors';
import fetch from 'node-fetch';
import { fromTemplateFlags } from '../../core/flags/generate/fromTemplate.flags';
import { proxyFlags } from '../../core/flags/proxy.flags';

interface IMapBaseUrlToFlag {
url: string,
@@ -57,7 +58,10 @@ export default class Template extends Command {
'asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template --param version=1.0.0 singleFile=true --output ./docs --force-write'
];

static flags = fromTemplateFlags();
static readonly flags = {
...fromTemplateFlags(),
...proxyFlags()
};

static args = {
asyncapi: Args.string({ description: '- Local path, url or context-name pointing to AsyncAPI file', required: true }),
@@ -72,6 +76,7 @@ export default class Template extends Command {

let { asyncapi, template } = args;
let output = flags.output as string;
const {proxyPort,proxyHost} = flags;
if (interactive) {
intro(inverse('AsyncAPI Generator'));

@@ -96,6 +101,11 @@ export default class Template extends Command {
token: flags['registry-token']
}
};

if (proxyHost && proxyPort) {
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
asyncapi = `${asyncapi}+${proxyUrl}`;
}
const asyncapiInput = (await load(asyncapi)) || (await load());

this.specFile = asyncapiInput;
13 changes: 11 additions & 2 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -5,18 +5,23 @@ import { cancel, intro, isCancel, select, spinner, text } from '@clack/prompts';
import { green, inverse } from 'picocolors';
import { generateModels, Languages, ModelinaArgs } from '@asyncapi/modelina-cli';
import { modelsFlags } from '../../core/flags/generate/models.flags';
import { proxyFlags } from '../../core/flags/proxy.flags';

export default class Models extends Command {
static description = 'Generates typed models';

static readonly args = ModelinaArgs as any;

static flags = modelsFlags();
static readonly flags = {
...modelsFlags(),
...proxyFlags(),
};

async run() {
const { args, flags } = await this.parse(Models);
let { language, file } = args;
let { language, file} = args;
let { output } = flags;
const {proxyPort,proxyHost} = flags;

const interactive = !flags['no-interactive'];

@@ -29,6 +34,10 @@ export default class Models extends Command {
output = parsedArgs.output;
}

if (proxyHost && proxyPort) {
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
file = `${file}+${proxyUrl}`;
}
const inputFile = (await load(file)) || (await load());

const { document, diagnostics ,status } = await parse(this, inputFile, flags as ValidateOptions);
22 changes: 22 additions & 0 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
@@ -69,6 +69,28 @@ describe('template', () => {
});
});

describe('should error out on proxy port', () => {
test
.stderr()
.stdout()
.command([
'generate:fromTemplate',
'http://localhost:8080/dummySpec.yml',
'@asyncapi/newtemplate',
'--output=./test/docs/2',
'--force-write',
'--proxyHost=host',
'--proxyPort=8080',
'--use-new-generator',
])
.it('should throw error when url is passed with proxyHost and proxyPort with invalid host', (ctx, done) => {
expect(ctx.stdout).to.contain('');
expect(ctx.stderr).to.equal('error loading AsyncAPI document from url: Failed to download http://localhost:8080/dummySpec.yml.\n');
cleanup('./test/docs/2');
done();
});
});

describe('git clash', () => {
const pathToOutput = './test/docs/2';
before(() => {
10 changes: 10 additions & 0 deletions test/integration/generate/models.test.ts
Original file line number Diff line number Diff line change
@@ -35,6 +35,16 @@ describe('models', () => {
);
done();
});

test
.stderr()
.stdout()
.command([...generalOptions,'typescript','http://localhost:8080/dummySpec.yml --proxyHost=host --proxyPort=8080'])
.it('should throw error when url is passed with proxyHost and proxyPort with invalid host ', (ctx, done) => {
expect(ctx.stdout).to.contain('');
expect(ctx.stderr).to.equal('error loading AsyncAPI document from url: Failed to download http://localhost:8080/dummySpec.yml --proxyHost=host --proxyPort=8080.\n');
done();
});

describe('with logging diagnostics', () => {
test

Unchanged files with check annotations Beta

asyncapi: '2.0.0'

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - ubuntu-latest

asyncapi-defaultContentType

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - ubuntu-latest

asyncapi-id

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - ubuntu-latest

asyncapi2-tags

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - macos-13

asyncapi-defaultContentType

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - macos-13

asyncapi-id

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - macos-13

asyncapi2-tags

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - windows-latest

asyncapi-defaultContentType

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - windows-latest

asyncapi-id

Check warning on line 1 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - windows-latest

asyncapi2-tags
info:

Check warning on line 2 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - ubuntu-latest

asyncapi-info-contact Info object should have "contact" object.

Check warning on line 2 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - macos-13

asyncapi-info-contact Info object should have "contact" object.

Check warning on line 2 in github-action/test/asyncapi.yml

GitHub Actions / Test NodeJS PR - windows-latest

asyncapi-info-contact Info object should have "contact" object.
title: Streetlights API
version: '1.0.0'
description: |
parser = new Parser();
async run() {

Check warning on line 59 in src/commands/optimize.ts

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed
const { args, flags } = await this.parse(Optimize); //NOSONAR
let filePath = args['spec-file'];
const proxyHost = flags['proxyHost'];
return existsSync(filePath);
}
export function start(filePath: string, port: number = DEFAULT_PORT): void {

Check warning on line 23 in src/core/models/Studio.ts

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 23 to the 15 allowed
if (filePath && !isValidFilePath(filePath)) {
throw new SpecificationFileNotFound(filePath);
}