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

@wfh/http-request-proxy

v5.1.8

Published

Work with Plink, isomophic http proxy server work with Angular HTTP client

Downloads

4

Readme

此工具和 http-proxy-middleware 的主要差别是支持 Angular project HTTPClient interceptor, this tool is not recommended if you are not running Angular.

HTTP 请求代理工具

Config your config.yaml or config.{env}.yaml

配置多个自启动代理

可以解决开发环境的ajax跨域问题

@dr/http-request-proxy:
    proxies:
        demo: https://www-demo.api.com
        stage: https://www.stage.api.com

修改客户端代码的ajax请求的URL, 比如,

  • 原来是请求 http://www-demo.api.com/services/profile?userid=456, 改为 http://localhost:/http-request-proxy/demo/services/profile?userid=456
  • 原来是请求 http://www.stage.api.com/services/profile?userid=456, 改为 http://localhost:/http-request-proxy/stage/services/profile?userid=456

配置组件包server端的代码 package.json

{
    "main": "server.js",
    "dr": {
        "type": "server"
    }
}

Typescript support

import {forName, forEach, ProxyInstance} from '@dr/http-request-proxy';

代理Cookie时去掉 domain属性

import {forName, forEach, ProxyInstance} from '@dr/http-request-proxy';

forEach((p: ProxyInstance) => addOptions({removeCookieDomain: true});

添加请求拦截function 跳过API请求, 直接返回客户端Mock响应

main server JS file server.js:

exports.activate = function() {
    if (api.argv['mock-api'] == null) // If there is command line arguments "--mock-api"
        return;
    var proxy = require('@dr/http-request-proxy').forName('demo'); // For the proxy target URL configured with "demo"
    proxy.mockResponse('/api/v2/user/profile', (req, hackedReqHeaders, requestBody, lastResult) => {
        ...
        return mockResponseBody; // JSON object | string | Buffer
        // Or
        // return Promise.resolve(...);
    });
    proxy.mockResponse('*', (req, hackedReqHeaders, requestBody, lastResult) => {
        if (/\/api\/v2\/user\/plans\/([^\/]+)\/buy/.test(req.url))
            return mockResponseBody;
        // return null or undefined to doing nothing
	});
};

对所有配置的代理目标都拦截

比如要对demo, stage 两套配置的proxy都做拦截

exports.activate = function() {
    if (api.argv['mock-api'] == null) // If there is command line arguments "--mock-api"
        return;
    require('@dr/http-request-proxy').forEach(proxy => {
        proxy.mockResponse('/api/v2/user/profile', (req, hackedReqHeaders, requestBody, lastResult) => {
            ...
            return mockResponseBody; // JSON object | string | Buffer
            // Or
            // return Promise.resolve(...);
        });
    });
};

添加响应拦截function 返回修改过的API响应body

main server JS file server.js:

exports.activate = function() {
    if (api.argv['mock-api'] == null) // If there is command line arguments "--mock-api"
        return;
    var proxy = require('@dr/http-request-proxy').forName('demo');
    proxy.interceptResponse('/api/v2/user/profile', (req, hackedReqHeaders, responseBody, lastResult) => {
        ...
        return hackedResponseBody; // JSON object | string | Buffer
        // Or
        // return Promise.resolve(...);
    });
    proxy.interceptResponse('*', (req, hackedReqHeaders, responseBody, lastResult) => {
        if (/\/api\/v2\/user\/plans\/([^\/]+)\/buy/.test(req.url))
            return hackedResponseBody;
        // return null or undefined to doing nothing
	});
};

添加请求拦截function 修改请求发送的内容 (修改后的请求依然会发送到后端API)

main server JS file server.js:

exports.activate = function() {
    if (api.argv['mock-api'] == null) // If there is command line arguments "--mock-api"
        return;
    var proxy = require('@dr/http-request-proxy').forName('demo');
    proxy.interceptRequest('/api/v2/user/profile', (req, hackedReqHeaders, requestBody, lastResult) => {
        return hackedRequestBody; // JSON object | string | Buffer
        // Or
        // return Promise.resolve(...);
        // return undefined or null meaning no changes to body
    });
    proxy.interceptRequest('*', (req, hackedReqHeaders, requestBody, lastResult) => {
        if (/\/api\/v2\/user\/plans\/([^\/]+)\/buy/.test(req.url))
            return hackedRequestBody;
        // return null or undefined to doing nothing
	});
};