Move jsonWithDefault to a util

This commit is contained in:
Michael Lange 2018-11-01 22:07:58 -07:00
parent c474b1f652
commit 36fed0919d
2 changed files with 11 additions and 5 deletions

View File

@ -1,13 +1,9 @@
import Service, { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { copy } from '@ember/object/internals';
import PromiseObject from '../utils/classes/promise-object';
import PromiseArray from '../utils/classes/promise-array';
import { namespace } from '../adapters/application';
// When the request isn't ok (e.g., forbidden) handle gracefully
const jsonWithDefault = defaultResponse => res =>
res.ok ? res.json() : copy(defaultResponse, true);
import jsonWithDefault from '../utils/json-with-default';
export default Service.extend({
token: service(),

View File

@ -0,0 +1,10 @@
import { copy } from '@ember/object/internals';
// Used with fetch.
// Fetch only goes into the promise catch if there is a network error.
// This means that handling a 4xx or 5xx error is the responsibility
// of the developer.
const jsonWithDefault = defaultResponse => res =>
res.ok ? res.json() : copy(defaultResponse, true);
export default jsonWithDefault;