Skip to content

Commit

Permalink
fix: parameters parsing and default parameters value (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
wit1312 committed Aug 11, 2023
1 parent 2f589e4 commit bae97b0
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 32 deletions.
4 changes: 2 additions & 2 deletions components/channel/reply.js
Expand Up @@ -6,8 +6,8 @@ import { Message, ChannelParameter } from '@asyncapi/parser';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/

/**
Expand Down
4 changes: 2 additions & 2 deletions components/index/reply.js
Expand Up @@ -5,8 +5,8 @@ import { Message, ChannelParameter } from '@asyncapi/parser';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-publish/package.json
Expand Up @@ -3,7 +3,7 @@
"example_name": "simple-publish"
},
"scripts": {
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"generateTestClient=true\" --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client:windows": "cd ..\\.. && .\\node_modules\\.bin\\ag --output \".\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client\" \".\\examples\\%npm_package_config_example_name%\\asyncapi.json\" \".\" --force-write --param \"promisifyReplyCallback=true\" && cd .\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client && npm i && npm run build && cd ..",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-reply/package.json
Expand Up @@ -3,7 +3,7 @@
"example_name": "simple-reply"
},
"scripts": {
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"generateTestClient=true\" --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client:windows": "cd ..\\.. && .\\node_modules\\.bin\\ag --output \".\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client\" \".\\examples\\%npm_package_config_example_name%\\asyncapi.json\" \".\" --force-write --param \"promisifyReplyCallback=true\" && cd .\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client && npm i && npm run build && cd ..",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-request/package.json
Expand Up @@ -3,7 +3,7 @@
"example_name": "simple-request"
},
"scripts": {
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"generateTestClient=true\" --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client:windows": "cd ..\\.. && .\\node_modules\\.bin\\ag --output \".\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client\" \".\\examples\\%npm_package_config_example_name%\\asyncapi.json\" \".\" --force-write --param \"promisifyReplyCallback=true\" && cd .\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client && npm i && npm run build && cd ..",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-subscribe/package.json
Expand Up @@ -3,7 +3,7 @@
"example_name": "simple-subscribe"
},
"scripts": {
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client": "../../node_modules/.bin/ag --output \"./asyncapi-nats-client\" \"./asyncapi.json\" \"../../\" --force-write --param \"generateTestClient=true\" --param \"promisifyReplyCallback=true\" && cd ./asyncapi-nats-client && npm i && npm run build && cd ..",
"generate:client:windows": "cd ..\\.. && .\\node_modules\\.bin\\ag --output \".\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client\" \".\\examples\\%npm_package_config_example_name%\\asyncapi.json\" \".\" --force-write --param \"promisifyReplyCallback=true\" && cd .\\examples\\%npm_package_config_example_name%\\asyncapi-nats-client && npm i && npm run build && cd ..",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -92,12 +92,12 @@
"parameters": {
"generateTestClient": {
"description": "Generate the test client",
"default": "false",
"default": false,
"required": false
},
"promisifyReplyCallback": {
"description": "Use promises as callbacks for reply operation",
"default": "false",
"default": false,
"required": false
}
},
Expand Down
4 changes: 2 additions & 2 deletions template/src/channels/$$channel$$.ts.js
Expand Up @@ -16,8 +16,8 @@ import { JetstreamPublish } from '../../../components/channel/jetstreamPublish';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/
/**
* @typedef RenderArgument
Expand Down
10 changes: 5 additions & 5 deletions template/src/index.ts.js
Expand Up @@ -4,7 +4,7 @@ import { Publish } from '../../components/index/publish';
import { Subscribe } from '../../components/index/subscribe';
import { Reply } from '../../components/index/reply';
import { Request } from '../../components/index/request';
import { isRequestReply, isReplier, isRequester, isPubsub} from '../../utils/index';
import { isRequestReply, isReplier, isRequester, isPubsub, shouldGenerateTestClient } from '../../utils/index';
// eslint-disable-next-line no-unused-vars
import { AsyncAPIDocument } from '@asyncapi/parser';
import { JetstreamPushSubscription } from '../../components/index/jetstreamPushSubscription';
Expand All @@ -16,8 +16,8 @@ import { JetstreamPublish } from '../../components/index/jetstreamPublish';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/

/**
Expand Down Expand Up @@ -120,11 +120,11 @@ export default function index({ asyncapi, params }) {
return (
<File name="index.ts">
{`
${params.generateTestClient && 'import * as TestClient from \'./testclient/\';'}
${shouldGenerateTestClient(params) ? 'import * as TestClient from \'./testclient/\';' : ''}
${getStandardHeaderCode(asyncapi, '.', './channels')}
export {ErrorCode, NatsTypescriptTemplateError}
${params.generateTestClient && 'export {TestClient};'}
${shouldGenerateTestClient(params) ? 'export {TestClient};' : ''}
/**
* @class NatsAsyncApiClient
Expand Down
8 changes: 4 additions & 4 deletions template/src/testclient/index.ts.js
Expand Up @@ -4,7 +4,7 @@ import { Publish } from '../../../components/index/publish';
import { Subscribe } from '../../../components/index/subscribe';
import { Reply } from '../../../components/index/reply';
import { Request } from '../../../components/index/request';
import { isRequestReply, isReplier, isRequester, isPubsub} from '../../../utils/index';
import { isRequestReply, isReplier, isRequester, isPubsub, shouldGenerateTestClient } from '../../../utils/index';
// eslint-disable-next-line no-unused-vars
import { AsyncAPIDocument, ChannelParameter } from '@asyncapi/parser';
import { JetstreamPullSubscribe } from '../../../components/index/jetStreamPullSubscription';
Expand All @@ -16,8 +16,8 @@ import { JetstreamPublish } from '../../../components/index/jetstreamPublish';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ function getChannelWrappers(asyncapi, params) {
* @param {RenderArgument} param0 render arguments received from the generator.
*/
export default function indexFile({ asyncapi, params }) {
if (!params.generateTestClient) {
if (!shouldGenerateTestClient(params)) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions template/src/testclient/testchannels/$$channel$$.ts.js
Expand Up @@ -4,7 +4,7 @@ import { Subscribe } from '../../../../components/channel/subscribe';
import { Reply } from '../../../../components/channel/reply';
import { Request } from '../../../../components/channel/request';
import { General } from '../../../../components/channel/general';
import { pascalCase, isRequestReply, isReplier, isRequester, isPubsub, camelCase} from '../../../../utils/index';
import { pascalCase, isRequestReply, isReplier, isRequester, isPubsub, camelCase, shouldGenerateTestClient } from '../../../../utils/index';
// eslint-disable-next-line no-unused-vars
import { AsyncAPIDocument, Channel } from '@asyncapi/parser';
import { JetstreamPushSubscription } from '../../../../components/channel/jetstreamPushSubscription';
Expand All @@ -16,8 +16,8 @@ import { JetstreamPullSubscription } from '../../../../components/channel/jetStr
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/
/**
* @typedef RenderArgument
Expand Down Expand Up @@ -106,7 +106,7 @@ function getChannelCode(channel, channelName, params) {
* @param {RenderArgument} param0 render arguments received from the generator.
*/
export default function channelRender({ channelName, channel, params }) {
if (!params.generateTestClient) {
if (!shouldGenerateTestClient(params)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions template/tests/integration/$$channel$$.spec.ts.js
@@ -1,7 +1,7 @@
import { File } from '@asyncapi/generator-react-sdk';
import { publish, subscribe } from '../../../components/test/publishSubscribe';
import { request, reply } from '../../../components/test/requestReply';
import { isRequestReply, isReplier, isRequester, isPubsub, pascalCase} from '../../../utils/index';
import { isRequestReply, isReplier, isRequester, isPubsub, pascalCase, shouldGenerateTestClient } from '../../../utils/index';
// eslint-disable-next-line no-unused-vars
import { Channel } from '@asyncapi/parser';

Expand Down Expand Up @@ -52,7 +52,7 @@ function getTestCode(channel, channelName) {
}

export default function channelRender({ channelName, channel, params }) {
if (!params.generateTestClient) {
if (!shouldGenerateTestClient(params)) {
return;
}

Expand Down
22 changes: 17 additions & 5 deletions utils/general.js
Expand Up @@ -9,18 +9,30 @@ const contentTypeBinary = 'application/octet-stream';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean} generateTestClient - whether or not test client should be generated.
* @property {boolean} promisifyReplyCallback - whether or not reply callbacks should be promisify.
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/

/**
* Should the callbacks be promisify.
*
* Should the test client be generated.
*
* @param {TemplateParameters} params passed to the template
* @returns {boolean} should it promisify callbacks
*/
export function shouldGenerateTestClient(params) {
return typeof params.generateTestClient === 'boolean' && params.generateTestClient
|| typeof params.generateTestClient === 'string' && params.generateTestClient === 'true';
}

/**
* Should the callbacks be promisified.
*
* @param {TemplateParameters} params passed to the template
* @returns {boolean} should it promisify callbacks
*/
export function shouldPromisifyCallbacks(params) {
return params.promisifyReplyCallback;
return typeof params.promisifyReplyCallback === 'boolean' && params.promisifyReplyCallback
|| typeof params.promisifyReplyCallback === 'string' && params.promisifyReplyCallback === 'true';
}

export function camelCase(string) {
Expand Down

0 comments on commit bae97b0

Please sign in to comment.