Skip to content

LongTengDao-fork-A/proxy-www

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

proxy-www

学会 Proxy 就可以为所欲为吗?

对,学会 Proxy 就可以为所欲为!

const www = new Proxy(() => 'https://www', {
    get(target, key, proxy) {
        return typeof key === 'string'
                   ? new Proxy(() => target() + '.' + key, this) :
               key === Symbol.toPrimitive
                   ? () => target() + '/' :
               Reflect.get(target, key, proxy);
    },
    apply(target, thisArg, args) {
        switch (typeof args[0]) {
            case 'function':
                return fetch(target().replace(/\.then$/, '')).then(...args);
            case 'object':
                const href = target() + '/' + String.raw(...args);
                return {
                    [Symbol.toPrimitive]: () => href,
                    then: (v, x) => fetch(href).then(v, x),
                };
        }
    },
});

访问百度

www.baidu.com.then(response => {
    console.log(response.status);
    // ==> 200
});

使用 async/await 语法:

const response = await www.baidu.com;

console.log(response.ok);
// ==> true

console.log(response.status);
// ==> 200

带上路径:

await www.baidu.com`s?wd=justjavac`;

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%