@warp-drive/legacy 5.9.0-alpha.2 → 5.9.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/declarations/adapter/-private/build-url-mixin.d.ts +156 -0
- package/declarations/adapter/error.d.ts +385 -169
- package/declarations/adapter/json-api.d.ts +12 -1
- package/declarations/adapter/rest.d.ts +74 -0
- package/declarations/adapter.d.ts +6 -0
- package/declarations/compat/builders/find-all.d.ts +0 -1
- package/declarations/compat/extensions.d.ts +40 -0
- package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +7 -0
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +4 -0
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +7 -0
- package/declarations/compat/utils.d.ts +11 -0
- package/declarations/compat.d.ts +32 -0
- package/declarations/index.d.ts +142 -0
- package/declarations/model/-private/attr.d.ts +21 -0
- package/declarations/model/-private/belongs-to.d.ts +21 -0
- package/declarations/model/-private/hooks.d.ts +26 -0
- package/declarations/model/-private/model.d.ts +55 -4
- package/declarations/model/-private/promise-many-array.d.ts +25 -1
- package/declarations/model/-private/schema-provider.d.ts +11 -1
- package/declarations/model/migration-support.d.ts +31 -5
- package/declarations/model-fragments/extensions/fragment-array.d.ts +38 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +35 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +7 -0
- package/declarations/model-fragments/index.d.ts +8 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +6 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +30 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +18 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +8 -0
- package/declarations/serializer/-private/transforms/boolean.d.ts +13 -0
- package/declarations/serializer/-private/transforms/date.d.ts +15 -1
- package/declarations/serializer/-private/transforms/number.d.ts +14 -1
- package/declarations/serializer/-private/transforms/string.d.ts +14 -1
- package/declarations/serializer/-private/utils.d.ts +24 -0
- package/declarations/serializer/json-api.d.ts +1 -1
- package/declarations/serializer.d.ts +4 -6
- package/declarations/store/-private.d.ts +2 -2
- package/declarations/store.d.ts +7 -0
- package/dist/adapter/error.js +386 -241
- package/dist/adapter/json-api.js +12 -0
- package/dist/adapter/rest.js +39 -3
- package/dist/adapter.js +23 -26
- package/dist/compat/builders.js +0 -1
- package/dist/compat/extensions.js +80 -3
- package/dist/compat/utils.js +14 -0
- package/dist/compat.js +17 -0
- package/dist/{errors-Cz5KrzBk.js → errors-DZKA206g.js} +24 -1
- package/dist/{hooks-D6diaM34.js → hooks-BvOqJBei.js} +33 -2
- package/dist/index.js +168 -13
- package/dist/{json-ChdEfB0X.js → json-DU8m1pGf.js} +83 -6
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +15 -2
- package/dist/{unpkg/dev-deprecated/model-for-CqXsIKws.js → model-for-CXkJw59V.js} +77 -1
- package/dist/model-fragments.js +18 -2
- package/dist/model.js +12 -8
- package/dist/{schema-provider-DJCV_6AF.js → schema-provider-DtPOp0d7.js} +93 -6
- package/dist/serializer/json-api.js +8 -3
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +3 -4
- package/dist/serializer/transform.js +51 -3
- package/dist/serializer.js +4 -7
- package/dist/store.js +13 -7
- package/dist/unpkg/dev/adapter/error.js +386 -241
- package/dist/unpkg/dev/adapter/json-api.js +13 -1
- package/dist/unpkg/dev/adapter/rest.js +39 -3
- package/dist/unpkg/dev/adapter.js +23 -26
- package/dist/unpkg/dev/compat/builders.js +0 -1
- package/dist/unpkg/dev/compat/extensions.js +80 -3
- package/dist/unpkg/dev/compat/utils.js +14 -0
- package/dist/unpkg/dev/compat.js +34 -1
- package/dist/unpkg/dev/{errors-DmGGJr3T.js → errors-DngRU-If.js} +24 -1
- package/dist/unpkg/dev/{hooks-CkYiE6Ud.js → hooks-UuiLxWxS.js} +33 -2
- package/dist/unpkg/dev/index.js +171 -13
- package/dist/unpkg/dev/json-K2Y9z6MN.js +1333 -0
- package/dist/unpkg/dev/model/-private.js +1 -1
- package/dist/unpkg/dev/model/migration-support.js +16 -2
- package/dist/unpkg/{prod-deprecated/model-for-CqXsIKws.js → dev/model-for-CXkJw59V.js} +77 -1
- package/dist/unpkg/dev/model-fragments.js +18 -2
- package/dist/unpkg/dev/model.js +12 -8
- package/dist/unpkg/dev/{schema-provider-DDVYxmUV.js → schema-provider-0D7f-QOn.js} +92 -6
- package/dist/unpkg/dev/serializer/json-api.js +9 -4
- package/dist/unpkg/dev/serializer/json.js +1 -1
- package/dist/unpkg/dev/serializer/rest.js +3 -4
- package/dist/unpkg/dev/serializer/transform.js +51 -3
- package/dist/unpkg/dev/serializer.js +4 -7
- package/dist/unpkg/dev/store.js +21 -7
- package/dist/unpkg/dev-deprecated/adapter/error.js +386 -241
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +13 -1
- package/dist/unpkg/dev-deprecated/adapter/rest.js +39 -3
- package/dist/unpkg/dev-deprecated/adapter.js +23 -26
- package/dist/unpkg/dev-deprecated/compat/builders.js +0 -1
- package/dist/unpkg/dev-deprecated/compat/extensions.js +80 -3
- package/dist/unpkg/dev-deprecated/compat/utils.js +14 -0
- package/dist/unpkg/dev-deprecated/compat.js +34 -1
- package/dist/unpkg/dev-deprecated/{errors-Spt6ubMd.js → errors-Bzidntpi.js} +24 -1
- package/dist/unpkg/dev-deprecated/{hooks-DOXegvhL.js → hooks-DVXhMW14.js} +33 -2
- package/dist/unpkg/dev-deprecated/index.js +171 -13
- package/dist/unpkg/{prod-deprecated/json-BWrZ5546.js → dev-deprecated/json-K2Y9z6MN.js} +96 -6
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -1
- package/dist/unpkg/dev-deprecated/model/migration-support.js +16 -2
- package/dist/unpkg/{dev/model-for-CqXsIKws.js → dev-deprecated/model-for-CXkJw59V.js} +77 -1
- package/dist/unpkg/dev-deprecated/model-fragments.js +18 -2
- package/dist/unpkg/dev-deprecated/model.js +12 -8
- package/dist/unpkg/dev-deprecated/{schema-provider-BP6_8N-V.js → schema-provider-ZwO5_fZw.js} +84 -3
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +9 -4
- package/dist/unpkg/dev-deprecated/serializer/json.js +1 -1
- package/dist/unpkg/dev-deprecated/serializer/rest.js +3 -4
- package/dist/unpkg/dev-deprecated/serializer/transform.js +51 -3
- package/dist/unpkg/dev-deprecated/serializer.js +4 -7
- package/dist/unpkg/dev-deprecated/store.js +21 -7
- package/dist/unpkg/prod/adapter/error.js +386 -241
- package/dist/unpkg/prod/adapter/json-api.js +13 -1
- package/dist/unpkg/prod/adapter/rest.js +39 -3
- package/dist/unpkg/prod/adapter.js +23 -26
- package/dist/unpkg/prod/compat/builders.js +0 -1
- package/dist/unpkg/prod/compat/extensions.js +70 -3
- package/dist/unpkg/prod/compat/utils.js +14 -0
- package/dist/unpkg/prod/compat.js +17 -0
- package/dist/unpkg/prod/{errors-BGVFCBmi.js → errors-B-PWeYlW.js} +24 -1
- package/dist/unpkg/prod/{hooks-BztVA_x0.js → hooks-Bx-zBziU.js} +32 -1
- package/dist/unpkg/prod/index.js +171 -5
- package/dist/unpkg/{dev/json-Cu1LNgmQ.js → prod/json-Qewu-UN9.js} +63 -19
- package/dist/unpkg/prod/model/-private.js +1 -1
- package/dist/unpkg/prod/model/migration-support.js +16 -2
- package/dist/unpkg/prod/model-for-CXkJw59V.js +297 -0
- package/dist/unpkg/prod/model-fragments.js +18 -2
- package/dist/unpkg/prod/model.js +12 -8
- package/dist/unpkg/prod/{schema-provider-DJtD_8jZ.js → schema-provider-BgRqk8i9.js} +96 -6
- package/dist/unpkg/prod/serializer/json-api.js +9 -4
- package/dist/unpkg/prod/serializer/json.js +1 -1
- package/dist/unpkg/prod/serializer/rest.js +3 -4
- package/dist/unpkg/prod/serializer/transform.js +51 -3
- package/dist/unpkg/prod/serializer.js +4 -7
- package/dist/unpkg/prod/store.js +21 -7
- package/dist/unpkg/prod-deprecated/adapter/error.js +386 -241
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +13 -1
- package/dist/unpkg/prod-deprecated/adapter/rest.js +39 -3
- package/dist/unpkg/prod-deprecated/adapter.js +23 -26
- package/dist/unpkg/prod-deprecated/compat/builders.js +0 -1
- package/dist/unpkg/prod-deprecated/compat/extensions.js +70 -3
- package/dist/unpkg/prod-deprecated/compat/utils.js +14 -0
- package/dist/unpkg/prod-deprecated/compat.js +17 -0
- package/dist/unpkg/prod-deprecated/{errors-CdDaK81x.js → errors-Dl2J9d1D.js} +24 -1
- package/dist/unpkg/prod-deprecated/{hooks-yId87yyG.js → hooks-Cm60wWTf.js} +32 -1
- package/dist/unpkg/prod-deprecated/index.js +171 -5
- package/dist/unpkg/{dev-deprecated/json-Cu1LNgmQ.js → prod-deprecated/json-Qewu-UN9.js} +63 -19
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -1
- package/dist/unpkg/prod-deprecated/model/migration-support.js +16 -2
- package/dist/{model-for-CqXsIKws.js → unpkg/prod-deprecated/model-for-CXkJw59V.js} +77 -1
- package/dist/unpkg/prod-deprecated/model-fragments.js +18 -2
- package/dist/unpkg/prod-deprecated/model.js +12 -8
- package/dist/unpkg/prod-deprecated/{schema-provider-CjX55uSY.js → schema-provider-Dh6tRqVI.js} +79 -3
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +9 -4
- package/dist/unpkg/prod-deprecated/serializer/json.js +1 -1
- package/dist/unpkg/prod-deprecated/serializer/rest.js +3 -4
- package/dist/unpkg/prod-deprecated/serializer/transform.js +51 -3
- package/dist/unpkg/prod-deprecated/serializer.js +4 -7
- package/dist/unpkg/prod-deprecated/store.js +21 -7
- package/package.json +8 -8
- package/dist/unpkg/prod/json-BWrZ5546.js +0 -1243
- package/dist/unpkg/prod/model-for-CqXsIKws.js +0 -221
package/dist/adapter/error.js
CHANGED
|
@@ -5,70 +5,6 @@ import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
|
5
5
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
## Overview
|
|
10
|
-
|
|
11
|
-
:::danger
|
|
12
|
-
⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
|
|
13
|
-
If starting a new app or thinking of implementing a new adapter, consider writing a
|
|
14
|
-
{@link Handler} instead to be used with the {@link RequestManager}
|
|
15
|
-
:::
|
|
16
|
-
|
|
17
|
-
An `AdapterError` is used by an adapter to signal that an error occurred
|
|
18
|
-
during a request to an external API. It indicates a generic error, and
|
|
19
|
-
subclasses are used to indicate specific error states.
|
|
20
|
-
|
|
21
|
-
To create a custom error to signal a specific error state in communicating
|
|
22
|
-
with an external API, extend the `AdapterError`. For example, if the
|
|
23
|
-
external API exclusively used HTTP `503 Service Unavailable` to indicate
|
|
24
|
-
it was closed for maintenance:
|
|
25
|
-
|
|
26
|
-
```js [app/adapters/maintenance-error.js]
|
|
27
|
-
import AdapterError from '@warp-drive/legacy/adapter/error';
|
|
28
|
-
|
|
29
|
-
export default AdapterError.extend({ message: "Down for maintenance." });
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
This error would then be returned by an adapter's `handleResponse` method:
|
|
33
|
-
|
|
34
|
-
```js [app/adapters/application.js]
|
|
35
|
-
import JSONAPIAdapter from '@warp-drive/legacy/adapter/json-api';
|
|
36
|
-
import MaintenanceError from './maintenance-error';
|
|
37
|
-
|
|
38
|
-
export default class ApplicationAdapter extends JSONAPIAdapter {
|
|
39
|
-
handleResponse(status) {
|
|
40
|
-
if (503 === status) {
|
|
41
|
-
return new MaintenanceError();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return super.handleResponse(...arguments);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
And can then be detected in an application and used to send the user to an
|
|
50
|
-
`under-maintenance` route:
|
|
51
|
-
|
|
52
|
-
```js [app/routes/application.js]
|
|
53
|
-
import MaintenanceError from '../adapters/maintenance-error';
|
|
54
|
-
|
|
55
|
-
export default class ApplicationRoute extends Route {
|
|
56
|
-
actions: {
|
|
57
|
-
error(error, transition) {
|
|
58
|
-
if (error instanceof MaintenanceError) {
|
|
59
|
-
this.transitionTo('under-maintenance');
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ...other error handling logic
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
@class AdapterError
|
|
70
|
-
@public
|
|
71
|
-
*/
|
|
72
8
|
function _AdapterError(errors, message = 'Adapter operation failed') {
|
|
73
9
|
this.isAdapterError = true;
|
|
74
10
|
const error = Error.call(this, message);
|
|
@@ -90,9 +26,122 @@ function _AdapterError(errors, message = 'Adapter operation failed') {
|
|
|
90
26
|
detail: message
|
|
91
27
|
}];
|
|
92
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The shape of the errors thrown/returned by {@link AdapterError} and its subclasses.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The static interface shared by {@link AdapterError} and its subclasses,
|
|
36
|
+
* allowing further subclassing via {@link AdapterRequestErrorConstructor.extend | extend}.
|
|
37
|
+
*/
|
|
38
|
+
|
|
93
39
|
_AdapterError.prototype = Object.create(Error.prototype);
|
|
94
40
|
_AdapterError.prototype.code = 'AdapterError';
|
|
95
41
|
_AdapterError.extend = extendFn(_AdapterError);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The {@link AdapterRequestError} shape thrown by the {@link AdapterError} constructor.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* :::danger
|
|
49
|
+
* ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
|
|
50
|
+
* If starting a new app or thinking of implementing a new adapter, consider writing a
|
|
51
|
+
* {@link Handler} instead to be used with the {@link RequestManager}
|
|
52
|
+
* :::
|
|
53
|
+
*
|
|
54
|
+
* An `AdapterError` is used by an adapter to signal that an error occurred
|
|
55
|
+
* during a request to an external API. It indicates a generic error, and
|
|
56
|
+
* subclasses are used to indicate specific error states.
|
|
57
|
+
*
|
|
58
|
+
* To create a custom error to signal a specific error state in communicating
|
|
59
|
+
* with an external API, extend the `AdapterError`. For example, if the
|
|
60
|
+
* external API exclusively used HTTP `503 Service Unavailable` to indicate
|
|
61
|
+
* it was closed for maintenance:
|
|
62
|
+
*
|
|
63
|
+
* ```js [app/adapters/maintenance-error.js]
|
|
64
|
+
* import AdapterError from '@warp-drive/legacy/adapter/error';
|
|
65
|
+
*
|
|
66
|
+
* export default AdapterError.extend({ message: "Down for maintenance." });
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* This error would then be returned by an adapter's `handleResponse` method:
|
|
70
|
+
*
|
|
71
|
+
* ```js [app/adapters/application.js]
|
|
72
|
+
* import JSONAPIAdapter from '@warp-drive/legacy/adapter/json-api';
|
|
73
|
+
* import MaintenanceError from './maintenance-error';
|
|
74
|
+
*
|
|
75
|
+
* export default class ApplicationAdapter extends JSONAPIAdapter {
|
|
76
|
+
* handleResponse(status) {
|
|
77
|
+
* if (503 === status) {
|
|
78
|
+
* return new MaintenanceError();
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* return super.handleResponse(...arguments);
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* And can then be detected in an application and used to send the user to an
|
|
87
|
+
* `under-maintenance` route:
|
|
88
|
+
*
|
|
89
|
+
* ```js [app/routes/application.js]
|
|
90
|
+
* import MaintenanceError from '../adapters/maintenance-error';
|
|
91
|
+
*
|
|
92
|
+
* export default class ApplicationRoute extends Route {
|
|
93
|
+
* actions: {
|
|
94
|
+
* error(error, transition) {
|
|
95
|
+
* if (error instanceof MaintenanceError) {
|
|
96
|
+
* this.transitionTo('under-maintenance');
|
|
97
|
+
* return;
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
* // ...other error handling logic
|
|
101
|
+
* }
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* ### Signaling an Error Without Extending `AdapterError`
|
|
107
|
+
*
|
|
108
|
+
* Extending `AdapterError` (or one of its subclasses) is a convenience, not
|
|
109
|
+
* a requirement. WarpDrive only inspects an error for the {@link AdapterRequestError}
|
|
110
|
+
* shape: an `isAdapterError` flag, a `code`, and an {json:api}-formatted
|
|
111
|
+
* `errors` array. Any object satisfying that shape — including a plain
|
|
112
|
+
* `Error` with those properties attached — will be handled identically to
|
|
113
|
+
* an instance created via `new AdapterError()` or one of its subclasses.
|
|
114
|
+
*
|
|
115
|
+
* This is useful when you'd rather not introduce a class hierarchy, or
|
|
116
|
+
* when the error needs to be constructed from data you don't control
|
|
117
|
+
* (for example, re-throwing an error surfaced by a third-party library):
|
|
118
|
+
*
|
|
119
|
+
* ```js [app/adapters/application.js]
|
|
120
|
+
* import JSONAPIAdapter from '@warp-drive/legacy/adapter/json-api';
|
|
121
|
+
*
|
|
122
|
+
* export default class ApplicationAdapter extends JSONAPIAdapter {
|
|
123
|
+
* handleResponse(status, headers, payload) {
|
|
124
|
+
* if (status === 503) {
|
|
125
|
+
* const error = new Error('Down for maintenance.');
|
|
126
|
+
* error.isAdapterError = true;
|
|
127
|
+
* error.code = 'MaintenanceError';
|
|
128
|
+
* error.errors = [{ title: 'Service Unavailable', detail: 'Down for maintenance.' }];
|
|
129
|
+
* return error;
|
|
130
|
+
* }
|
|
131
|
+
*
|
|
132
|
+
* return super.handleResponse(status, headers, payload);
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* Because `code` is just a string you control, `error instanceof AdapterError`
|
|
138
|
+
* checks won't match a plain object built this way — consumers should
|
|
139
|
+
* instead branch on `error.isAdapterError && error.code === 'MaintenanceError'`,
|
|
140
|
+
* or on whichever of the {@link AdapterError} subclasses' `code` values
|
|
141
|
+
* (e.g. `'InvalidError'`, `'NotFoundError'`) the error's `code` matches.
|
|
142
|
+
*
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
96
145
|
const AdapterError = getOrSetGlobal('AdapterError', _AdapterError);
|
|
97
146
|
function extendFn(ErrorClass) {
|
|
98
147
|
return function ({
|
|
@@ -117,220 +166,316 @@ function extend(ParentErrorClass, defaultMessage) {
|
|
|
117
166
|
}
|
|
118
167
|
|
|
119
168
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
semantically correct or meaningful per the API. Usually, this means a
|
|
123
|
-
record failed some form of server-side validation. When a promise
|
|
124
|
-
from an adapter is rejected with a `InvalidError` the record will
|
|
125
|
-
transition to the `invalid` state and the errors will be set to the
|
|
126
|
-
`errors` property on the record.
|
|
127
|
-
|
|
128
|
-
For WarpDrive to correctly map errors to their corresponding
|
|
129
|
-
properties on the model, WarpDrive expects each error to be
|
|
130
|
-
a valid JSON-API error object with a `source/pointer` that matches
|
|
131
|
-
the property name. For example, if you had a Post model that
|
|
132
|
-
looked like this.
|
|
133
|
-
|
|
134
|
-
```js [app/models/post.js]
|
|
135
|
-
import { Model, attr } from '@warp-drive/legacy/model';
|
|
136
|
-
|
|
137
|
-
export default class PostModel extends Model {
|
|
138
|
-
@attr('string') title;
|
|
139
|
-
@attr('string') content;
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
To show an error from the server related to the `title` and
|
|
144
|
-
`content` properties your adapter could return a promise that
|
|
145
|
-
rejects with a `InvalidError` object that looks like this:
|
|
146
|
-
|
|
147
|
-
```js [app/adapters/post.js]
|
|
148
|
-
import RSVP from 'RSVP';
|
|
149
|
-
import RESTAdapter from '@warp-drive/legacy/adapter/rest';
|
|
150
|
-
import { InvalidError } from '@warp-drive/legacy/adapter/error';
|
|
151
|
-
|
|
152
|
-
export default class ApplicationAdapter extends RESTAdapter {
|
|
153
|
-
updateRecord() {
|
|
154
|
-
// Fictional adapter that always rejects
|
|
155
|
-
return RSVP.reject(new InvalidError([
|
|
156
|
-
{
|
|
157
|
-
detail: 'Must be unique',
|
|
158
|
-
source: { pointer: '/data/attributes/title' }
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
detail: 'Must not be blank',
|
|
162
|
-
source: { pointer: '/data/attributes/content'}
|
|
163
|
-
}
|
|
164
|
-
]));
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Your backend may use different property names for your records the
|
|
170
|
-
store will attempt to extract and normalize the errors using the
|
|
171
|
-
serializer's `extractErrors` method before the errors get added to
|
|
172
|
-
the model. As a result, it is safe for the `InvalidError` to
|
|
173
|
-
wrap the error payload unaltered.
|
|
174
|
-
|
|
175
|
-
@class InvalidError
|
|
176
|
-
@public
|
|
177
|
-
*/
|
|
169
|
+
* The {@link AdapterRequestError} shape thrown by the {@link InvalidError} constructor.
|
|
170
|
+
*/
|
|
178
171
|
// TODO @deprecate extractError documentation
|
|
179
172
|
|
|
173
|
+
/**
|
|
174
|
+
* An `InvalidError` is used by an adapter to signal that the external API
|
|
175
|
+
* was unable to process a request because the content was not semantically
|
|
176
|
+
* correct or meaningful per the API. Usually, this means a record failed
|
|
177
|
+
* some form of server-side validation. When a promise from an adapter is
|
|
178
|
+
* rejected with an `InvalidError` the record will transition to the
|
|
179
|
+
* `invalid` state and the errors will be set to the `errors` property on
|
|
180
|
+
* the record.
|
|
181
|
+
*
|
|
182
|
+
* For WarpDrive to correctly map errors to their corresponding properties
|
|
183
|
+
* on the model, WarpDrive expects each error to be a valid {json:api} error
|
|
184
|
+
* object with a `source.pointer` that matches the property name. For
|
|
185
|
+
* example, if you had a `Post` model that looked like this:
|
|
186
|
+
*
|
|
187
|
+
* ```js [app/models/post.js]
|
|
188
|
+
* import { Model, attr } from '@warp-drive/legacy/model';
|
|
189
|
+
*
|
|
190
|
+
* export default class PostModel extends Model {
|
|
191
|
+
* @attr('string') title;
|
|
192
|
+
* @attr('string') content;
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* To show an error from the server related to the `title` and `content`
|
|
197
|
+
* properties your adapter could return a promise that rejects with an
|
|
198
|
+
* `InvalidError` that looks like this:
|
|
199
|
+
*
|
|
200
|
+
* ```js [app/adapters/post.js]
|
|
201
|
+
* import RSVP from 'RSVP';
|
|
202
|
+
* import RESTAdapter from '@warp-drive/legacy/adapter/rest';
|
|
203
|
+
* import { InvalidError } from '@warp-drive/legacy/adapter/error';
|
|
204
|
+
*
|
|
205
|
+
* export default class ApplicationAdapter extends RESTAdapter {
|
|
206
|
+
* updateRecord() {
|
|
207
|
+
* // Fictional adapter that always rejects
|
|
208
|
+
* return RSVP.reject(new InvalidError([
|
|
209
|
+
* {
|
|
210
|
+
* detail: 'Must be unique',
|
|
211
|
+
* source: { pointer: '/data/attributes/title' }
|
|
212
|
+
* },
|
|
213
|
+
* {
|
|
214
|
+
* detail: 'Must not be blank',
|
|
215
|
+
* source: { pointer: '/data/attributes/content' }
|
|
216
|
+
* }
|
|
217
|
+
* ]));
|
|
218
|
+
* }
|
|
219
|
+
* }
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* Your backend may use different property names for your records; the
|
|
223
|
+
* store will attempt to extract and normalize the errors using the
|
|
224
|
+
* serializer's `extractErrors` method before the errors get added to the
|
|
225
|
+
* model. As a result, it is safe for the `InvalidError` to wrap the error
|
|
226
|
+
* payload unaltered.
|
|
227
|
+
*
|
|
228
|
+
* @public
|
|
229
|
+
*/
|
|
180
230
|
const InvalidError = getOrSetGlobal('InvalidError', extend(AdapterError, 'The adapter rejected the commit because it was invalid'));
|
|
181
231
|
InvalidError.prototype.code = 'InvalidError';
|
|
182
232
|
|
|
183
233
|
/**
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
the external API within an allowed time period.
|
|
187
|
-
|
|
188
|
-
An example use case would be to warn the user to check their internet
|
|
189
|
-
connection if an adapter operation has timed out:
|
|
190
|
-
|
|
191
|
-
```js [app/routes/application.js]
|
|
192
|
-
import { TimeoutError } from '@warp-drive/legacy/adapter/error';
|
|
193
|
-
|
|
194
|
-
export default class ApplicationRoute extends Route {
|
|
195
|
-
@action
|
|
196
|
-
error(error, transition) {
|
|
197
|
-
if (error instanceof TimeoutError) {
|
|
198
|
-
// alert the user
|
|
199
|
-
alert('Are you still connected to the Internet?');
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// ...other error handling logic
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
@class TimeoutError
|
|
209
|
-
@public
|
|
210
|
-
*/
|
|
234
|
+
* The {@link AdapterRequestError} shape thrown by the {@link TimeoutError} constructor.
|
|
235
|
+
*/
|
|
211
236
|
|
|
237
|
+
/**
|
|
238
|
+
* A `TimeoutError` is used by an adapter to signal that a request to the
|
|
239
|
+
* external API has timed out, i.e. no response was received from the
|
|
240
|
+
* external API within an allowed time period.
|
|
241
|
+
*
|
|
242
|
+
* An example use case would be to warn the user to check their internet
|
|
243
|
+
* connection if an adapter operation has timed out:
|
|
244
|
+
*
|
|
245
|
+
* ```js [app/routes/application.js]
|
|
246
|
+
* import { TimeoutError } from '@warp-drive/legacy/adapter/error';
|
|
247
|
+
*
|
|
248
|
+
* export default class ApplicationRoute extends Route {
|
|
249
|
+
* @action
|
|
250
|
+
* error(error, transition) {
|
|
251
|
+
* if (error instanceof TimeoutError) {
|
|
252
|
+
* // alert the user
|
|
253
|
+
* alert('Are you still connected to the Internet?');
|
|
254
|
+
* return;
|
|
255
|
+
* }
|
|
256
|
+
*
|
|
257
|
+
* // ...other error handling logic
|
|
258
|
+
* }
|
|
259
|
+
* }
|
|
260
|
+
* ```
|
|
261
|
+
*
|
|
262
|
+
* @public
|
|
263
|
+
*/
|
|
212
264
|
const TimeoutError = getOrSetGlobal('TimeoutError', extend(AdapterError, 'The adapter operation timed out'));
|
|
213
265
|
TimeoutError.prototype.code = 'TimeoutError';
|
|
214
266
|
|
|
215
267
|
/**
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
navigates away from the current page after a request to the external API
|
|
219
|
-
has been initiated but before a response has been received.
|
|
220
|
-
|
|
221
|
-
@class AbortError
|
|
222
|
-
@public
|
|
223
|
-
*/
|
|
268
|
+
* The {@link AdapterRequestError} shape thrown by the {@link AbortError} constructor.
|
|
269
|
+
*/
|
|
224
270
|
|
|
271
|
+
/**
|
|
272
|
+
* An `AbortError` is used by an adapter to signal that a request to the
|
|
273
|
+
* external API was aborted. For example, this can occur if the user
|
|
274
|
+
* navigates away from the current page after a request to the external API
|
|
275
|
+
* has been initiated but before a response has been received.
|
|
276
|
+
*
|
|
277
|
+
* Because an aborted request is typically expected (the user chose to
|
|
278
|
+
* navigate away, or a newer request superseded this one) rather than
|
|
279
|
+
* exceptional, an example use case would be to silently ignore it instead
|
|
280
|
+
* of surfacing an error to the user:
|
|
281
|
+
*
|
|
282
|
+
* ```js [app/routes/application.js]
|
|
283
|
+
* import { AbortError } from '@warp-drive/legacy/adapter/error';
|
|
284
|
+
*
|
|
285
|
+
* export default class ApplicationRoute extends Route {
|
|
286
|
+
* @action
|
|
287
|
+
* error(error, transition) {
|
|
288
|
+
* if (error instanceof AbortError) {
|
|
289
|
+
* // the request was aborted, nothing to report
|
|
290
|
+
* return;
|
|
291
|
+
* }
|
|
292
|
+
*
|
|
293
|
+
* // ...other error handling logic
|
|
294
|
+
* }
|
|
295
|
+
* }
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @public
|
|
299
|
+
*/
|
|
225
300
|
const AbortError = getOrSetGlobal('AbortError', extend(AdapterError, 'The adapter operation was aborted'));
|
|
226
301
|
AbortError.prototype.code = 'AbortError';
|
|
227
302
|
|
|
228
303
|
/**
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
API was rejected because authorization is required and has failed or has not
|
|
232
|
-
yet been provided.
|
|
233
|
-
|
|
234
|
-
An example use case would be to redirect the user to a login route if a
|
|
235
|
-
request is unauthorized:
|
|
236
|
-
|
|
237
|
-
```js [app/routes/application.js]
|
|
238
|
-
import { UnauthorizedError } from '@warp-drive/legacy/adapter/error';
|
|
239
|
-
|
|
240
|
-
export default class ApplicationRoute extends Route {
|
|
241
|
-
@action
|
|
242
|
-
error(error, transition) {
|
|
243
|
-
if (error instanceof UnauthorizedError) {
|
|
244
|
-
// go to the login route
|
|
245
|
-
this.transitionTo('login');
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// ...other error handling logic
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
@class UnauthorizedError
|
|
255
|
-
@public
|
|
256
|
-
*/
|
|
304
|
+
* The {@link AdapterRequestError} shape thrown by the {@link UnauthorizedError} constructor.
|
|
305
|
+
*/
|
|
257
306
|
|
|
307
|
+
/**
|
|
308
|
+
* A `UnauthorizedError` equates to an HTTP `401 Unauthorized` response
|
|
309
|
+
* status. It is used by an adapter to signal that a request to the external
|
|
310
|
+
* API was rejected because authorization is required and has failed or has
|
|
311
|
+
* not yet been provided.
|
|
312
|
+
*
|
|
313
|
+
* An example use case would be to redirect the user to a login route if a
|
|
314
|
+
* request is unauthorized:
|
|
315
|
+
*
|
|
316
|
+
* ```js [app/routes/application.js]
|
|
317
|
+
* import { UnauthorizedError } from '@warp-drive/legacy/adapter/error';
|
|
318
|
+
*
|
|
319
|
+
* export default class ApplicationRoute extends Route {
|
|
320
|
+
* @action
|
|
321
|
+
* error(error, transition) {
|
|
322
|
+
* if (error instanceof UnauthorizedError) {
|
|
323
|
+
* // go to the login route
|
|
324
|
+
* this.transitionTo('login');
|
|
325
|
+
* return;
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* // ...other error handling logic
|
|
329
|
+
* }
|
|
330
|
+
* }
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @public
|
|
334
|
+
*/
|
|
258
335
|
const UnauthorizedError = getOrSetGlobal('UnauthorizedError', extend(AdapterError, 'The adapter operation is unauthorized'));
|
|
259
336
|
UnauthorizedError.prototype.code = 'UnauthorizedError';
|
|
260
337
|
|
|
261
338
|
/**
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
valid but the server is refusing to respond to it. If authorization was
|
|
265
|
-
provided and is valid, then the authenticated user does not have the
|
|
266
|
-
necessary permissions for the request.
|
|
267
|
-
|
|
268
|
-
@class ForbiddenError
|
|
269
|
-
@public
|
|
270
|
-
*/
|
|
339
|
+
* The {@link AdapterRequestError} shape thrown by the {@link ForbiddenError} constructor.
|
|
340
|
+
*/
|
|
271
341
|
|
|
342
|
+
/**
|
|
343
|
+
* A `ForbiddenError` equates to an HTTP `403 Forbidden` response status.
|
|
344
|
+
* It is used by an adapter to signal that a request to the external API was
|
|
345
|
+
* valid but the server is refusing to respond to it. If authorization was
|
|
346
|
+
* provided and is valid, then the authenticated user does not have the
|
|
347
|
+
* necessary permissions for the request.
|
|
348
|
+
*
|
|
349
|
+
* Unlike an {@link UnauthorizedError}, retrying the request with different
|
|
350
|
+
* credentials will not help; the currently authenticated user simply lacks
|
|
351
|
+
* permission. An example use case would be to show the user a "you don't
|
|
352
|
+
* have access to this" message rather than redirecting them to log in:
|
|
353
|
+
*
|
|
354
|
+
* ```js [app/routes/application.js]
|
|
355
|
+
* import { ForbiddenError } from '@warp-drive/legacy/adapter/error';
|
|
356
|
+
*
|
|
357
|
+
* export default class ApplicationRoute extends Route {
|
|
358
|
+
* @action
|
|
359
|
+
* error(error, transition) {
|
|
360
|
+
* if (error instanceof ForbiddenError) {
|
|
361
|
+
* this.transitionTo('forbidden');
|
|
362
|
+
* return;
|
|
363
|
+
* }
|
|
364
|
+
*
|
|
365
|
+
* // ...other error handling logic
|
|
366
|
+
* }
|
|
367
|
+
* }
|
|
368
|
+
* ```
|
|
369
|
+
*
|
|
370
|
+
* @public
|
|
371
|
+
*/
|
|
272
372
|
const ForbiddenError = getOrSetGlobal('ForbiddenError', extend(AdapterError, 'The adapter operation is forbidden'));
|
|
273
373
|
ForbiddenError.prototype.code = 'ForbiddenError';
|
|
274
374
|
|
|
275
375
|
/**
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
was rejected because the resource could not be found on the API.
|
|
279
|
-
|
|
280
|
-
An example use case would be to detect if the user has entered a route
|
|
281
|
-
for a specific model that does not exist. For example:
|
|
282
|
-
|
|
283
|
-
```js [app/routes/post.js]
|
|
284
|
-
import { NotFoundError } from '@warp-drive/legacy/adapter/error';
|
|
285
|
-
|
|
286
|
-
export default class PostRoute extends Route {
|
|
287
|
-
@service store;
|
|
288
|
-
model(params) {
|
|
289
|
-
return this.store.findRecord('post', params.post_id);
|
|
290
|
-
}
|
|
291
|
-
@action
|
|
292
|
-
error(error, transition) {
|
|
293
|
-
if (error instanceof NotFoundError) {
|
|
294
|
-
// redirect to a list of all posts instead
|
|
295
|
-
this.transitionTo('posts');
|
|
296
|
-
} else {
|
|
297
|
-
// otherwise let the error bubble
|
|
298
|
-
return true;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
@class NotFoundError
|
|
305
|
-
@public
|
|
306
|
-
*/
|
|
376
|
+
* The {@link AdapterRequestError} shape thrown by the {@link NotFoundError} constructor.
|
|
377
|
+
*/
|
|
307
378
|
|
|
379
|
+
/**
|
|
380
|
+
* A `NotFoundError` equates to an HTTP `404 Not Found` response status.
|
|
381
|
+
* It is used by an adapter to signal that a request to the external API
|
|
382
|
+
* was rejected because the resource could not be found on the API.
|
|
383
|
+
*
|
|
384
|
+
* An example use case would be to detect if the user has entered a route
|
|
385
|
+
* for a specific model that does not exist. For example:
|
|
386
|
+
*
|
|
387
|
+
* ```js [app/routes/post.js]
|
|
388
|
+
* import { NotFoundError } from '@warp-drive/legacy/adapter/error';
|
|
389
|
+
*
|
|
390
|
+
* export default class PostRoute extends Route {
|
|
391
|
+
* @service store;
|
|
392
|
+
* model(params) {
|
|
393
|
+
* return this.store.findRecord('post', params.post_id);
|
|
394
|
+
* }
|
|
395
|
+
* @action
|
|
396
|
+
* error(error, transition) {
|
|
397
|
+
* if (error instanceof NotFoundError) {
|
|
398
|
+
* // redirect to a list of all posts instead
|
|
399
|
+
* this.transitionTo('posts');
|
|
400
|
+
* } else {
|
|
401
|
+
* // otherwise let the error bubble
|
|
402
|
+
* return true;
|
|
403
|
+
* }
|
|
404
|
+
* }
|
|
405
|
+
* }
|
|
406
|
+
* ```
|
|
407
|
+
*
|
|
408
|
+
* @public
|
|
409
|
+
*/
|
|
308
410
|
const NotFoundError = getOrSetGlobal('NotFoundError', extend(AdapterError, 'The adapter could not find the resource'));
|
|
309
411
|
NotFoundError.prototype.code = 'NotFoundError';
|
|
310
412
|
|
|
311
413
|
/**
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
because of a conflict in the request. An example scenario would be when
|
|
315
|
-
creating a record with a client-generated ID but that ID is already known
|
|
316
|
-
to the external API.
|
|
317
|
-
|
|
318
|
-
@class ConflictError
|
|
319
|
-
@public
|
|
320
|
-
*/
|
|
414
|
+
* The {@link AdapterRequestError} shape thrown by the {@link ConflictError} constructor.
|
|
415
|
+
*/
|
|
321
416
|
|
|
417
|
+
/**
|
|
418
|
+
* A `ConflictError` equates to an HTTP `409 Conflict` response status.
|
|
419
|
+
* It is used by an adapter to indicate that the request could not be
|
|
420
|
+
* processed because of a conflict in the request. An example scenario
|
|
421
|
+
* would be when creating a record with a client-generated ID but that ID
|
|
422
|
+
* is already known to the external API.
|
|
423
|
+
*
|
|
424
|
+
* An example use case would be to surface a conflict-specific message so
|
|
425
|
+
* the user can retry with different input:
|
|
426
|
+
*
|
|
427
|
+
* ```js [app/routes/application.js]
|
|
428
|
+
* import { ConflictError } from '@warp-drive/legacy/adapter/error';
|
|
429
|
+
*
|
|
430
|
+
* export default class ApplicationRoute extends Route {
|
|
431
|
+
* @action
|
|
432
|
+
* error(error, transition) {
|
|
433
|
+
* if (error instanceof ConflictError) {
|
|
434
|
+
* alert('That identifier is already in use, please choose another.');
|
|
435
|
+
* return;
|
|
436
|
+
* }
|
|
437
|
+
*
|
|
438
|
+
* // ...other error handling logic
|
|
439
|
+
* }
|
|
440
|
+
* }
|
|
441
|
+
* ```
|
|
442
|
+
*
|
|
443
|
+
* @public
|
|
444
|
+
*/
|
|
322
445
|
const ConflictError = getOrSetGlobal('ConflictError', extend(AdapterError, 'The adapter operation failed due to a conflict'));
|
|
323
446
|
ConflictError.prototype.code = 'ConflictError';
|
|
324
447
|
|
|
325
448
|
/**
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
because of an error in the external API.
|
|
329
|
-
|
|
330
|
-
@class ServerError
|
|
331
|
-
@public
|
|
332
|
-
*/
|
|
449
|
+
* The {@link AdapterRequestError} shape thrown by the {@link ServerError} constructor.
|
|
450
|
+
*/
|
|
333
451
|
|
|
452
|
+
/**
|
|
453
|
+
* A `ServerError` equates to an HTTP `500 Internal Server Error` response
|
|
454
|
+
* status. It is used by the adapter to indicate that a request has failed
|
|
455
|
+
* because of an error in the external API, and is unlikely to succeed if
|
|
456
|
+
* retried immediately.
|
|
457
|
+
*
|
|
458
|
+
* An example use case would be to show a generic "something went wrong on
|
|
459
|
+
* our end" message rather than one implying the user made a mistake:
|
|
460
|
+
*
|
|
461
|
+
* ```js [app/routes/application.js]
|
|
462
|
+
* import { ServerError } from '@warp-drive/legacy/adapter/error';
|
|
463
|
+
*
|
|
464
|
+
* export default class ApplicationRoute extends Route {
|
|
465
|
+
* @action
|
|
466
|
+
* error(error, transition) {
|
|
467
|
+
* if (error instanceof ServerError) {
|
|
468
|
+
* alert('Something went wrong on our end. Please try again later.');
|
|
469
|
+
* return;
|
|
470
|
+
* }
|
|
471
|
+
*
|
|
472
|
+
* // ...other error handling logic
|
|
473
|
+
* }
|
|
474
|
+
* }
|
|
475
|
+
* ```
|
|
476
|
+
*
|
|
477
|
+
* @public
|
|
478
|
+
*/
|
|
334
479
|
const ServerError = getOrSetGlobal('ServerError', extend(AdapterError, 'The adapter operation failed due to a server error'));
|
|
335
480
|
ServerError.prototype.code = 'ServerError';
|
|
336
481
|
export { AbortError, AdapterError, ConflictError, ForbiddenError, InvalidError, NotFoundError, ServerError, TimeoutError, UnauthorizedError };
|