From bae97b000a08dd1bff16074909f0f5c7d8a5dc4d Mon Sep 17 00:00:00 2001 From: Vitaly Ivanov Date: Fri, 11 Aug 2023 19:38:38 +0700 Subject: [PATCH] fix: parameters parsing and default parameters value (#550) --- components/channel/reply.js | 4 ++-- components/index/reply.js | 4 ++-- examples/simple-publish/package.json | 2 +- examples/simple-reply/package.json | 2 +- examples/simple-request/package.json | 2 +- examples/simple-subscribe/package.json | 2 +- package.json | 4 ++-- template/src/channels/$$channel$$.ts.js | 4 ++-- template/src/index.ts.js | 10 ++++----- template/src/testclient/index.ts.js | 8 +++---- .../testclient/testchannels/$$channel$$.ts.js | 8 +++---- .../tests/integration/$$channel$$.spec.ts.js | 4 ++-- utils/general.js | 22 ++++++++++++++----- 13 files changed, 44 insertions(+), 32 deletions(-) diff --git a/components/channel/reply.js b/components/channel/reply.js index c38104e40..02a2e9351 100644 --- a/components/channel/reply.js +++ b/components/channel/reply.js @@ -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. */ /** diff --git a/components/index/reply.js b/components/index/reply.js index f5ff343ea..3d614180d 100644 --- a/components/index/reply.js +++ b/components/index/reply.js @@ -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. */ /** diff --git a/examples/simple-publish/package.json b/examples/simple-publish/package.json index e775d6fd1..b1151bfc2 100644 --- a/examples/simple-publish/package.json +++ b/examples/simple-publish/package.json @@ -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", diff --git a/examples/simple-reply/package.json b/examples/simple-reply/package.json index 711f10141..303a6619d 100644 --- a/examples/simple-reply/package.json +++ b/examples/simple-reply/package.json @@ -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", diff --git a/examples/simple-request/package.json b/examples/simple-request/package.json index 84bd3347c..25425441e 100644 --- a/examples/simple-request/package.json +++ b/examples/simple-request/package.json @@ -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", diff --git a/examples/simple-subscribe/package.json b/examples/simple-subscribe/package.json index 285ec0e36..9c035810e 100644 --- a/examples/simple-subscribe/package.json +++ b/examples/simple-subscribe/package.json @@ -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", diff --git a/package.json b/package.json index d8f98ae46..e0c7bf7b7 100644 --- a/package.json +++ b/package.json @@ -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 } }, diff --git a/template/src/channels/$$channel$$.ts.js b/template/src/channels/$$channel$$.ts.js index 948f78818..c286316e8 100644 --- a/template/src/channels/$$channel$$.ts.js +++ b/template/src/channels/$$channel$$.ts.js @@ -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 diff --git a/template/src/index.ts.js b/template/src/index.ts.js index 354115e90..e674a8c6c 100644 --- a/template/src/index.ts.js +++ b/template/src/index.ts.js @@ -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'; @@ -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. */ /** @@ -120,11 +120,11 @@ export default function index({ asyncapi, params }) { return ( {` -${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 diff --git a/template/src/testclient/index.ts.js b/template/src/testclient/index.ts.js index d72b37fd7..705148f53 100644 --- a/template/src/testclient/index.ts.js +++ b/template/src/testclient/index.ts.js @@ -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'; @@ -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. */ /** @@ -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; } diff --git a/template/src/testclient/testchannels/$$channel$$.ts.js b/template/src/testclient/testchannels/$$channel$$.ts.js index 691e64912..b9ba8029b 100644 --- a/template/src/testclient/testchannels/$$channel$$.ts.js +++ b/template/src/testclient/testchannels/$$channel$$.ts.js @@ -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'; @@ -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 @@ -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; } diff --git a/template/tests/integration/$$channel$$.spec.ts.js b/template/tests/integration/$$channel$$.spec.ts.js index 0e50db059..34e427ac2 100644 --- a/template/tests/integration/$$channel$$.spec.ts.js +++ b/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'; @@ -52,7 +52,7 @@ function getTestCode(channel, channelName) { } export default function channelRender({ channelName, channel, params }) { - if (!params.generateTestClient) { + if (!shouldGenerateTestClient(params)) { return; } diff --git a/utils/general.js b/utils/general.js index 8cc436d8a..8eaa329f1 100644 --- a/utils/general.js +++ b/utils/general.js @@ -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) {