@@ -24,6 +24,58 @@ function isExisting(identifier: StableRecordIdentifier): identifier is StableExi
2424 return 'id' in identifier && identifier . id !== null && 'type' in identifier && identifier . type !== null ;
2525}
2626
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+ */
2779export function deleteRecord ( record : unknown , options : ConstrainedRequestOptions = { } ) : DeleteRequestOptions {
2880 const identifier = recordIdentifierFor ( record ) ;
2981 assert ( `Expected to be given a record instance` , identifier ) ;
@@ -52,10 +104,51 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
52104 } ;
53105}
54106
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+ */
55149export function createRecord ( record : unknown , options : ConstrainedRequestOptions = { } ) : CreateRequestOptions {
56150 const identifier = recordIdentifierFor ( record ) ;
57151 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 ) ) ;
59152
60153 const urlOptions : CreateRecordUrlOptions = {
61154 identifier : identifier ,
@@ -80,13 +173,58 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions
80173 } ;
81174}
82175
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+ */
83221export function updateRecord (
84222 record : unknown ,
85223 options : ConstrainedRequestOptions & { patch ?: boolean } = { }
86224) : UpdateRequestOptions {
87225 const identifier = recordIdentifierFor ( record ) ;
88226 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 ) ) ;
90228
91229 const urlOptions : UpdateRecordUrlOptions = {
92230 identifier : identifier ,
0 commit comments