Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
7251e90395 | |||
62839e2f54 | |||
2d9844eb61 | |||
76c2b714b5 | |||
cf10f51089 | |||
2f05d0edc4 | |||
1cde47da68 | |||
a687b639d2 | |||
a0ffc7c4d7 | |||
d82c58e608 |
47
changelog.md
47
changelog.md
@@ -1,5 +1,52 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-10-07 - 1.4.1 - fix(tests)
|
||||
Remove updated_at from post and page update test payloads
|
||||
|
||||
- Stop setting updated_at in test update payloads to avoid mutating server-managed timestamps
|
||||
- Changed test/test.post.node.ts: removed updated_at assignment when updating a post
|
||||
- Changed test/test.page.node.ts: removed updated_at assignment when updating a page
|
||||
|
||||
## 2025-10-07 - 1.4.0 - feat(classes.ghost)
|
||||
Add members, settings and webhooks support; implement Member class and add tests
|
||||
|
||||
- Introduce IMember and Member class (ts/classes.member.ts) with CRUD (update, delete) and JSON helpers
|
||||
- Add member management API to Ghost: getMembers (with minimatch filtering), getMemberById, getMemberByEmail, createMember
|
||||
- Add site settings API to Ghost: getSettings and updateSettings (admin API wrappers)
|
||||
- Add webhooks management to Ghost: getWebhooks, getWebhookById, createWebhook, updateWebhook, deleteWebhook
|
||||
- Wire smartmatch filtering for members using @push.rocks/smartmatch
|
||||
- Export Member from ts/index.ts so it's part of the public API
|
||||
- Add comprehensive node tests for Ghost, Author, Member, Page, Post, Tag, Settings and Webhooks (test/*.node.ts)
|
||||
- Fix Post construction usages in classes.ghost to pass the Ghost instance when creating Post objects
|
||||
|
||||
## 2025-10-07 - 1.3.0 - feat(core)
|
||||
Add Ghost CMS API client classes and README documentation
|
||||
|
||||
- Implemented core TypeScript classes: Ghost, Post, Tag, Author and Page with CRUD methods and helpers
|
||||
- Added TypeScript interfaces for posts, tags, authors and pages to provide typings
|
||||
- Included comprehensive README with installation, usage examples and API method documentation
|
||||
- Added unit test scaffold under test/test.ts
|
||||
- Updated package metadata and commitinfo (version 1.2.0)
|
||||
|
||||
## 2025-10-07 - 1.2.0 - feat(ghost)
|
||||
Implement Tag, Author and Page models; add advanced filtering, search, bulk operations, image upload, related-posts, update tests and bump dependencies
|
||||
|
||||
- Add fully implemented Author, Tag and Page classes with CRUD methods
|
||||
- Enhance Ghost class with: improved getPosts filtering (tag/author/featured/custom filters), getTags/getAuthors/getPages with minimatch filtering, getTag/getAuthor/getPage by id/slug, createTag/createPage, searchPosts, uploadImage, bulkUpdatePosts, bulkDeletePosts and getRelatedPosts
|
||||
- Refactor Post types (IPost) and update Post class to use the new type and consistent constructors/serialization
|
||||
- Export new modules (author, tag, page) from index.ts
|
||||
- Integrate @push.rocks/smartmatch for pattern filtering and expose it via ghost.plugins
|
||||
- Update tests to exercise tags, authors, pages, filtering, search and related posts; change test baseUrl to localhost and adjust imports
|
||||
- Bump devDependencies and dependencies versions, adjust test script, add packageManager metadata and add pnpm-workspace.yaml
|
||||
|
||||
## 2024-07-06 - 1.1.0 - feat(core)
|
||||
Enhanced post fetching and creation with additional metadata and support for HTML source
|
||||
|
||||
- Added support for fetching posts with included tags and authors.
|
||||
- Implemented helper method to create posts directly from HTML content.
|
||||
- Enhanced Post class to include additional metadata such as visibility, created_at, updated_at, published_at, etc.
|
||||
- Modified getPosts method in Ghost class to include tags and authors in the response.
|
||||
|
||||
## 2024-07-01 - 1.0.3 - fix(docs)
|
||||
Updated the project keywords and readme content for better clarity and SEO
|
||||
|
||||
|
26
package.json
26
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiclient.xyz/ghost",
|
||||
"version": "1.0.3",
|
||||
"version": "1.4.1",
|
||||
"private": false,
|
||||
"description": "An unofficial Ghost CMS API package enabling content and admin functionality for managing posts.",
|
||||
"main": "dist_ts/index.js",
|
||||
@@ -9,22 +9,23 @@
|
||||
"author": "Task Venture Capital GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"test": "(tstest test/ --verbose)",
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"buildDocs": "(tsdoc)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.82",
|
||||
"@git.zone/tsbundle": "^2.0.5",
|
||||
"@git.zone/tsrun": "^1.2.49",
|
||||
"@git.zone/tstest": "^1.0.44",
|
||||
"@push.rocks/qenv": "^6.0.5",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.14.9"
|
||||
"@git.zone/tsbuild": "^2.6.8",
|
||||
"@git.zone/tsbundle": "^2.5.1",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^2.3.8",
|
||||
"@push.rocks/qenv": "^6.1.3",
|
||||
"@push.rocks/tapbundle": "^6.0.3",
|
||||
"@types/node": "^22.12.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tryghost/admin-api": "^1.13.12",
|
||||
"@tryghost/content-api": "^1.11.21"
|
||||
"@push.rocks/smartmatch": "^2.0.0",
|
||||
"@tryghost/admin-api": "^1.14.0",
|
||||
"@tryghost/content-api": "^1.12.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -58,5 +59,6 @@
|
||||
"TypeScript",
|
||||
"post management",
|
||||
"blog management"
|
||||
]
|
||||
],
|
||||
"packageManager": "pnpm@10.18.1+sha512.77a884a165cbba2d8d1c19e3b4880eee6d2fcabd0d879121e282196b80042351d5eb3ca0935fa599da1dc51265cc68816ad2bddd2a2de5ea9fdf92adbec7cd34"
|
||||
}
|
||||
|
8631
pnpm-lock.yaml
generated
8631
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
4
pnpm-workspace.yaml
Normal file
4
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
onlyBuiltDependencies:
|
||||
- esbuild
|
||||
- mongodb-memory-server
|
||||
- puppeteer
|
284
readme.md
284
readme.md
@@ -172,6 +172,290 @@ filteredPosts.forEach(post => {
|
||||
});
|
||||
```
|
||||
|
||||
#### Fetching Tags
|
||||
|
||||
You can fetch all tags from your Ghost site using the `getTags` method.
|
||||
|
||||
```typescript
|
||||
const tags = await ghostInstance.getTags();
|
||||
|
||||
tags.forEach(tag => {
|
||||
console.log(`${tag.name} (${tag.slug})`);
|
||||
});
|
||||
```
|
||||
|
||||
#### Fetching Tags with Minimatch Filtering
|
||||
|
||||
The `getTags` method supports filtering tags using minimatch patterns on tag slugs.
|
||||
|
||||
```typescript
|
||||
// Fetch all tags starting with 'tech-'
|
||||
const techTags = await ghostInstance.getTags({ filter: 'tech-*' });
|
||||
|
||||
// Fetch all tags containing 'blog'
|
||||
const blogTags = await ghostInstance.getTags({ filter: '*blog*' });
|
||||
|
||||
// Fetch with limit
|
||||
const limitedTags = await ghostInstance.getTags({ limit: 10, filter: 'a*' });
|
||||
|
||||
limitedTags.forEach(tag => {
|
||||
console.log(tag.name);
|
||||
});
|
||||
```
|
||||
|
||||
#### Working with Tag Class
|
||||
|
||||
Fetch individual tags and manage them with the `Tag` class.
|
||||
|
||||
```typescript
|
||||
// Get tag by slug
|
||||
const tag = await ghostInstance.getTagBySlug('tech');
|
||||
console.log(tag.getName());
|
||||
console.log(tag.getDescription());
|
||||
|
||||
// Create a new tag
|
||||
const newTag = await ghostInstance.createTag({
|
||||
name: 'JavaScript',
|
||||
slug: 'javascript',
|
||||
description: 'Posts about JavaScript'
|
||||
});
|
||||
|
||||
// Update a tag
|
||||
await newTag.update({
|
||||
description: 'All things JavaScript'
|
||||
});
|
||||
|
||||
// Delete a tag
|
||||
await newTag.delete();
|
||||
```
|
||||
|
||||
#### Fetching Authors
|
||||
|
||||
Manage and filter authors with minimatch patterns.
|
||||
|
||||
```typescript
|
||||
// Get all authors
|
||||
const authors = await ghostInstance.getAuthors();
|
||||
authors.forEach(author => {
|
||||
console.log(`${author.getName()} (${author.getSlug()})`);
|
||||
});
|
||||
|
||||
// Filter authors with minimatch
|
||||
const filteredAuthors = await ghostInstance.getAuthors({ filter: 'j*' });
|
||||
|
||||
// Get author by slug
|
||||
const author = await ghostInstance.getAuthorBySlug('john-doe');
|
||||
console.log(author.getBio());
|
||||
|
||||
// Update author
|
||||
await author.update({
|
||||
bio: 'Updated bio information'
|
||||
});
|
||||
```
|
||||
|
||||
#### Working with Pages
|
||||
|
||||
Ghost pages are similar to posts but for static content.
|
||||
|
||||
```typescript
|
||||
// Get all pages
|
||||
const pages = await ghostInstance.getPages();
|
||||
pages.forEach(page => {
|
||||
console.log(page.getTitle());
|
||||
});
|
||||
|
||||
// Filter pages with minimatch
|
||||
const filteredPages = await ghostInstance.getPages({ filter: 'about*' });
|
||||
|
||||
// Get page by slug
|
||||
const page = await ghostInstance.getPageBySlug('about');
|
||||
console.log(page.getHtml());
|
||||
|
||||
// Create a new page
|
||||
const newPage = await ghostInstance.createPage({
|
||||
title: 'Contact Us',
|
||||
html: '<p>Contact information...</p>'
|
||||
});
|
||||
|
||||
// Update a page
|
||||
await newPage.update({
|
||||
html: '<p>Updated contact information...</p>'
|
||||
});
|
||||
|
||||
// Delete a page
|
||||
await newPage.delete();
|
||||
```
|
||||
|
||||
#### Enhanced Post Filtering
|
||||
|
||||
The `getPosts` method now supports multiple filtering options.
|
||||
|
||||
```typescript
|
||||
// Filter by tag
|
||||
const techPosts = await ghostInstance.getPosts({ tag: 'tech', limit: 10 });
|
||||
|
||||
// Filter by author
|
||||
const authorPosts = await ghostInstance.getPosts({ author: 'john-doe' });
|
||||
|
||||
// Get only featured posts
|
||||
const featuredPosts = await ghostInstance.getPosts({ featured: true });
|
||||
|
||||
// Combine multiple filters
|
||||
const filteredPosts = await ghostInstance.getPosts({
|
||||
tag: 'javascript',
|
||||
featured: true,
|
||||
limit: 5
|
||||
});
|
||||
```
|
||||
|
||||
#### Searching Posts
|
||||
|
||||
Full-text search across post titles and excerpts.
|
||||
|
||||
```typescript
|
||||
// Search for posts containing a keyword
|
||||
const searchResults = await ghostInstance.searchPosts('ghost api', { limit: 10 });
|
||||
|
||||
searchResults.forEach(post => {
|
||||
console.log(post.getTitle());
|
||||
console.log(post.getExcerpt());
|
||||
});
|
||||
```
|
||||
|
||||
#### Finding Related Posts
|
||||
|
||||
Get posts with similar tags.
|
||||
|
||||
```typescript
|
||||
const post = await ghostInstance.getPostById('some-post-id');
|
||||
|
||||
// Get related posts based on tags
|
||||
const relatedPosts = await ghostInstance.getRelatedPosts(post.getId(), 5);
|
||||
|
||||
relatedPosts.forEach(relatedPost => {
|
||||
console.log(`Related: ${relatedPost.getTitle()}`);
|
||||
});
|
||||
```
|
||||
|
||||
#### Image Upload
|
||||
|
||||
Upload images to your Ghost site.
|
||||
|
||||
```typescript
|
||||
// Upload an image file
|
||||
const imageUrl = await ghostInstance.uploadImage('/path/to/image.jpg');
|
||||
|
||||
// Use the uploaded image URL in a post
|
||||
await ghostInstance.createPost({
|
||||
title: 'Post with Image',
|
||||
html: '<p>Content here</p>',
|
||||
feature_image: imageUrl
|
||||
});
|
||||
```
|
||||
|
||||
#### Bulk Operations
|
||||
|
||||
Perform operations on multiple posts at once.
|
||||
|
||||
```typescript
|
||||
// Bulk update posts
|
||||
const postIds = ['id1', 'id2', 'id3'];
|
||||
await ghostInstance.bulkUpdatePosts(postIds, {
|
||||
featured: true
|
||||
});
|
||||
|
||||
// Bulk delete posts
|
||||
await ghostInstance.bulkDeletePosts(['id4', 'id5']);
|
||||
```
|
||||
|
||||
#### Members Management
|
||||
|
||||
Manage your Ghost site members (requires Ghost membership features enabled).
|
||||
|
||||
```typescript
|
||||
// Get all members
|
||||
const members = await ghostInstance.getMembers({ limit: 100 });
|
||||
members.forEach(member => {
|
||||
console.log(`${member.getName()} - ${member.getEmail()}`);
|
||||
});
|
||||
|
||||
// Filter members with minimatch
|
||||
const filteredMembers = await ghostInstance.getMembers({
|
||||
filter: '*@gmail.com'
|
||||
});
|
||||
|
||||
// Get member by email
|
||||
const member = await ghostInstance.getMemberByEmail('user@example.com');
|
||||
console.log(member.getStatus());
|
||||
|
||||
// Create a new member
|
||||
const newMember = await ghostInstance.createMember({
|
||||
email: 'newuser@example.com',
|
||||
name: 'New User'
|
||||
});
|
||||
|
||||
// Update a member
|
||||
await newMember.update({
|
||||
name: 'Updated Name',
|
||||
note: 'VIP member'
|
||||
});
|
||||
|
||||
// Delete a member
|
||||
await newMember.delete();
|
||||
```
|
||||
|
||||
#### Site Settings
|
||||
|
||||
Read and update Ghost site settings.
|
||||
|
||||
```typescript
|
||||
// Get all settings
|
||||
const settings = await ghostInstance.getSettings();
|
||||
console.log(settings);
|
||||
|
||||
// Update settings
|
||||
await ghostInstance.updateSettings([
|
||||
{
|
||||
key: 'title',
|
||||
value: 'My Updated Site Title'
|
||||
},
|
||||
{
|
||||
key: 'description',
|
||||
value: 'My site description'
|
||||
}
|
||||
]);
|
||||
```
|
||||
|
||||
#### Webhooks Management
|
||||
|
||||
Manage webhooks for Ghost events.
|
||||
|
||||
```typescript
|
||||
// Get all webhooks
|
||||
const webhooks = await ghostInstance.getWebhooks();
|
||||
webhooks.forEach(webhook => {
|
||||
console.log(`${webhook.name}: ${webhook.target_url}`);
|
||||
});
|
||||
|
||||
// Get webhook by ID
|
||||
const webhook = await ghostInstance.getWebhookById('webhook-id');
|
||||
|
||||
// Create a webhook
|
||||
const newWebhook = await ghostInstance.createWebhook({
|
||||
event: 'post.published',
|
||||
target_url: 'https://example.com/webhook',
|
||||
name: 'Post Published Webhook'
|
||||
});
|
||||
|
||||
// Update a webhook
|
||||
await ghostInstance.updateWebhook(newWebhook.id, {
|
||||
target_url: 'https://example.com/new-webhook'
|
||||
});
|
||||
|
||||
// Delete a webhook
|
||||
await ghostInstance.deleteWebhook(newWebhook.id);
|
||||
```
|
||||
|
||||
### Example Projects
|
||||
|
||||
To give you a comprehensive understanding, let's look at a couple of example projects.
|
||||
|
107
test/test.author.node.ts
Normal file
107
test/test.author.node.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all authors', async () => {
|
||||
const authors = await testGhostInstance.getAuthors();
|
||||
expect(authors).toBeArray();
|
||||
console.log(`Found ${authors.length} authors`);
|
||||
if (authors.length > 0) {
|
||||
expect(authors[0]).toBeInstanceOf(ghost.Author);
|
||||
console.log(`First author: ${authors[0].getName()} (${authors[0].getSlug()})`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get authors with limit', async () => {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 2 });
|
||||
expect(authors).toBeArray();
|
||||
expect(authors.length).toBeLessThanOrEqual(2);
|
||||
});
|
||||
|
||||
tap.test('should filter authors with minimatch pattern', async () => {
|
||||
const authors = await testGhostInstance.getAuthors();
|
||||
if (authors.length > 0) {
|
||||
const firstAuthorSlug = authors[0].getSlug();
|
||||
const pattern = `${firstAuthorSlug.charAt(0)}*`;
|
||||
const filteredAuthors = await testGhostInstance.getAuthors({ filter: pattern });
|
||||
expect(filteredAuthors).toBeArray();
|
||||
console.log(`Filtered authors with pattern '${pattern}': found ${filteredAuthors.length}`);
|
||||
filteredAuthors.forEach((author) => {
|
||||
expect(author.getSlug()).toMatch(new RegExp(`^${firstAuthorSlug.charAt(0)}`));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get author by slug', async () => {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
||||
if (authors.length > 0) {
|
||||
const author = await testGhostInstance.getAuthorBySlug(authors[0].getSlug());
|
||||
expect(author).toBeInstanceOf(ghost.Author);
|
||||
expect(author.getSlug()).toEqual(authors[0].getSlug());
|
||||
console.log(`Got author by slug: ${author.getName()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get author by ID', async () => {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
||||
if (authors.length > 0) {
|
||||
const author = await testGhostInstance.getAuthorById(authors[0].getId());
|
||||
expect(author).toBeInstanceOf(ghost.Author);
|
||||
expect(author.getId()).toEqual(authors[0].getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should access author methods', async () => {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
||||
if (authors.length > 0) {
|
||||
const author = authors[0];
|
||||
expect(author.getId()).toBeTruthy();
|
||||
expect(author.getName()).toBeTruthy();
|
||||
expect(author.getSlug()).toBeTruthy();
|
||||
const json = author.toJson();
|
||||
expect(json).toBeTruthy();
|
||||
expect(json.id).toEqual(author.getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update author bio', async () => {
|
||||
try {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
||||
if (authors.length > 0) {
|
||||
const author = authors[0];
|
||||
const originalBio = author.getBio();
|
||||
|
||||
const updatedAuthor = await author.update({
|
||||
bio: 'Updated bio for testing'
|
||||
});
|
||||
expect(updatedAuthor).toBeInstanceOf(ghost.Author);
|
||||
expect(updatedAuthor.getBio()).toEqual('Updated bio for testing');
|
||||
console.log(`Updated author bio: ${updatedAuthor.getName()}`);
|
||||
|
||||
await updatedAuthor.update({
|
||||
bio: originalBio
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.type === 'NotImplementedError') {
|
||||
console.log('Author updates not supported in this Ghost version - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
28
test/test.ghost.node.ts
Normal file
28
test/test.ghost.node.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
|
||||
tap.test('should create a valid instance of Ghost', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
expect(testGhostInstance.options.baseUrl).toEqual('http://localhost:2368');
|
||||
});
|
||||
|
||||
tap.test('should have adminApi configured', async () => {
|
||||
expect(testGhostInstance.adminApi).toBeTruthy();
|
||||
});
|
||||
|
||||
tap.test('should have contentApi configured', async () => {
|
||||
expect(testGhostInstance.contentApi).toBeTruthy();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
export { testGhostInstance };
|
151
test/test.member.node.ts
Normal file
151
test/test.member.node.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
let createdMember: ghost.Member;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all members', async () => {
|
||||
try {
|
||||
const members = await testGhostInstance.getMembers({ limit: 10 });
|
||||
expect(members).toBeArray();
|
||||
console.log(`Found ${members.length} members`);
|
||||
if (members.length > 0) {
|
||||
expect(members[0]).toBeInstanceOf(ghost.Member);
|
||||
console.log(`First member: ${members[0].getEmail()}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available or requires permissions - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get members with limit', async () => {
|
||||
try {
|
||||
const members = await testGhostInstance.getMembers({ limit: 2 });
|
||||
expect(members).toBeArray();
|
||||
expect(members.length).toBeLessThanOrEqual(2);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should filter members with minimatch pattern', async () => {
|
||||
try {
|
||||
const members = await testGhostInstance.getMembers({ filter: '*@gmail.com' });
|
||||
expect(members).toBeArray();
|
||||
console.log(`Found ${members.length} Gmail members`);
|
||||
if (members.length > 0) {
|
||||
members.forEach((member) => {
|
||||
expect(member.getEmail()).toContain('@gmail.com');
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create member', async () => {
|
||||
try {
|
||||
const timestamp = Date.now();
|
||||
createdMember = await testGhostInstance.createMember({
|
||||
email: `test${timestamp}@example.com`,
|
||||
name: `Test Member ${timestamp}`
|
||||
});
|
||||
expect(createdMember).toBeInstanceOf(ghost.Member);
|
||||
expect(createdMember.getEmail()).toEqual(`test${timestamp}@example.com`);
|
||||
console.log(`Created member: ${createdMember.getId()}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should access member methods', async () => {
|
||||
if (createdMember) {
|
||||
expect(createdMember.getId()).toBeTruthy();
|
||||
expect(createdMember.getEmail()).toBeTruthy();
|
||||
expect(createdMember.getName()).toBeTruthy();
|
||||
const json = createdMember.toJson();
|
||||
expect(json).toBeTruthy();
|
||||
expect(json.id).toEqual(createdMember.getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get member by email', async () => {
|
||||
if (createdMember) {
|
||||
try {
|
||||
const member = await testGhostInstance.getMemberByEmail(createdMember.getEmail());
|
||||
expect(member).toBeInstanceOf(ghost.Member);
|
||||
expect(member.getEmail()).toEqual(createdMember.getEmail());
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403 || error.type === 'RequestNotAcceptableError') {
|
||||
console.log('Member by email not supported in this Ghost version - using ID instead');
|
||||
const member = await testGhostInstance.getMemberById(createdMember.getId());
|
||||
expect(member).toBeInstanceOf(ghost.Member);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update member', async () => {
|
||||
if (createdMember) {
|
||||
try {
|
||||
const updatedMember = await createdMember.update({
|
||||
note: 'Updated by automated tests'
|
||||
});
|
||||
expect(updatedMember).toBeInstanceOf(ghost.Member);
|
||||
console.log(`Updated member: ${updatedMember.getId()}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should delete member', async () => {
|
||||
if (createdMember) {
|
||||
try {
|
||||
await createdMember.delete();
|
||||
console.log(`Deleted member: ${createdMember.getId()}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
106
test/test.page.node.ts
Normal file
106
test/test.page.node.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
let createdPage: ghost.Page;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all pages', async () => {
|
||||
const pages = await testGhostInstance.getPages();
|
||||
expect(pages).toBeArray();
|
||||
console.log(`Found ${pages.length} pages`);
|
||||
if (pages.length > 0) {
|
||||
expect(pages[0]).toBeInstanceOf(ghost.Page);
|
||||
console.log(`First page: ${pages[0].getTitle()} (${pages[0].getSlug()})`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get pages with limit', async () => {
|
||||
const pages = await testGhostInstance.getPages({ limit: 3 });
|
||||
expect(pages).toBeArray();
|
||||
expect(pages.length).toBeLessThanOrEqual(3);
|
||||
});
|
||||
|
||||
tap.test('should filter pages with minimatch pattern', async () => {
|
||||
const pages = await testGhostInstance.getPages();
|
||||
if (pages.length > 0) {
|
||||
const firstPageSlug = pages[0].getSlug();
|
||||
const pattern = `${firstPageSlug.charAt(0)}*`;
|
||||
const filteredPages = await testGhostInstance.getPages({ filter: pattern });
|
||||
expect(filteredPages).toBeArray();
|
||||
console.log(`Filtered pages with pattern '${pattern}': found ${filteredPages.length}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get page by slug', async () => {
|
||||
const pages = await testGhostInstance.getPages({ limit: 1 });
|
||||
if (pages.length > 0) {
|
||||
const page = await testGhostInstance.getPageBySlug(pages[0].getSlug());
|
||||
expect(page).toBeInstanceOf(ghost.Page);
|
||||
expect(page.getSlug()).toEqual(pages[0].getSlug());
|
||||
console.log(`Got page by slug: ${page.getTitle()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get page by ID', async () => {
|
||||
const pages = await testGhostInstance.getPages({ limit: 1 });
|
||||
if (pages.length > 0) {
|
||||
const page = await testGhostInstance.getPageById(pages[0].getId());
|
||||
expect(page).toBeInstanceOf(ghost.Page);
|
||||
expect(page.getId()).toEqual(pages[0].getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create page', async () => {
|
||||
const timestamp = Date.now();
|
||||
createdPage = await testGhostInstance.createPage({
|
||||
title: `Test Page ${timestamp}`,
|
||||
html: '<p>This is a test page created by automated tests.</p>',
|
||||
status: 'published'
|
||||
} as any);
|
||||
expect(createdPage).toBeInstanceOf(ghost.Page);
|
||||
expect(createdPage.getTitle()).toEqual(`Test Page ${timestamp}`);
|
||||
console.log(`Created page: ${createdPage.getId()}`);
|
||||
});
|
||||
|
||||
tap.test('should access page methods', async () => {
|
||||
if (createdPage) {
|
||||
expect(createdPage.getId()).toBeTruthy();
|
||||
expect(createdPage.getTitle()).toBeTruthy();
|
||||
expect(createdPage.getSlug()).toBeTruthy();
|
||||
const json = createdPage.toJson();
|
||||
expect(json).toBeTruthy();
|
||||
expect(json.id).toEqual(createdPage.getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update page', async () => {
|
||||
if (createdPage) {
|
||||
const updatedPage = await createdPage.update({
|
||||
...createdPage.pageData,
|
||||
html: '<p>This page has been updated.</p>'
|
||||
});
|
||||
expect(updatedPage).toBeInstanceOf(ghost.Page);
|
||||
console.log(`Updated page: ${updatedPage.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should delete page', async () => {
|
||||
if (createdPage) {
|
||||
await createdPage.delete();
|
||||
console.log(`Deleted page: ${createdPage.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
144
test/test.post.node.ts
Normal file
144
test/test.post.node.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
let createdPost: ghost.Post;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts();
|
||||
expect(posts).toBeArray();
|
||||
if (posts.length > 0) {
|
||||
expect(posts[0]).toBeInstanceOf(ghost.Post);
|
||||
console.log(`Found ${posts.length} posts`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get posts with limit', async () => {
|
||||
const posts = await testGhostInstance.getPosts({ limit: 5 });
|
||||
expect(posts).toBeArray();
|
||||
expect(posts.length).toBeLessThanOrEqual(5);
|
||||
});
|
||||
|
||||
tap.test('should filter posts by tag', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 1 });
|
||||
if (tags.length > 0) {
|
||||
const posts = await testGhostInstance.getPosts({ tag: tags[0].slug, limit: 5 });
|
||||
expect(posts).toBeArray();
|
||||
console.log(`Found ${posts.length} posts with tag '${tags[0].name}'`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should filter posts by author', async () => {
|
||||
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
||||
if (authors.length > 0) {
|
||||
const posts = await testGhostInstance.getPosts({ author: authors[0].getSlug(), limit: 5 });
|
||||
expect(posts).toBeArray();
|
||||
console.log(`Found ${posts.length} posts by author '${authors[0].getName()}'`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should filter posts by featured status', async () => {
|
||||
const featuredPosts = await testGhostInstance.getPosts({ featured: true, limit: 5 });
|
||||
expect(featuredPosts).toBeArray();
|
||||
console.log(`Found ${featuredPosts.length} featured posts`);
|
||||
if (featuredPosts.length > 0) {
|
||||
expect(featuredPosts[0].postData.featured).toEqual(true);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should search posts by title', async () => {
|
||||
const searchResults = await testGhostInstance.searchPosts('the', { limit: 5 });
|
||||
expect(searchResults).toBeArray();
|
||||
console.log(`Found ${searchResults.length} posts matching 'the'`);
|
||||
});
|
||||
|
||||
tap.test('should get post by ID', async () => {
|
||||
const posts = await testGhostInstance.getPosts({ limit: 1 });
|
||||
if (posts.length > 0) {
|
||||
const post = await testGhostInstance.getPostById(posts[0].getId());
|
||||
expect(post).toBeInstanceOf(ghost.Post);
|
||||
expect(post.getId()).toEqual(posts[0].getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get related posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts({ limit: 1 });
|
||||
if (posts.length > 0) {
|
||||
const relatedPosts = await testGhostInstance.getRelatedPosts(posts[0].getId(), 3);
|
||||
expect(relatedPosts).toBeArray();
|
||||
console.log(`Found ${relatedPosts.length} related posts for '${posts[0].getTitle()}'`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create post from HTML', async () => {
|
||||
const timestamp = Date.now();
|
||||
createdPost = await testGhostInstance.createPostFromHtml({
|
||||
title: `Test Post ${timestamp}`,
|
||||
html: '<p>This is a test post created by automated tests.</p>',
|
||||
status: 'published'
|
||||
} as any);
|
||||
expect(createdPost).toBeInstanceOf(ghost.Post);
|
||||
expect(createdPost.getTitle()).toEqual(`Test Post ${timestamp}`);
|
||||
console.log(`Created post: ${createdPost.getId()}`);
|
||||
});
|
||||
|
||||
tap.test('should access post methods', async () => {
|
||||
if (createdPost) {
|
||||
expect(createdPost.getId()).toBeTruthy();
|
||||
expect(createdPost.getTitle()).toBeTruthy();
|
||||
const json = createdPost.toJson();
|
||||
expect(json).toBeTruthy();
|
||||
expect(json.id).toEqual(createdPost.getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update post', async () => {
|
||||
if (createdPost) {
|
||||
const updatedPost = await createdPost.update({
|
||||
...createdPost.postData,
|
||||
html: '<p>This post has been updated.</p>'
|
||||
});
|
||||
expect(updatedPost).toBeInstanceOf(ghost.Post);
|
||||
console.log(`Updated post: ${updatedPost.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should delete post', async () => {
|
||||
if (createdPost) {
|
||||
await createdPost.delete();
|
||||
console.log(`Deleted post: ${createdPost.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should bulk update posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts({ limit: 2 });
|
||||
if (posts.length >= 2) {
|
||||
const postIds = posts.map(p => p.getId());
|
||||
const originalFeatured = posts[0].postData.featured;
|
||||
|
||||
const updatedPosts = await testGhostInstance.bulkUpdatePosts(postIds, {
|
||||
featured: !originalFeatured
|
||||
});
|
||||
expect(updatedPosts).toBeArray();
|
||||
expect(updatedPosts.length).toEqual(postIds.length);
|
||||
|
||||
await testGhostInstance.bulkUpdatePosts(postIds, {
|
||||
featured: originalFeatured
|
||||
});
|
||||
console.log(`Bulk updated ${updatedPosts.length} posts`);
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
62
test/test.settings.node.ts
Normal file
62
test/test.settings.node.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get settings', async () => {
|
||||
try {
|
||||
const settings = await testGhostInstance.getSettings();
|
||||
expect(settings).toBeTruthy();
|
||||
console.log(`Retrieved ${settings.settings?.length || 0} settings`);
|
||||
if (settings.settings && settings.settings.length > 0) {
|
||||
console.log(`Sample setting: ${settings.settings[0].key}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('undefined') || error.statusCode === 403) {
|
||||
console.log('Settings API not available in this Ghost version - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update settings', async () => {
|
||||
try {
|
||||
const settings = await testGhostInstance.getSettings();
|
||||
if (settings.settings && settings.settings.length > 0) {
|
||||
const titleSetting = settings.settings.find((s: any) => s.key === 'title');
|
||||
if (titleSetting) {
|
||||
const originalTitle = titleSetting.value;
|
||||
|
||||
const updated = await testGhostInstance.updateSettings([
|
||||
{
|
||||
key: 'title',
|
||||
value: originalTitle
|
||||
}
|
||||
]);
|
||||
expect(updated).toBeTruthy();
|
||||
console.log('Settings updated successfully');
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('undefined') || error.statusCode === 403) {
|
||||
console.log('Settings API not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
110
test/test.tag.node.ts
Normal file
110
test/test.tag.node.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
let createdTag: ghost.Tag;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all tags', async () => {
|
||||
const tags = await testGhostInstance.getTags();
|
||||
expect(tags).toBeArray();
|
||||
console.log(`Found ${tags.length} tags`);
|
||||
if (tags.length > 0) {
|
||||
console.log(`First tag: ${tags[0].name} (${tags[0].slug})`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get tags with limit', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 3 });
|
||||
expect(tags).toBeArray();
|
||||
expect(tags.length).toBeLessThanOrEqual(3);
|
||||
});
|
||||
|
||||
tap.test('should filter tags with minimatch pattern', async () => {
|
||||
const allTags = await testGhostInstance.getTags();
|
||||
if (allTags.length > 0) {
|
||||
const firstTagSlug = allTags[0].slug;
|
||||
const pattern = `${firstTagSlug.charAt(0)}*`;
|
||||
|
||||
const filteredTags = await testGhostInstance.getTags({ filter: pattern });
|
||||
expect(filteredTags).toBeArray();
|
||||
console.log(`Filtered tags with pattern '${pattern}': found ${filteredTags.length}`);
|
||||
filteredTags.forEach((tag) => {
|
||||
expect(tag.slug).toMatch(new RegExp(`^${firstTagSlug.charAt(0)}`));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get tag by slug', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 1 });
|
||||
if (tags.length > 0) {
|
||||
const tag = await testGhostInstance.getTagBySlug(tags[0].slug);
|
||||
expect(tag).toBeInstanceOf(ghost.Tag);
|
||||
expect(tag.getSlug()).toEqual(tags[0].slug);
|
||||
console.log(`Got tag by slug: ${tag.getName()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get tag by ID', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 1 });
|
||||
if (tags.length > 0) {
|
||||
const tag = await testGhostInstance.getTagById(tags[0].id);
|
||||
expect(tag).toBeInstanceOf(ghost.Tag);
|
||||
expect(tag.getId()).toEqual(tags[0].id);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create tag', async () => {
|
||||
const timestamp = Date.now();
|
||||
createdTag = await testGhostInstance.createTag({
|
||||
name: `Test Tag ${timestamp}`,
|
||||
slug: `test-tag-${timestamp}`,
|
||||
description: 'A test tag created by automated tests'
|
||||
});
|
||||
expect(createdTag).toBeInstanceOf(ghost.Tag);
|
||||
expect(createdTag.getName()).toEqual(`Test Tag ${timestamp}`);
|
||||
console.log(`Created tag: ${createdTag.getId()}`);
|
||||
});
|
||||
|
||||
tap.test('should access tag methods', async () => {
|
||||
if (createdTag) {
|
||||
expect(createdTag.getId()).toBeTruthy();
|
||||
expect(createdTag.getName()).toBeTruthy();
|
||||
expect(createdTag.getSlug()).toBeTruthy();
|
||||
expect(createdTag.getDescription()).toBeTruthy();
|
||||
const json = createdTag.toJson();
|
||||
expect(json).toBeTruthy();
|
||||
expect(json.id).toEqual(createdTag.getId());
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update tag', async () => {
|
||||
if (createdTag) {
|
||||
const updatedTag = await createdTag.update({
|
||||
description: 'Updated description for test tag'
|
||||
});
|
||||
expect(updatedTag).toBeInstanceOf(ghost.Tag);
|
||||
expect(updatedTag.getDescription()).toEqual('Updated description for test tag');
|
||||
console.log(`Updated tag: ${updatedTag.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should delete tag', async () => {
|
||||
if (createdTag) {
|
||||
await createdTag.delete();
|
||||
console.log(`Deleted tag: ${createdTag.getId()}`);
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
25
test/test.ts
25
test/test.ts
@@ -1,25 +0,0 @@
|
||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js'
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
|
||||
tap.test('should create a valid instance of Ghost', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'https://coffee.link',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts();
|
||||
expect(posts).toBeArray();
|
||||
expect(posts[0]).toBeInstanceOf(ghost.Post);
|
||||
console.log(posts.map((post) => post.getTitle()));
|
||||
})
|
||||
|
||||
tap.start()
|
186
test/test.ts.old
Normal file
186
test/test.ts.old
Normal file
@@ -0,0 +1,186 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
// make sure we can import the IPost type
|
||||
import {type IPost} from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
|
||||
tap.test('should create a valid instance of Ghost', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts();
|
||||
expect(posts).toBeArray();
|
||||
expect(posts[0]).toBeInstanceOf(ghost.Post);
|
||||
console.log(JSON.stringify(posts[0].postData, null, 2));
|
||||
posts.map((post) => {
|
||||
// console.log(JSON.stringify(post.postData, null, 2));
|
||||
console.log(`-> ${post.getTitle()}`);
|
||||
console.log(`by ${post.getAuthor().name}`)
|
||||
console.log(post.getExcerpt());
|
||||
console.log(`===============`)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
tap.test('should get all tags', async () => {
|
||||
const tags = await testGhostInstance.getTags();
|
||||
expect(tags).toBeArray();
|
||||
console.log(`Found ${tags.length} tags:`);
|
||||
tags.forEach((tag) => {
|
||||
console.log(`-> ${tag.name} (${tag.slug})`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should filter tags with minimatch pattern', async () => {
|
||||
const allTags = await testGhostInstance.getTags();
|
||||
|
||||
if (allTags.length > 0) {
|
||||
const firstTagSlug = allTags[0].slug;
|
||||
const pattern = `${firstTagSlug.charAt(0)}*`;
|
||||
|
||||
const filteredTags = await testGhostInstance.getTags({ filter: pattern });
|
||||
expect(filteredTags).toBeArray();
|
||||
console.log(`Filtered tags with pattern '${pattern}':`);
|
||||
filteredTags.forEach((tag) => {
|
||||
console.log(`-> ${tag.name} (${tag.slug})`);
|
||||
expect(tag.slug).toMatch(new RegExp(`^${firstTagSlug.charAt(0)}`));
|
||||
});
|
||||
} else {
|
||||
console.log('No tags available to test filtering');
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get tag by slug', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 1 });
|
||||
if (tags.length > 0) {
|
||||
const tag = await testGhostInstance.getTagBySlug(tags[0].slug);
|
||||
expect(tag).toBeInstanceOf(ghost.Tag);
|
||||
console.log(`Got tag: ${tag.getName()} (${tag.getSlug()})`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get all authors', async () => {
|
||||
const authors = await testGhostInstance.getAuthors();
|
||||
expect(authors).toBeArray();
|
||||
console.log(`Found ${authors.length} authors:`);
|
||||
authors.forEach((author) => {
|
||||
console.log(`-> ${author.getName()} (${author.getSlug()})`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should filter authors with minimatch pattern', async () => {
|
||||
const authors = await testGhostInstance.getAuthors();
|
||||
if (authors.length > 0) {
|
||||
const firstAuthorSlug = authors[0].getSlug();
|
||||
const pattern = `${firstAuthorSlug.charAt(0)}*`;
|
||||
const filteredAuthors = await testGhostInstance.getAuthors({ filter: pattern });
|
||||
expect(filteredAuthors).toBeArray();
|
||||
console.log(`Filtered authors with pattern '${pattern}':`);
|
||||
filteredAuthors.forEach((author) => {
|
||||
console.log(`-> ${author.getName()} (${author.getSlug()})`);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get all pages', async () => {
|
||||
const pages = await testGhostInstance.getPages();
|
||||
expect(pages).toBeArray();
|
||||
console.log(`Found ${pages.length} pages:`);
|
||||
pages.forEach((page) => {
|
||||
console.log(`-> ${page.getTitle()} (${page.getSlug()})`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should filter posts by tag', async () => {
|
||||
const tags = await testGhostInstance.getTags({ limit: 1 });
|
||||
if (tags.length > 0) {
|
||||
const posts = await testGhostInstance.getPosts({ tag: tags[0].slug, limit: 5 });
|
||||
expect(posts).toBeArray();
|
||||
console.log(`Found ${posts.length} posts with tag '${tags[0].name}'`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should filter posts by featured status', async () => {
|
||||
const featuredPosts = await testGhostInstance.getPosts({ featured: true, limit: 5 });
|
||||
expect(featuredPosts).toBeArray();
|
||||
console.log(`Found ${featuredPosts.length} featured posts`);
|
||||
});
|
||||
|
||||
tap.test('should search posts', async () => {
|
||||
const searchResults = await testGhostInstance.searchPosts('the', { limit: 5 });
|
||||
expect(searchResults).toBeArray();
|
||||
console.log(`Found ${searchResults.length} posts matching 'the':`);
|
||||
searchResults.forEach((post) => {
|
||||
console.log(`-> ${post.getTitle()}`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should get related posts', async () => {
|
||||
const posts = await testGhostInstance.getPosts({ limit: 1 });
|
||||
if (posts.length > 0) {
|
||||
const relatedPosts = await testGhostInstance.getRelatedPosts(posts[0].getId(), 3);
|
||||
expect(relatedPosts).toBeArray();
|
||||
console.log(`Found ${relatedPosts.length} related posts for '${posts[0].getTitle()}'`);
|
||||
relatedPosts.forEach((post) => {
|
||||
console.log(`-> ${post.getTitle()}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get members', async () => {
|
||||
try {
|
||||
const members = await testGhostInstance.getMembers({ limit: 10 });
|
||||
expect(members).toBeArray();
|
||||
console.log(`Found ${members.length} members`);
|
||||
if (members.length > 0) {
|
||||
console.log(`First member: ${members[0].getEmail()}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('members') || error.statusCode === 403) {
|
||||
console.log('Members feature not available or requires permissions');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get settings', async () => {
|
||||
try {
|
||||
const settings = await testGhostInstance.getSettings();
|
||||
expect(settings).toBeTruthy();
|
||||
console.log(`Retrieved ${settings.settings?.length || 0} settings`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('undefined') || error.statusCode === 403) {
|
||||
console.log('Settings API not available or requires different permissions');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get webhooks', async () => {
|
||||
try {
|
||||
const webhooks = await testGhostInstance.getWebhooks();
|
||||
expect(webhooks).toBeArray();
|
||||
console.log(`Found ${webhooks.length} webhooks`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available in this Ghost version');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.start()
|
106
test/test.webhook.node.ts
Normal file
106
test/test.webhook.node.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||
|
||||
import * as ghost from '../ts/index.js';
|
||||
|
||||
let testGhostInstance: ghost.Ghost;
|
||||
let createdWebhook: any;
|
||||
|
||||
tap.test('initialize Ghost instance', async () => {
|
||||
testGhostInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||||
});
|
||||
|
||||
tap.test('should get all webhooks', async () => {
|
||||
try {
|
||||
const webhooks = await testGhostInstance.getWebhooks();
|
||||
expect(webhooks).toBeArray();
|
||||
console.log(`Found ${webhooks.length} webhooks`);
|
||||
if (webhooks.length > 0) {
|
||||
console.log(`First webhook: ${webhooks[0].name || 'unnamed'}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available in this Ghost version - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create webhook', async () => {
|
||||
try {
|
||||
const timestamp = Date.now();
|
||||
createdWebhook = await testGhostInstance.createWebhook({
|
||||
event: 'post.published',
|
||||
target_url: `https://example.com/webhook/${timestamp}`,
|
||||
name: `Test Webhook ${timestamp}`
|
||||
});
|
||||
expect(createdWebhook).toBeTruthy();
|
||||
expect(createdWebhook.id).toBeTruthy();
|
||||
console.log(`Created webhook: ${createdWebhook.id}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should get webhook by ID', async () => {
|
||||
if (createdWebhook) {
|
||||
try {
|
||||
const webhook = await testGhostInstance.getWebhookById(createdWebhook.id);
|
||||
expect(webhook).toBeTruthy();
|
||||
expect(webhook.id).toEqual(createdWebhook.id);
|
||||
console.log(`Got webhook by ID: ${webhook.id}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should update webhook', async () => {
|
||||
if (createdWebhook) {
|
||||
try {
|
||||
const updatedWebhook = await testGhostInstance.updateWebhook(createdWebhook.id, {
|
||||
target_url: 'https://example.com/webhook/updated'
|
||||
});
|
||||
expect(updatedWebhook).toBeTruthy();
|
||||
console.log(`Updated webhook: ${updatedWebhook.id}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should delete webhook', async () => {
|
||||
if (createdWebhook) {
|
||||
try {
|
||||
await testGhostInstance.deleteWebhook(createdWebhook.id);
|
||||
console.log(`Deleted webhook: ${createdWebhook.id}`);
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||||
console.log('Webhooks API not available - skipping test');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/ghost',
|
||||
version: '1.0.3',
|
||||
version: '1.4.1',
|
||||
description: 'An unofficial Ghost CMS API package enabling content and admin functionality for managing posts.'
|
||||
}
|
||||
|
50
ts/classes.author.ts
Normal file
50
ts/classes.author.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import type { IAuthor } from './classes.post.js';
|
||||
|
||||
export class Author {
|
||||
public ghostInstanceRef: Ghost;
|
||||
public authorData: IAuthor;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, authorData: IAuthor) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.authorData = authorData;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.authorData.id;
|
||||
}
|
||||
|
||||
public getName(): string {
|
||||
return this.authorData.name;
|
||||
}
|
||||
|
||||
public getSlug(): string {
|
||||
return this.authorData.slug;
|
||||
}
|
||||
|
||||
public getProfileImage(): string | undefined {
|
||||
return this.authorData.profile_image;
|
||||
}
|
||||
|
||||
public getBio(): string | undefined {
|
||||
return this.authorData.bio;
|
||||
}
|
||||
|
||||
public toJson(): IAuthor {
|
||||
return this.authorData;
|
||||
}
|
||||
|
||||
public async update(authorData: Partial<IAuthor>): Promise<Author> {
|
||||
try {
|
||||
const updatedAuthorData = await this.ghostInstanceRef.adminApi.users.edit({
|
||||
...authorData,
|
||||
id: this.getId()
|
||||
});
|
||||
this.authorData = updatedAuthorData;
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error updating author:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,9 @@
|
||||
import * as plugins from './ghost.plugins.js';
|
||||
import { Post, type IPostOptions } from './classes.post.js';
|
||||
import { Post, type IPost, type ITag, type IAuthor } from './classes.post.js';
|
||||
import { Author } from './classes.author.js';
|
||||
import { Tag } from './classes.tag.js';
|
||||
import { Page, type IPage } from './classes.page.js';
|
||||
import { Member, type IMember } from './classes.member.js';
|
||||
|
||||
export interface IGhostConstructorOptions {
|
||||
baseUrl: string;
|
||||
@@ -18,20 +22,48 @@ 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[]> {
|
||||
public async getPosts(optionsArg?: {
|
||||
limit?: number;
|
||||
tag?: string;
|
||||
author?: string;
|
||||
featured?: boolean;
|
||||
filter?: string;
|
||||
}): Promise<Post[]> {
|
||||
try {
|
||||
const postsData = await this.contentApi.posts.browse({ limit });
|
||||
return postsData.map((postData: IPostOptions) => new Post(this, postData));
|
||||
const limit = optionsArg?.limit || 1000;
|
||||
const filters: string[] = [];
|
||||
|
||||
if (optionsArg?.tag) {
|
||||
filters.push(`tag:${optionsArg.tag}`);
|
||||
}
|
||||
if (optionsArg?.author) {
|
||||
filters.push(`author:${optionsArg.author}`);
|
||||
}
|
||||
if (optionsArg?.featured !== undefined) {
|
||||
filters.push(`featured:${optionsArg.featured}`);
|
||||
}
|
||||
if (optionsArg?.filter) {
|
||||
filters.push(optionsArg.filter);
|
||||
}
|
||||
|
||||
const filterString = filters.length > 0 ? filters.join('+') : undefined;
|
||||
|
||||
const postsData = await this.contentApi.posts.browse({
|
||||
limit,
|
||||
include: 'tags,authors',
|
||||
...(filterString && { filter: filterString })
|
||||
});
|
||||
return postsData.map((postData: IPost) => new Post(this, postData));
|
||||
} catch (error) {
|
||||
console.error('Error fetching posts:', error);
|
||||
throw error;
|
||||
@@ -41,20 +73,356 @@ export class Ghost {
|
||||
public async getPostById(id: string): Promise<Post> {
|
||||
try {
|
||||
const postData = await this.contentApi.posts.read({ id });
|
||||
return new Post(postData, this.adminApi);
|
||||
return new Post(this, postData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching post with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createPost(postData: IPostOptions): Promise<Post> {
|
||||
public async createPost(postData: IPost): Promise<Post> {
|
||||
try {
|
||||
const createdPostData = await this.adminApi.posts.add(postData);
|
||||
return new Post(createdPostData, this.adminApi);
|
||||
return new Post(this, createdPostData);
|
||||
} catch (error) {
|
||||
console.error('Error creating post:', error);
|
||||
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);
|
||||
}
|
||||
|
||||
public async getTags(optionsArg?: { filter?: string; limit?: number }): Promise<ITag[]> {
|
||||
try {
|
||||
const limit = optionsArg?.limit || 1000;
|
||||
const tagsData = await this.contentApi.tags.browse({ limit });
|
||||
|
||||
if (optionsArg?.filter) {
|
||||
const matcher = new plugins.smartmatch.SmartMatch(optionsArg.filter);
|
||||
return tagsData.filter((tag: ITag) => matcher.match(tag.slug));
|
||||
}
|
||||
|
||||
return tagsData;
|
||||
} catch (error) {
|
||||
console.error('Error fetching tags:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getTagById(id: string): Promise<Tag> {
|
||||
try {
|
||||
const tagData = await this.contentApi.tags.read({ id });
|
||||
return new Tag(this, tagData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching tag with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getTagBySlug(slug: string): Promise<Tag> {
|
||||
try {
|
||||
const tagData = await this.contentApi.tags.read({ slug });
|
||||
return new Tag(this, tagData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching tag with slug ${slug}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createTag(tagData: Partial<ITag>): Promise<Tag> {
|
||||
try {
|
||||
const createdTagData = await this.adminApi.tags.add(tagData);
|
||||
return new Tag(this, createdTagData);
|
||||
} catch (error) {
|
||||
console.error('Error creating tag:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getAuthors(optionsArg?: { filter?: string; limit?: number }): Promise<Author[]> {
|
||||
try {
|
||||
const limit = optionsArg?.limit || 1000;
|
||||
const authorsData = await this.contentApi.authors.browse({ limit });
|
||||
|
||||
if (optionsArg?.filter) {
|
||||
const matcher = new plugins.smartmatch.SmartMatch(optionsArg.filter);
|
||||
return authorsData
|
||||
.filter((author: IAuthor) => matcher.match(author.slug))
|
||||
.map((author: IAuthor) => new Author(this, author));
|
||||
}
|
||||
|
||||
return authorsData.map((author: IAuthor) => new Author(this, author));
|
||||
} catch (error) {
|
||||
console.error('Error fetching authors:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getAuthorById(id: string): Promise<Author> {
|
||||
try {
|
||||
const authorData = await this.contentApi.authors.read({ id });
|
||||
return new Author(this, authorData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching author with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getAuthorBySlug(slug: string): Promise<Author> {
|
||||
try {
|
||||
const authorData = await this.contentApi.authors.read({ slug });
|
||||
return new Author(this, authorData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching author with slug ${slug}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getPages(optionsArg?: { limit?: number; filter?: string }): Promise<Page[]> {
|
||||
try {
|
||||
const limit = optionsArg?.limit || 1000;
|
||||
const pagesData = await this.contentApi.pages.browse({ limit, include: 'tags,authors' });
|
||||
|
||||
if (optionsArg?.filter) {
|
||||
const matcher = new plugins.smartmatch.SmartMatch(optionsArg.filter);
|
||||
return pagesData
|
||||
.filter((page: IPage) => matcher.match(page.slug))
|
||||
.map((page: IPage) => new Page(this, page));
|
||||
}
|
||||
|
||||
return pagesData.map((pageData: IPage) => new Page(this, pageData));
|
||||
} catch (error) {
|
||||
console.error('Error fetching pages:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getPageById(id: string): Promise<Page> {
|
||||
try {
|
||||
const pageData = await this.contentApi.pages.read({ id });
|
||||
return new Page(this, pageData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching page with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getPageBySlug(slug: string): Promise<Page> {
|
||||
try {
|
||||
const pageData = await this.contentApi.pages.read({ slug });
|
||||
return new Page(this, pageData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching page with slug ${slug}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createPage(pageData: Partial<IPage>): Promise<Page> {
|
||||
try {
|
||||
const createdPageData = await this.adminApi.pages.add(pageData);
|
||||
return new Page(this, createdPageData);
|
||||
} catch (error) {
|
||||
console.error('Error creating page:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async searchPosts(query: string, optionsArg?: { limit?: number }): Promise<Post[]> {
|
||||
try {
|
||||
const limit = optionsArg?.limit || 100;
|
||||
const postsData = await this.contentApi.posts.browse({
|
||||
limit,
|
||||
filter: `title:~'${query}'`,
|
||||
include: 'tags,authors'
|
||||
});
|
||||
return postsData.map((postData: IPost) => new Post(this, postData));
|
||||
} catch (error) {
|
||||
console.error('Error searching posts:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async uploadImage(filePath: string): Promise<string> {
|
||||
try {
|
||||
const result = await this.adminApi.images.upload({ file: filePath });
|
||||
return result.url;
|
||||
} catch (error) {
|
||||
console.error('Error uploading image:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async bulkUpdatePosts(postIds: string[], updates: Partial<IPost>): Promise<Post[]> {
|
||||
try {
|
||||
const updatePromises = postIds.map(async (id) => {
|
||||
const post = await this.getPostById(id);
|
||||
return post.update({ ...post.postData, ...updates, id });
|
||||
});
|
||||
return await Promise.all(updatePromises);
|
||||
} catch (error) {
|
||||
console.error('Error bulk updating posts:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async bulkDeletePosts(postIds: string[]): Promise<void> {
|
||||
try {
|
||||
const deletePromises = postIds.map(async (id) => {
|
||||
const post = await this.getPostById(id);
|
||||
return post.delete();
|
||||
});
|
||||
await Promise.all(deletePromises);
|
||||
} catch (error) {
|
||||
console.error('Error bulk deleting posts:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getRelatedPosts(postId: string, limit: number = 5): Promise<Post[]> {
|
||||
try {
|
||||
const post = await this.getPostById(postId);
|
||||
const tags = post.postData.tags;
|
||||
|
||||
if (!tags || !Array.isArray(tags) || tags.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const tagSlugs = tags.map(tag => tag.slug).join(',');
|
||||
const postsData = await this.contentApi.posts.browse({
|
||||
limit,
|
||||
filter: `tag:[${tagSlugs}]+id:-${postId}`,
|
||||
include: 'tags,authors'
|
||||
});
|
||||
|
||||
return postsData.map((postData: IPost) => new Post(this, postData));
|
||||
} catch (error) {
|
||||
console.error('Error fetching related posts:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getMembers(optionsArg?: { filter?: string; limit?: number }): Promise<Member[]> {
|
||||
try {
|
||||
const limit = optionsArg?.limit || 1000;
|
||||
const membersData = await this.adminApi.members.browse({ limit });
|
||||
|
||||
if (optionsArg?.filter) {
|
||||
const matcher = new plugins.smartmatch.SmartMatch(optionsArg.filter);
|
||||
return membersData
|
||||
.filter((member: IMember) => matcher.match(member.email))
|
||||
.map((member: IMember) => new Member(this, member));
|
||||
}
|
||||
|
||||
return membersData.map((member: IMember) => new Member(this, member));
|
||||
} catch (error) {
|
||||
console.error('Error fetching members:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getMemberById(id: string): Promise<Member> {
|
||||
try {
|
||||
const memberData = await this.adminApi.members.read({ id });
|
||||
return new Member(this, memberData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching member with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getMemberByEmail(email: string): Promise<Member> {
|
||||
try {
|
||||
const memberData = await this.adminApi.members.read({ email });
|
||||
return new Member(this, memberData);
|
||||
} catch (error) {
|
||||
console.error(`Error fetching member with email ${email}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createMember(memberData: Partial<IMember>): Promise<Member> {
|
||||
try {
|
||||
const createdMemberData = await this.adminApi.members.add(memberData);
|
||||
return new Member(this, createdMemberData);
|
||||
} catch (error) {
|
||||
console.error('Error creating member:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getSettings(): Promise<any> {
|
||||
try {
|
||||
return await this.adminApi.settings.browse();
|
||||
} catch (error) {
|
||||
console.error('Error fetching settings:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async updateSettings(settings: any[]): Promise<any> {
|
||||
try {
|
||||
return await this.adminApi.settings.edit(settings);
|
||||
} catch (error) {
|
||||
console.error('Error updating settings:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getWebhooks(): Promise<any[]> {
|
||||
try {
|
||||
return await this.adminApi.webhooks.browse();
|
||||
} catch (error) {
|
||||
console.error('Error fetching webhooks:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getWebhookById(id: string): Promise<any> {
|
||||
try {
|
||||
return await this.adminApi.webhooks.read({ id });
|
||||
} catch (error) {
|
||||
console.error(`Error fetching webhook with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async createWebhook(webhookData: {
|
||||
event: string;
|
||||
target_url: string;
|
||||
name?: string;
|
||||
secret?: string;
|
||||
api_version?: string;
|
||||
integration_id?: string;
|
||||
}): Promise<any> {
|
||||
try {
|
||||
return await this.adminApi.webhooks.add(webhookData);
|
||||
} catch (error) {
|
||||
console.error('Error creating webhook:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async updateWebhook(id: string, webhookData: any): Promise<any> {
|
||||
try {
|
||||
return await this.adminApi.webhooks.edit({ ...webhookData, id });
|
||||
} catch (error) {
|
||||
console.error('Error updating webhook:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteWebhook(id: string): Promise<void> {
|
||||
try {
|
||||
await this.adminApi.webhooks.delete({ id });
|
||||
} catch (error) {
|
||||
console.error(`Error deleting webhook with id ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
87
ts/classes.member.ts
Normal file
87
ts/classes.member.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
|
||||
export interface IMember {
|
||||
id: string;
|
||||
uuid: string;
|
||||
email: string;
|
||||
name?: string;
|
||||
note?: string;
|
||||
geolocation?: string;
|
||||
enable_comment_notifications?: boolean;
|
||||
subscribed?: boolean;
|
||||
email_count?: number;
|
||||
email_opened_count?: number;
|
||||
email_open_rate?: number;
|
||||
status?: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
labels?: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
}>;
|
||||
subscriptions?: any[];
|
||||
avatar_image?: string;
|
||||
comped?: boolean;
|
||||
email_suppression?: {
|
||||
suppressed: boolean;
|
||||
info?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class Member {
|
||||
public ghostInstanceRef: Ghost;
|
||||
public memberData: IMember;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, memberData: IMember) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.memberData = memberData;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.memberData.id;
|
||||
}
|
||||
|
||||
public getEmail(): string {
|
||||
return this.memberData.email;
|
||||
}
|
||||
|
||||
public getName(): string | undefined {
|
||||
return this.memberData.name;
|
||||
}
|
||||
|
||||
public getStatus(): string | undefined {
|
||||
return this.memberData.status;
|
||||
}
|
||||
|
||||
public getLabels(): Array<{ id: string; name: string; slug: string }> | undefined {
|
||||
return this.memberData.labels;
|
||||
}
|
||||
|
||||
public toJson(): IMember {
|
||||
return this.memberData;
|
||||
}
|
||||
|
||||
public async update(memberData: Partial<IMember>): Promise<Member> {
|
||||
try {
|
||||
const updatedMemberData = await this.ghostInstanceRef.adminApi.members.edit({
|
||||
...memberData,
|
||||
id: this.getId()
|
||||
});
|
||||
this.memberData = updatedMemberData;
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error updating member:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(): Promise<void> {
|
||||
try {
|
||||
await this.ghostInstanceRef.adminApi.members.delete({ id: this.getId() });
|
||||
} catch (error) {
|
||||
console.error(`Error deleting member with id ${this.getId()}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
65
ts/classes.page.ts
Normal file
65
ts/classes.page.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import type { IPost, IAuthor } from './classes.post.js';
|
||||
|
||||
export interface IPage extends IPost {}
|
||||
|
||||
export class Page {
|
||||
public ghostInstanceRef: Ghost;
|
||||
public pageData: IPage;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, pageData: IPage) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.pageData = pageData;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.pageData.id;
|
||||
}
|
||||
|
||||
public getTitle(): string {
|
||||
return this.pageData.title;
|
||||
}
|
||||
|
||||
public getHtml(): string {
|
||||
return this.pageData.html;
|
||||
}
|
||||
|
||||
public getSlug(): string {
|
||||
return this.pageData.slug;
|
||||
}
|
||||
|
||||
public getFeatureImage(): string | undefined {
|
||||
return this.pageData.feature_image;
|
||||
}
|
||||
|
||||
public getAuthor(): IAuthor {
|
||||
return this.pageData.primary_author;
|
||||
}
|
||||
|
||||
public toJson(): IPage {
|
||||
return this.pageData;
|
||||
}
|
||||
|
||||
public async update(pageData: Partial<IPage>): Promise<Page> {
|
||||
try {
|
||||
const updatedPageData = await this.ghostInstanceRef.adminApi.pages.edit({
|
||||
...pageData,
|
||||
id: this.getId()
|
||||
});
|
||||
this.pageData = updatedPageData;
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error updating page:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(): Promise<void> {
|
||||
try {
|
||||
await this.ghostInstanceRef.adminApi.pages.delete({ id: this.getId() });
|
||||
} catch (error) {
|
||||
console.error(`Error deleting page with id ${this.getId()}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,20 +1,91 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import * as plugins from './ghost.plugins.js';
|
||||
|
||||
export interface IPostOptions {
|
||||
export interface IAuthor {
|
||||
id: string;
|
||||
title: string;
|
||||
html: string;
|
||||
excerpt?: 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 IPost {
|
||||
id: string;
|
||||
uuid: string;
|
||||
title: string;
|
||||
slug: string;
|
||||
html: 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: IPost;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, postData: IPostOptions) {
|
||||
constructor(ghostInstanceRefArg: Ghost, postData: IPost) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.postData = postData;
|
||||
}
|
||||
@@ -39,11 +110,15 @@ export class Post {
|
||||
return this.postData.feature_image;
|
||||
}
|
||||
|
||||
public toJson(): IPostOptions {
|
||||
public getAuthor(): IAuthor {
|
||||
return this.postData.primary_author;
|
||||
}
|
||||
|
||||
public toJson(): IPost {
|
||||
return this.postData;
|
||||
}
|
||||
|
||||
public async update(postData: IPostOptions): Promise<Post> {
|
||||
public async update(postData: IPost): Promise<Post> {
|
||||
try {
|
||||
const updatedPostData = await this.ghostInstanceRef.adminApi.posts.edit(postData);
|
||||
this.postData = updatedPostData;
|
||||
|
55
ts/classes.tag.ts
Normal file
55
ts/classes.tag.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import type { ITag } from './classes.post.js';
|
||||
|
||||
export class Tag {
|
||||
public ghostInstanceRef: Ghost;
|
||||
public tagData: ITag;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, tagData: ITag) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.tagData = tagData;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.tagData.id;
|
||||
}
|
||||
|
||||
public getName(): string {
|
||||
return this.tagData.name;
|
||||
}
|
||||
|
||||
public getSlug(): string {
|
||||
return this.tagData.slug;
|
||||
}
|
||||
|
||||
public getDescription(): string | undefined {
|
||||
return this.tagData.description;
|
||||
}
|
||||
|
||||
public toJson(): ITag {
|
||||
return this.tagData;
|
||||
}
|
||||
|
||||
public async update(tagData: Partial<ITag>): Promise<Tag> {
|
||||
try {
|
||||
const updatedTagData = await this.ghostInstanceRef.adminApi.tags.edit({
|
||||
...tagData,
|
||||
id: this.getId()
|
||||
});
|
||||
this.tagData = updatedTagData;
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error updating tag:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(): Promise<void> {
|
||||
try {
|
||||
await this.ghostInstanceRef.adminApi.tags.delete({ id: this.getId() });
|
||||
} catch (error) {
|
||||
console.error(`Error deleting tag with id ${this.getId()}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,7 +1,9 @@
|
||||
import GhostContentAPI from '@tryghost/content-api';
|
||||
import GhostAdminAPI from '@tryghost/admin-api';
|
||||
import * as smartmatch from '@push.rocks/smartmatch';
|
||||
|
||||
export {
|
||||
GhostContentAPI,
|
||||
GhostAdminAPI
|
||||
GhostAdminAPI,
|
||||
smartmatch
|
||||
}
|
||||
|
@@ -1,2 +1,6 @@
|
||||
export * from './classes.ghost.js';
|
||||
export * from './classes.post.js';
|
||||
export * from './classes.author.js';
|
||||
export * from './classes.tag.js';
|
||||
export * from './classes.page.js';
|
||||
export * from './classes.member.js';
|
Reference in New Issue
Block a user