Move jsonWithDefault to a util
This commit is contained in:
parent
c474b1f652
commit
36fed0919d
|
@ -1,13 +1,9 @@
|
||||||
import Service, { inject as service } from '@ember/service';
|
import Service, { inject as service } from '@ember/service';
|
||||||
import { computed } from '@ember/object';
|
import { computed } from '@ember/object';
|
||||||
import { copy } from '@ember/object/internals';
|
|
||||||
import PromiseObject from '../utils/classes/promise-object';
|
import PromiseObject from '../utils/classes/promise-object';
|
||||||
import PromiseArray from '../utils/classes/promise-array';
|
import PromiseArray from '../utils/classes/promise-array';
|
||||||
import { namespace } from '../adapters/application';
|
import { namespace } from '../adapters/application';
|
||||||
|
import jsonWithDefault from '../utils/json-with-default';
|
||||||
// When the request isn't ok (e.g., forbidden) handle gracefully
|
|
||||||
const jsonWithDefault = defaultResponse => res =>
|
|
||||||
res.ok ? res.json() : copy(defaultResponse, true);
|
|
||||||
|
|
||||||
export default Service.extend({
|
export default Service.extend({
|
||||||
token: service(),
|
token: service(),
|
||||||
|
|
10
ui/app/utils/json-with-default.js
Normal file
10
ui/app/utils/json-with-default.js
Normal 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;
|
Loading…
Reference in a new issue