Skip to content

Commit

Permalink
allow context to be mutated in parse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0uch committed Sep 5, 2015
1 parent 7ece364 commit 5a00450
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

##### 1.3.3 (2015-09-05)
- Polish: add JSON parsing error, disallow empty payload in default serializer for create and update.
- Fix: allow context to be mutated in `parseUpdate` and `parseCreate`.


##### 1.3.2 (2015-09-03)
Expand Down
20 changes: 12 additions & 8 deletions lib/dispatch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import * as updateHelpers from './update_helpers'
*/
export default function (context) {
const { adapter, serializer, recordTypes, transforms } = this
let records

const { type, meta } = context.request
const transform = transforms[type]
const fields = recordTypes[type]
const links = Object.keys(fields)
.filter(field => fields[field][linkKey])

const updates = {}

let transaction
let records
let type
let meta
let transform
let fields
let links

return serializer.parseCreate(context)

Expand All @@ -44,6 +43,11 @@ export default function (context) {
throw new BadRequestError(
`There are no valid records in the request.`)

; ({ request: { type, meta } } = context)
transform = transforms[type]
fields = recordTypes[type]
links = Object.keys(fields).filter(field => fields[field][linkKey])

// Delete denormalized inverse fields.
for (let field in fields)
if (fields[field][denormalizedInverseKey])
Expand Down
17 changes: 11 additions & 6 deletions lib/dispatch/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export default function (context) {
// Keyed by update, valued by hash of linked records.
const linkedMap = new WeakMap()

const { request: { type, meta } } = context
const fields = recordTypes[type]
const transform = transforms[type]
const links = Object.keys(fields)
.filter(field => fields[field][linkKey])

const relatedUpdates = {}
const transformedUpdates = []

let transaction
let updates
let type
let meta
let fields
let transform
let links

return serializer.parseUpdate(context)

Expand All @@ -50,6 +50,11 @@ export default function (context) {

validateUpdates(updates)

; ({ request: { type, meta } } = context)
fields = recordTypes[type]
transform = transforms[type]
links = Object.keys(fields).filter(field => fields[field][linkKey])

// Delete denormalized inverse fields, can't be updated.
for (let field in fields)
if (fields[field][denormalizedInverseKey])
Expand Down
4 changes: 2 additions & 2 deletions lib/serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class Serializer {
/**
* Parse a request payload for creating records. This method should return
* an array of records as expected by calling the `adapter.create` method.
* It should not mutate the context object.
* It may also mutate the context object.
*
* @param {Object} context
* @return {Promise|Object[]}
Expand All @@ -123,7 +123,7 @@ export default class Serializer {
/**
* Parse a request payload for updating records. This method should return
* an array of updates as expected by calling the `adapter.update` method.
* It should not mutate the context object.
* It may also mutate the context object.
*
* @param {Object} context
* @return {Promise|Object[]}
Expand Down
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.3.3",
"version": "1.3.4",
"license": "MIT",
"author": {
"email": "[email protected]",
Expand Down

0 comments on commit 5a00450

Please sign in to comment.