Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0uch committed Sep 1, 2015
1 parent 8a6c406 commit 9363598
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 65 deletions.
5 changes: 5 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog


##### 1.3.0 (2015-09-01)
- Polish: all serializer methods may return a Promise.
- Feature: Form serializer now accepts `application/x-www-form-urlencoded` or `multipart/form-data`.


##### 1.2.5 (2015-08-29)
- Polish: drop `node-fetch` as a dependency for testing, instead use `http` module directly.

Expand Down
124 changes: 60 additions & 64 deletions lib/serializer/serializers/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,73 @@ const formUrlEncodedType = 'application/x-www-form-urlencoded'
const formDataType = 'multipart/form-data'


function processRequest () {
throw new this.errors.UnsupportedError(`Form input only.`)
}


function parseUpdate () {
throw new this.errors.UnsupportedError(`Can not update records.`)
}


function parseCreate (context) {
const { request: { meta, payload, type } } = context
const { keys, recordTypes, options, castValue } = this
const fields = recordTypes[type]
const busboy = new Busboy({ headers: meta })
const bufferStream = new stream.PassThrough()
const record = {}

return new Promise(resolve => {
busboy.on('file', (field, file, filename) => {
const fieldDefinition = fields[field] || {}
const fieldIsArray = fieldDefinition[keys.isArray]
const chunks = []

if (fieldIsArray && !(field in record)) record[field] = []

file.on('data', chunk => chunks.push(chunk))
file.on('end', () => {
const data = Buffer.concat(chunks)
data.filename = filename
if (fieldIsArray) {
record[field].push(data)
return
}
record[field] = data
function inherit (Serializer) {
return class FormSerializer extends Serializer {

processRequest () {
throw new this.errors.UnsupportedError(`Form input only.`)
}

parseCreate (context) {
const { request: { meta, payload, type } } = context
const { keys, recordTypes, options, castValue } = this
const fields = recordTypes[type]
const busboy = new Busboy({ headers: meta })
const bufferStream = new stream.PassThrough()
const record = {}

return new Promise(resolve => {
busboy.on('file', (field, file, filename) => {
const fieldDefinition = fields[field] || {}
const fieldIsArray = fieldDefinition[keys.isArray]
const chunks = []

if (fieldIsArray && !(field in record)) record[field] = []

file.on('data', chunk => chunks.push(chunk))
file.on('end', () => {
const data = Buffer.concat(chunks)
data.filename = filename
if (fieldIsArray) {
record[field].push(data)
return
}
record[field] = data
})
})

busboy.on('field', (field, value) => {
const fieldDefinition = fields[field] || {}
const fieldType = fieldDefinition[keys.type]
const fieldIsArray = fieldDefinition[keys.isArray]

if (fieldIsArray) {
if (!(field in record)) record[field] = []
record[field].push(castValue(value, fieldType, options))
return
}

record[field] = castValue(value, fieldType, options)
})

busboy.on('finish', () => resolve([ record ]))

bufferStream.end(payload)
bufferStream.pipe(busboy)
})
})

busboy.on('field', (field, value) => {
const fieldDefinition = fields[field] || {}
const fieldType = fieldDefinition[keys.type]
const fieldIsArray = fieldDefinition[keys.isArray]

if (fieldIsArray) {
if (!(field in record)) record[field] = []
record[field].push(castValue(value, fieldType, options))
return
}

record[field] = castValue(value, fieldType, options)
})
}

busboy.on('finish', () => resolve([ record ]))
parseUpdate () {
throw new this.errors.UnsupportedError(`Can not update records.`)
}

bufferStream.end(payload)
bufferStream.pipe(busboy)
})
}
}


export const formUrlEncoded = Serializer => Object.assign(
class FormUrlEncodedSerializer extends Serializer {
processRequest () { processRequest.call(this) }
parseCreate () { return parseCreate.apply(this, arguments) }
parseUpdate () { parseUpdate.call(this) }
}, { id: formUrlEncodedType })
inherit(Serializer), { id: formUrlEncodedType })


export const formData = Serializer => Object.assign(
class FormDataSerializer extends Serializer {
processRequest () { processRequest.call(this) }
parseCreate () { return parseCreate.apply(this, arguments) }
parseUpdate () { parseUpdate.call(this) }
}, { id: formDataType })
inherit(Serializer), { id: formDataType })
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune",
"description": "High-level I/O for web applications.",
"version": "1.2.5",
"version": "1.3.0",
"license": "MIT",
"author": {
"email": "0x8890@airmail.cc",
Expand Down

0 comments on commit 9363598

Please sign in to comment.