Skip to content

Commit

Permalink
[fix] move common logic to helper fn.
Browse files Browse the repository at this point in the history
  • Loading branch information
legander committed Apr 1, 2020
1 parent 47d1a4e commit 429a5b2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/aid.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function parseContentType (str = '') {
}
}

function getContentTypeValue (str = '') {
return str.split(';')[0]
}

function isBlacklistedHeader (headerName = '') {
const HEADERS_BLACKLIST = ['Content-Length']
const [name] = headerName.split(';')
Expand All @@ -59,5 +63,6 @@ module.exports = {
isString,
extrinsic,
nought,
parseContentType
parseContentType,
getContentTypeValue
}
4 changes: 2 additions & 2 deletions src/parse/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const headers = require('./headers')
const postData = require('./postData')
const queryString = require('./queryString')
const state = require('./state/request')
const { emptyObject } = require('../aid')
const { emptyObject, getContentTypeValue } = require('../aid')

function request (node, spec) {
spec.method = node.method.toUpperCase()
Expand Down Expand Up @@ -41,7 +41,7 @@ function addBoundary (boundary, headers) {
if (headers.has('Content-Type')) {
const items = [...headers.get('Content-Type').values()]
const newItems = items.map(item => {
const value = item.value.split(';')[0]
const value = getContentTypeValue(item.value)
if (value === 'multipart/form-data') {
return { value: `${value}; boundary=${boundary}` }
}
Expand Down
3 changes: 2 additions & 1 deletion src/render/post/structured.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const multipart = require('./multipart')
const url = require('./url')
const { UnrecognizedError } = require('../../error')
const { getContentTypeValue } = require('../../aid')

function structured (spec) {
switch (spec.post.type.split(';')[0]) {
switch (getContentTypeValue(spec.post.type)) {
case 'application/x-www-form-urlencoded':
return url(spec)
case 'multipart/form-data':
Expand Down
4 changes: 2 additions & 2 deletions src/validate/postData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const params = require('./params')
const { empty, emptyObject, seralizeURLSearchParams } = require('../aid')
const { empty, emptyObject, seralizeURLSearchParams, getContentTypeValue } = require('../aid')
const { InvalidArchiveError } = require('../error')

/*
Expand Down Expand Up @@ -76,7 +76,7 @@ function validate (node, i) {
![
'application/x-www-form-urlencoded',
'multipart/form-data'
].includes(node.mimeType.split(';')[0])
].includes(getContentTypeValue(node.mimeType))
) {
throw new InvalidArchiveError(
{ name: 'InvalidPostDataType' },
Expand Down
6 changes: 3 additions & 3 deletions src/validate/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const headers = require('./headers')
const isPlainObject = require('is-plain-object')
const postData = require('./postData')
const queryString = require('./queryString')
const { empty, emptyObject } = require('../aid')
const { empty, emptyObject, getContentTypeValue } = require('../aid')
const { absoluteUrl, variableStart } = require('../expression')
const { InvalidArchiveError } = require('../error')

Expand Down Expand Up @@ -116,8 +116,8 @@ function relation (node, i) {
node.headers.findIndex(findContentType) !== -1
) {
const header = node.headers.find(findContentType)
const headerType = header.value ? header.value.split(';')[0] : ''
const postType = node.postData.mimeType ? node.postData.mimeType.split(';')[0] : ''
const headerType = getContentTypeValue(header.value)
const postType = getContentTypeValue(node.postData.mimeType)
if (headerType !== postType) {
throw new InvalidArchiveError(
{ name: 'InconsistentContentType' },
Expand Down

0 comments on commit 429a5b2

Please sign in to comment.