legible
legible copied to clipboard
Allow response.text() don't always assume json
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
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).
Yep. I know. Trying to think of a good way to handle it.
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
`
}
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!