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