| // a bit terser code than I usually write but it's 10 LOC within 80 cols | |
| // if you are struggling to follow the code you can replace 1-char | |
| // references around with the following one, hoping that helps :-) | |
| // d => descriptors | |
| // k => key | |
| // p => promise | |
| // r => response | |
| const d = Object.getOwnPropertyDescriptors(Response.prototype); | |
| const isFunction = value => typeof value === 'function'; | |
| const bypass = (p, k, { get, value }) => get || !isFunction(value) ? | |
| p.then(r => r[k]) : | |
| (...args) => p.then(r => r[k](...args)); | |
| const direct = (p, value) => isFunction(value) ? value.bind(p) : value; | |
| const handler = { | |
| get: (p, k) => d.hasOwnProperty(k) ? bypass(p, k, d[k]) : direct(p, p[k]) | |
| }; | |
| /** | |
| * @param {RequestInfo | URL} input | |
| * @param {...RequestInit} init | |
| * @returns {Promise<Response> & Response} | |
| */ | |
| export default (input, ...init) => new Proxy(fetch(input, ...init), handler); | |