4
4
import { pluralize } from 'ember-inflector' ;
5
5
6
6
import { buildBaseURL , buildQueryParams , QueryParamsSource , type QueryUrlOptions } from '@ember-data/request-utils' ;
7
- import type { ConstrainedRequestOptions , QueryRequestOptions } from '@ember-data/types/request' ;
7
+ import type {
8
+ CacheOptions ,
9
+ ConstrainedRequestOptions ,
10
+ PostQueryRequestOptions ,
11
+ QueryRequestOptions ,
12
+ } from '@ember-data/types/request' ;
8
13
9
- import { copyForwardUrlOptions , extractCacheOptions } from './-utils' ;
14
+ import { ACCEPT_HEADER_VALUE , copyForwardUrlOptions , extractCacheOptions } from './-utils' ;
10
15
11
16
/**
12
17
* Builds request options to query for resources, usually by a primary
@@ -75,7 +80,7 @@ export function query(
75
80
76
81
const url = buildBaseURL ( urlOptions ) ;
77
82
const headers = new Headers ( ) ;
78
- headers . append ( 'Accept' , 'application/vnd.api+json' ) ;
83
+ headers . append ( 'Accept' , ACCEPT_HEADER_VALUE ) ;
79
84
80
85
return {
81
86
url : `${ url } ?${ buildQueryParams ( query , options . urlParamsSettings ) } ` ,
@@ -85,3 +90,75 @@ export function query(
85
90
op : 'query' ,
86
91
} ;
87
92
}
93
+
94
+ /**
95
+ * Builds request options to query for resources, usually by a primary
96
+ * type, configured for the url and header expectations of most JSON:API APIs.
97
+ *
98
+ * ```ts
99
+ * import { postQuery } from '@ember-data/json-api/request';
100
+ *
101
+ * const options = query('person', { include: ['pets', 'friends'] });
102
+ * const data = await store.request(options);
103
+ * ```
104
+ *
105
+ * **Supplying Options to Modify the Request Behavior**
106
+ *
107
+ * The following options are supported:
108
+ *
109
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
110
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
111
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
112
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
113
+ * option will delegate to the store's lifetimes service, defaulting to `false` if none is configured.
114
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
115
+ * promise with the cached value, not supplying this option will delegate to the store's lifetimes service,
116
+ * defaulting to `false` if none is configured.
117
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
118
+ *
119
+ * ```ts
120
+ * import { query } from '@ember-data/json-api/request';
121
+ *
122
+ * const options = query('person', { include: ['pets', 'friends'] }, { reload: true });
123
+ * const data = await store.request(options);
124
+ * ```
125
+ *
126
+ * @method postQuery
127
+ * @public
128
+ * @static
129
+ * @for @ember -data/json-api/request
130
+ * @param identifier
131
+ * @param query
132
+ * @param options
133
+ */
134
+ export function postQuery (
135
+ type : string ,
136
+ // eslint-disable-next-line @typescript-eslint/no-shadow
137
+ query : QueryParamsSource = { } ,
138
+ options : ConstrainedRequestOptions = { }
139
+ ) : PostQueryRequestOptions {
140
+ const cacheOptions = extractCacheOptions ( options ) ;
141
+ const urlOptions : QueryUrlOptions = {
142
+ identifier : { type } ,
143
+ op : 'query' ,
144
+ resourcePath : options . resourcePath ?? pluralize ( type ) ,
145
+ } ;
146
+
147
+ copyForwardUrlOptions ( urlOptions , options ) ;
148
+
149
+ const url = buildBaseURL ( urlOptions ) ;
150
+ const headers = new Headers ( ) ;
151
+ headers . append ( 'Accept' , ACCEPT_HEADER_VALUE ) ;
152
+
153
+ const queryData = structuredClone ( query ) ;
154
+ cacheOptions . key = cacheOptions . key ?? `${ url } ?${ buildQueryParams ( queryData , options . urlParamsSettings ) } ` ;
155
+
156
+ return {
157
+ url,
158
+ method : 'POST' ,
159
+ body : JSON . stringify ( query ) ,
160
+ headers,
161
+ cacheOptions : cacheOptions as CacheOptions & { key : string } ,
162
+ op : 'query' ,
163
+ } ;
164
+ }
0 commit comments