Skip to content

Commit

Permalink
feat: added proxy support for the generate commands. (#1665)
Browse files Browse the repository at this point in the history
Co-authored-by: Moderator <60972989+AayushSaini101@users.noreply.github.com>
  • Loading branch information
neoandmatrix and AayushSaini101 authored Feb 15, 2025
1 parent cb7c78f commit cec8081
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
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
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }),
Expand All @@ -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'));

Expand All @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
10 changes: 10 additions & 0 deletions test/integration/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cec8081

Please sign in to comment.