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

koa-api-combo

v4.0.0

Published

proxy multiple http or https requests, and response the result array by combine all the result.

Downloads

10

Readme

koa-api-combo

proxy multiple http or https requests, and response the result array by combine all the result.

Usage

const Combo = require('koa-api-combo');

// GET /combo?urls=encodeURIComponent('/x,/xx?xxx,/xxx')&...
app.use(Combo('/combo', { apiHost: 'a.com' });
// equals to
// GET /x?...
// GET /xx?xxx&...
// GET /xxx?...

// GET /combo?urls=encodeURIComponent('/x,/xx?xxx,/xxx')&...
// GET /combo/ignore?urls=encodeURIComponent('/x,/xx?xxx,/xxx')&...
// the request result data will be set to null if the request is error
// and the result should be like [null] [null, {"a":1}] ...
app.use(Combo.withIgnoreError('/combo', { apiHost: 'a.com' }));

Install

npm install koa-api-combo --save

Middleware

Combo(path, comboConfig, apiRequestConfig)

  • @param {string} path is the route string. And should be exactly equal to ctx.path
  • @param {Object} apiRequestConfig the same as ApiRequest
  • @param {Object} comboConfig
  • @param {boolean} [comboConfig.supportIgnoreError] will use null instead the response data if request url error,
  •   and need request by append `/ignore` to the `path` parameter if set to true
  • @param {Function} [comboConfig.isValidUrl] for filter possible illegal url if needed, and response 400 with parameters error

ApiRequest(config)

  • @param {string} config.apiHost
  • @param {number} [config.port]
  • @param {string} [config.protocol=http] the protocol that request api server
  • @param {number} [config.dnsCacheTime=10] the time for dns cache, default 10 seconds, do not use dns cache if set 0
  • @param {number} [config.timeout=7] timeout for each api request, default 7 seconds
  • @param {boolean} [config.compress=false] whether accept encoding from api server, and only gzip and deflate support.
  • @param {string} [config.headers='Cookie,User-Agent,Referrer'] the header you want to send to api server

Query

The key of query is urls.

The value is the url array separate from ,, and should encode it by use encodeURIComponent.

Each url can have ? and &, and will append with the querystring from ctx.querystring except urls.

So this means parameters are shared by each url if you set other parameter but urls to current request.

Some Example:

GET /combo?urls=/a,/b&p=1 # urls=encodeURIComponent('/a,/b')
// equivalent to
// /a?p=1 and /b?p=1

GET /combo?h=2&urls=/a?c=1,/b?p=1&p=1 # urls=encodeURIComponent('/a?c=1,/b?p=1')
// equivalent to
// /a?c=1&h=2&p=1 and /b?p=1&h=2&p=1

Note::: it just append to the url for the same query! And query from url is first.

Test

npm test