Skip to content

The plugin extends [request-response](https://github.com/trooba/trooba-request-response) trooba plugin to support handlers in koa like execution template

License

Notifications You must be signed in to change notification settings

trooba/trooba-request-response-async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trooba-request-response-async

The plugin extends request-response trooba plugin to koa like execution template.

Requires trooba framework version 3+ and node 8+

Install

npm install trooba -S
npm install trooba-request-response-async -S

Usage

Request/response example

const Trooba = require('trooba');

Trooba
.use('trooba-request-response-async')
.use(async (ctx, next) => {
    console.log(ctx.request); // > ping
    // wait for response
    await next();
    console.log(ctx.response); // > pong
})
.use(ctx => {
    console.log(ctx.request); // > ping
    ctx.response = 'pong';
})
.build()
.create();

const response = await pipe.request('ping');

Streaming data

const Trooba = require('trooba');

Trooba
.use('trooba-request-response-async')
.use(async ctx => {
    console.log(ctx.request); // > ping
    const requestBody = readBody(ctx.stream);
    // set response metadata that will be flushed with the first chunk
    ctx.response = 'pong';
    // or you can also do ctx.response({status:200, headers: {...}})
    // Now write a stream using standard streams
    fs.createReadStream('large.txt')
        .pipe(ctx.stream);
    // or via writer
    // ctx.stream.write('bar').write('bar').end();
})
.build()
.create();

var ctx = pipe.request('ping');
// or you can do  pipe.request({ headers: {...}, path: 'path/to/resource' })
ctx.stream.write('foo').write('foo').end();

var response = await ctx.response;
console.log(response); // > pong
const body = readBody(ctx.stream)

async function readBody(stream) {
    var body = [];
    while(true) {
        const data = await stream.read();
        if (data === undefined) {
            break;
        }
        body.push(data);
    }
    return body;
}

Throw error

const Trooba = require('trooba');

var pipe = Trooba
.use('trooba-request-response-async')
.use(async (ctx, next) => {
    console.log(ctx.request); // > ping
    // wait for response
    try {
        await next();
    }
    catch (err) {
        throw err;
    }
})
.use(ctx => {
    console.log(ctx.request); // > ping
    ctx.response = 'pong';
})
.build()
.create();

pipe.request('ping'); // will throw error

Retry

const Trooba = require('trooba');

var retryCount = 1;
var pipe = Trooba
.use('trooba-request-response-async')
.use(async (ctx, next) => {
    console.log(ctx.request); // > ping
    // wait for response
    try {
        await next(); // will fail first time
    }
    catch (err) {
        // retry again
        await next();
    }
})
.use(ctx => {
    console.log(ctx.request); // > ping
    if (retryCount-- > 0) {
        throw new Error('Boom');
    }
    ctx.response = 'pong';
})
.build()
.create();

pipe.request('ping'); // will throw error

About

The plugin extends [request-response](https://github.com/trooba/trooba-request-response) trooba plugin to support handlers in koa like execution template

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published