@wordpress/e2e-test-utils-playwright 1.48.0 → 1.48.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/build/admin/create-new-post.js +31 -0
  2. package/build/admin/create-new-post.js.map +1 -0
  3. package/build/admin/edit-post.js +20 -0
  4. package/build/admin/edit-post.js.map +1 -0
  5. package/{src/admin/get-page-error.ts → build/admin/get-page-error.js} +9 -12
  6. package/build/admin/get-page-error.js.map +1 -0
  7. package/build/admin/index.js +40 -0
  8. package/build/admin/index.js.map +1 -0
  9. package/build/admin/visit-admin-page.js +32 -0
  10. package/build/admin/visit-admin-page.js.map +1 -0
  11. package/build/admin/visit-site-editor.js +45 -0
  12. package/build/admin/visit-site-editor.js.map +1 -0
  13. package/build/admin/wait-for-site-editor.js +41 -0
  14. package/build/admin/wait-for-site-editor.js.map +1 -0
  15. package/build/config.js +13 -0
  16. package/build/config.js.map +1 -0
  17. package/build/editor/click-block-options-menu-item.js +17 -0
  18. package/build/editor/click-block-options-menu-item.js.map +1 -0
  19. package/build/editor/click-block-toolbar-button.js +16 -0
  20. package/build/editor/click-block-toolbar-button.js.map +1 -0
  21. package/build/editor/get-blocks.js +45 -0
  22. package/build/editor/get-blocks.js.map +1 -0
  23. package/build/editor/get-edited-post-content.js +15 -0
  24. package/build/editor/get-edited-post-content.js.map +1 -0
  25. package/build/editor/index.js +75 -0
  26. package/build/editor/index.js.map +1 -0
  27. package/build/editor/insert-block.js +23 -0
  28. package/build/editor/insert-block.js.map +1 -0
  29. package/build/editor/open-document-settings-sidebar.js +27 -0
  30. package/build/editor/open-document-settings-sidebar.js.map +1 -0
  31. package/build/editor/preview.js +21 -0
  32. package/build/editor/preview.js.map +1 -0
  33. package/build/editor/publish-post.js +45 -0
  34. package/build/editor/publish-post.js.map +1 -0
  35. package/build/editor/save-draft.js +18 -0
  36. package/build/editor/save-draft.js.map +1 -0
  37. package/build/editor/select-blocks.js +28 -0
  38. package/build/editor/select-blocks.js.map +1 -0
  39. package/build/editor/set-content.js +17 -0
  40. package/build/editor/set-content.js.map +1 -0
  41. package/build/editor/set-is-fixed-toolbar.js +18 -0
  42. package/build/editor/set-is-fixed-toolbar.js.map +1 -0
  43. package/build/editor/set-preferences.js +21 -0
  44. package/build/editor/set-preferences.js.map +1 -0
  45. package/build/editor/show-block-toolbar.js +19 -0
  46. package/build/editor/show-block-toolbar.js.map +1 -0
  47. package/build/editor/site-editor.js +44 -0
  48. package/build/editor/site-editor.js.map +1 -0
  49. package/build/editor/switch-editor-tool.js +33 -0
  50. package/build/editor/switch-editor-tool.js.map +1 -0
  51. package/build/editor/switch-to-legacy-canvas.js +18 -0
  52. package/build/editor/switch-to-legacy-canvas.js.map +1 -0
  53. package/build/editor/transform-block-to.js +24 -0
  54. package/build/editor/transform-block-to.js.map +1 -0
  55. package/build/index.js +19 -0
  56. package/build/index.js.map +1 -0
  57. package/build/lighthouse/index.js +61 -0
  58. package/build/lighthouse/index.js.map +1 -0
  59. package/build/metrics/index.js +314 -0
  60. package/build/metrics/index.js.map +1 -0
  61. package/build/page-utils/drag-files.js +119 -0
  62. package/build/page-utils/drag-files.js.map +1 -0
  63. package/build/page-utils/emulate-network-conditions.js +71 -0
  64. package/build/page-utils/emulate-network-conditions.js.map +1 -0
  65. package/build/page-utils/index.js +37 -0
  66. package/build/page-utils/index.js.map +1 -0
  67. package/build/page-utils/is-current-url.js +21 -0
  68. package/build/page-utils/is-current-url.js.map +1 -0
  69. package/build/page-utils/keycodes.js +41 -0
  70. package/build/page-utils/keycodes.js.map +1 -0
  71. package/build/page-utils/press-keys.js +141 -0
  72. package/build/page-utils/press-keys.js.map +1 -0
  73. package/{src → build}/page-utils/set-browser-viewport.js +12 -14
  74. package/build/page-utils/set-browser-viewport.js.map +1 -0
  75. package/build/request-utils/blocks.js +40 -0
  76. package/build/request-utils/blocks.js.map +1 -0
  77. package/build/request-utils/comments.js +52 -0
  78. package/build/request-utils/comments.js.map +1 -0
  79. package/build/request-utils/gutenberg-experiments.js +47 -0
  80. package/build/request-utils/gutenberg-experiments.js.map +1 -0
  81. package/build/request-utils/index.js +196 -0
  82. package/build/request-utils/index.js.map +1 -0
  83. package/build/request-utils/login.js +21 -0
  84. package/build/request-utils/login.js.map +1 -0
  85. package/build/request-utils/media.js +106 -0
  86. package/build/request-utils/media.js.map +1 -0
  87. package/build/request-utils/menus.js +116 -0
  88. package/build/request-utils/menus.js.map +1 -0
  89. package/build/request-utils/pages.js +53 -0
  90. package/build/request-utils/pages.js.map +1 -0
  91. package/build/request-utils/patterns.js +27 -0
  92. package/build/request-utils/patterns.js.map +1 -0
  93. package/build/request-utils/plugins.js +85 -0
  94. package/build/request-utils/plugins.js.map +1 -0
  95. package/build/request-utils/posts.js +42 -0
  96. package/build/request-utils/posts.js.map +1 -0
  97. package/build/request-utils/preferences.js +54 -0
  98. package/build/request-utils/preferences.js.map +1 -0
  99. package/{src/request-utils/records.ts → build/request-utils/records.js} +10 -15
  100. package/build/request-utils/records.js.map +1 -0
  101. package/build/request-utils/rest.js +181 -0
  102. package/build/request-utils/rest.js.map +1 -0
  103. package/build/request-utils/site-settings.js +33 -0
  104. package/build/request-utils/site-settings.js.map +1 -0
  105. package/build/request-utils/templates.js +54 -0
  106. package/build/request-utils/templates.js.map +1 -0
  107. package/build/request-utils/themes.js +69 -0
  108. package/build/request-utils/themes.js.map +1 -0
  109. package/build/request-utils/users.js +81 -0
  110. package/build/request-utils/users.js.map +1 -0
  111. package/build/request-utils/widgets.js +61 -0
  112. package/build/request-utils/widgets.js.map +1 -0
  113. package/build/test.js +189 -0
  114. package/build/test.js.map +1 -0
  115. package/build/types.js +3 -0
  116. package/build/types.js.map +1 -0
  117. package/build-types/admin/index.d.ts.map +1 -1
  118. package/build-types/config.d.ts.map +1 -1
  119. package/build-types/editor/index.d.ts.map +1 -1
  120. package/build-types/lighthouse/index.d.ts.map +1 -1
  121. package/build-types/metrics/index.d.ts.map +1 -1
  122. package/build-types/page-utils/index.d.ts.map +1 -1
  123. package/build-types/page-utils/press-keys.d.ts +1 -1
  124. package/build-types/page-utils/set-browser-viewport.d.ts +22 -12
  125. package/build-types/page-utils/set-browser-viewport.d.ts.map +1 -1
  126. package/build-types/request-utils/index.d.ts +2 -2
  127. package/build-types/request-utils/index.d.ts.map +1 -1
  128. package/build-types/request-utils/pages.d.ts +1 -1
  129. package/build-types/request-utils/pages.d.ts.map +1 -1
  130. package/build-types/request-utils/widgets.d.ts +2 -2
  131. package/build-types/request-utils/widgets.d.ts.map +1 -1
  132. package/build-types/types.d.ts.map +1 -1
  133. package/package.json +8 -5
  134. package/src/admin/create-new-post.ts +0 -47
  135. package/src/admin/edit-post.ts +0 -24
  136. package/src/admin/index.ts +0 -52
  137. package/src/admin/visit-admin-page.ts +0 -43
  138. package/src/admin/visit-site-editor.ts +0 -62
  139. package/src/admin/wait-for-site-editor.ts +0 -52
  140. package/src/config.ts +0 -12
  141. package/src/editor/click-block-options-menu-item.ts +0 -18
  142. package/src/editor/click-block-toolbar-button.ts +0 -21
  143. package/src/editor/get-blocks.ts +0 -75
  144. package/src/editor/get-edited-post-content.ts +0 -19
  145. package/src/editor/index.ts +0 -95
  146. package/src/editor/insert-block.ts +0 -54
  147. package/src/editor/open-document-settings-sidebar.ts +0 -31
  148. package/src/editor/preview.ts +0 -32
  149. package/src/editor/publish-post.ts +0 -51
  150. package/src/editor/save-draft.ts +0 -20
  151. package/src/editor/select-blocks.ts +0 -48
  152. package/src/editor/set-content.ts +0 -24
  153. package/src/editor/set-is-fixed-toolbar.ts +0 -20
  154. package/src/editor/set-preferences.ts +0 -37
  155. package/src/editor/show-block-toolbar.ts +0 -20
  156. package/src/editor/site-editor.ts +0 -55
  157. package/src/editor/switch-editor-tool.ts +0 -34
  158. package/src/editor/switch-to-legacy-canvas.ts +0 -20
  159. package/src/editor/transform-block-to.ts +0 -34
  160. package/src/index.ts +0 -8
  161. package/src/lighthouse/index.ts +0 -75
  162. package/src/metrics/index.ts +0 -441
  163. package/src/page-utils/drag-files.ts +0 -173
  164. package/src/page-utils/emulate-network-conditions.ts +0 -101
  165. package/src/page-utils/index.ts +0 -54
  166. package/src/page-utils/is-current-url.ts +0 -20
  167. package/src/page-utils/keycodes.ts +0 -48
  168. package/src/page-utils/press-keys.ts +0 -195
  169. package/src/request-utils/blocks.ts +0 -58
  170. package/src/request-utils/comments.ts +0 -80
  171. package/src/request-utils/gutenberg-experiments.ts +0 -62
  172. package/src/request-utils/index.ts +0 -222
  173. package/src/request-utils/login.ts +0 -34
  174. package/src/request-utils/media.ts +0 -102
  175. package/src/request-utils/menus.ts +0 -145
  176. package/src/request-utils/pages.ts +0 -75
  177. package/src/request-utils/patterns.ts +0 -31
  178. package/src/request-utils/plugins.ts +0 -103
  179. package/src/request-utils/posts.ts +0 -69
  180. package/src/request-utils/preferences.ts +0 -63
  181. package/src/request-utils/rest.ts +0 -222
  182. package/src/request-utils/site-settings.ts +0 -58
  183. package/src/request-utils/templates.ts +0 -83
  184. package/src/request-utils/themes.ts +0 -110
  185. package/src/request-utils/users.ts +0 -125
  186. package/src/request-utils/widgets.js +0 -68
  187. package/src/test.ts +0 -204
  188. package/src/types.ts +0 -13
