From 93635989b1f6c2d7046b0ae5c7491f72bfd3560d Mon Sep 17 00:00:00 2001 From: Dali Zheng Date: Tue, 1 Sep 2015 09:40:18 +0200 Subject: [PATCH] small refactor --- doc/CHANGELOG.md | 5 + lib/serializer/serializers/form/index.js | 124 +++++++++++------------ package.json | 2 +- 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/doc/CHANGELOG.md b/doc/CHANGELOG.md index 762a3da16..1d713745f 100644 --- a/doc/CHANGELOG.md +++ b/doc/CHANGELOG.md @@ -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. diff --git a/lib/serializer/serializers/form/index.js b/lib/serializer/serializers/form/index.js index 7b4e0aab1..2e88cb09f 100644 --- a/lib/serializer/serializers/form/index.js +++ b/lib/serializer/serializers/form/index.js @@ -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 }) diff --git a/package.json b/package.json index a538b4855..9548f4ef7 100644 --- a/package.json +++ b/package.json @@ -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",