Skip to content

Commit

Permalink
Accept URL encoded form data instead of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
paulshryock committed Jul 26, 2024
1 parent 409ded8 commit 2d5181f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/routes/api/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const DEFAULT_HEADERS = {
} as const

export class Handler {
#allowedContentTypes = ['application/json']
#allowedContentType = 'application/x-www-form-urlencoded'
#allowedMethods = ['POST']
#storage: RecordStorage

Expand Down Expand Up @@ -103,7 +103,7 @@ export class Handler {
if (!this.#validateContentType(request))
return new Response(
JSON.stringify({
allowedContentType: 'application/json',
allowedContentType: this.#allowedContentType,
contentType: request.headers.get('Content-Type'),
error: 'Invalid Content-Type header.',
status: 400,
Expand All @@ -118,10 +118,17 @@ export class Handler {
},
)

let body: Record<(typeof this.requiredFields)[number], string>
let body: Record<string, string>

try {
body = await request.json()
body = [...(await request.formData()).entries()].reduce(
(record, [key, value]) => {
return { ...record, [key]: value }
},
{},
)

if (Object.keys(body).length === 0) throw new Error('invalid body')
} catch (error) {
console.error(error)
return this.#getResponse(this.responseData.invalidBody)
Expand Down Expand Up @@ -208,9 +215,7 @@ export class Handler {
* @since unreleased
*/
#validateContentType(request: Request): boolean {
return this.#allowedContentTypes.includes(
`${request.headers.get('Content-Type')}`,
)
return this.#allowedContentType === request.headers.get('Content-Type')
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/routes/api/contact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe(PATH, () => {
const headers = new Headers({ Origin: site.origin })

describe.each([
'application/x-www-form-urlencoded',
'application/json',
'multipart/form-data',
'text/html',
'text/plain',
Expand Down Expand Up @@ -119,10 +119,10 @@ describe(PATH, () => {
)
})

describe('when Content-Type header is application/json', () => {
headers.set('Content-Type', 'application/json')
describe('when Content-Type header is application/x-www-form-urlencoded', () => {
headers.set('Content-Type', 'application/x-www-form-urlencoded')

describe('when a valid json body is not provided', () => {
describe('when a body is not provided', () => {
const request = new Request(ROUTE, { headers, method })
const consoleError = console.error

Expand Down Expand Up @@ -195,7 +195,7 @@ describe(PATH, () => {
})

describe('when all required fields are valid', () => {
const body = JSON.stringify({
const body = new URLSearchParams({
email: '[email protected]',
message: 'This is an example message.',
name: 'Example',
Expand Down

0 comments on commit 2d5181f

Please sign in to comment.