npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa2-request-middleware

v2.2.11

Published

koa2 request middleware

Downloads

40

Readme

koa2-request-middleware

Build Status Coverage Status

a middleware for koa2 to separate request and response data's use

1.Usage

Use with koa2

const Koax = require('koa2-request-middleware');
const Koa = require('koa');
const koaRouter =  require('koa-router');
let koax = new Koax();
let app = new Koa();
let approuter = new KoaRouter();
koax.mount(()=>{
	return koax.setName('testKoax1').cached().request({
		uri:'http://localhost:8012/testkoax1',
		method:'GET'
	})
});
approuter.get('/test',(ctx,next) => {
	ctx.body = ctx.koax.testKoax1;
	ctx.status = 200;
});
app.use(koax.middleware());
app.use(approuter.routes());

ctx.koax.testKoax1 is the response of testKoax1's request, you can read the data ctx.koax in any middleware you want.
For more examples you can take a look at test

Use with koa-router

const Koax = require('koa2-request-middleware');
const Koa = require('koa');
const koaRouter =  require('koa-router');
let koax = new Koax();
let app = new Koa();
let approuter = new KoaRouter();
koax.mount(()=>{
	return koax.setName('testKoax1').cached().request({
		uri:'http://localhost:8012/testkoax1',
		method:'GET'
	})
});
approuter.get('/test',koax.middleware(),(ctx,next) => {
	ctx.body = ctx.koax.testKoax1;
	ctx.status = 200;
});
app.use(approuter.routes());

This usage will just make request when come in router '/test',but the ctx.koax is in global.

2.APIs

  • koax.mount(Function) //Function must return a promise
    Use this API to mount request, and it will return koax instance

  • koax.setName(String)
    Use this API to set a name for response data, and it will return koax instance, if many requests has the same name, the last name's request will cover all the same name's data, because the data will mount on the instance of koa2

  • koax.request(options,[name])
    If you use the call chaining(koax.setName().request()),you can ignore the second argument,koax will cache the name you have setted. Options details please see request-promise for more information, and it will return promise instance

  • koax.cached([name])
    If you use the call chaining(koax.setName().cached()),you can ignore the second argument.When you use cached(), koax will only make request for once time and store the data in memory, and next time will take the data in memory, and it will return koax instance

  • koax.middleware()
    Make koax as a middleware in koa2

  • koax.cancelCache([name])
    This api is forced to change one of the requests from cached to uncached, it will return koax instance

  • koax.reCache([name])
    This api is forced to change one of the requests from uncached to cached, it will return koax instance

  • koax.list
    It will show you how many request is mounted on this instance of koax

3.request chain usage

You can use request like this

koax.mount(async ()=>{
	let tes1 = await koax.setName('testKoax1').cached().request({
		uri:'http://localhost:8012/testkoax1',
		method:'GET'
	});
	await koax.setName('testKoax2').request({
			uri:'http://localhost:8012/testKoax2',
			method:'GET',
			qs:JSON.parse(tes1)
	},'testKoax2');
});

Please pay attention to koax.mount() function argument needs a promise