@warp-drive/legacy 5.8.0-beta.0 → 5.8.0-beta.2
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 +16 -26
- package/declarations/adapter/error.d.ts +7 -7
- package/declarations/adapter/json-api.d.ts +6 -8
- package/declarations/adapter/rest.d.ts +26 -112
- package/declarations/adapter.d.ts +6 -8
- package/declarations/compat/-private.d.ts +1 -1
- package/declarations/compat/builders/find-all.d.ts +6 -6
- package/declarations/compat/builders/find-record.d.ts +8 -8
- package/declarations/compat/builders/query.d.ts +12 -12
- package/declarations/compat/extensions.d.ts +1 -1
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
- package/declarations/compat/utils.d.ts +17 -17
- package/declarations/compat.d.ts +35 -11
- package/declarations/index.d.ts +102 -0
- package/declarations/model/-private/attr.d.ts +5 -6
- package/declarations/model/-private/belongs-to.d.ts +4 -5
- package/declarations/model/-private/has-many.d.ts +4 -5
- package/declarations/model/-private/hooks.d.ts +1 -1
- package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
- package/declarations/model/-private/model.d.ts +18 -59
- package/declarations/model/-private/promise-many-array.d.ts +0 -18
- package/declarations/model/-private/record-state.d.ts +1 -1
- package/declarations/model/-private/references/belongs-to.d.ts +19 -29
- package/declarations/model/-private/references/has-many.d.ts +14 -16
- package/declarations/model/migration-support.d.ts +46 -21
- package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
- package/declarations/model-fragments/index.d.ts +5 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
- package/declarations/model-fragments.d.ts +9 -0
- package/declarations/model.d.ts +2 -2
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
- package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
- package/declarations/serializer/-private/transforms/date.d.ts +2 -2
- package/declarations/serializer/-private/transforms/number.d.ts +1 -1
- package/declarations/serializer/-private/transforms/string.d.ts +1 -1
- package/declarations/serializer/json-api.d.ts +11 -12
- package/declarations/serializer/json.d.ts +9 -11
- package/declarations/serializer/rest.d.ts +4 -6
- package/declarations/serializer.d.ts +9 -12
- package/dist/{-private-8UmnAf9J.js → -private-BG3bMiKp.js} +3 -2
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +14 -15
- package/dist/adapter/json-api.js +4 -1
- package/dist/adapter/rest.js +38 -125
- package/dist/adapter.js +6 -8
- package/dist/compat/-private.js +1 -1
- package/dist/compat/builders.js +26 -26
- package/dist/compat/utils.js +17 -18
- package/dist/compat.js +61 -43
- package/dist/{errors-8kD2mSe_.js → errors-Cz5KrzBk.js} +115 -122
- package/dist/hooks-D6diaM34.js +74 -0
- package/dist/index.js +195 -0
- package/dist/{json-Et4mt_LM.js → json-ChdEfB0X.js} +18 -35
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +59 -27
- package/dist/model-for-CqXsIKws.js +221 -0
- package/dist/model-fragments.js +76 -0
- package/dist/model.js +18 -90
- package/dist/{schema-provider-DQu4Rjco.js → schema-provider-DJCV_6AF.js} +50 -95
- package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-DPZYoF-i.js} +1 -1
- package/dist/serializer/json-api.js +18 -45
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +14 -21
- package/dist/serializer/transform.js +15 -6
- package/dist/serializer.js +9 -13
- package/dist/store.js +5 -1
- package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev/adapter/-private.js +1 -0
- package/dist/unpkg/dev/adapter/error.js +335 -0
- package/dist/unpkg/dev/adapter/json-api.js +271 -0
- package/dist/unpkg/dev/adapter/rest.js +1171 -0
- package/dist/unpkg/dev/adapter.js +1252 -0
- package/dist/unpkg/dev/compat/-private.js +1 -0
- package/dist/unpkg/dev/compat/builders.js +275 -0
- package/dist/unpkg/dev/compat/extensions.js +242 -0
- package/dist/unpkg/dev/compat/utils.js +223 -0
- package/dist/unpkg/dev/compat.js +1147 -0
- package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
- package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +553 -0
- package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev/model-fragments.js +76 -0
- package/dist/unpkg/dev/model.js +678 -0
- package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
- package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +649 -0
- package/dist/unpkg/dev/serializer/json.js +4 -0
- package/dist/unpkg/dev/serializer/rest.js +1242 -0
- package/dist/unpkg/dev/serializer/transform.js +278 -0
- package/dist/unpkg/dev/serializer.js +248 -0
- package/dist/unpkg/dev/store.js +637 -0
- package/dist/unpkg/dev/util-DvanW33H.js +20 -0
- package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
- package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
- package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
- package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
- package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
- package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
- package/dist/unpkg/dev-deprecated/compat.js +1147 -0
- package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
- package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
- package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/dev-deprecated/model.js +682 -0
- package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
- package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
- package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/dev-deprecated/serializer.js +248 -0
- package/dist/unpkg/dev-deprecated/store.js +637 -0
- package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
- package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
- package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod/adapter/-private.js +1 -0
- package/dist/unpkg/prod/adapter/error.js +330 -0
- package/dist/unpkg/prod/adapter/json-api.js +266 -0
- package/dist/unpkg/prod/adapter/rest.js +1134 -0
- package/dist/unpkg/prod/adapter.js +1219 -0
- package/dist/unpkg/prod/compat/-private.js +1 -0
- package/dist/unpkg/prod/compat/builders.js +210 -0
- package/dist/unpkg/prod/compat/extensions.js +232 -0
- package/dist/unpkg/prod/compat/utils.js +218 -0
- package/dist/unpkg/prod/compat.js +727 -0
- package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
- package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +546 -0
- package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod/model-fragments.js +76 -0
- package/dist/unpkg/prod/model.js +593 -0
- package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
- package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +592 -0
- package/dist/unpkg/prod/serializer/json.js +4 -0
- package/dist/unpkg/prod/serializer/rest.js +1210 -0
- package/dist/unpkg/prod/serializer/transform.js +278 -0
- package/dist/unpkg/prod/serializer.js +248 -0
- package/dist/unpkg/prod/store.js +505 -0
- package/dist/unpkg/prod/util-DvanW33H.js +20 -0
- package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
- package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
- package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
- package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
- package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
- package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
- package/dist/unpkg/prod-deprecated/compat.js +727 -0
- package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
- package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
- package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/prod-deprecated/model.js +596 -0
- package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
- package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
- package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/prod-deprecated/serializer.js +248 -0
- package/dist/unpkg/prod-deprecated/store.js +505 -0
- package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
- package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +15 -7
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { d as determineBodyPromise, g as fetch, p as parseResponseHeaders, b as serializeIntoHash, s as serializeQueryParams, a as setupFastboot } from "../serialize-into-hash-DGlzQteF.js";
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { getOrSetGlobal } from '@warp-drive/core/types/-private';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
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
|
+
function _AdapterError(errors, message = 'Adapter operation failed') {
|
|
72
|
+
this.isAdapterError = true;
|
|
73
|
+
const error = Error.call(this, message);
|
|
74
|
+
if (error) {
|
|
75
|
+
this.stack = error.stack;
|
|
76
|
+
// @ts-expect-error untyped
|
|
77
|
+
this.description = error.description;
|
|
78
|
+
// @ts-expect-error untyped
|
|
79
|
+
this.fileName = error.fileName;
|
|
80
|
+
// @ts-expect-error untyped
|
|
81
|
+
this.lineNumber = error.lineNumber;
|
|
82
|
+
this.message = error.message;
|
|
83
|
+
this.name = error.name;
|
|
84
|
+
// @ts-expect-error untyped
|
|
85
|
+
this.number = error.number;
|
|
86
|
+
}
|
|
87
|
+
this.errors = errors || [{
|
|
88
|
+
title: 'Adapter Error',
|
|
89
|
+
detail: message
|
|
90
|
+
}];
|
|
91
|
+
}
|
|
92
|
+
_AdapterError.prototype = Object.create(Error.prototype);
|
|
93
|
+
_AdapterError.prototype.code = 'AdapterError';
|
|
94
|
+
_AdapterError.extend = extendFn(_AdapterError);
|
|
95
|
+
const AdapterError = getOrSetGlobal('AdapterError', _AdapterError);
|
|
96
|
+
function extendFn(ErrorClass) {
|
|
97
|
+
return function ({
|
|
98
|
+
message: defaultMessage
|
|
99
|
+
} = {}) {
|
|
100
|
+
return extend(ErrorClass, defaultMessage);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function extend(ParentErrorClass, defaultMessage) {
|
|
104
|
+
const ErrorClass = function (errors, message) {
|
|
105
|
+
ParentErrorClass.call(this, errors, message || defaultMessage);
|
|
106
|
+
};
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
108
|
+
ErrorClass.prototype = Object.create(ParentErrorClass.prototype);
|
|
109
|
+
ErrorClass.extend = extendFn(ErrorClass);
|
|
110
|
+
return ErrorClass;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
A `InvalidError` is used by an adapter to signal the external API
|
|
115
|
+
was unable to process a request because the content was not
|
|
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
|
+
*/
|
|
172
|
+
// TODO @deprecate extractError documentation
|
|
173
|
+
|
|
174
|
+
const InvalidError = getOrSetGlobal('InvalidError', extend(AdapterError, 'The adapter rejected the commit because it was invalid'));
|
|
175
|
+
InvalidError.prototype.code = 'InvalidError';
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
A `TimeoutError` is used by an adapter to signal that a request
|
|
179
|
+
to the external API has timed out. I.e. no response was received from
|
|
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
|
+
*/
|
|
205
|
+
|
|
206
|
+
const TimeoutError = getOrSetGlobal('TimeoutError', extend(AdapterError, 'The adapter operation timed out'));
|
|
207
|
+
TimeoutError.prototype.code = 'TimeoutError';
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
A `AbortError` is used by an adapter to signal that a request to
|
|
211
|
+
the external API was aborted. For example, this can occur if the user
|
|
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
|
+
*/
|
|
218
|
+
|
|
219
|
+
const AbortError = getOrSetGlobal('AbortError', extend(AdapterError, 'The adapter operation was aborted'));
|
|
220
|
+
AbortError.prototype.code = 'AbortError';
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
A `UnauthorizedError` equates to a HTTP `401 Unauthorized` response
|
|
224
|
+
status. It is used by an adapter to signal that a request to the external
|
|
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
|
+
*/
|
|
251
|
+
|
|
252
|
+
const UnauthorizedError = getOrSetGlobal('UnauthorizedError', extend(AdapterError, 'The adapter operation is unauthorized'));
|
|
253
|
+
UnauthorizedError.prototype.code = 'UnauthorizedError';
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
A `ForbiddenError` equates to a HTTP `403 Forbidden` response status.
|
|
257
|
+
It is used by an adapter to signal that a request to the external API was
|
|
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
|
+
*/
|
|
265
|
+
|
|
266
|
+
const ForbiddenError = getOrSetGlobal('ForbiddenError', extend(AdapterError, 'The adapter operation is forbidden'));
|
|
267
|
+
ForbiddenError.prototype.code = 'ForbiddenError';
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
A `NotFoundError` equates to a HTTP `404 Not Found` response status.
|
|
271
|
+
It is used by an adapter to signal that a request to the external API
|
|
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
|
+
*/
|
|
301
|
+
|
|
302
|
+
const NotFoundError = getOrSetGlobal('NotFoundError', extend(AdapterError, 'The adapter could not find the resource'));
|
|
303
|
+
NotFoundError.prototype.code = 'NotFoundError';
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
A `ConflictError` equates to a HTTP `409 Conflict` response status.
|
|
307
|
+
It is used by an adapter to indicate that the request could not be processed
|
|
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
|
+
*/
|
|
315
|
+
|
|
316
|
+
const ConflictError = getOrSetGlobal('ConflictError', extend(AdapterError, 'The adapter operation failed due to a conflict'));
|
|
317
|
+
ConflictError.prototype.code = 'ConflictError';
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
A `ServerError` equates to a HTTP `500 Internal Server Error` response
|
|
321
|
+
status. It is used by the adapter to indicate that a request has failed
|
|
322
|
+
because of an error in the external API.
|
|
323
|
+
|
|
324
|
+
@class ServerError
|
|
325
|
+
@public
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
const ServerError = getOrSetGlobal('ServerError', extend(AdapterError, 'The adapter operation failed due to a server error'));
|
|
329
|
+
ServerError.prototype.code = 'ServerError';
|
|
330
|
+
export { AbortError, AdapterError, ConflictError, ForbiddenError, InvalidError, NotFoundError, ServerError, TimeoutError, UnauthorizedError };
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { dasherize, pluralize } from '@warp-drive/utilities/string';
|
|
2
|
+
import { b as serializeIntoHash } from "../serialize-into-hash-DGlzQteF.js";
|
|
3
|
+
import { RESTAdapter } from './rest.js';
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
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
|
+
The `JSONAPIAdapter` is an adapter whichtransforms the store's
|
|
17
|
+
requests into HTTP requests that follow the [JSON API format](http://jsonapi.org/format/).
|
|
18
|
+
|
|
19
|
+
## JSON API Conventions
|
|
20
|
+
|
|
21
|
+
The JSONAPIAdapter uses JSON API conventions for building the URL
|
|
22
|
+
for a record and selecting the HTTP verb to use with a request. The
|
|
23
|
+
actions you can take on a record map onto the following URLs in the
|
|
24
|
+
JSON API adapter:
|
|
25
|
+
|
|
26
|
+
<table>
|
|
27
|
+
<tr>
|
|
28
|
+
<th>
|
|
29
|
+
Action
|
|
30
|
+
</th>
|
|
31
|
+
<th>
|
|
32
|
+
HTTP Verb
|
|
33
|
+
</th>
|
|
34
|
+
<th>
|
|
35
|
+
URL
|
|
36
|
+
</th>
|
|
37
|
+
</tr>
|
|
38
|
+
<tr>
|
|
39
|
+
<th>
|
|
40
|
+
`store.findRecord('post', 123)`
|
|
41
|
+
</th>
|
|
42
|
+
<td>
|
|
43
|
+
GET
|
|
44
|
+
</td>
|
|
45
|
+
<td>
|
|
46
|
+
/posts/123
|
|
47
|
+
</td>
|
|
48
|
+
</tr>
|
|
49
|
+
<tr>
|
|
50
|
+
<th>
|
|
51
|
+
`store.findAll('post')`
|
|
52
|
+
</th>
|
|
53
|
+
<td>
|
|
54
|
+
GET
|
|
55
|
+
</td>
|
|
56
|
+
<td>
|
|
57
|
+
/posts
|
|
58
|
+
</td>
|
|
59
|
+
</tr>
|
|
60
|
+
<tr>
|
|
61
|
+
<th>
|
|
62
|
+
Update `postRecord.save()`
|
|
63
|
+
</th>
|
|
64
|
+
<td>
|
|
65
|
+
PATCH
|
|
66
|
+
</td>
|
|
67
|
+
<td>
|
|
68
|
+
/posts/123
|
|
69
|
+
</td>
|
|
70
|
+
</tr>
|
|
71
|
+
<tr>
|
|
72
|
+
<th>
|
|
73
|
+
Create `store.createRecord('post').save()`
|
|
74
|
+
</th>
|
|
75
|
+
<td>
|
|
76
|
+
POST
|
|
77
|
+
</td>
|
|
78
|
+
<td>
|
|
79
|
+
/posts
|
|
80
|
+
</td>
|
|
81
|
+
</tr>
|
|
82
|
+
<tr>
|
|
83
|
+
<th>
|
|
84
|
+
Delete `postRecord.destroyRecord()`
|
|
85
|
+
</th>
|
|
86
|
+
<td>
|
|
87
|
+
DELETE
|
|
88
|
+
</td>
|
|
89
|
+
<td>
|
|
90
|
+
/posts/123
|
|
91
|
+
</td>
|
|
92
|
+
</tr>
|
|
93
|
+
</table>
|
|
94
|
+
|
|
95
|
+
## Success and failure
|
|
96
|
+
|
|
97
|
+
The JSONAPIAdapter will consider a success any response with a
|
|
98
|
+
status code of the 2xx family ("Success"), as well as 304 ("Not
|
|
99
|
+
Modified"). Any other status code will be considered a failure.
|
|
100
|
+
|
|
101
|
+
On success, the request promise will be resolved with the full
|
|
102
|
+
response payload.
|
|
103
|
+
|
|
104
|
+
Failed responses with status code 422 ("Unprocessable Entity") will
|
|
105
|
+
be considered "invalid". The response will be discarded, except for
|
|
106
|
+
the `errors` key. The request promise will be rejected with a
|
|
107
|
+
`InvalidError`. This error object will encapsulate the saved
|
|
108
|
+
`errors` value.
|
|
109
|
+
|
|
110
|
+
Any other status codes will be treated as an adapter error. The
|
|
111
|
+
request promise will be rejected, similarly to the invalid case,
|
|
112
|
+
but with an instance of `AdapterError` instead.
|
|
113
|
+
|
|
114
|
+
### Endpoint path customization
|
|
115
|
+
|
|
116
|
+
Endpoint paths can be prefixed with a `namespace` by setting the
|
|
117
|
+
namespace property on the adapter:
|
|
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
|
+
namespace = 'api/1';
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
Requests for the `person` model would now target `/api/1/people/1`.
|
|
127
|
+
|
|
128
|
+
### Host customization
|
|
129
|
+
|
|
130
|
+
An adapter can target other hosts by setting the `host` property.
|
|
131
|
+
|
|
132
|
+
```js [app/adapters/application.js]
|
|
133
|
+
import JSONAPIAdapter from '@warp-drive/legacy/adapter/json-api';
|
|
134
|
+
|
|
135
|
+
export default class ApplicationAdapter extends JSONAPIAdapter {
|
|
136
|
+
host = 'https://api.example.com';
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Requests for the `person` model would now target
|
|
141
|
+
`https://api.example.com/people/1`.
|
|
142
|
+
|
|
143
|
+
@since 1.13.0
|
|
144
|
+
@class JSONAPIAdapter
|
|
145
|
+
@public
|
|
146
|
+
@constructor
|
|
147
|
+
*/
|
|
148
|
+
class JSONAPIAdapter extends RESTAdapter {
|
|
149
|
+
_defaultContentType = 'application/vnd.api+json';
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
@private
|
|
153
|
+
@param {String} url
|
|
154
|
+
@param {String} type The request type GET, POST, PUT, DELETE etc.
|
|
155
|
+
@param {Object} options
|
|
156
|
+
@return {Object}
|
|
157
|
+
*/
|
|
158
|
+
ajaxOptions(url, type, options = {}) {
|
|
159
|
+
const hash = super.ajaxOptions(url, type, options);
|
|
160
|
+
const headers = hash.headers = hash.headers || {};
|
|
161
|
+
headers['Accept'] = headers['Accept'] || 'application/vnd.api+json';
|
|
162
|
+
return hash;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
By default the JSONAPIAdapter will send each find request coming from a `store.find`
|
|
167
|
+
or from accessing a relationship separately to the server. If your server supports passing
|
|
168
|
+
ids as a query string, you can set coalesceFindRequests to true to coalesce all find requests
|
|
169
|
+
within a single runloop.
|
|
170
|
+
For example, if you have an initial payload of:
|
|
171
|
+
```javascript
|
|
172
|
+
{
|
|
173
|
+
data: {
|
|
174
|
+
id: 1,
|
|
175
|
+
type: 'post',
|
|
176
|
+
relationship: {
|
|
177
|
+
comments: {
|
|
178
|
+
data: [
|
|
179
|
+
{ id: 1, type: 'comment' },
|
|
180
|
+
{ id: 2, type: 'comment' }
|
|
181
|
+
]
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
By default calling `post.comments` will trigger the following requests(assuming the
|
|
188
|
+
comments haven't been loaded before):
|
|
189
|
+
```
|
|
190
|
+
GET /comments/1
|
|
191
|
+
GET /comments/2
|
|
192
|
+
```
|
|
193
|
+
If you set coalesceFindRequests to `true` it will instead trigger the following request:
|
|
194
|
+
```
|
|
195
|
+
GET /comments?filter[id]=1,2
|
|
196
|
+
```
|
|
197
|
+
Setting coalesceFindRequests to `true` also works for `store.find` requests and `belongsTo`
|
|
198
|
+
relationships accessed within the same runloop. If you set `coalesceFindRequests: true`
|
|
199
|
+
```javascript
|
|
200
|
+
store.findRecord('comment', 1);
|
|
201
|
+
store.findRecord('comment', 2);
|
|
202
|
+
```
|
|
203
|
+
will also send a request to: `GET /comments?filter[id]=1,2`
|
|
204
|
+
Note: Requests coalescing rely on URL building strategy. So if you override `buildURL` in your app
|
|
205
|
+
`groupRecordsForFindMany` more likely should be overridden as well in order for coalescing to work.
|
|
206
|
+
@property coalesceFindRequests
|
|
207
|
+
@public
|
|
208
|
+
@type {Boolean}
|
|
209
|
+
*/
|
|
210
|
+
get coalesceFindRequests() {
|
|
211
|
+
const coalesceFindRequests = this._coalesceFindRequests;
|
|
212
|
+
if (typeof coalesceFindRequests === 'boolean') {
|
|
213
|
+
return coalesceFindRequests;
|
|
214
|
+
}
|
|
215
|
+
return this._coalesceFindRequests = false;
|
|
216
|
+
}
|
|
217
|
+
set coalesceFindRequests(value) {
|
|
218
|
+
this._coalesceFindRequests = value;
|
|
219
|
+
}
|
|
220
|
+
findMany(store, type, ids, snapshots) {
|
|
221
|
+
const url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
|
|
222
|
+
return this.ajax(url, 'GET', {
|
|
223
|
+
data: {
|
|
224
|
+
filter: {
|
|
225
|
+
id: ids.join(',')
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
pathForType(modelName) {
|
|
231
|
+
const dasherized = dasherize(modelName);
|
|
232
|
+
return pluralize(dasherized);
|
|
233
|
+
}
|
|
234
|
+
updateRecord(store, schema, snapshot) {
|
|
235
|
+
const data = serializeIntoHash(store, schema, snapshot);
|
|
236
|
+
const type = snapshot.modelName;
|
|
237
|
+
const id = snapshot.id;
|
|
238
|
+
const url = this.buildURL(type, id, snapshot, 'updateRecord');
|
|
239
|
+
return this.ajax(url, 'PATCH', {
|
|
240
|
+
data: data
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
Used by `findAll` and `findRecord` to build the query's `data` hash
|
|
246
|
+
supplied to the ajax method.
|
|
247
|
+
@since 2.5.0
|
|
248
|
+
@public
|
|
249
|
+
@param {Snapshot} snapshot
|
|
250
|
+
@return {Object}
|
|
251
|
+
*/
|
|
252
|
+
buildQuery(snapshot) {
|
|
253
|
+
const query = {};
|
|
254
|
+
if (snapshot) {
|
|
255
|
+
const {
|
|
256
|
+
include
|
|
257
|
+
} = snapshot;
|
|
258
|
+
const normalizedInclude = Array.isArray(include) ? include.join(',') : include;
|
|
259
|
+
if (normalizedInclude) {
|
|
260
|
+
query.include = normalizedInclude;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return query;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
export { JSONAPIAdapter };
|