wp-astrojs-integration 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +466 -0
- package/dist/client/auth.d.ts +11 -0
- package/dist/client/auth.js +7 -0
- package/dist/client/categories.d.ts +39 -0
- package/dist/client/categories.js +69 -0
- package/dist/client/index.d.ts +102 -0
- package/dist/client/index.js +164 -0
- package/dist/client/media.d.ts +47 -0
- package/dist/client/media.js +82 -0
- package/dist/client/pages.d.ts +39 -0
- package/dist/client/pages.js +69 -0
- package/dist/client/posts.d.ts +39 -0
- package/dist/client/posts.js +69 -0
- package/dist/client/settings.d.ts +13 -0
- package/dist/client/settings.js +19 -0
- package/dist/client/tags.d.ts +39 -0
- package/dist/client/tags.js +69 -0
- package/dist/client/types.d.ts +166 -0
- package/dist/client/types.js +33 -0
- package/dist/client/users.d.ts +42 -0
- package/dist/client/users.js +74 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +8 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +19 -0
- package/dist/loaders/index.d.ts +9 -0
- package/dist/loaders/index.js +10 -0
- package/dist/loaders/live.d.ts +104 -0
- package/dist/loaders/live.js +253 -0
- package/dist/loaders/static.d.ts +77 -0
- package/dist/loaders/static.js +193 -0
- package/dist/loaders/types.d.ts +59 -0
- package/dist/loaders/types.js +4 -0
- package/dist/schemas/index.d.ts +992 -0
- package/dist/schemas/index.js +199 -0
- package/package.json +60 -0
- package/src/components/WPContent.astro +48 -0
- package/src/components/WPImage.astro +106 -0
- package/src/components/index.ts +9 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { createBasicAuthHeader } from './auth';
|
|
2
|
+
import { createPostsMethods } from './posts';
|
|
3
|
+
import { createPagesMethods } from './pages';
|
|
4
|
+
import { createMediaMethods } from './media';
|
|
5
|
+
import { createCategoriesMethods } from './categories';
|
|
6
|
+
import { createTagsMethods } from './tags';
|
|
7
|
+
import { createUsersMethods } from './users';
|
|
8
|
+
import { createSettingsMethods } from './settings';
|
|
9
|
+
/**
|
|
10
|
+
* WordPress API Client
|
|
11
|
+
* Provides direct access to WordPress REST API endpoints
|
|
12
|
+
* Used internally by loaders and available for runtime data fetching
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Without auth
|
|
16
|
+
* const wp = new WordPressClient({ baseUrl: 'https://example.com' });
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // With auth
|
|
20
|
+
* const wp = new WordPressClient({
|
|
21
|
+
* baseUrl: 'https://example.com',
|
|
22
|
+
* auth: { username: 'admin', password: 'app-password' }
|
|
23
|
+
* });
|
|
24
|
+
*/
|
|
25
|
+
export class WordPressClient {
|
|
26
|
+
baseUrl;
|
|
27
|
+
apiBase;
|
|
28
|
+
authHeader;
|
|
29
|
+
// Posts methods
|
|
30
|
+
getPosts;
|
|
31
|
+
getAllPosts;
|
|
32
|
+
getPostsPaginated;
|
|
33
|
+
getPost;
|
|
34
|
+
getPostBySlug;
|
|
35
|
+
// Pages methods
|
|
36
|
+
getPages;
|
|
37
|
+
getAllPages;
|
|
38
|
+
getPagesPaginated;
|
|
39
|
+
getPage;
|
|
40
|
+
getPageBySlug;
|
|
41
|
+
// Media methods
|
|
42
|
+
getMedia;
|
|
43
|
+
getAllMedia;
|
|
44
|
+
getMediaPaginated;
|
|
45
|
+
getMediaItem;
|
|
46
|
+
getMediaBySlug;
|
|
47
|
+
getImageUrl;
|
|
48
|
+
// Categories methods
|
|
49
|
+
getCategories;
|
|
50
|
+
getAllCategories;
|
|
51
|
+
getCategoriesPaginated;
|
|
52
|
+
getCategory;
|
|
53
|
+
getCategoryBySlug;
|
|
54
|
+
// Tags methods
|
|
55
|
+
getTags;
|
|
56
|
+
getAllTags;
|
|
57
|
+
getTagsPaginated;
|
|
58
|
+
getTag;
|
|
59
|
+
getTagBySlug;
|
|
60
|
+
// Users methods
|
|
61
|
+
getUsers;
|
|
62
|
+
getAllUsers;
|
|
63
|
+
getUsersPaginated;
|
|
64
|
+
getUser;
|
|
65
|
+
getCurrentUser;
|
|
66
|
+
// Settings methods
|
|
67
|
+
getSettings;
|
|
68
|
+
constructor(config) {
|
|
69
|
+
this.baseUrl = config.baseUrl;
|
|
70
|
+
this.authHeader = config.auth ? createBasicAuthHeader(config.auth) : undefined;
|
|
71
|
+
this.apiBase = `${this.baseUrl}/index.php?rest_route=/wp/v2`;
|
|
72
|
+
// Bind fetchAPI and hasAuth for resource methods
|
|
73
|
+
const fetchAPI = this.fetchAPI.bind(this);
|
|
74
|
+
const fetchAPIPaginated = this.fetchAPIPaginated.bind(this);
|
|
75
|
+
const hasAuth = this.hasAuth.bind(this);
|
|
76
|
+
// Initialize resource methods
|
|
77
|
+
const posts = createPostsMethods(fetchAPI, fetchAPIPaginated);
|
|
78
|
+
this.getPosts = posts.getPosts;
|
|
79
|
+
this.getAllPosts = posts.getAllPosts;
|
|
80
|
+
this.getPostsPaginated = posts.getPostsPaginated;
|
|
81
|
+
this.getPost = posts.getPost;
|
|
82
|
+
this.getPostBySlug = posts.getPostBySlug;
|
|
83
|
+
const pages = createPagesMethods(fetchAPI, fetchAPIPaginated);
|
|
84
|
+
this.getPages = pages.getPages;
|
|
85
|
+
this.getAllPages = pages.getAllPages;
|
|
86
|
+
this.getPagesPaginated = pages.getPagesPaginated;
|
|
87
|
+
this.getPage = pages.getPage;
|
|
88
|
+
this.getPageBySlug = pages.getPageBySlug;
|
|
89
|
+
const media = createMediaMethods(fetchAPI, fetchAPIPaginated);
|
|
90
|
+
this.getMedia = media.getMedia;
|
|
91
|
+
this.getAllMedia = media.getAllMedia;
|
|
92
|
+
this.getMediaPaginated = media.getMediaPaginated;
|
|
93
|
+
this.getMediaItem = media.getMediaItem;
|
|
94
|
+
this.getMediaBySlug = media.getMediaBySlug;
|
|
95
|
+
this.getImageUrl = media.getImageUrl;
|
|
96
|
+
const categories = createCategoriesMethods(fetchAPI, fetchAPIPaginated);
|
|
97
|
+
this.getCategories = categories.getCategories;
|
|
98
|
+
this.getAllCategories = categories.getAllCategories;
|
|
99
|
+
this.getCategoriesPaginated = categories.getCategoriesPaginated;
|
|
100
|
+
this.getCategory = categories.getCategory;
|
|
101
|
+
this.getCategoryBySlug = categories.getCategoryBySlug;
|
|
102
|
+
const tags = createTagsMethods(fetchAPI, fetchAPIPaginated);
|
|
103
|
+
this.getTags = tags.getTags;
|
|
104
|
+
this.getAllTags = tags.getAllTags;
|
|
105
|
+
this.getTagsPaginated = tags.getTagsPaginated;
|
|
106
|
+
this.getTag = tags.getTag;
|
|
107
|
+
this.getTagBySlug = tags.getTagBySlug;
|
|
108
|
+
const users = createUsersMethods(fetchAPI, fetchAPIPaginated, hasAuth);
|
|
109
|
+
this.getUsers = users.getUsers;
|
|
110
|
+
this.getAllUsers = users.getAllUsers;
|
|
111
|
+
this.getUsersPaginated = users.getUsersPaginated;
|
|
112
|
+
this.getUser = users.getUser;
|
|
113
|
+
this.getCurrentUser = users.getCurrentUser;
|
|
114
|
+
const settings = createSettingsMethods(fetchAPI, hasAuth);
|
|
115
|
+
this.getSettings = settings.getSettings;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Checks if authentication is configured
|
|
119
|
+
*/
|
|
120
|
+
hasAuth() {
|
|
121
|
+
return this.authHeader !== undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Fetches data from WordPress REST API
|
|
125
|
+
* Auth header is automatically added if configured
|
|
126
|
+
*
|
|
127
|
+
* @param endpoint - API endpoint path (e.g., '/posts', '/settings')
|
|
128
|
+
* @param params - Query parameters to append to the request
|
|
129
|
+
*/
|
|
130
|
+
async fetchAPI(endpoint, params = {}) {
|
|
131
|
+
const result = await this.fetchAPIPaginated(endpoint, params);
|
|
132
|
+
return result.data;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Fetches data from WordPress REST API with pagination info
|
|
136
|
+
* Auth header is automatically added if configured
|
|
137
|
+
*
|
|
138
|
+
* @param endpoint - API endpoint path (e.g., '/posts', '/settings')
|
|
139
|
+
* @param params - Query parameters to append to the request
|
|
140
|
+
* @returns Object with data and pagination headers
|
|
141
|
+
*/
|
|
142
|
+
async fetchAPIPaginated(endpoint, params = {}) {
|
|
143
|
+
const startTime = performance.now();
|
|
144
|
+
const url = new URL(`${this.apiBase}${endpoint}`);
|
|
145
|
+
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value));
|
|
146
|
+
const headers = {
|
|
147
|
+
'Content-Type': 'application/json',
|
|
148
|
+
};
|
|
149
|
+
if (this.authHeader) {
|
|
150
|
+
headers['Authorization'] = this.authHeader;
|
|
151
|
+
}
|
|
152
|
+
const response = await fetch(url.toString(), { headers });
|
|
153
|
+
if (!response.ok) {
|
|
154
|
+
throw new Error(`WordPress API error: ${response.status} ${response.statusText}`);
|
|
155
|
+
}
|
|
156
|
+
const data = await response.json();
|
|
157
|
+
const duration = Math.round(performance.now() - startTime);
|
|
158
|
+
console.log(`[WP API] ${endpoint} - ${duration}ms - params:`, JSON.stringify(params));
|
|
159
|
+
// Extract pagination headers
|
|
160
|
+
const total = parseInt(response.headers.get('X-WP-Total') || '0', 10);
|
|
161
|
+
const totalPages = parseInt(response.headers.get('X-WP-TotalPages') || '0', 10);
|
|
162
|
+
return { data, total, totalPages };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { WordPressMedia } from '../schemas';
|
|
2
|
+
import type { FetchResult } from './index';
|
|
3
|
+
import type { MediaFilter, PaginatedResponse } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Media API methods factory
|
|
6
|
+
* Creates type-safe methods for fetching WordPress media with filtering and pagination
|
|
7
|
+
*/
|
|
8
|
+
export declare function createMediaMethods(fetchAPI: <T>(endpoint: string, params?: Record<string, string>) => Promise<T>, fetchAPIPaginated: <T>(endpoint: string, params?: Record<string, string>) => Promise<FetchResult<T>>): {
|
|
9
|
+
/**
|
|
10
|
+
* Gets media items with optional filtering (single page, max 100 items)
|
|
11
|
+
*
|
|
12
|
+
* @param filter - Filter options (mediaType, mimeType, author, parent, etc.)
|
|
13
|
+
* @returns Array of media items matching the filter criteria
|
|
14
|
+
*/
|
|
15
|
+
getMedia(filter?: MediaFilter): Promise<WordPressMedia[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Gets ALL media items by automatically paginating through all pages
|
|
18
|
+
* Use this for static site generation to ensure all content is fetched
|
|
19
|
+
*
|
|
20
|
+
* @param filter - Filter options (mediaType, mimeType, author, parent, etc.)
|
|
21
|
+
* @returns Array of all media items matching the filter criteria
|
|
22
|
+
*/
|
|
23
|
+
getAllMedia(filter?: Omit<MediaFilter, "page">): Promise<WordPressMedia[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets media items with pagination metadata
|
|
26
|
+
*
|
|
27
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
28
|
+
* @returns Paginated response with media items and total counts
|
|
29
|
+
*/
|
|
30
|
+
getMediaPaginated(filter?: MediaFilter): Promise<PaginatedResponse<WordPressMedia>>;
|
|
31
|
+
/**
|
|
32
|
+
* Gets a single media item by ID
|
|
33
|
+
*/
|
|
34
|
+
getMediaItem(id: number): Promise<WordPressMedia>;
|
|
35
|
+
/**
|
|
36
|
+
* Gets a single media item by slug
|
|
37
|
+
*/
|
|
38
|
+
getMediaBySlug(slug: string): Promise<WordPressMedia | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* Gets the URL for a specific media size
|
|
41
|
+
*
|
|
42
|
+
* @param media - WordPress media object
|
|
43
|
+
* @param size - Size name (e.g., 'thumbnail', 'medium', 'large', 'full')
|
|
44
|
+
* @returns Image URL for the specified size
|
|
45
|
+
*/
|
|
46
|
+
getImageUrl(media: WordPressMedia, size?: string): string;
|
|
47
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { filterToParams } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Media API methods factory
|
|
4
|
+
* Creates type-safe methods for fetching WordPress media with filtering and pagination
|
|
5
|
+
*/
|
|
6
|
+
export function createMediaMethods(fetchAPI, fetchAPIPaginated) {
|
|
7
|
+
return {
|
|
8
|
+
/**
|
|
9
|
+
* Gets media items with optional filtering (single page, max 100 items)
|
|
10
|
+
*
|
|
11
|
+
* @param filter - Filter options (mediaType, mimeType, author, parent, etc.)
|
|
12
|
+
* @returns Array of media items matching the filter criteria
|
|
13
|
+
*/
|
|
14
|
+
async getMedia(filter = {}) {
|
|
15
|
+
const params = filterToParams(filter);
|
|
16
|
+
return fetchAPI('/media', params);
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* Gets ALL media items by automatically paginating through all pages
|
|
20
|
+
* Use this for static site generation to ensure all content is fetched
|
|
21
|
+
*
|
|
22
|
+
* @param filter - Filter options (mediaType, mimeType, author, parent, etc.)
|
|
23
|
+
* @returns Array of all media items matching the filter criteria
|
|
24
|
+
*/
|
|
25
|
+
async getAllMedia(filter = {}) {
|
|
26
|
+
const allMedia = [];
|
|
27
|
+
let page = 1;
|
|
28
|
+
let totalPages = 1;
|
|
29
|
+
do {
|
|
30
|
+
const params = filterToParams({ ...filter, page, perPage: 100 });
|
|
31
|
+
const result = await fetchAPIPaginated('/media', params);
|
|
32
|
+
allMedia.push(...result.data);
|
|
33
|
+
totalPages = result.totalPages;
|
|
34
|
+
page++;
|
|
35
|
+
} while (page <= totalPages);
|
|
36
|
+
return allMedia;
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Gets media items with pagination metadata
|
|
40
|
+
*
|
|
41
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
42
|
+
* @returns Paginated response with media items and total counts
|
|
43
|
+
*/
|
|
44
|
+
async getMediaPaginated(filter = {}) {
|
|
45
|
+
const params = filterToParams(filter);
|
|
46
|
+
const result = await fetchAPIPaginated('/media', params);
|
|
47
|
+
return {
|
|
48
|
+
data: result.data,
|
|
49
|
+
total: result.total,
|
|
50
|
+
totalPages: result.totalPages,
|
|
51
|
+
page: filter.page || 1,
|
|
52
|
+
perPage: filter.perPage || 100,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Gets a single media item by ID
|
|
57
|
+
*/
|
|
58
|
+
async getMediaItem(id) {
|
|
59
|
+
return fetchAPI(`/media/${id}`);
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Gets a single media item by slug
|
|
63
|
+
*/
|
|
64
|
+
async getMediaBySlug(slug) {
|
|
65
|
+
const media = await fetchAPI('/media', { slug });
|
|
66
|
+
return media[0];
|
|
67
|
+
},
|
|
68
|
+
/**
|
|
69
|
+
* Gets the URL for a specific media size
|
|
70
|
+
*
|
|
71
|
+
* @param media - WordPress media object
|
|
72
|
+
* @param size - Size name (e.g., 'thumbnail', 'medium', 'large', 'full')
|
|
73
|
+
* @returns Image URL for the specified size
|
|
74
|
+
*/
|
|
75
|
+
getImageUrl(media, size = 'full') {
|
|
76
|
+
if (size === 'full' || !media.media_details.sizes[size]) {
|
|
77
|
+
return media.source_url;
|
|
78
|
+
}
|
|
79
|
+
return media.media_details.sizes[size].source_url;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { WordPressPage } from '../schemas';
|
|
2
|
+
import type { FetchResult } from './index';
|
|
3
|
+
import type { PagesFilter, PaginatedResponse } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Pages API methods factory
|
|
6
|
+
* Creates type-safe methods for fetching WordPress pages with filtering and pagination
|
|
7
|
+
*/
|
|
8
|
+
export declare function createPagesMethods(fetchAPI: <T>(endpoint: string, params?: Record<string, string>) => Promise<T>, fetchAPIPaginated: <T>(endpoint: string, params?: Record<string, string>) => Promise<FetchResult<T>>): {
|
|
9
|
+
/**
|
|
10
|
+
* Gets pages with optional filtering (single page, max 100 items)
|
|
11
|
+
*
|
|
12
|
+
* @param filter - Filter options (status, parent, author, search, etc.)
|
|
13
|
+
* @returns Array of pages matching the filter criteria
|
|
14
|
+
*/
|
|
15
|
+
getPages(filter?: PagesFilter): Promise<WordPressPage[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Gets ALL pages by automatically paginating through all pages
|
|
18
|
+
* Use this for static site generation to ensure all content is fetched
|
|
19
|
+
*
|
|
20
|
+
* @param filter - Filter options (status, parent, author, search, etc.)
|
|
21
|
+
* @returns Array of all pages matching the filter criteria
|
|
22
|
+
*/
|
|
23
|
+
getAllPages(filter?: Omit<PagesFilter, "page">): Promise<WordPressPage[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets pages with pagination metadata
|
|
26
|
+
*
|
|
27
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
28
|
+
* @returns Paginated response with pages and total counts
|
|
29
|
+
*/
|
|
30
|
+
getPagesPaginated(filter?: PagesFilter): Promise<PaginatedResponse<WordPressPage>>;
|
|
31
|
+
/**
|
|
32
|
+
* Gets a single page by ID
|
|
33
|
+
*/
|
|
34
|
+
getPage(id: number): Promise<WordPressPage>;
|
|
35
|
+
/**
|
|
36
|
+
* Gets a single page by slug
|
|
37
|
+
*/
|
|
38
|
+
getPageBySlug(slug: string): Promise<WordPressPage | undefined>;
|
|
39
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { filterToParams } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Pages API methods factory
|
|
4
|
+
* Creates type-safe methods for fetching WordPress pages with filtering and pagination
|
|
5
|
+
*/
|
|
6
|
+
export function createPagesMethods(fetchAPI, fetchAPIPaginated) {
|
|
7
|
+
return {
|
|
8
|
+
/**
|
|
9
|
+
* Gets pages with optional filtering (single page, max 100 items)
|
|
10
|
+
*
|
|
11
|
+
* @param filter - Filter options (status, parent, author, search, etc.)
|
|
12
|
+
* @returns Array of pages matching the filter criteria
|
|
13
|
+
*/
|
|
14
|
+
async getPages(filter = {}) {
|
|
15
|
+
const params = filterToParams({ ...filter, _embed: 'true' });
|
|
16
|
+
return fetchAPI('/pages', params);
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* Gets ALL pages by automatically paginating through all pages
|
|
20
|
+
* Use this for static site generation to ensure all content is fetched
|
|
21
|
+
*
|
|
22
|
+
* @param filter - Filter options (status, parent, author, search, etc.)
|
|
23
|
+
* @returns Array of all pages matching the filter criteria
|
|
24
|
+
*/
|
|
25
|
+
async getAllPages(filter = {}) {
|
|
26
|
+
const allPages = [];
|
|
27
|
+
let page = 1;
|
|
28
|
+
let totalPages = 1;
|
|
29
|
+
do {
|
|
30
|
+
const params = filterToParams({ ...filter, page, perPage: 100, _embed: 'true' });
|
|
31
|
+
const result = await fetchAPIPaginated('/pages', params);
|
|
32
|
+
allPages.push(...result.data);
|
|
33
|
+
totalPages = result.totalPages;
|
|
34
|
+
page++;
|
|
35
|
+
} while (page <= totalPages);
|
|
36
|
+
return allPages;
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Gets pages with pagination metadata
|
|
40
|
+
*
|
|
41
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
42
|
+
* @returns Paginated response with pages and total counts
|
|
43
|
+
*/
|
|
44
|
+
async getPagesPaginated(filter = {}) {
|
|
45
|
+
const params = filterToParams({ ...filter, _embed: 'true' });
|
|
46
|
+
const result = await fetchAPIPaginated('/pages', params);
|
|
47
|
+
return {
|
|
48
|
+
data: result.data,
|
|
49
|
+
total: result.total,
|
|
50
|
+
totalPages: result.totalPages,
|
|
51
|
+
page: filter.page || 1,
|
|
52
|
+
perPage: filter.perPage || 100,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Gets a single page by ID
|
|
57
|
+
*/
|
|
58
|
+
async getPage(id) {
|
|
59
|
+
return fetchAPI(`/pages/${id}`, { _embed: 'true' });
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Gets a single page by slug
|
|
63
|
+
*/
|
|
64
|
+
async getPageBySlug(slug) {
|
|
65
|
+
const pages = await fetchAPI('/pages', { slug, _embed: 'true' });
|
|
66
|
+
return pages[0];
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { WordPressPost } from '../schemas';
|
|
2
|
+
import type { FetchResult } from './index';
|
|
3
|
+
import type { PostsFilter, PaginatedResponse } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Posts API methods factory
|
|
6
|
+
* Creates type-safe methods for fetching WordPress posts with filtering and pagination
|
|
7
|
+
*/
|
|
8
|
+
export declare function createPostsMethods(fetchAPI: <T>(endpoint: string, params?: Record<string, string>) => Promise<T>, fetchAPIPaginated: <T>(endpoint: string, params?: Record<string, string>) => Promise<FetchResult<T>>): {
|
|
9
|
+
/**
|
|
10
|
+
* Gets posts with optional filtering (single page, max 100 items)
|
|
11
|
+
*
|
|
12
|
+
* @param filter - Filter options (status, categories, tags, author, search, etc.)
|
|
13
|
+
* @returns Array of posts matching the filter criteria
|
|
14
|
+
*/
|
|
15
|
+
getPosts(filter?: PostsFilter): Promise<WordPressPost[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Gets ALL posts by automatically paginating through all pages
|
|
18
|
+
* Use this for static site generation to ensure all content is fetched
|
|
19
|
+
*
|
|
20
|
+
* @param filter - Filter options (status, categories, tags, author, search, etc.)
|
|
21
|
+
* @returns Array of all posts matching the filter criteria
|
|
22
|
+
*/
|
|
23
|
+
getAllPosts(filter?: Omit<PostsFilter, "page">): Promise<WordPressPost[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets posts with pagination metadata
|
|
26
|
+
*
|
|
27
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
28
|
+
* @returns Paginated response with posts and total counts
|
|
29
|
+
*/
|
|
30
|
+
getPostsPaginated(filter?: PostsFilter): Promise<PaginatedResponse<WordPressPost>>;
|
|
31
|
+
/**
|
|
32
|
+
* Gets a single post by ID
|
|
33
|
+
*/
|
|
34
|
+
getPost(id: number): Promise<WordPressPost>;
|
|
35
|
+
/**
|
|
36
|
+
* Gets a single post by slug
|
|
37
|
+
*/
|
|
38
|
+
getPostBySlug(slug: string): Promise<WordPressPost | undefined>;
|
|
39
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { filterToParams } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Posts API methods factory
|
|
4
|
+
* Creates type-safe methods for fetching WordPress posts with filtering and pagination
|
|
5
|
+
*/
|
|
6
|
+
export function createPostsMethods(fetchAPI, fetchAPIPaginated) {
|
|
7
|
+
return {
|
|
8
|
+
/**
|
|
9
|
+
* Gets posts with optional filtering (single page, max 100 items)
|
|
10
|
+
*
|
|
11
|
+
* @param filter - Filter options (status, categories, tags, author, search, etc.)
|
|
12
|
+
* @returns Array of posts matching the filter criteria
|
|
13
|
+
*/
|
|
14
|
+
async getPosts(filter = {}) {
|
|
15
|
+
const params = filterToParams({ ...filter, _embed: 'true' });
|
|
16
|
+
return fetchAPI('/posts', params);
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* Gets ALL posts by automatically paginating through all pages
|
|
20
|
+
* Use this for static site generation to ensure all content is fetched
|
|
21
|
+
*
|
|
22
|
+
* @param filter - Filter options (status, categories, tags, author, search, etc.)
|
|
23
|
+
* @returns Array of all posts matching the filter criteria
|
|
24
|
+
*/
|
|
25
|
+
async getAllPosts(filter = {}) {
|
|
26
|
+
const allPosts = [];
|
|
27
|
+
let page = 1;
|
|
28
|
+
let totalPages = 1;
|
|
29
|
+
do {
|
|
30
|
+
const params = filterToParams({ ...filter, page, perPage: 100, _embed: 'true' });
|
|
31
|
+
const result = await fetchAPIPaginated('/posts', params);
|
|
32
|
+
allPosts.push(...result.data);
|
|
33
|
+
totalPages = result.totalPages;
|
|
34
|
+
page++;
|
|
35
|
+
} while (page <= totalPages);
|
|
36
|
+
return allPosts;
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Gets posts with pagination metadata
|
|
40
|
+
*
|
|
41
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
42
|
+
* @returns Paginated response with posts and total counts
|
|
43
|
+
*/
|
|
44
|
+
async getPostsPaginated(filter = {}) {
|
|
45
|
+
const params = filterToParams({ ...filter, _embed: 'true' });
|
|
46
|
+
const result = await fetchAPIPaginated('/posts', params);
|
|
47
|
+
return {
|
|
48
|
+
data: result.data,
|
|
49
|
+
total: result.total,
|
|
50
|
+
totalPages: result.totalPages,
|
|
51
|
+
page: filter.page || 1,
|
|
52
|
+
perPage: filter.perPage || 100,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Gets a single post by ID
|
|
57
|
+
*/
|
|
58
|
+
async getPost(id) {
|
|
59
|
+
return fetchAPI(`/posts/${id}`, { _embed: 'true' });
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Gets a single post by slug
|
|
63
|
+
*/
|
|
64
|
+
async getPostBySlug(slug) {
|
|
65
|
+
const posts = await fetchAPI('/posts', { slug, _embed: 'true' });
|
|
66
|
+
return posts[0];
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { WordPressSettings } from '../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* Settings API methods
|
|
4
|
+
*/
|
|
5
|
+
export declare function createSettingsMethods(fetchAPI: <T>(endpoint: string, params?: Record<string, string>) => Promise<T>, hasAuth: () => boolean): {
|
|
6
|
+
/**
|
|
7
|
+
* Gets WordPress site settings (requires authentication)
|
|
8
|
+
*
|
|
9
|
+
* @returns Site settings including title, description, URL, etc.
|
|
10
|
+
* @throws Error if authentication is not configured
|
|
11
|
+
*/
|
|
12
|
+
getSettings(): Promise<WordPressSettings>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settings API methods
|
|
3
|
+
*/
|
|
4
|
+
export function createSettingsMethods(fetchAPI, hasAuth) {
|
|
5
|
+
return {
|
|
6
|
+
/**
|
|
7
|
+
* Gets WordPress site settings (requires authentication)
|
|
8
|
+
*
|
|
9
|
+
* @returns Site settings including title, description, URL, etc.
|
|
10
|
+
* @throws Error if authentication is not configured
|
|
11
|
+
*/
|
|
12
|
+
async getSettings() {
|
|
13
|
+
if (!hasAuth()) {
|
|
14
|
+
throw new Error('Authentication required for /settings endpoint. Configure auth in client options.');
|
|
15
|
+
}
|
|
16
|
+
return fetchAPI('/settings');
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { WordPressTag } from '../schemas';
|
|
2
|
+
import type { FetchResult } from './index';
|
|
3
|
+
import type { TagsFilter, PaginatedResponse } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Tags API methods factory
|
|
6
|
+
* Creates type-safe methods for fetching WordPress tags with filtering and pagination
|
|
7
|
+
*/
|
|
8
|
+
export declare function createTagsMethods(fetchAPI: <T>(endpoint: string, params?: Record<string, string>) => Promise<T>, fetchAPIPaginated: <T>(endpoint: string, params?: Record<string, string>) => Promise<FetchResult<T>>): {
|
|
9
|
+
/**
|
|
10
|
+
* Gets tags with optional filtering (single page, max 100 items)
|
|
11
|
+
*
|
|
12
|
+
* @param filter - Filter options (hideEmpty, include, exclude, search, etc.)
|
|
13
|
+
* @returns Array of tags matching the filter criteria
|
|
14
|
+
*/
|
|
15
|
+
getTags(filter?: TagsFilter): Promise<WordPressTag[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Gets ALL tags by automatically paginating through all pages
|
|
18
|
+
* Use this for static site generation to ensure all content is fetched
|
|
19
|
+
*
|
|
20
|
+
* @param filter - Filter options (hideEmpty, include, exclude, search, etc.)
|
|
21
|
+
* @returns Array of all tags matching the filter criteria
|
|
22
|
+
*/
|
|
23
|
+
getAllTags(filter?: Omit<TagsFilter, "page">): Promise<WordPressTag[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets tags with pagination metadata
|
|
26
|
+
*
|
|
27
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
28
|
+
* @returns Paginated response with tags and total counts
|
|
29
|
+
*/
|
|
30
|
+
getTagsPaginated(filter?: TagsFilter): Promise<PaginatedResponse<WordPressTag>>;
|
|
31
|
+
/**
|
|
32
|
+
* Gets a single tag by ID
|
|
33
|
+
*/
|
|
34
|
+
getTag(id: number): Promise<WordPressTag>;
|
|
35
|
+
/**
|
|
36
|
+
* Gets a single tag by slug
|
|
37
|
+
*/
|
|
38
|
+
getTagBySlug(slug: string): Promise<WordPressTag | undefined>;
|
|
39
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { filterToParams } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Tags API methods factory
|
|
4
|
+
* Creates type-safe methods for fetching WordPress tags with filtering and pagination
|
|
5
|
+
*/
|
|
6
|
+
export function createTagsMethods(fetchAPI, fetchAPIPaginated) {
|
|
7
|
+
return {
|
|
8
|
+
/**
|
|
9
|
+
* Gets tags with optional filtering (single page, max 100 items)
|
|
10
|
+
*
|
|
11
|
+
* @param filter - Filter options (hideEmpty, include, exclude, search, etc.)
|
|
12
|
+
* @returns Array of tags matching the filter criteria
|
|
13
|
+
*/
|
|
14
|
+
async getTags(filter = {}) {
|
|
15
|
+
const params = filterToParams(filter);
|
|
16
|
+
return fetchAPI('/tags', params);
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* Gets ALL tags by automatically paginating through all pages
|
|
20
|
+
* Use this for static site generation to ensure all content is fetched
|
|
21
|
+
*
|
|
22
|
+
* @param filter - Filter options (hideEmpty, include, exclude, search, etc.)
|
|
23
|
+
* @returns Array of all tags matching the filter criteria
|
|
24
|
+
*/
|
|
25
|
+
async getAllTags(filter = {}) {
|
|
26
|
+
const allTags = [];
|
|
27
|
+
let page = 1;
|
|
28
|
+
let totalPages = 1;
|
|
29
|
+
do {
|
|
30
|
+
const params = filterToParams({ ...filter, page, perPage: 100 });
|
|
31
|
+
const result = await fetchAPIPaginated('/tags', params);
|
|
32
|
+
allTags.push(...result.data);
|
|
33
|
+
totalPages = result.totalPages;
|
|
34
|
+
page++;
|
|
35
|
+
} while (page <= totalPages);
|
|
36
|
+
return allTags;
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Gets tags with pagination metadata
|
|
40
|
+
*
|
|
41
|
+
* @param filter - Filter options including pagination (perPage, page)
|
|
42
|
+
* @returns Paginated response with tags and total counts
|
|
43
|
+
*/
|
|
44
|
+
async getTagsPaginated(filter = {}) {
|
|
45
|
+
const params = filterToParams(filter);
|
|
46
|
+
const result = await fetchAPIPaginated('/tags', params);
|
|
47
|
+
return {
|
|
48
|
+
data: result.data,
|
|
49
|
+
total: result.total,
|
|
50
|
+
totalPages: result.totalPages,
|
|
51
|
+
page: filter.page || 1,
|
|
52
|
+
perPage: filter.perPage || 100,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Gets a single tag by ID
|
|
57
|
+
*/
|
|
58
|
+
async getTag(id) {
|
|
59
|
+
return fetchAPI(`/tags/${id}`);
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Gets a single tag by slug
|
|
63
|
+
*/
|
|
64
|
+
async getTagBySlug(slug) {
|
|
65
|
+
const tags = await fetchAPI('/tags', { slug });
|
|
66
|
+
return tags[0];
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|