@@ -1,75 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- const PAGE_STATUS = [
7
- 'publish',
8
- 'future',
9
- 'draft',
10
- 'pending',
11
- 'private',
12
- 'trash',
13
- ] as const;
14
-
15
- export type Page = {
16
- id: number;
17
- status: ( typeof PAGE_STATUS )[ number ];
18
- };
19
-
20
- export type CreatePagePayload = {
21
- title?: string;
22
- content?: string;
23
- status: ( typeof PAGE_STATUS )[ number ];
24
- date?: string;
25
- date_gmt?: string;
26
- };
27
-
28
- export async function deletePage( this: RequestUtils, id: number ) {
29
- // https://developer.wordpress.org/rest-api/reference/pages/#delete-a-page
30
- return await this.rest( {
31
- method: 'DELETE',
32
- path: `/wp/v2/pages/${ id }`,
33
- params: {
34
- force: true,
35
- },
36
- } );
37
- }
38
-
39
- /**
40
- * Delete all pages using REST API.
41
- *
42
- * @param this
43
- */
44
- export async function deleteAllPages( this: RequestUtils ) {
45
- // List all pages.
46
- // https://developer.wordpress.org/rest-api/reference/pages/#list-pages
47
- const pages = await this.rest< Page[] >( {
48
- path: '/wp/v2/pages',
49
- params: {
50
- per_page: 100,
51
-
52
- status: PAGE_STATUS.join( ',' ),
53
- },
54
- } );
55
-
56
- // Delete all pages one by one.
57
- // "/wp/v2/pages" not yet supports batch requests.
58
- await Promise.all(
59
- pages.map( ( page ) => deletePage.call( this, page.id ) )
60
- );
61
- }
62
-
63
- /**
64
- * Create a new page.
65
- *
66
- * @param this
67
- * @param payload The page payload.
68
- */
69
- export async function createPage(
70
- this: RequestUtils,
71
- payload: CreatePagePayload
72
- ) {
73
- // https://developer.wordpress.org/rest-api/reference/pages/#create-a-page
74
- return this.createRecord< Page >( 'pages', { ...payload } );
75
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- /**
7
- * Delete all pattern categories using REST API.
8
- *
9
- * @see https://developer.wordpress.org/rest-api/reference/categories/#list-categories
10
- * @param this
11
- */
12
- export async function deleteAllPatternCategories( this: RequestUtils ) {
13
- // List all pattern categories.
14
- // https://developer.wordpress.org/rest-api/reference/categories/#list-categories
15
- const categories = await this.rest( {
16
- path: '/wp/v2/wp_pattern_category',
17
- params: {
18
- per_page: 100,
19
- },
20
- } );
21
-
22
- // Delete pattern categories.
23
- // https://developer.wordpress.org/rest-api/reference/categories/#delete-a-category
24
- // "/wp/v2/category" does not yet supports batch requests.
25
- await this.batchRest(
26
- categories.map( ( category: { id: number } ) => ( {
27
- method: 'DELETE',
28
- path: `/wp/v2/wp_pattern_category/${ category.id }?force=true`,
29
- } ) )
30
- );
31
- }
@@ -1,103 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { paramCase as kebabCase } from 'change-case';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import type { RequestUtils } from './index';
10
-
11
- /**
12
- * Fetch the plugins from API and cache them in memory,
13
- * since they are unlikely to change during testing.
14
- *
15
- * @param this
16
- * @param forceRefetch Force refetch the installed plugins to update the cache.
17
- */
18
- async function getPluginsMap( this: RequestUtils, forceRefetch = false ) {
19
- if ( ! forceRefetch && this.pluginsMap ) {
20
- return this.pluginsMap;
21
- }
22
-
23
- const plugins = await this.rest( {
24
- path: '/wp/v2/plugins',
25
- } );
26
- this.pluginsMap = {};
27
- for ( const plugin of plugins ) {
28
- // Ideally, we should be using sanitize_title() in PHP rather than kebabCase(),
29
- // but we don't have the exact port of it in JS.
30
- // This is a good approximation though.
31
- const slug = kebabCase( plugin.name.toLowerCase() );
32
- this.pluginsMap[ slug ] = plugin.plugin;
33
- }
34
- return this.pluginsMap;
35
- }
36
-
37
- /**
38
- * Finds a plugin in the plugin map.
39
- *
40
- * Attempts to provide a helpful error message if not found.
41
- *
42
- * @param slug Plugin slug.
43
- * @param pluginsMap Plugins map.
44
- */
45
- function getPluginFromMap(
46
- slug: string,
47
- pluginsMap: Record< string, string >
48
- ) {
49
- const plugin = pluginsMap[ slug ];
50
-
51
- if ( ! plugin ) {
52
- for ( const key of Object.keys( pluginsMap ) ) {
53
- if (
54
- key.toLowerCase().replace( /-/g, '' ) ===
55
- slug.toLowerCase().replace( /-/g, '' )
56
- ) {
57
- throw new Error(
58
- `The plugin "${ slug }" isn't installed. Did you perhaps mean "${ key }"?`
59
- );
60
- }
61
- }
62
-
63
- throw new Error( `The plugin "${ slug }" isn't installed` );
64
- }
65
-
66
- return plugin;
67
- }
68
-
69
- /**
70
- * Activates an installed plugin.
71
- *
72
- * @param this RequestUtils.
73
- * @param slug Plugin slug.
74
- */
75
- async function activatePlugin( this: RequestUtils, slug: string ) {
76
- const pluginsMap = await this.getPluginsMap();
77
- const plugin = getPluginFromMap( slug, pluginsMap );
78
-
79
- await this.rest( {
80
- method: 'PUT',
81
- path: `/wp/v2/plugins/${ plugin }`,
82
- data: { status: 'active' },
83
- } );
84
- }
85
-
86
- /**
87
- * Deactivates an active plugin.
88
- *
89
- * @param this RequestUtils.
90
- * @param slug Plugin slug.
91
- */
92
- async function deactivatePlugin( this: RequestUtils, slug: string ) {
93
- const pluginsMap = await this.getPluginsMap();
94
- const plugin = getPluginFromMap( slug, pluginsMap );
95
-
96
- await this.rest( {
97
- method: 'PUT',
98
- path: `/wp/v2/plugins/${ plugin }`,
99
- data: { status: 'inactive' },
100
- } );
101
- }
102
-
103
- export { getPluginsMap, activatePlugin, deactivatePlugin };
@@ -1,69 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- export interface Post {
7
- id: number;
8
- content: string;
9
- status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
10
- link: string;
11
- }
12
-
13
- export interface CreatePostPayload {
14
- title?: string;
15
- content?: string;
16
- status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
17
- date?: string;
18
- date_gmt: string;
19
- }
20
-
21
- /**
22
- * Delete all posts using REST API.
23
- *
24
- * @param this
25
- * @param postType The type of post to delete. Defaults to 'posts'.
26
- */
27
- export async function deleteAllPosts(
28
- this: RequestUtils,
29
- postType: string = 'posts'
30
- ) {
31
- // List all posts.
32
- // https://developer.wordpress.org/rest-api/reference/posts/#list-posts
33
- const posts = await this.rest< Post[] >( {
34
- path: `/wp/v2/${ postType }`,
35
- params: {
36
- per_page: 100,
37
- // All possible statuses.
38
- status: 'publish,future,draft,pending,private,trash',
39
- },
40
- } );
41
-
42
- // Delete all posts one by one.
43
- // https://developer.wordpress.org/rest-api/reference/posts/#delete-a-post
44
- // "/wp/v2/posts" not yet supports batch requests.
45
- await Promise.all(
46
- posts.map( ( post ) =>
47
- this.rest( {
48
- method: 'DELETE',
49
- path: `/wp/v2/${ postType }/${ post.id }`,
50
- params: {
51
- force: true,
52
- },
53
- } )
54
- )
55
- );
56
- }
57
-
58
- /**
59
- * Creates a new post using the REST API.
60
- *
61
- * @param this
62
- * @param payload Post attributes.
63
- */
64
- export async function createPost(
65
- this: RequestUtils,
66
- payload: CreatePostPayload
67
- ) {
68
- return this.createRecord< Post >( 'posts', { ...payload } );
69
- }
@@ -1,63 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- /**
7
- * Reset user preferences
8
- *
9
- * @param this Request utils.
10
- */
11
- export async function resetPreferences( this: RequestUtils ) {
12
- await this.rest( {
13
- path: '/wp/v2/users/me',
14
- method: 'PUT',
15
- data: {
16
- meta: {
17
- persisted_preferences: {},
18
- },
19
- },
20
- } );
21
- }
22
-
23
- /**
24
- * Set scoped user preferences via the REST API.
25
- *
26
- * Reads the current persisted preferences, merges the new values under the
27
- * given scope, and writes them back.
28
- *
29
- * @param this Request utils.
30
- * @param scope The preference scope (e.g. 'core/edit-post').
31
- * @param preferences Key/value map of preference names to values.
32
- */
33
- export async function setPreferences(
34
- this: RequestUtils,
35
- scope: string,
36
- preferences: Record< string, any >
37
- ) {
38
- const user = await this.rest< {
39
- meta: { persisted_preferences: Record< string, any > };
40
- } >( {
41
- path: '/wp/v2/users/me',
42
- method: 'GET',
43
- params: { context: 'edit' },
44
- } );
45
-
46
- const currentPreferences = user.meta?.persisted_preferences ?? {};
47
-
48
- await this.rest( {
49
- path: '/wp/v2/users/me',
50
- method: 'PUT',
51
- data: {
52
- meta: {
53
- persisted_preferences: {
54
- ...currentPreferences,
55
- [ scope ]: {
56
- ...( currentPreferences[ scope ] ?? {} ),
57
- ...preferences,
58
- },
59
- },
60
- },
61
- },
62
- } );
63
- }
@@ -1,222 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import * as fs from 'fs/promises';
5
- import { dirname } from 'path';
6
- import { expect } from '@playwright/test';
7
- import type { APIRequestContext } from '@playwright/test';
8
-
9
- /**
10
- * Internal dependencies
11
- */
12
- import { WP_BASE_URL } from '../config';
13
- import type { RequestUtils, StorageState } from './index';
14
-
15
- function splitRequestsToChunks( requests: BatchRequest[], chunkSize: number ) {
16
- const arr = [ ...requests ];
17
- const cache = [];
18
- while ( arr.length ) {
19
- cache.push( arr.splice( 0, chunkSize ) );
20
- }
21
-
22
- return cache;
23
- }
24
-
25
- async function getAPIRootURL( request: APIRequestContext ) {
26
- // Discover the API root url using link header.
27
- // See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#link-header
28
- const response = await request.head( WP_BASE_URL );
29
- const links = response.headers().link;
30
- const restLink = links?.match( /<([^>]+)>; rel="https:\/\/api\.w\.org\/"/ );
31
-
32
- if ( ! restLink ) {
33
- throw new Error( `Failed to discover REST API endpoint.
34
- Link header: ${ links }` );
35
- }
36
-
37
- const [ , rootURL ] = restLink;
38
-
39
- return rootURL;
40
- }
41
-
42
- async function setupRest( this: RequestUtils ): Promise< StorageState > {
43
- let nonce = '';
44
- let rootURL = '';
45
-
46
- // Poll until the REST API is discovered.
47
- // See https://github.com/WordPress/gutenberg/issues/61627
48
- await expect
49
- .poll(
50
- async () => {
51
- try {
52
- [ nonce, rootURL ] = await Promise.all( [
53
- this.login(),
54
- getAPIRootURL( this.request ),
55
- ] );
56
- } catch ( error ) {
57
- // Prints the error if the timeout is reached.
58
- return error;
59
- }
60
-
61
- return !! ( nonce && rootURL );
62
- },
63
- {
64
- message: 'Failed to setup REST API.',
65
- timeout: 60_000, // 1 minute.
66
- }
67
- )
68
- .toBe( true );
69
-
70
- const { cookies } = await this.request.storageState();
71
-
72
- const storageState: StorageState = {
73
- cookies,
74
- nonce,
75
- rootURL,
76
- };
77
-
78
- if ( this.storageStatePath ) {
79
- await fs.mkdir( dirname( this.storageStatePath ), { recursive: true } );
80
- await fs.writeFile(
81
- this.storageStatePath,
82
- JSON.stringify( storageState ),
83
- 'utf-8'
84
- );
85
- }
86
-
87
- this.storageState = storageState;
88
-
89
- return storageState;
90
- }
91
-
92
- type RequestFetchOptions = Exclude<
93
- Parameters< APIRequestContext[ 'fetch' ] >[ 1 ],
94
- undefined
95
- >;
96
- export interface RestOptions extends RequestFetchOptions {
97
- path: string;
98
- }
99
-
100
- async function rest< RestResponse = any >(
101
- this: RequestUtils,
102
- options: RestOptions
103
- ): Promise< RestResponse > {
104
- const { path, ...fetchOptions } = options;
105
-
106
- if ( ! path ) {
107
- throw new Error( '"path" is required to make a REST call' );
108
- }
109
-
110
- if ( ! this.storageState?.nonce || ! this.storageState?.rootURL ) {
111
- await this.setupRest();
112
- }
113
-
114
- const relativePath = path.startsWith( '/' ) ? path.slice( 1 ) : path;
115
-
116
- const url = this.storageState!.rootURL + relativePath;
117
-
118
- try {
119
- const response = await this.request.fetch( url, {
120
- ...fetchOptions,
121
- failOnStatusCode: false,
122
- headers: {
123
- 'X-WP-Nonce': this.storageState!.nonce,
124
- ...( fetchOptions.headers || {} ),
125
- },
126
- } );
127
- const json: RestResponse = await response.json();
128
-
129
- if ( ! response.ok() ) {
130
- throw json;
131
- }
132
-
133
- return json;
134
- } catch ( error ) {
135
- // Nonce in invalid, retry again with a renewed nonce.
136
- if (
137
- typeof error === 'object' &&
138
- error !== null &&
139
- Object.prototype.hasOwnProperty.call( error, 'code' ) &&
140
- ( error as { code: string } ).code === 'rest_cookie_invalid_nonce'
141
- ) {
142
- await this.setupRest();
143
-
144
- return this.rest( options );
145
- }
146
-
147
- throw error;
148
- }
149
- }
150
-
151
- /**
152
- * Get the maximum batch size for the REST API.
153
- *
154
- * @param this
155
- * @param forceRefetch Force revalidate the cached max batch size.
156
- */
157
- async function getMaxBatchSize( this: RequestUtils, forceRefetch = false ) {
158
- if ( ! forceRefetch && this.maxBatchSize ) {
159
- return this.maxBatchSize;
160
- }
161
-
162
- const response = await this.rest< {
163
- endpoints: {
164
- args: {
165
- requests: {
166
- maxItems: number;
167
- };
168
- };
169
- }[];
170
- } >( {
171
- method: 'OPTIONS',
172
- path: '/batch/v1',
173
- } );
174
- this.maxBatchSize = response.endpoints[ 0 ].args.requests.maxItems;
175
- return this.maxBatchSize;
176
- }
177
-
178
- export interface BatchRequest {
179
- method?: string;
180
- path: string;
181
- headers?: Record< string, string | string[] >;
182
- body?: any;
183
- }
184
-
185
- async function batchRest< BatchResponse >(
186
- this: RequestUtils,
187
- requests: BatchRequest[]
188
- ): Promise< BatchResponse[] > {
189
- const maxBatchSize = await this.getMaxBatchSize();
190
-
191
- if ( requests.length > maxBatchSize ) {
192
- const chunks = splitRequestsToChunks( requests, maxBatchSize );
193
-
194
- const chunkResponses = await Promise.all(
195
- chunks.map( ( chunkRequests ) =>
196
- this.batchRest< BatchResponse >( chunkRequests )
197
- )
198
- );
199
-
200
- return chunkResponses.flat();
201
- }
202
-
203
- const batchResponses = await this.rest< {
204
- failed?: string;
205
- responses: BatchResponse[];
206
- } >( {
207
- method: 'POST',
208
- path: '/batch/v1',
209
- data: {
210
- requests,
211
- validation: 'require-all-validate',
212
- },
213
- } );
214
-
215
- if ( batchResponses.failed ) {
216
- throw batchResponses;
217
- }
218
-
219
- return batchResponses.responses;
220
- }
221
-
222
- export { setupRest, rest, getMaxBatchSize, batchRest };
@@ -1,58 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- type SiteSettings = {
7
- title: string;
8
- description: string;
9
- url: string;
10
- email: string;
11
- timezone: string;
12
- date_format: string;
13
- time_format: string;
14
- start_of_week: number;
15
- language: string;
16
- use_smilies: boolean;
17
- default_category: number;
18
- default_post_format: string;
19
- posts_per_page: number;
20
- default_ping_status: 'open' | 'closed';
21
- default_comment_status: 'open' | 'closed';
22
- show_on_front: 'posts' | 'page';
23
- page_on_front: number;
24
- page_for_posts: number;
25
- };
26
-
27
- /**
28
- * Get the site settings.
29
- *
30
- * @see https://developer.wordpress.org/rest-api/reference/settings/#retrieve-a-site-setting
31
- *
32
- * @param this RequestUtils.
33
- */
34
- export async function getSiteSettings( this: RequestUtils ) {
35
- return await this.rest< SiteSettings >( {
36
- path: '/wp/v2/settings',
37
- method: 'GET',
38
- } );
39
- }
40
-
41
- /**
42
- * Update the site settings.
43
- *
44
- * @see https://developer.wordpress.org/rest-api/reference/settings/#update-a-site-setting
45
- *
46
- * @param this RequestUtils.
47
- * @param siteSettings The partial settings payload to update.
48
- */
49
- export async function updateSiteSettings(
50
- this: RequestUtils,
51
- siteSettings: Partial< SiteSettings >
52
- ) {
53
- return await this.rest< SiteSettings >( {
54
- path: '/wp/v2/settings',
55
- method: 'POST',
56
- data: siteSettings,
57
- } );
58
- }
@@ -1,83 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import type { RequestUtils } from './index';
5
-
6
- type TemplateType = 'wp_template' | 'wp_template_part';
7
-
8
- interface Template {
9
- wp_id: number;
10
- id: string;
11
- }
12
-
13
- interface CreateTemplatePayload {
14
- slug: string;
15
- title?: string;
16
- content?: string;
17
- description?: string;
18
- }
19
-
20
- const PATH_MAPPING = {
21
- wp_template: '/wp/v2/templates',
22
- wp_template_part: '/wp/v2/template-parts',
23
- };
24
-
25
- /**
26
- * Delete all the templates of given type.
27
- *
28
- * @param this
29
- * @param type - Template type to delete.
30
- */
31
- async function deleteAllTemplates( this: RequestUtils, type: TemplateType ) {
32
- const path = PATH_MAPPING[ type ];
33
-
34
- if ( ! path ) {
35
- throw new Error( `Unsupported template type: ${ type }.` );
36
- }
37
-
38
- const templates = await this.rest< Template[] >( { path } );
39
-
40
- for ( const template of templates ) {
41
- if ( ! template?.id || ! template?.wp_id ) {
42
- continue;
43
- }
44
-
45
- try {
46
- await this.rest( {
47
- method: 'DELETE',
48
- path: `${ path }/${ template.id }`,
49
- params: { force: true },
50
- } );
51
- } catch ( responseError ) {
52
- // Disable reason - the error provides valuable feedback about issues with tests.
53
- // eslint-disable-next-line no-console
54
- console.warn(
55
- `deleteAllTemplates failed to delete template (id: ${ template.wp_id }) with the following error`,
56
- responseError
57
- );
58
- }
59
- }
60
- }
61
-
62
- /**
63
- * Creates a new template using the REST API.
64
- *
65
- * @param this
66
- * @param type Template type to delete.
67
- * @param payload Template attributes.
68
- */
69
- async function createTemplate(
70
- this: RequestUtils,
71
- type: TemplateType,
72
- payload: CreateTemplatePayload
73
- ) {
74
- const template = await this.rest< Template >( {
75
- method: 'POST',
76
- path: PATH_MAPPING[ type ],
77
- params: { ...payload, type, status: 'publish', is_wp_suggestion: true },
78
- } );
79
-
80
- return template;
81
- }
82
-
83
- export { deleteAllTemplates, createTemplate };