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

JSONP Plugin #13

Open
blakeembrey opened this issue Apr 28, 2015 · 5 comments
Open

JSONP Plugin #13

blakeembrey opened this issue Apr 28, 2015 · 5 comments
Assignees

Comments

@blakeembrey
Copy link
Member

It should be really easy to write. Just a before and after request hook to receive the data coming back from the global function that will be exposed in the before hook.

@tmedwards
Copy link

As this issue is approaching two years old and doesn't seem to be moving, do you have any guidance on how this might be done?

A project I work on needs both JSONP and non-JSONP XHR support. We're currently using jQuery to handle our requests, but that's the only thing we use it for and we'd like to switch to something which doesn't include the kitchen sink. Popsicle seems like it would be a nice fit, but not currently having JSONP support—natively or via a plugin—makes it a non-starter.

@blakeembrey
Copy link
Member Author

It's pretty easy to do, I just haven't needed it myself since I prefer to enable CORS over supporting JSONP APIs. All you'd need to do is write a middleware function that writes a script tag and listens for onload of the script - then returns a Response object. I'm happy to write it if it's something you really need 😄

@tmedwards
Copy link

That would be great, honestly. We'd be much obliged if you would.

@tmedwards
Copy link

I hate to be that guy, but I was wondering if there'd been any progress on this?

@blakeembrey
Copy link
Member Author

blakeembrey commented Apr 25, 2017

No, sorry. It's fine to be that guy, I should have given an update. I haven't started working on it, I've been burnt out from open source and trying to pull back on my commitments overall. I'll try to get to it this week since I've left it long enough. Here's a good start:

const transport = {
  open (req) {
    return new Promise((resolve, reject) => {
      const key = `callback${Math.random().toString(36).substr(2)}`
      const script = document.createElement('script')

      script.type = 'text/javascript'
      script.src = `${req.url}?callback=${key}`
      script.onerror = () => {
        delete window[key]
        script.parentNode.removeChild(script)
        return reject(new Error())
      }
      
      document.body.appendChild(script)

      window[key] = (body) => {
        delete window[key]
        script.parentNode.removeChild(script)
        return resolve(new Response({ body }))
      }
    })
  },
  use: []
}

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