2022-09-06 03:51:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import fetch from 'node-fetch';
|
2023-04-27 22:16:19 +00:00
|
|
|
import { Logger } from '@woocommerce/monorepo-utils/src/core/logger';
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-03-16 19:37:53 +00:00
|
|
|
// Typing just the things we need from the WP.com Post object.
|
|
|
|
// (which is not the same as WP Post object or API Post object).
|
|
|
|
// See example response here: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/posts/ to add more props.
|
|
|
|
type WordpressComPost = {
|
|
|
|
title: string;
|
|
|
|
URL: string;
|
|
|
|
};
|
|
|
|
|
2022-09-06 03:51:28 +00:00
|
|
|
/**
|
2023-01-23 14:31:59 +00:00
|
|
|
* Fetch a post from WordPress.com
|
|
|
|
*
|
|
|
|
* @param {string} siteId - The site to fetch from.
|
|
|
|
* @param {string} postId - The id of the post to fetch.
|
|
|
|
* @param {string} authToken - WordPress.com auth token.
|
|
|
|
* @return {Promise} - A promise that resolves to the JSON API response.
|
2022-09-06 03:51:28 +00:00
|
|
|
*/
|
2023-01-23 14:31:59 +00:00
|
|
|
export const fetchWpComPost = async (
|
|
|
|
siteId: string,
|
|
|
|
postId: string,
|
|
|
|
authToken: string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const post = await fetch(
|
|
|
|
`https://public-api.wordpress.com/rest/v1.1/sites/${ siteId }/posts/${ postId }`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${ authToken }`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( post.status !== 200 ) {
|
|
|
|
const text = await post.text();
|
|
|
|
throw new Error( `Error creating draft post: ${ text }` );
|
|
|
|
}
|
|
|
|
|
|
|
|
return post.json();
|
|
|
|
} catch ( e: unknown ) {
|
|
|
|
if ( e instanceof Error ) {
|
|
|
|
Logger.error( e.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-09-06 03:51:28 +00:00
|
|
|
|
2023-03-16 19:37:53 +00:00
|
|
|
export const searchForPostsByCategory = async (
|
|
|
|
siteId: string,
|
|
|
|
search: string,
|
|
|
|
category: string,
|
|
|
|
authToken: string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const post = await fetch(
|
|
|
|
`https://public-api.wordpress.com/rest/v1.1/sites/${ siteId }/posts?${ new URLSearchParams(
|
|
|
|
{ search, category }
|
|
|
|
) }`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${ authToken }`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( post.status !== 200 ) {
|
|
|
|
const text = await post.text();
|
|
|
|
throw new Error( `Error creating draft post: ${ text }` );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( await post.json() ).posts as WordpressComPost[];
|
|
|
|
} catch ( e: unknown ) {
|
|
|
|
if ( e instanceof Error ) {
|
|
|
|
Logger.error( e.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-06 03:51:28 +00:00
|
|
|
/**
|
2023-01-23 14:31:59 +00:00
|
|
|
* Edit a post on wordpress.com
|
2022-09-06 03:51:28 +00:00
|
|
|
*
|
|
|
|
* @param {string} siteId - The site to post to.
|
2023-01-23 14:31:59 +00:00
|
|
|
* @param {string} postId - The post to edit.
|
2022-09-06 03:51:28 +00:00
|
|
|
* @param {string} postContent - Post content.
|
2023-01-23 14:31:59 +00:00
|
|
|
* @param {string} authToken - WordPress.com auth token.
|
2022-09-06 03:51:28 +00:00
|
|
|
* @return {Promise} - A promise that resolves to the JSON API response.
|
|
|
|
*/
|
2023-01-23 14:31:59 +00:00
|
|
|
export const editWpComPostContent = async (
|
2022-09-06 03:51:28 +00:00
|
|
|
siteId: string,
|
2023-01-23 14:31:59 +00:00
|
|
|
postId: string,
|
2022-09-10 21:55:53 +00:00
|
|
|
postContent: string,
|
2023-01-23 14:31:59 +00:00
|
|
|
authToken: string
|
2022-09-06 03:51:28 +00:00
|
|
|
) => {
|
|
|
|
try {
|
2023-01-23 14:31:59 +00:00
|
|
|
const post = await fetch(
|
|
|
|
`https://public-api.wordpress.com/rest/v1.2/sites/${ siteId }/posts/${ postId }`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${ authToken }`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify( {
|
|
|
|
content: postContent,
|
|
|
|
} ),
|
|
|
|
}
|
2022-09-06 03:51:28 +00:00
|
|
|
);
|
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
if ( post.status !== 200 ) {
|
|
|
|
const text = await post.text();
|
|
|
|
throw new Error( `Error creating draft post: ${ text }` );
|
2022-09-06 03:51:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 14:31:59 +00:00
|
|
|
return post.json();
|
|
|
|
} catch ( e: unknown ) {
|
|
|
|
if ( e instanceof Error ) {
|
|
|
|
Logger.error( e.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a draft of a post on wordpress.com
|
|
|
|
*
|
|
|
|
* @param {string} siteId - The site to post to.
|
|
|
|
* @param {string} postTitle - Post title.
|
|
|
|
* @param {string} postContent - Post content.
|
|
|
|
* @param {string} authToken - WordPress.com auth token.
|
|
|
|
* @return {Promise} - A promise that resolves to the JSON API response.
|
|
|
|
*/
|
|
|
|
export const createWpComDraftPost = async (
|
|
|
|
siteId: string,
|
|
|
|
postTitle: string,
|
|
|
|
postContent: string,
|
|
|
|
tags: string[],
|
|
|
|
authToken: string
|
|
|
|
) => {
|
|
|
|
try {
|
2022-09-06 03:51:28 +00:00
|
|
|
const post = await fetch(
|
|
|
|
`https://public-api.wordpress.com/rest/v1.2/sites/${ siteId }/posts/new`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${ authToken }`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify( {
|
|
|
|
title: postTitle,
|
|
|
|
content: postContent,
|
|
|
|
status: 'draft',
|
2022-09-10 21:55:53 +00:00
|
|
|
tags,
|
2022-09-06 03:51:28 +00:00
|
|
|
} ),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( post.status !== 200 ) {
|
|
|
|
const text = await post.text();
|
|
|
|
throw new Error( `Error creating draft post: ${ text }` );
|
|
|
|
}
|
|
|
|
|
|
|
|
return post.json();
|
|
|
|
} catch ( e: unknown ) {
|
|
|
|
if ( e instanceof Error ) {
|
|
|
|
Logger.error( e.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|