Commit 8b7d61c0 authored by bernardosunderhus's avatar bernardosunderhus

adding @curio/client to test the api in a real world application

parent 9907e6b0
registry=http://nexus.conexops.com.br/repository/npm/
registry=https://nexus.dev.evologica.com.br/repository/npm/
init.author.name = admin
init.author.email = suporte@evologica.com.br
# an email is required to publish npm packages
......
This diff is collapsed.
......@@ -7,9 +7,10 @@
"siscon-cli": "./index.js"
},
"publishConfig": {
"registry": "http://nexus.conexops.com.br/repository/npm-internal/"
"registry": "https://nexus.dev.evologica.com.br/repository/npm-internal/"
},
"scripts": {
"start": "ts-node ./src/index.ts",
"compile": "rimraf ./dist && tsc -p . --module commonjs --outDir ./dist && gulp copy-package",
"release": "npm run compile && gulp release",
"copy-package": "gulp copy-package",
......@@ -18,6 +19,7 @@
"author": "Bernardo Sunderhus",
"license": "ISC",
"dependencies": {
"@curio/client": "^1.0.0-rc",
"appdata": "^1.1.0",
"chalk": "^2.1.0",
"cli-table": "^0.3.1",
......@@ -28,7 +30,8 @@
"inquirer": "^3.3.0",
"inquirer-autocomplete-prompt": "^0.11.1",
"moment": "^2.18.1",
"ora": "^1.3.0"
"ora": "^1.3.0",
"rxjs": "^5.5.8"
},
"devDependencies": {
"@types/chalk": "^0.4.31",
......
import * as Rx from 'rxjs/Observable'
import * as curio from '@curio/client'
import { HttpConnection } from 'curiojs'
import config from '../config'
curio.setObservableConstructor(Rx.Observable)
const connection = new HttpConnection(config().service)
connection.debug = false
/**
* the sink of truth
*/
export const sink = new curio.Dispatcher<curio.Message>()
/**
* the source of truth
*/
export const source = sink
.pipe(
curio.operators.map((message) => {
switch (message.signalName) {
case curio.constants.OPEN_SESSION_SIGNAL_NAME: return message.recycle({
body: {
SYSTEM_CODE: config().service.system,
LOGIN: message.body.login,
PASSWORD: message.body.password
}
})
default: return message
}
}),
connect(connection)
)
/**
* the service itself
*/
export const service = new curio.Service(source.pipe(
curio.operators.map((message) => {
if (message.error) return message
switch (message.signalName) {
case curio.constants.OPEN_SESSION_SIGNAL_NAME: return message.recycle({
body: Number(message.body.UCID[0]._)
})
default: return message
}
})
))
service.subscribe(sink)
function connect (conn: HttpConnection): curio.Operator<curio.Message> {
return (source) => new curio.Observable((subscriber) => source.subscribe((request) => {
conn.request(request.recipient, request.signalName, request.body)
.then((response) => request.recycle({
recipient: request.sender,
sender: request.recipient,
body: response
}))
.catch((error) => request.recycle({
recipient: request.sender,
sender: request.recipient,
body: null,
error
}))
.then(subscriber.next.bind(subscriber))
}))
}
......@@ -7,7 +7,7 @@ import * as chalk from 'chalk'
const figlet = require('figlet')
program
.version('0.0.1', '-v, --version')
.version('0.0.2', '-v, --version')
.parse(process.argv)
if (!program.args.length) {
......
......@@ -8,6 +8,8 @@ import * as makeSpinner from 'ora'
import entrada from './entrada'
import saida from './saida'
import * as next from '../../curio/next'
program
.command('ponto')
.usage('[opções]')
......@@ -34,3 +36,34 @@ program
else console.error(error)
}
})
program
.command('next-ponto')
.usage('[opções]')
.option('-u, --username <username>', 'Adicione o nome do usuário')
.option('-p, --password <password>', 'Adicione o password diretamente na linha do comando')
.action(async function ponto (options: program.CommanderStatic) {
let {username, password} = options
if (!username) username = (await prompt(questions.username)).username
if (!password) password = (await prompt(questions.password)).password
const spinner = makeSpinner()
try {
spinner.start(chalk.cyan('Realizando login...'))
const session = await next.service.openSession(username, password)
// session.setSource(next.source)
// session.subscribe(next.sink)
// const ucRegistrarPonto = await session.openUseCase(788)
// console.log(ucRegistrarPonto)
// const {Domains} = await ucRegistrarPonto.obterContexto()
// const {UltimoRegistroPonto, RegistroPonto} = await ucRegistrarPonto.incluirRegistroPonto()
spinner.succeed(`Login realizado com ${chalk.bold.italic.underline.green('sucesso')}`)
// if (UltimoRegistroPonto && UltimoRegistroPonto[0]._Tipo === 'E') saida(Domains[0], UltimoRegistroPonto[0], RegistroPonto[0], ucRegistrarPonto)
// else entrada(RegistroPonto[0], ucRegistrarPonto)
} catch (error) {
if (error instanceof BusinessError) spinner.fail(chalk.bgRed.white.bold(error.message))
else if (error instanceof ConnectionError) spinner.fail(chalk.bgRed.white.bold(error.message))
else if (error instanceof SystemError) spinner.fail(chalk.bgRed.white.bold(error.message))
else if (error instanceof Error) spinner.fail(chalk.bgRed.white.bold(error.message))
else (spinner.fail(), console.error(error))
}
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment