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

Allow response.text() don't always assume json #11

Open
zackify opened this issue Dec 9, 2016 · 4 comments
Open

Allow response.text() don't always assume json #11

zackify opened this issue Dec 9, 2016 · 4 comments

Comments

@zackify
Copy link
Member

zackify commented Dec 9, 2016

What if we add this ability into the middleware?

import { observer } from 'legible'

const request => response => modify => {
  modify('response', response.text())
}

Something like that? Not sure how I feel about the syntax but we need some way to have access to every piece of the request and modify it

@raygesualdo
Copy link
Member

One thing we're going to have to consider is that some middleware will need to be chained (modifying the response after it's been transformed to JSON or text) and others will need to replace whatever functionality we have in place (such as actually doing response.text() or response.json() as these can only be fired once).

@zackify
Copy link
Member Author

zackify commented Dec 21, 2016

Yep. I know. Trying to think of a good way to handle it.

@raygesualdo
Copy link
Member

raygesualdo commented Dec 21, 2016

Piggybacking off of #17, we could add a property responseType with only two valid values (text and json) that handles the parsing of the response accordingly.

import { partial } from 'legible'

const twitter = {
  register: partial`
    url: https://api.twitter.com/register,
    method: POST
    headers: ${{ Authorization: localStorage.getItem('authorization') }}
    responseType: text
  `
}

@zackify
Copy link
Member Author

zackify commented Dec 21, 2016

And just default it to json. Sounds good. Now we can do middleware like this:

import { middleware } from 'legible'

const test = request => async function(next) {
  //request is the whole object from normalize
  //response is the response obviously. Call next to get it

  let response = await next(request)
  console.log(response)
}

const test = request => next => {

  //modify the request
  next({url: 'http://blah.com', options: request.options })
  
}

middleware.add(test)

I like that and it's simple!

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