Skip to content

Commit

Permalink
Add support to receive x-www-form-urlencoded response
Browse files Browse the repository at this point in the history
  • Loading branch information
zellwk committed Dec 24, 2021
1 parent 554a402 commit d5d526a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/handleResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,24 @@ const formatOutput = (response, body) => {
: Promise.reject(returnValue)
}

const parseResponse = (response, type) => {
// TODO: Create test for formData format
async function parseResponse (response, type) {
// Parse form data into JavaScript object
if (type === 'formData') {
let body = await response.text()
if (typeof URLSearchParams !== 'undefined') {
const query = new URLSearchParams(body)
body = Object.fromEntries(query)
} else {
const querystring = require('querystring')
body = querystring.parse(body)
}
return formatOutput(response, body)
}

// We use bracket notation to allow multiple types to be parsed at the same time.
return response[type]().then(body => formatOutput(response, body))
const body = await response[type]()
return formatOutput(response, body)
}

export const handleResponse = (response, options) => {
Expand All @@ -57,6 +72,9 @@ export const handleResponse = (response, options) => {
if (type.includes('json')) return parseResponse(response, 'json')
if (type.includes('text')) return parseResponse(response, 'text')
if (type.includes('image')) return parseResponse(response, 'blob')
if (type.includes('x-www-form-urlencoded')) {
return parseResponse(response, 'formData')
}

// Need to check for FormData, Blob and ArrayBuffer content types
throw new Error(`zlFetch does not support content-type ${type} yet`)
Expand Down
11 changes: 11 additions & 0 deletions test/helpers/createServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bodyParser from 'body-parser'
import express from 'express'
import querystring from 'querystring'

const app = express()

Expand Down Expand Up @@ -40,6 +41,16 @@ app.use('/text-error', (req, res) => {
res.status(400).send('An error message')
})

// x-www-form-urlencoded
app.use('/x-www-form-urlencoded', (req, res) => {
res.format({
'application/x-www-form-urlencoded' () {
const data = { message: 'Error message' }
res.send(querystring.encode(data))
}
})
})

// Forms
app.use('/forms', (req, res) => {
res.send(req.body)
Expand Down
10 changes: 6 additions & 4 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ describe('Sending requests', () => {

expect(response.body.message).toBe('good game')
})

// TODO: Requires setting to multipart/form-data
// Or possibly leave empty... and let fetch do its job...?
it.todo('Sending FormData')
})

describe('Response Types', () => {
Expand All @@ -81,6 +77,12 @@ describe('Response Types', () => {
expect(error.body).toBe('An error message')
}
})

it('Should handle x-www-urlencoded', async () => {
const response = await zlFetch(`${rootendpoint}/x-www-form-urlencoded
`)
expect(response.body.message).toBe('Error message')
})
})

it('Test sending to Github', async () => {
Expand Down

0 comments on commit d5d526a

Please sign in to comment.