Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions addon/utils/serialize-and-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import SerializerRegistry from 'ember-data/types/registries/serializer';
import { CollectionResourceDoc, Document as JSONApiDoc, DocWithData, SingleResourceDoc } from 'jsonapi-typescript';
import { _getModelClass, _getModelName, _getStoreFromRecord } from './build-url';

function isJsonApi(raw: any): raw is JSONApiDoc {
return raw.jsonapi && raw.jsonapi.version;
}
function isDocWithData(doc: any): doc is DocWithData {
return isJsonApi(doc) && ['object', 'array'].indexOf(typeOf((doc as DocWithData).data)) >= 0;
function isValidResponse(doc: any): doc is DocWithData {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I don't understand typescript, but why isn't the return type boolean?

// If the document has this signature we can trust what we're about to receive
if (doc.jsonapi && doc.jsonapi.version) {
return true
}

console.debug("Received a repsonse that doesn't have a /jsonapi/version property")

// Don't even bother if the data propery doesn't exist
return typeof response.data !== 'undefined';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be doc.data?

}

export default function serializeAndPush(this: Model, response: any) {
if (!isDocWithData(response)) {
if (!isValidResponse(response)) {
// tslint:disable-next-line:no-console
console.warn(
'serializeAndPush may only be used with a JSON API document. Ignoring response. ' +
Expand Down