-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathresolvers.js
80 lines (73 loc) · 2.1 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
/**
* External dependencies
*/
import { find } from 'lodash';
/**
* WordPress dependencies
*/
import apiRequest from '@wordpress/api-request';
/**
* Internal dependencies
*/
import {
receiveTerms,
receiveUserQuery,
receiveEntityRecords,
receiveThemeSupportsFromIndex,
} from './actions';
import { getKindEntities } from './entities';
/**
* Requests categories from the REST API, yielding action objects on request
* progress.
*/
export async function* getCategories() {
const categories = await apiRequest( { path: '/wp/v2/categories?per_page=-1' } );
yield receiveTerms( 'categories', categories );
}
/**
* Requests authors from the REST API.
*/
export async function* getAuthors() {
const users = await apiRequest( { path: '/wp/v2/users/?who=authors&per_page=-1' } );
yield receiveUserQuery( 'authors', users );
}
/**
* Requests an entity's record from the REST API.
*
* @param {Object} state State tree
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {number} key Record's key
*/
export async function* getEntityRecord( state, kind, name, key ) {
const entities = yield* await getKindEntities( state, kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}
const record = await apiRequest( { path: `${ entity.baseUrl }/${ key }?context=edit` } );
yield receiveEntityRecords( kind, name, record );
}
/**
* Requests the entity's records from the REST API.
*
* @param {Object} state State tree
* @param {string} kind Entity kind.
* @param {string} name Entity name.
*/
export async function* getEntityRecords( state, kind, name ) {
const entities = yield* await getKindEntities( state, kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}
const records = await apiRequest( { path: `${ entity.baseUrl }?context=edit` } );
yield receiveEntityRecords( kind, name, Object.values( records ) );
}
/**
* Requests theme supports data from the index.
*/
export async function* getThemeSupports() {
const index = await apiRequest( { path: '/' } );
yield receiveThemeSupportsFromIndex( index );
}