@@ -21,34 +21,15 @@ function splitRequestsToChunks( requests: BatchRequest[], chunkSize: number ) {
2121 return cache ;
2222}
2323
24- async function getAPIRootURL ( request : APIRequestContext ) {
25- // Discover the API root url using link header.
26- // See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#link-header
27- const response = await request . head ( WP_BASE_URL ) ;
28- const links = response . headers ( ) . link ;
29- const restLink = links ?. match ( / < ( [ ^ > ] + ) > ; r e l = " h t t p s : \/ \/ a p i \. w \. o r g \/ " / ) ;
30-
31- if ( ! restLink ) {
32- throw new Error ( `Failed to discover REST API endpoint.
33- Link header: ${ links } ` ) ;
34- }
35-
36- const [ , rootURL ] = restLink ;
37-
38- return rootURL ;
39- }
40-
4124async function setupRest ( this : RequestUtils ) : Promise < StorageState > {
42- const [ nonce , rootURL ] = await Promise . all ( [
43- this . login ( ) ,
44- getAPIRootURL ( this . request ) ,
45- ] ) ;
25+ const rootURL = 'https://public-api.wordpress.com/' ;
26+ const bearerToken = await this . login ( ) ;
4627
4728 const { cookies } = await this . request . storageState ( ) ;
4829
4930 const storageState : StorageState = {
5031 cookies,
51- nonce ,
32+ bearerToken ,
5233 rootURL,
5334 } ;
5435
@@ -84,11 +65,18 @@ async function rest< RestResponse = any >(
8465 throw new Error ( '"path" is required to make a REST call' ) ;
8566 }
8667
87- if ( ! this . storageState ?. nonce || ! this . storageState ?. rootURL ) {
68+ if ( ! this . storageState ?. bearerToken || ! this . storageState ?. rootURL ) {
8869 await this . setupRest ( ) ;
8970 }
9071
91- const relativePath = path . startsWith ( '/' ) ? path . slice ( 1 ) : path ;
72+ const wpcomPath = path . replace (
73+ '/wp/v2/' ,
74+ `/wp/v2/sites/${ new URL ( WP_BASE_URL ) . host } /`
75+ ) ;
76+
77+ const relativePath = wpcomPath . startsWith ( '/' )
78+ ? wpcomPath . slice ( 1 )
79+ : wpcomPath ;
9280
9381 const url = this . storageState ! . rootURL + relativePath ;
9482
@@ -97,7 +85,7 @@ async function rest< RestResponse = any >(
9785 ...fetchOptions ,
9886 failOnStatusCode : false ,
9987 headers : {
100- 'X-WP-Nonce' : this . storageState ! . nonce ,
88+ Authorization : `Bearer ${ this . storageState ! . bearerToken } ` ,
10189 ...( fetchOptions . headers || { } ) ,
10290 } ,
10391 } ) ;
@@ -110,16 +98,16 @@ async function rest< RestResponse = any >(
11098 return json ;
11199 } catch ( error ) {
112100 // Nonce in invalid, retry again with a renewed nonce.
113- if (
114- typeof error === 'object' &&
115- error !== null &&
116- Object . prototype . hasOwnProperty . call ( error , 'code' ) &&
117- ( error as { code : string } ) . code === 'rest_cookie_invalid_nonce'
118- ) {
119- await this . setupRest ( ) ;
120-
121- return this . rest ( options ) ;
122- }
101+ // if (
102+ // typeof error === 'object' &&
103+ // error !== null &&
104+ // Object.prototype.hasOwnProperty.call( error, 'code' ) &&
105+ // ( error as { code: string } ).code === 'rest_cookie_invalid_nonce'
106+ // ) {
107+ // await this.setupRest();
108+
109+ // return this.rest( options );
110+ // }
123111
124112 throw error ;
125113 }
0 commit comments