-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stop global pollution and clean up methods/exports #12
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/* globals fetch, Headers */ | ||
/* istanbul ignore next */ | ||
if (!process.browser) { | ||
global.fetch = require('node-fetch') | ||
global.Headers = global.fetch.Headers | ||
var fetch = require('node-fetch') | ||
var Headers = fetch.Headers | ||
} | ||
|
||
const caseless = require('caseless') | ||
|
@@ -89,36 +89,6 @@ class R2 { | |
if (opts.headers) this.setHeaders(opts.headers) | ||
this.opts = opts | ||
} | ||
put (...args) { | ||
this.opts.method = 'PUT' | ||
this._args(...args) | ||
return this | ||
} | ||
get (...args) { | ||
this.opts.method = 'GET' | ||
this._args(...args) | ||
return this | ||
} | ||
post (...args) { | ||
this.opts.method = 'POST' | ||
this._args(...args) | ||
return this | ||
} | ||
head (...args) { | ||
this.opts.method = 'HEAD' | ||
this._args(...args) | ||
return this | ||
} | ||
patch (...args) { | ||
this.opts.method = 'PATCH' | ||
this._args(...args) | ||
return this | ||
} | ||
delete (...args) { | ||
this.opts.method = 'DELETE' | ||
this._args(...args) | ||
return this | ||
} | ||
_request () { | ||
let url = this.opts.url | ||
delete this.opts.url | ||
|
@@ -155,9 +125,12 @@ class R2 { | |
} | ||
|
||
module.exports = (...args) => new R2(...args) | ||
module.exports.put = (...args) => new R2().put(...args) | ||
module.exports.get = (...args) => new R2().get(...args) | ||
module.exports.post = (...args) => new R2().post(...args) | ||
module.exports.head = (...args) => new R2().head(...args) | ||
module.exports.patch = (...args) => new R2().patch(...args) | ||
module.exports.delete = (...args) => new R2().delete(...args) | ||
const methods = ['get', 'put', 'post', 'head', 'patch', 'delete'] | ||
for (const method of methods) { | ||
R2.prototype[method] = function(...args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually much slower, and also not equivalent (enumerability, for one) - these should stay as explicit prototype methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that enumerability matters in this context, and the speed of attaching things to exports isn't really an issue in any situation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm referring to the speed of using the |
||
this.opts.method = method.toUpperCase(); | ||
this._args(...args) | ||
return this | ||
} | ||
module.exports[method] = (...args) => new R2()[method](...args) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will shadow the global
fetch
in browsers withundefined
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it won't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
var fetch
will in fact be hoisted, unconditionally, to the top of the scope, which since this is a module, won't be the global scope - it will be a function wrapper.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...That code won't run in browsers, and in node it will hoist to the bootstrap wrapper function in module compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var fetch
runs unconditionally, even if it's in theif
block - it's hoisted.Which means that it's the same as: