You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a server i'm communicating with requires a signature to be generated from a key-pair and appended as a header for every request. i decided to try and use a request transformer to keep this signature generating code in one place, instead of passing a signature value in with every api.whatever('/where/ever') call.
the module i'm using for generating these signatures works exclusively with promises. as a result of this constraint, i have tried these two approaches:
// approach oneapi.addRequestTransform(asyncfunction(request){varnow=crypto.createHash('sha256').update(Date.now()).digest()request.headers['x-message']=now.toString('hex')request.headers['x-signature']=awaitsign(Buffer.from(privatekey,'hex'),now)})// approach twoapi.addRequestTransform(function(request){varnow=crypto.createHash('sha256').update(Date.now()).digest()request.headers['x-message']=now.toString('hex')sign(Buffer.from(privatekey,'hex'),now).then(function(sig){request.headers['x-signature']=sig.toString('hex')}).catch(function(err){// abort the request, somehow})})
when the request is fired, the value that i attempted to set in the transformer is not present. after reading the request transformer part of the source code, it appears to be attributed to ramda foreach not waiting for my promise to resolve.
so, is it okay to be using asynchronous functions within a transformer? (i'm presuming the answer is no 😄 ) or should i try and find a way to generate these signatures synchronously?
edit:
is it possible to support asynchronous transformers? if so, any pointers for implementing? i'd be happy to have a crack at it!
The text was updated successfully, but these errors were encountered:
If, internally, we wrapped all transformers with a promise (that weren't already one), then that ramda loop becomes a Promise.all(). That'd mean changing the internals of apisauce to be promise-based as well.
a server i'm communicating with requires a signature to be generated from a key-pair and appended as a header for every request. i decided to try and use a request transformer to keep this signature generating code in one place, instead of passing a signature value in with every
api.whatever('/where/ever')
call.the module i'm using for generating these signatures works exclusively with promises. as a result of this constraint, i have tried these two approaches:
when the request is fired, the value that i attempted to set in the transformer is not present. after reading the request transformer part of the source code, it appears to be attributed to ramda foreach not waiting for my promise to resolve.
so, is it okay to be using asynchronous functions within a transformer? (i'm presuming the answer is no 😄 ) or should i try and find a way to generate these signatures synchronously?
edit:
is it possible to support asynchronous transformers? if so, any pointers for implementing? i'd be happy to have a crack at it!
The text was updated successfully, but these errors were encountered: