Skip to content

Commit

Permalink
rework: subscribing for events using call method with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
DanilSord committed Mar 11, 2022
1 parent 33516ee commit cab8aba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
23 changes: 12 additions & 11 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,30 @@ const request = (data, hostname, port) =>
request.end();
});

const getFieldsTypes = (fields = {}) => {
const { properties = {}, required = [] } = fields;
let result = '';
const getFieldsTypes = (fields = {}, spaces = 0) => {
const { properties = {}, required = [], description = '' } = fields;
let result = `${spaces === 0 ? description : ''}\n`;
for (const field in properties) {
const name = `* - \`${field}\` `;
const name = `* ${''.padStart(spaces, ' ')}- \`${field}\` `;
const type = `{ *${properties[field].type}* } `;
const description = properties[field].description ? `- ${properties[field].description} ` : '';
const requiredFlag = required.includes(field) ? '**Required** ' : '';
result += `${name}${type}${description}${requiredFlag}\n`;
if (properties[field].type === 'object') result += getFieldsTypes(properties[field], spaces + 4);
}
return result;
};

const getMethodTypes = (methodName, method) => {
const name = `*@property {function} ${methodName}\n`;
const description = `***${method.description || ''}**\n`;
const params = `*___\n* Params:\n${getFieldsTypes(method.params)}\n`;
const returns = `*___\n* Returns:\n${getFieldsTypes(method.result)}\n`;
return `${name}${description}${params}${returns}`;
const description = method.description ? `***${method.description}**\n` : '';
const params = Object.keys(method.params || {}).length ? `*___\n* Params: ${getFieldsTypes(method.params)}\n` : '';
const returns = Object.keys(method.result || {}).length ? `*___\n* Returns: ${getFieldsTypes(method.result)}\n` : '';
const emit = Object.keys(method.emit || {}).length ? `*___\n* Emit: ${getFieldsTypes(method.emit)}\n` : '';
return `${name}${description}${params}${returns}${emit}`;
};

const getModuleTypes = (moduleName, module) => {
const schema = module.schema;
const getModuleTypes = (moduleName, schema) => {
let result = `/**\n*@typedef {object} ${moduleName};\n`;
for (const methodName in schema) {
result += getMethodTypes(methodName, schema[methodName]);
Expand Down Expand Up @@ -99,7 +100,7 @@ const generateTypes = (modules = {}) => {
fs.writeFileSync(path.resolve(WORKING_DIRECTORY, options.path, 'api.js'), apiTemplate);
} else if (options.command === 'types') {
if (!options.host || !options.port) throw new Error('Host and port option required for "types" command.');
const modules = (await request(requestIntrospectionData, options.host, options.port))?.result?.modules;
const modules = (await request(requestIntrospectionData, options.host, options.port))?.result;
fs.writeFileSync(path.resolve(WORKING_DIRECTORY, options.path, 'api-types.js'), generateTypes(modules));
}
})();
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
import { Api } from './lib/api';

export { Api };
11 changes: 5 additions & 6 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ export class Api {
this.schema = {};
}

async subscribe(event, callback, params) {
async subscribe(event, callback) {
if (!this.subscriptions.has(event)) this.subscriptions.set(event, []);
const result = await this.ws.call(event, params);
const callbacks = this.subscriptions.get(event);
callbacks.push( callback );
return result;
}

onEvent( event, data ) {
Expand All @@ -36,11 +34,11 @@ export class Api {
}

async build() {
const {modules} = await this.http.call(this.config.getModulesMethod);
const modules = await this.http.call(this.config.getModulesMethod);
if (modules) {
this.schema = modules;
for (const moduleName in modules) {
const methods = modules[moduleName].schema;
const methods = modules[moduleName];
this[moduleName] = {};
for (const methodName in methods) {
const transport = methods[methodName].transport || 'ws';
Expand All @@ -53,9 +51,10 @@ export class Api {

getHandler(moduleName, methodName, transport) {
const method = `${moduleName}/${methodName}`;
return async (params) => {
return async (params, callback) => {
const result = await this[transport].call(method, params);
if (transport === 'http') this.resetWSConnection();
if (callback) this.subscribe(method, callback);
return result;
};
}
Expand Down

0 comments on commit cab8aba

Please sign in to comment.