legible icon indicating copy to clipboard operation
legible copied to clipboard

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

Open zackify opened this issue 8 years ago • 4 comments
trafficstars

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

zackify avatar Dec 09 '16 21:12 zackify

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).

raygesualdo avatar Dec 21 '16 02:12 raygesualdo

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

zackify avatar Dec 21 '16 02:12 zackify

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
  `
}

raygesualdo avatar Dec 21 '16 18:12 raygesualdo

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!

zackify avatar Dec 21 '16 18:12 zackify