Sha256: 78a2ff2be66e1c07de5936d7016691a94b9f4fd3554f4c83d7255f1742d61cbd

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

const AbstractHandler = require('./AbstractHandler')

class InvokeStandaloneMethodHandler extends AbstractHandler {
    requiredParametersCount = 2

    constructor() {
        super()
    }

    process(command) {
        try {
            if (command.payload.length < this.requiredParametersCount) {
                throw new Error('Invoke standalone Method parameters mismatch')
            }

            const { payload } = command
            const type = payload[0]
            const methodName = payload[1]
            const args = payload.slice(2)

            if (typeof type === 'function') {
                // assuming that function is exported as single (default)
                return Reflect.apply(type, undefined, args)
            }

            if (typeof type === 'object') {
                // assuming that function inside of multiple exports
                const method = type[methodName]

                if (typeof method === 'function') {
                    return Reflect.apply(method, undefined, args)
                }
                return this.throwError(type, methodName)
            } else {
                throw new Error(`Function ${methodName} not found in libary.`)
            }
        } catch (error) {
            throw this.process_stack_trace(error, this.constructor.name)
        }
    }

    throwError(type, methodName) {
        const methods = Object.getOwnPropertyNames(type).filter(
            (property) => typeof type[property] === 'function'
        )

        let message = `Function ${methodName} not found in libary. Available standalone methods:\n`
        methods.forEach((methodIter) => {
            message += `${methodIter}\n`
        })

        throw new Error(message)
    }
}

module.exports = new InvokeStandaloneMethodHandler()

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
javonet-ruby-sdk-2.5.11 lib/javonet-ruby-sdk/Binaries/Nodejs/lib/core/handler/InvokeStandaloneMethodHandler.js
javonet-ruby-sdk-2.5.10 lib/javonet-ruby-sdk/Binaries/Nodejs/lib/core/handler/InvokeStandaloneMethodHandler.js
javonet-ruby-sdk-2.5.9 lib/javonet-ruby-sdk/Binaries/Nodejs/lib/core/handler/InvokeStandaloneMethodHandler.js