Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extract serializeAndPush into a util exported by this add-on
- Loading branch information
1 parent
10590b7
commit 67dbe8a
Showing
3 changed files
with
22 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import memberAction from './utils/member-action'; | ||
import collectionAction from './utils/collection-action'; | ||
import serializeAndPush from './utils/serialize-and-push'; | ||
|
||
export const classOp = collectionAction; | ||
export const instanceOp = memberAction; | ||
|
||
export { collectionAction, memberAction }; | ||
export { collectionAction, memberAction, serializeAndPush }; |
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,19 @@ | ||
import { isArray } from '@ember/array'; | ||
import { _getModelClass, _getModelName, _getStoreFromRecord } from './build-url'; | ||
|
||
export default function serializeAndPush(response) { | ||
const isJsonApi = response.jsonapi && response.jsonapi.version; | ||
if (!isJsonApi) { | ||
// eslint-disable-next-line no-console | ||
console.warn('serializeAndPush may only be used with a JSON API document. Ignoring response. Document must have a mandatory JSON API object. See https://jsonapi.org/format/#document-jsonapi-object.'); | ||
return response; | ||
} | ||
|
||
const recordClass = _getModelClass(this); | ||
const modelName = _getModelName(recordClass); | ||
const store = _getStoreFromRecord(this); | ||
const serializer = store.serializerFor(modelName); | ||
const normalized = isArray(response.data) ? serializer.normalizeArrayResponse(store, recordClass, response) : | ||
serializer.normalizeSingleResponse(store, recordClass, response); | ||
return this.store.push(normalized); | ||
} |
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