| import fs from 'fs'; |
| import FormData from 'form-data'; |
| import { SecretsManager } from './secrets'; |
| import { |
| BearerTokenErrorResponse, |
| TestFile, |
| SettingsParams, |
| PluginParams, |
| AllDomainsResponse, |
| DomainData, |
| PublicizeConnectionDeletedResponse, |
| PublicizeConnection, |
| SubscriberDeletedResponse, |
| PostCountsResponse, |
| } from './types'; |
| import type { Roles } from './lib'; |
| import type { |
| AccountDetails, |
| BearerTokenResponse, |
| MyAccountInformationResponse, |
| AccountClosureResponse, |
| SiteDeletionResponse, |
| CalypsoPreferencesResponse, |
| ErrorResponse, |
| AccountCredentials, |
| NewSiteResponse, |
| NewSiteParams, |
| NewInviteResponse, |
| NewCommentResponse, |
| AllInvitesResponse, |
| DeleteInvitesResponse, |
| NewPostParams, |
| NewMediaResponse, |
| PostResponse, |
| ReaderResponse, |
| Invite, |
| AllPluginsResponse, |
| PluginResponse, |
| PluginRemovalResponse, |
| AllWidgetsResponse, |
| CommentLikeResponse, |
| JetpackSearchResponse, |
| JetpackSearchParams, |
| Subscriber, |
| SitePostState, |
| } from './types'; |
|
|
| |
|
|
| |
| |
| |
| type EndpointVersions = '1' | '1.1' | '1.2' | '1.3' | '2'; |
| type EndpointNamespace = 'rest' | 'wpcom' | 'wp'; |
|
|
| |
| |
| |
|
|
| type RequestParams = Pick< RequestInit, 'method' | 'headers' | 'body' >; |
|
|
| |
|
|
| export const BEARER_TOKEN_URL = 'https://wordpress.com/wp-login.php?action=login-endpoint'; |
| const REST_API_BASE_URL = 'https://public-api.wordpress.com'; |
|
|
| |
| |
| |
| |
| |
| |
| export class RestAPIClient { |
| readonly credentials: AccountCredentials; |
| private bearerToken: string | null; |
|
|
| |
| |
| |
| |
| |
| |
| constructor( credentials: AccountCredentials, bearerToken?: string ) { |
| this.credentials = credentials; |
| this.bearerToken = bearerToken ? bearerToken : null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async getBearerToken(): Promise< string > { |
| if ( this.bearerToken !== null ) { |
| return this.bearerToken; |
| } |
|
|
| |
| const params = new URLSearchParams(); |
| params.append( 'username', this.credentials.username ); |
| params.append( 'password', this.credentials.password ); |
| params.append( 'client_id', SecretsManager.secrets.calypsoOauthApplication.client_id ); |
| params.append( 'client_secret', SecretsManager.secrets.calypsoOauthApplication.client_secret ); |
| params.append( 'get_bearer_token', '1' ); |
|
|
| |
| const response: BearerTokenResponse | BearerTokenErrorResponse = await this.sendRequest( |
| new URL( BEARER_TOKEN_URL ), |
| { |
| method: 'post', |
| body: params, |
| } |
| ); |
|
|
| if ( response.success === false ) { |
| const firstError = response.data.errors.at( 0 ); |
| throw new Error( `${ firstError?.code }: ${ firstError?.message }` ); |
| } |
|
|
| this.bearerToken = response.data.bearer_token; |
| return response.data.bearer_token; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| async getAuthorizationHeader( scheme: 'bearer' ): Promise< string > { |
| if ( scheme === 'bearer' ) { |
| const bearerToken = await this.getBearerToken(); |
| return `Bearer ${ bearerToken }`; |
| } |
|
|
| throw new Error( 'Unsupported authorization scheme specified.' ); |
| } |
|
|
| |
| |
| |
| |
| |
| private getContentTypeHeader( value: 'json' ): string { |
| return `application/${ value }`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| getRequestURL( |
| version: EndpointVersions, |
| endpoint: string, |
| namespace: EndpointNamespace = 'rest' |
| ): URL { |
| const path = `/${ namespace }/v${ version }/${ endpoint }`.replace( /([^:]\/)\/+/g, '$1' ); |
| const sanitizedPath = path.trimEnd().replace( /\/$/, '' ); |
| return new URL( sanitizedPath, REST_API_BASE_URL ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async sendRequest( url: URL, params: RequestParams | URLSearchParams ): Promise< any > { |
| const response = await fetch( url.toString(), params as RequestInit ); |
| return response.json(); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async getAllDomains(): Promise< AllDomainsResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( this.getRequestURL( '1.1', '/all-domains/' ), params ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async createSite( newSiteParams: NewSiteParams ): Promise< NewSiteResponse > { |
| const body = { |
| client_id: SecretsManager.secrets.calypsoOauthApplication.client_id, |
| client_secret: SecretsManager.secrets.calypsoOauthApplication.client_secret, |
| blog_name: newSiteParams.name, |
| blog_title: newSiteParams.title, |
| }; |
|
|
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( body ), |
| }; |
|
|
| const response = await this.sendRequest( this.getRequestURL( '1.1', '/sites/new' ), params ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| |
| |
| response[ 'blog_details' ][ 'blogid' ] = parseInt( response[ 'blog_details' ][ 'blogid' ] ); |
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async deleteSite( targetSite: { |
| id: number; |
| domain: string; |
| } ): Promise< SiteDeletionResponse | null > { |
| |
| if ( ! targetSite.domain.includes( 'e2e' ) ) { |
| return null; |
| } |
|
|
| console.log( `Deleting site ${ targetSite.domain }.` ); |
|
|
| const scheme = 'http://'; |
| const targetDomain = targetSite.domain.startsWith( scheme ) |
| ? targetSite.domain.replace( scheme, '' ) |
| : targetSite.domain; |
|
|
| const mySites: AllDomainsResponse = await this.getAllDomains(); |
|
|
| const match = mySites.domains.filter( ( site: DomainData ) => { |
| site.blog_id === targetSite.id && site.domain === targetDomain; |
| } ); |
|
|
| if ( ! match ) { |
| return null; |
| } |
|
|
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ targetSite.id }/delete` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| console.warn( `Failed to delete site ID ${ targetSite.domain }` ); |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| console.log( `Successfully deleted site ID ${ targetSite.domain }` ); |
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async createInvite( |
| siteID: number, |
| { |
| email, |
| role, |
| message, |
| }: { |
| email: string[]; |
| role: Roles; |
| message?: string; |
| } |
| ): Promise< NewInviteResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( { |
| invitees: email, |
| role: role, |
| message: message !== undefined ? message : '', |
| } ), |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/invites/new` ), |
| params |
| ); |
|
|
| |
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| |
| |
| if ( response.errors.length ) { |
| for ( const err of response.errors ) { |
| console.error( `${ err.code }: ${ err.message }` ); |
| } |
| throw new Error( `Failed to create invite due to ${ response.errors.length } errors.` ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| async getInvites( siteID: number ): Promise< AllInvitesResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/invites` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response[ 'invites' ]; |
| } |
|
|
| |
| |
| |
| |
| |
| async deleteInvite( siteID: number, email: string ): Promise< boolean > { |
| const invites = await this.getInvites( siteID ); |
|
|
| let inviteID = undefined; |
|
|
| Object.values( invites ).forEach( ( invite: Invite ) => { |
| if ( |
| invite.invited_by.site_ID === siteID && |
| invite.is_pending && |
| invite.user.email === email |
| ) { |
| inviteID = invite.invite_key; |
| } |
| } ); |
|
|
| if ( inviteID === undefined ) { |
| throw new Error( |
| `Aborting invite deletion: inviteID not found for email: ${ email } and siteID: ${ siteID }}` |
| ); |
| } |
|
|
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( { |
| invite_ids: [ inviteID ], |
| } ), |
| }; |
|
|
| const response: DeleteInvitesResponse = await this.sendRequest( |
| this.getRequestURL( '2', `/sites/${ siteID }/invites/delete`, 'wpcom' ), |
| params |
| ); |
|
|
| |
| |
| |
| |
| if ( response.deleted.includes( inviteID ) ) { |
| return true; |
| } |
| return false; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| async getMyAccountInformation(): Promise< MyAccountInformationResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( this.getRequestURL( '1.1', '/me' ), params ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async setMySettings( details: SettingsParams ): Promise< { [ key: string ]: string | number } > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( details ), |
| }; |
|
|
| const response = await this.sendRequest( this.getRequestURL( '1.1', '/me/settings' ), params ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async closeAccount( expectedAccountDetails: AccountDetails ): Promise< AccountClosureResponse > { |
| const accountInformation = await this.getMyAccountInformation(); |
|
|
| |
| |
| |
| |
|
|
| |
| |
| if ( ! accountInformation.email.includes( 'mailosaur' ) ) { |
| console.warn( |
| 'Aborting account closure: email address provided is not for an e2e test user.' |
| ); |
| return { success: false }; |
| } |
|
|
| if ( ! accountInformation.username.includes( 'e2eflowtesting' ) ) { |
| console.warn( 'Aborting account closure: username is not for a test user.' ); |
| return { success: false }; |
| } |
|
|
| |
| |
| if ( expectedAccountDetails.userID !== accountInformation.ID ) { |
| console.warn( |
| `Failed to close account: target account user ID did not match.\nExpected: ${ accountInformation.ID }, Got: ${ expectedAccountDetails.userID }` |
| ); |
| return { success: false }; |
| } |
|
|
| if ( expectedAccountDetails.username !== accountInformation.username ) { |
| console.warn( |
| `Failed to close account: target account username did not match.\nExpected: ${ accountInformation.username }, Got: ${ expectedAccountDetails.username }` |
| ); |
| return { success: false }; |
| } |
|
|
| if ( expectedAccountDetails.email !== accountInformation.email ) { |
| console.warn( |
| `Failed to close account: target account email did not match.\nExpected: ${ accountInformation.email }, Got: ${ expectedAccountDetails.email }` |
| ); |
| return { success: false }; |
| } |
|
|
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| return await this.sendRequest( this.getRequestURL( '1.1', '/me/account/close' ), params ); |
| } |
|
|
| |
| |
| |
| |
| |
| async getCalypsoPreferences(): Promise< CalypsoPreferencesResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| return await this.sendRequest( this.getRequestURL( '1.1', '/me/preferences' ), params ); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| async getReaderFeed(): Promise< ReaderResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', '/read/following' ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| async siteHasPost( |
| siteID: number, |
| { state = 'publish' }: { state: SitePostState } |
| ): Promise< boolean > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response: PostCountsResponse = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/post-counts/post` ), |
| params |
| ); |
|
|
| return response.counts.all[ state ] !== undefined && response.counts.all[ state ] > 0; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async createPost( siteID: number, details: NewPostParams ): Promise< PostResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( details ), |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/posts/new` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async deletePost( siteID: number, postID: number ): Promise< PostResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/posts/${ postID }/delete` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async createComment( |
| siteID: number, |
| postID: number, |
| comment: string |
| ): Promise< NewCommentResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( { content: comment } ), |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/posts/${ postID }/replies/new` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| async deleteComment( siteID: number, commentID: number ): Promise< any > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/comments/${ commentID }/delete` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async commentAction( |
| action: 'like' | 'unlike', |
| siteID: number, |
| commentID: number |
| ): Promise< CommentLikeResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| let endpoint: URL; |
| if ( action === 'like' ) { |
| endpoint = this.getRequestURL( |
| '1.1', |
| `/sites/${ siteID }/comments/${ commentID }/likes/new` |
| ); |
| } else { |
| endpoint = this.getRequestURL( |
| '1.1', |
| `/sites/${ siteID }/comments/${ commentID }/likes/mine/delete` |
| ); |
| } |
|
|
| const response = await this.sendRequest( endpoint, params ); |
|
|
| |
| |
| if ( action === 'like' && response.like_count !== 1 ) { |
| throw new Error( `Failed to like ${ commentID } on site ${ siteID }` ); |
| } |
| |
| |
| if ( action === 'unlike' && response.like_count !== 0 ) { |
| throw new Error( `Failed to unlike ${ commentID } on site ${ siteID }` ); |
| } |
|
|
| |
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async uploadMedia( |
| siteID: number, |
| { media, mediaURL }: { media?: TestFile; mediaURL?: string } |
| ): Promise< NewMediaResponse > { |
| let params: RequestParams | undefined; |
|
|
| if ( ! media && ! mediaURL ) { |
| throw new Error( 'Either `media` or `mediaURL` parameter must be defined.' ); |
| } |
|
|
| if ( media ) { |
| const data = new FormData(); |
| data.append( 'media[]', fs.createReadStream( media.fullpath ) ); |
|
|
| params = { |
| method: 'post', |
| headers: { |
| |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| ...data.getHeaders(), |
| }, |
| body: data.getBuffer(), |
| }; |
| } |
| if ( mediaURL ) { |
| params = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( { media_urls: mediaURL } ), |
| }; |
| } |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/media/new` ), |
| params as RequestParams |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| async clearShoppingCart( siteID: number ): Promise< { success: true } > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/shopping-cart/clear` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| async getAllPlugins( siteID: number ): Promise< AllPluginsResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/plugins` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async modifyPlugin( |
| siteID: number, |
| pluginID: string, |
| details: PluginParams |
| ): Promise< PluginResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| body: JSON.stringify( details ), |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/plugins/${ pluginID }` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async removePluginIfInstalled( |
| siteID: number, |
| pluginName: string |
| ): Promise< PluginRemovalResponse | null > { |
| const myPlugins = await this.getAllPlugins( siteID ); |
| for ( const plugin of myPlugins.plugins ) { |
| if ( plugin.name === pluginName ) { |
| const pluginID = encodeURIComponent( plugin.id ); |
| |
| if ( plugin.active ) { |
| await this.modifyPlugin( siteID, pluginID, { active: false } ); |
| } |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| return await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/plugins/${ pluginID }/delete` ), |
| params |
| ); |
| } |
| } |
|
|
| |
| return null; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async deleteWidget( siteID: number, widgetID: string ): Promise< void > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/widgets/widget:${ widgetID }/delete` ), |
| params |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| if ( response.hasOwnProperty( 'error' ) && response.error === 'not_found' ) { |
| console.info( `Widget ${ widgetID } not found.` ); |
| return; |
| } else if ( response.length === 0 ) { |
| console.info( `Deleted widget ${ widgetID }.` ); |
| } else if ( response.id === widgetID ) { |
| console.info( `Deactivated widget ${ widgetID }` ); |
| } else { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async getAllWidgets( siteID: number ): Promise< AllWidgetsResponse > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/widgets` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response.widgets; |
| } |
|
|
| |
| |
| |
| |
| |
| async deleteAllWidgets( siteID: number ): Promise< void > { |
| const widgets = await this.getAllWidgets( siteID ); |
|
|
| widgets.map( async ( widget ) => await this.deleteWidget( siteID, widget.id ) ); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| async jetpackSearch( |
| siteId: number, |
| searchParams: JetpackSearchParams |
| ): Promise< JetpackSearchResponse > { |
| |
| const requestParams: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| }, |
| }; |
|
|
| const requestUrl = this.getRequestURL( '1.3', `/sites/${ siteId }/search` ); |
|
|
| const { query, size } = searchParams; |
| requestUrl.searchParams.append( 'query', query ); |
| if ( size ) { |
| requestUrl.searchParams.append( 'size', size.toString() ); |
| } |
|
|
| const response = await this.sendRequest( requestUrl, requestParams ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response; |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| async getAllPublicizeConnections( siteID: number ): Promise< Array< PublicizeConnection > > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const response = await this.sendRequest( |
| this.getRequestURL( '1.1', `/sites/${ siteID }/publicize-connections` ), |
| params |
| ); |
|
|
| if ( response.hasOwnProperty( 'error' ) ) { |
| throw new Error( |
| `${ ( response as ErrorResponse ).error }: ${ ( response as ErrorResponse ).message }` |
| ); |
| } |
|
|
| return response[ 'connections' ]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async deletePublicizeConnection( |
| siteID: number, |
| connectionID: number |
| ): Promise< PublicizeConnectionDeletedResponse > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| return await this.sendRequest( |
| this.getRequestURL( |
| '1.1', |
| `/sites/${ siteID }/publicize-connections/${ connectionID }/delete` |
| ), |
| params |
| ); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| async getAllSubscribers( siteID: number ): Promise< Subscriber[] > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| |
| const response = await this.sendRequest( |
| this.getRequestURL( '2', `/sites/${ siteID }/subscribers`, 'wpcom' ), |
| params |
| ); |
|
|
| return response[ 'subscribers' ]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async deleteSubscriber( |
| siteID: number, |
| email: string |
| ): Promise< SubscriberDeletedResponse | null > { |
| const params: RequestParams = { |
| method: 'post', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| const subscribers = await this.getAllSubscribers( siteID ); |
|
|
| for ( const subscriber of subscribers ) { |
| if ( subscriber.email_address.trim() === email.trim() ) { |
| return await this.sendRequest( |
| this.getRequestURL( |
| '1.1', |
| `/sites/${ siteID }/email-followers/${ subscriber.subscription_id }/delete` |
| ), |
| params |
| ); |
| } |
| } |
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| async getActiveTheme( siteID: number ): Promise< string > { |
| const params: RequestParams = { |
| method: 'get', |
| headers: { |
| Authorization: await this.getAuthorizationHeader( 'bearer' ), |
| 'Content-Type': this.getContentTypeHeader( 'json' ), |
| }, |
| }; |
|
|
| |
| const response = await this.sendRequest( |
| this.getRequestURL( '2', `/sites/${ siteID }/themes?status=active`, 'wp' ), |
| params |
| ); |
|
|
| return response[ 0 ].stylesheet; |
| } |
| } |
|
|