Closed mrchrisadams closed 2 months ago
@mrchrisadams the plan is to move towards using an options object for anything additional (like userAgentIdentifier, verbose, or anything else we add later).
We've started down that path with #189, and I've covered off how we can guide developers to use the options object to pass in optional information for a greencheck (https://github.com/thegreenwebfoundation/co2.js/pull/192/files/ccd5bc196e34a598bb6c9cbe9f0304d55d9604e1#r1554588557)
Thanks @fershad , that works for me.
I'm closing this, as I think your last comment addresses anything I initially raised. I realised the nifty default parameter thing after suggesting the options parameter approach discussed in #189, and TBH I'm not familiar enough to advocate hard for it, so the options approach sounds good.
In our dev docs, for the check API, the
check()
method is described as follows:In the actual code, the implementation looks like this
We currently pass in this
userAgentIdentifier
parameter all the way through the call chain, like this, in the checkAgainst API functionYou can't actually see what the
userAgentIdentifier
is likely to be until right before the the API request is made, like so - see the source in our helper fileA proposal
Right now, we say the
userAgentIdentifer
parameter is optional in our docs, but it's actually required in our top level check() function signature - we just sort of tidy up scenarios where it's undefined, by callinggetApiRequestHeaders
right at the end.I didn't realise you can pass in callable functions as default parameters with javascript, which are evalulated at call time.
With this in mind, I think it might actually be clearer and simpler to have a function signature like this, where we clearly pass in a default function to identify an API client, when it's not specified. Something like this:
And for our
identifyAPIClient()
we might have some code like this, which is only concerned about identifying itself, rather than building or changing request headers too.If we did it this way:
check()
function we could assume that any functions it wraps can expect a string to be passed in as the identifier for the API client. Fewer things that can change to keep track are a good thing.Anyway @fershad, I'm leaving this issue here to discuss next week, as it's something I realised when reading over #189, and I think it might help with handling stuff like
verbose = true
as a default parameter there too, along with anything else we choose in future.