Skip to content

Commit

Permalink
feat: Extract serializeAndPush into a util exported by this add-on
Browse files Browse the repository at this point in the history
  • Loading branch information
iezer authored and mike-north committed Oct 31, 2018
1 parent 10590b7 commit 67dbe8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
3 changes: 2 additions & 1 deletion addon/index.js
@@ -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 };
19 changes: 19 additions & 0 deletions addon/utils/serialize-and-push.js
@@ -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);
}
16 changes: 1 addition & 15 deletions tests/dummy/app/models/fruit.js
@@ -1,8 +1,7 @@
// BEGIN-SNIPPET fruit-model
import DS from 'ember-data';
import { memberAction, collectionAction } from 'ember-api-actions';
import { memberAction, collectionAction, serializeAndPush } from 'ember-api-actions';
import { assign } from '@ember/polyfills';
import { isArray } from '@ember/array';

const { attr, Model } = DS;

Expand All @@ -12,19 +11,6 @@ function mergeAttributes(attributes) {
return payload;
}

// This is an example of how to extract JSON API responses and push
// them into the store.
// TODO extract and export as part of this addon
function serializeAndPush(response) {
const recordClass = this.constructor;
const modelName = recordClass.modelName;
const { store } = 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);
}

export default Model.extend({
name: attr('string'),
ripen: memberAction({ path: 'doRipen' }),
Expand Down

0 comments on commit 67dbe8a

Please sign in to comment.