@@ -24,6 +24,58 @@ function isExisting(identifier: StableRecordIdentifier): identifier is StableExi
24
24
return 'id' in identifier && identifier . id !== null && 'type' in identifier && identifier . type !== null ;
25
25
}
26
26
27
+ /**
28
+ * Builds request options to delete record for resources,
29
+ * configured for the url, method and header expectations of ActiveRecord APIs.
30
+ *
31
+ * **Basic Usage**
32
+ *
33
+ * ```ts
34
+ * import { deleteRecord } from '@ember-data/active-record/request';
35
+ *
36
+ * const person = this.store.peekRecord('person', '1');
37
+ *
38
+ * // mark record as deleted
39
+ * store.deleteRecord(person);
40
+ *
41
+ * // persist deletion
42
+ * const data = await store.request(deleteRecord(person));
43
+ * ```
44
+ *
45
+ * **Supplying Options to Modify the Request Behavior**
46
+ *
47
+ * The following options are supported:
48
+ *
49
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
50
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
51
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
52
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
53
+ * option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
54
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
55
+ * promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
56
+ * defaulting to `false` if none is configured.
57
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
58
+ *
59
+ * ```ts
60
+ * import { deleteRecord } from '@ember-data/active-record/request';
61
+ *
62
+ * const person = this.store.peekRecord('person', '1');
63
+ *
64
+ * // mark record as deleted
65
+ * store.deleteRecord(person);
66
+ *
67
+ * // persist deletion
68
+ * const options = deleteRecord(person, { namespace: 'api/v1' });
69
+ * const data = await store.request(options);
70
+ * ```
71
+ *
72
+ * @method deleteRecord
73
+ * @public
74
+ * @static
75
+ * @for @ember -data/active-record/request
76
+ * @param record
77
+ * @param options
78
+ */
27
79
export function deleteRecord ( record : unknown , options : ConstrainedRequestOptions = { } ) : DeleteRequestOptions {
28
80
const identifier = recordIdentifierFor ( record ) ;
29
81
assert ( `Expected to be given a record instance` , identifier ) ;
@@ -52,10 +104,51 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
52
104
} ;
53
105
}
54
106
107
+ /**
108
+ * Builds request options to create new record for resources,
109
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
110
+ *
111
+ * **Basic Usage**
112
+ *
113
+ * ```ts
114
+ * import { createRecord } from '@ember-data/active-record/request';
115
+ *
116
+ * const person = this.store.createRecord('person', { name: 'Ted' });
117
+ * const data = await store.request(createRecord(person));
118
+ * ```
119
+ *
120
+ * **Supplying Options to Modify the Request Behavior**
121
+ *
122
+ * The following options are supported:
123
+ *
124
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
125
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
126
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
127
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
128
+ * option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
129
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
130
+ * promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
131
+ * defaulting to `false` if none is configured.
132
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
133
+ *
134
+ * ```ts
135
+ * import { createRecord } from '@ember-data/active-record/request';
136
+ *
137
+ * const person = this.store.createRecord('person', { name: 'Ted' });
138
+ * const options = createRecord(person, { namespace: 'api/v1' });
139
+ * const data = await store.request(options);
140
+ * ```
141
+ *
142
+ * @method createRecord
143
+ * @public
144
+ * @static
145
+ * @for @ember -data/active-record/request
146
+ * @param record
147
+ * @param options
148
+ */
55
149
export function createRecord ( record : unknown , options : ConstrainedRequestOptions = { } ) : CreateRequestOptions {
56
150
const identifier = recordIdentifierFor ( record ) ;
57
151
assert ( `Expected to be given a record instance` , identifier ) ;
58
- assert ( `Cannot delete a record that does not have an associated type and id.` , isExisting ( identifier ) ) ;
59
152
60
153
const urlOptions : CreateRecordUrlOptions = {
61
154
identifier : identifier ,
@@ -80,13 +173,58 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions
80
173
} ;
81
174
}
82
175
176
+ /**
177
+ * Builds request options to update existing record for resources,
178
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
179
+ *
180
+ * **Basic Usage**
181
+ *
182
+ * ```ts
183
+ * import { updateRecord } from '@ember-data/active-record/request';
184
+ *
185
+ * const person = this.store.peekRecord('person', '1');
186
+ * person.name = 'Chris';
187
+ * const data = await store.request(updateRecord(person));
188
+ * ```
189
+ *
190
+ * **Supplying Options to Modify the Request Behavior**
191
+ *
192
+ * The following options are supported:
193
+ *
194
+ * - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
195
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
196
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
197
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
198
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
199
+ * option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
200
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
201
+ * promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
202
+ * defaulting to `false` if none is configured.
203
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
204
+ *
205
+ * ```ts
206
+ * import { updateRecord } from '@ember-data/active-record/request';
207
+ *
208
+ * const person = this.store.peekRecord('person', '1');
209
+ * person.name = 'Chris';
210
+ * const options = updateRecord(person, { patch: true });
211
+ * const data = await store.request(options);
212
+ * ```
213
+ *
214
+ * @method updateRecord
215
+ * @public
216
+ * @static
217
+ * @for @ember -data/active-record/request
218
+ * @param record
219
+ * @param options
220
+ */
83
221
export function updateRecord (
84
222
record : unknown ,
85
223
options : ConstrainedRequestOptions & { patch ?: boolean } = { }
86
224
) : UpdateRequestOptions {
87
225
const identifier = recordIdentifierFor ( record ) ;
88
226
assert ( `Expected to be given a record instance` , identifier ) ;
89
- assert ( `Cannot delete a record that does not have an associated type and id.` , isExisting ( identifier ) ) ;
227
+ assert ( `Cannot update a record that does not have an associated type and id.` , isExisting ( identifier ) ) ;
90
228
91
229
const urlOptions : UpdateRecordUrlOptions = {
92
230
identifier : identifier ,
0 commit comments