-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from fortunejs/form-serializer
Form serializer
- Loading branch information
Showing
14 changed files
with
233 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Busboy from 'busboy' | ||
import stream from 'stream' | ||
|
||
|
||
const formUrlEncodedType = 'application/x-www-form-urlencoded' | ||
const formDataType = 'multipart/form-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) | ||
}) | ||
} | ||
|
||
parseUpdate () { | ||
throw new this.errors.UnsupportedError(`Can not update records.`) | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
export const formUrlEncoded = Serializer => Object.assign( | ||
inherit(Serializer), { id: formUrlEncodedType }) | ||
|
||
|
||
export const formData = Serializer => Object.assign( | ||
inherit(Serializer), { id: formDataType }) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.