Skip to content
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

asynchronous transformers #30

Closed
skibz opened this issue Sep 23, 2016 · 2 comments
Closed

asynchronous transformers #30

skibz opened this issue Sep 23, 2016 · 2 comments

Comments

@skibz
Copy link
Contributor

skibz commented Sep 23, 2016

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 one
api.addRequestTransform(async function(request) {
    var now = crypto.createHash('sha256').update(Date.now()).digest()
    request.headers['x-message'] = now.toString('hex')
    request.headers['x-signature'] = await sign(Buffer.from(privatekey, 'hex'), now)
})
// approach two
api.addRequestTransform(function(request) {
    var now = 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!

@skellock
Copy link
Contributor

Interesting. Seems reasonable.

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.

Hmm. I like where this is going.

@skibz
Copy link
Contributor Author

skibz commented Feb 6, 2017

closed by #31

@skibz skibz closed this as completed Feb 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants