-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathget-context.ts
More file actions
30 lines (27 loc) · 870 Bytes
/
get-context.ts
File metadata and controls
30 lines (27 loc) · 870 Bytes
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
/**
* Internal dependencies
*/
import { runAbility } from '../../../utils/run-ability';
import type { GetPostDetailsAbilityInput, PostContext } from '../types';
/**
* Gets the context for the given post ID.
*
* @param {number} postId The ID of the post to get the context for.
* @return {Promise<PostContext>} A promise that resolves to the context.
*/
export async function getContext( postId: number ): Promise< PostContext > {
const params: GetPostDetailsAbilityInput = {
post_id: postId,
fields: [ 'title', 'type' ],
};
return await runAbility< PostContext >( 'ai/get-post-details', params )
.then( ( response ) => {
if ( response && typeof response === 'object' ) {
return response as PostContext;
}
throw new Error( 'Invalid response from get context' );
} )
.catch( ( error ) => {
throw new Error( error.message );
} );
}