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

Within an after function, how can I discover the actual route used? #22

Open
biofractal opened this issue Aug 20, 2015 · 6 comments
Open

Comments

@biofractal
Copy link
Contributor

I am attempting to add the ability to mock out calls to resource.js. When a call is made to a resource.js end-point, the actual call is swapped out for an equivalent call to a mock url (apiary blueprint in my case).

I am doing this in the following way:

before:->
    (req, res, next) ->
        req.skipResource = true;
        next()

after:(baseUrl)->
    (req, res, next) ->
        resourceUrl = res.resource.route //<- Problem is Here
        options =
            method: "#{req.method}"
            uri:"#{baseUrl}/#{resourceUrl}"

        request options, req.headers, (err, response, data)->
            res.json data

The before function prevents resource.js from processing the call. The after function composes the mock-url and calls it returning any results. The mock-url consists of two parts, the baseUrl (the static part of the url pointing to the mock api) and the resourceUrl, the actual route that resource.js would have used if it had been allowed to process.

The problem I have is getting hold of the resourceUrl component. The code above sees me trying the res.resource.route but this is undefined. I think this would be wrong in any case as I really need the final route resource.js would be using, including any ids, filters etc.

Do you have any idea if this route is available currently? If not, some pointers to the best place to store this route for each call so that it is available to the after function would be greatly appreciated.

Thanks

@biofractal biofractal changed the title How can I discover the final route used in an after function Within and after function, how can I discover the actual route used? Aug 20, 2015
@biofractal biofractal changed the title Within and after function, how can I discover the actual route used? Within and after function, how can I discover the actual route used? Aug 20, 2015
@biofractal biofractal changed the title Within and after function, how can I discover the actual route used? Within an after function, how can I discover the actual route used? Aug 20, 2015
@zackurben
Copy link
Contributor

Are you looking for the ResourceJS path, or the literal url used?

ResourceJS: /resource/:resourceId
Literal: /resource/123456789

@biofractal
Copy link
Contributor Author

I am after the literal

Literal: /resource/123456789

@zackurben
Copy link
Contributor

Hmm, since ResourceJS build for express, its possibly you could still access the url by: req.originalUrl (express dependent) or req.url (node http dependent)..

I think originalUrl is what you're looking for, but I haven't actually tested it. Let me know if that works for you.

@biofractal
Copy link
Contributor Author

Unfortunately using the express urls doesn't work. This is because I am not actually calling into resource.js using express instead I am using your cool internal method calls. Here is a partial code snippet to show this:

router.get '/callback/:key', (req, res, next) ->
    req.noResponse = true
    resj = req.app.resourcejs
    async.waterfall [
        (cb)->
            req.query = key : req.params.key
            resj['/api'].get.call this, req, res, ->
                console.log 'res.resource', res.resource
                return cb res.resource.status unless res.resource.status is 200
                cb null, res.resource.item[0]

So the actual express call here is ../callback/12345 and this is what I get when I log the req.originalUrl and req.url. The fact that I am calling resourcejs via the line resj['/api'].get.call this, req, res, -> does not affect the current express route.

I think I need to find a place in the resourcejs code where the actual url is know (the final url that contains the actual ids etc) and store this on the res that is passed into the after function. I will take a look at doing this but if you have any suggestions ...

Thanks

@zackurben
Copy link
Contributor

Ah I understand more now, however, the req.originalUrl should be correct any point inside the manual resourcejs call: console.log 'res.resource', res.resource

@biofractal
Copy link
Contributor Author

Unfortunately it would be too late to call out to the mock url by the time we got to console.log 'res.resource', res.resource. Also I want to apply a general process via a global after function that will mock out all the calls to any resource. Therefore I need to circumvent all the resourcejs processing and instead call my mock url via the after function so I can return the mock data rather than the actual resource. To call the mock URL I need the actual URL resource.js was about to use.

Perhaps I can somehow insert some code into your middleware feature, where the correct url will be available? I could then set the url onto the res and short circuit the rest of the process and proceed directly to the defined after function. I can't see how to do this mind you, so if you think this idea is worth a go then I would really appreciate some pointers.

Thanks for taking the trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants