-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
resolvers.js
222 lines (203 loc) · 6.11 KB
/
resolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* External dependencies
*/
import { find, includes, get, hasIn, compact } from 'lodash';
/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import {
receiveUserQuery,
receiveCurrentUser,
receiveEntityRecords,
receiveThemeSupports,
receiveEmbedPreview,
receiveUserPermission,
receiveAutosaves,
} from './actions';
import { getKindEntities } from './entities';
import { apiFetch, resolveSelect } from './controls';
/**
* Requests authors from the REST API.
*/
export function* getAuthors() {
const users = yield apiFetch( {
path: '/wp/v2/users/?who=authors&per_page=-1',
} );
yield receiveUserQuery( 'authors', users );
}
/**
* Requests the current user from the REST API.
*/
export function* getCurrentUser() {
const currentUser = yield apiFetch( { path: '/wp/v2/users/me' } );
yield receiveCurrentUser( currentUser );
}
/**
* Requests an entity's record from the REST API.
*
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {number} key Record's key
*/
export function* getEntityRecord( kind, name, key = '' ) {
const entities = yield getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}
const record = yield apiFetch( {
path: `${ entity.baseURL }/${ key }?context=edit`,
} );
yield receiveEntityRecords( kind, name, record );
}
/**
* Requests the entity's records from the REST API.
*
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {Object?} query Query Object.
*/
export function* getEntityRecords( kind, name, query = {} ) {
const entities = yield getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}
const path = addQueryArgs( entity.baseURL, {
...query,
context: 'edit',
} );
const records = yield apiFetch( { path } );
yield receiveEntityRecords( kind, name, Object.values( records ), query );
}
getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
return (
action.type === 'RECEIVE_ITEMS' &&
action.invalidateCache &&
kind === action.kind &&
name === action.name
);
};
/**
* Requests theme supports data from the index.
*/
export function* getThemeSupports() {
const activeThemes = yield apiFetch( {
path: '/wp/v2/themes?status=active',
} );
yield receiveThemeSupports( activeThemes[ 0 ].theme_supports );
}
/**
* Requests a preview from the from the Embed API.
*
* @param {string} url URL to get the preview for.
*/
export function* getEmbedPreview( url ) {
try {
const embedProxyResponse = yield apiFetch( {
path: addQueryArgs( '/oembed/1.0/proxy', { url } ),
} );
yield receiveEmbedPreview( url, embedProxyResponse );
} catch ( error ) {
// Embed API 404s if the URL cannot be embedded, so we have to catch the error from the apiRequest here.
yield receiveEmbedPreview( url, false );
}
}
/**
* Requests Upload Permissions from the REST API.
*
* @deprecated since 5.0. Callers should use the more generic `canUser()` selector instead of
* `hasUploadPermissions()`, e.g. `canUser( 'create', 'media' )`.
*/
export function* hasUploadPermissions() {
deprecated( "select( 'core' ).hasUploadPermissions()", {
alternative: "select( 'core' ).canUser( 'create', 'media' )",
} );
yield* canUser( 'create', 'media' );
}
/**
* Checks whether the current user can perform the given action on the given
* REST resource.
*
* @param {string} action Action to check. One of: 'create', 'read', 'update',
* 'delete'.
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
* @param {?string} id ID of the rest resource to check.
*/
export function* canUser( action, resource, id ) {
const methods = {
create: 'POST',
read: 'GET',
update: 'PUT',
delete: 'DELETE',
};
const method = methods[ action ];
if ( ! method ) {
throw new Error( `'${ action }' is not a valid action.` );
}
const path = id ? `/wp/v2/${ resource }/${ id }` : `/wp/v2/${ resource }`;
let response;
try {
response = yield apiFetch( {
path,
// Ideally this would always be an OPTIONS request, but unfortunately there's
// a bug in the REST API which causes the Allow header to not be sent on
// OPTIONS requests to /posts/:id routes.
// https://core.trac.wordpress.org/ticket/45753
method: id ? 'GET' : 'OPTIONS',
parse: false,
} );
} catch ( error ) {
// Do nothing if our OPTIONS request comes back with an API error (4xx or
// 5xx). The previously determined isAllowed value will remain in the store.
return;
}
let allowHeader;
if ( hasIn( response, [ 'headers', 'get' ] ) ) {
// If the request is fetched using the fetch api, the header can be
// retrieved using the 'get' method.
allowHeader = response.headers.get( 'allow' );
} else {
// If the request was preloaded server-side and is returned by the
// preloading middleware, the header will be a simple property.
allowHeader = get( response, [ 'headers', 'Allow' ], '' );
}
const key = compact( [ action, resource, id ] ).join( '/' );
const isAllowed = includes( allowHeader, method );
yield receiveUserPermission( key, isAllowed );
}
/**
* Request autosave data from the REST API.
*
* @param {string} postType The type of the parent post.
* @param {number} postId The id of the parent post.
*/
export function* getAutosaves( postType, postId ) {
const { rest_base: restBase } = yield resolveSelect(
'getPostType',
postType
);
const autosaves = yield apiFetch( {
path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`,
} );
if ( autosaves && autosaves.length ) {
yield receiveAutosaves( postId, autosaves );
}
}
/**
* Request autosave data from the REST API.
*
* This resolver exists to ensure the underlying autosaves are fetched via
* `getAutosaves` when a call to the `getAutosave` selector is made.
*
* @param {string} postType The type of the parent post.
* @param {number} postId The id of the parent post.
*/
export function* getAutosave( postType, postId ) {
yield resolveSelect( 'getAutosaves', postType, postId );
}