feat(core): Enhanced post fetching and creation with additional metadata and support for HTML source
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/ghost',
|
||||
version: '1.0.3',
|
||||
version: '1.1.0',
|
||||
description: 'An unofficial Ghost CMS API package enabling content and admin functionality for managing posts.'
|
||||
}
|
||||
|
@ -18,19 +18,19 @@ export class Ghost {
|
||||
this.adminApi = new plugins.GhostAdminAPI({
|
||||
url: this.options.baseUrl,
|
||||
key: this.options.adminApiKey,
|
||||
version: "v3"
|
||||
version: 'v3',
|
||||
});
|
||||
|
||||
this.contentApi = new plugins.GhostContentAPI({
|
||||
url: this.options.baseUrl,
|
||||
key: this.options.contentApiKey,
|
||||
version: "v3"
|
||||
version: 'v3',
|
||||
});
|
||||
}
|
||||
|
||||
public async getPosts(limit: number = 1000): Promise<Post[]> {
|
||||
try {
|
||||
const postsData = await this.contentApi.posts.browse({ limit });
|
||||
const postsData = await this.contentApi.posts.browse({ limit, include: 'tags,authors' });
|
||||
return postsData.map((postData: IPostOptions) => new Post(this, postData));
|
||||
} catch (error) {
|
||||
console.error('Error fetching posts:', error);
|
||||
@ -57,4 +57,12 @@ export class Ghost {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createPostFromHtml(optionsArg: { title: string; html: string }) {
|
||||
const postData = await this.adminApi.posts.add(
|
||||
{ title: optionsArg.title, html: optionsArg.html },
|
||||
{ source: 'html' } // Tell the API to use HTML as the content source, instead of Lexical
|
||||
);
|
||||
return new Post(this, postData);
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,89 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import * as plugins from './ghost.plugins.js';
|
||||
|
||||
export interface IAuthor {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
profile_image?: string;
|
||||
cover_image?: string;
|
||||
bio?: string;
|
||||
website?: string;
|
||||
location?: string;
|
||||
facebook?: string;
|
||||
twitter?: string;
|
||||
meta_title?: string;
|
||||
meta_description?: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface ITag {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
description?: string;
|
||||
feature_image?: string;
|
||||
visibility: string;
|
||||
og_image?: string;
|
||||
og_title?: string;
|
||||
og_description?: string;
|
||||
twitter_image?: string;
|
||||
twitter_title?: string;
|
||||
twitter_description?: string;
|
||||
meta_title?: string;
|
||||
meta_description?: string;
|
||||
codeinjection_head?: string;
|
||||
codeinjection_foot?: string;
|
||||
canonical_url?: string;
|
||||
accent_color?: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface IPostOptions {
|
||||
id: string;
|
||||
uuid: string;
|
||||
title: string;
|
||||
slug: string;
|
||||
html: string;
|
||||
excerpt?: string;
|
||||
comment_id: string;
|
||||
feature_image?: string;
|
||||
featured: boolean;
|
||||
visibility: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
published_at: string;
|
||||
custom_excerpt?: string;
|
||||
codeinjection_head?: string | null;
|
||||
codeinjection_foot?: string | null;
|
||||
custom_template?: string | null;
|
||||
canonical_url?: string | null;
|
||||
url: string;
|
||||
excerpt?: string;
|
||||
reading_time: number;
|
||||
access: boolean;
|
||||
comments: boolean;
|
||||
og_image?: string | null;
|
||||
og_title?: string | null;
|
||||
og_description?: string | null;
|
||||
twitter_image?: string | null;
|
||||
twitter_title?: string | null;
|
||||
twitter_description?: string | null;
|
||||
meta_title?: string | null;
|
||||
meta_description?: string | null;
|
||||
email_subject?: string | null;
|
||||
frontmatter?: string | null;
|
||||
feature_image_alt?: string | null;
|
||||
feature_image_caption?: string | null;
|
||||
authors: IAuthor[];
|
||||
tags: ITag[];
|
||||
primary_author: IAuthor;
|
||||
primary_tag: ITag;
|
||||
[key: string]: any; // To allow for additional properties
|
||||
}
|
||||
|
||||
export class Post {
|
||||
public ghostInstanceRef: Ghost;
|
||||
private postData: IPostOptions;
|
||||
public postData: IPostOptions;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, postData: IPostOptions) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
@ -39,6 +110,10 @@ export class Post {
|
||||
return this.postData.feature_image;
|
||||
}
|
||||
|
||||
public getAuthor(): IAuthor {
|
||||
return this.postData.primary_author;
|
||||
}
|
||||
|
||||
public toJson(): IPostOptions {
|
||||
return this.postData;
|
||||
}
|
||||
|
Reference in New Issue
Block a user