This commit is contained in:
2024-07-01 16:32:22 +02:00
commit 3fb6e3b3c3
16 changed files with 7389 additions and 0 deletions

60
ts/classes.ghost.ts Normal file
View File

@ -0,0 +1,60 @@
import * as plugins from './ghost.plugins.js';
import { Post, type IPostOptions } from './classes.post.js';
export interface IGhostConstructorOptions {
baseUrl: string;
contentApiKey: string;
adminApiKey: string;
}
export class Ghost {
public options: IGhostConstructorOptions;
public adminApi: any;
public contentApi: any;
constructor(optionsArg: IGhostConstructorOptions) {
this.options = optionsArg;
this.adminApi = new plugins.GhostAdminAPI({
url: this.options.baseUrl,
key: this.options.adminApiKey,
version: "v3"
});
this.contentApi = new plugins.GhostContentAPI({
url: this.options.baseUrl,
key: this.options.contentApiKey,
version: "v3"
});
}
public async getPosts(limit: number = 1000): Promise<Post[]> {
try {
const postsData = await this.contentApi.posts.browse({ limit });
return postsData.map((postData: IPostOptions) => new Post(this, postData));
} catch (error) {
console.error('Error fetching posts:', error);
throw error;
}
}
public async getPostById(id: string): Promise<Post> {
try {
const postData = await this.contentApi.posts.read({ id });
return new Post(postData, this.adminApi);
} catch (error) {
console.error(`Error fetching post with id ${id}:`, error);
throw error;
}
}
public async createPost(postData: IPostOptions): Promise<Post> {
try {
const createdPostData = await this.adminApi.posts.add(postData);
return new Post(createdPostData, this.adminApi);
} catch (error) {
console.error('Error creating post:', error);
throw error;
}
}
}

70
ts/classes.post.ts Normal file
View File

@ -0,0 +1,70 @@
import type { Ghost } from './classes.ghost.js';
import * as plugins from './ghost.plugins.js';
export interface IPostOptions {
id: string;
title: string;
html: string;
excerpt?: string;
feature_image?: string;
[key: string]: any; // To allow for additional properties
}
export class Post {
public ghostInstanceRef: Ghost;
private postData: IPostOptions;
constructor(ghostInstanceRefArg: Ghost, postData: IPostOptions) {
this.ghostInstanceRef = ghostInstanceRefArg;
this.postData = postData;
}
public getId(): string {
return this.postData.id;
}
public getTitle(): string {
return this.postData.title;
}
public getHtml(): string {
return this.postData.html;
}
public getExcerpt(): string {
return this.postData.excerpt || this.generateExcerpt();
}
public getFeatureImage(): string | undefined {
return this.postData.feature_image;
}
public toJson(): IPostOptions {
return this.postData;
}
public async update(postData: IPostOptions): Promise<Post> {
try {
const updatedPostData = await this.ghostInstanceRef.adminApi.posts.edit(postData);
this.postData = updatedPostData;
return this;
} catch (error) {
console.error('Error updating post:', error);
throw error;
}
}
public async delete(): Promise<void> {
try {
await this.ghostInstanceRef.adminApi.posts.delete({ id: this.getId() });
} catch (error) {
console.error(`Error deleting post with id ${this.getId()}:`, error);
throw error;
}
}
private generateExcerpt(length: number = 100): string {
const plainText = this.postData.html.replace(/<[^>]+>/g, ''); // Strip HTML tags
return plainText.length > length ? plainText.substring(0, length) + '...' : plainText;
}
}

7
ts/ghost.plugins.ts Normal file
View File

@ -0,0 +1,7 @@
import GhostContentAPI from '@tryghost/content-api';
import GhostAdminAPI from '@tryghost/admin-api';
export {
GhostContentAPI,
GhostAdminAPI
}

2
ts/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './classes.ghost.js';
export * from './classes.post.js';