wpheadless-lib 1.0.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/dist/api/categories.d.ts +29 -0
- package/dist/api/categories.js +91 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.js +6 -0
- package/dist/api/mappers.d.ts +5 -0
- package/dist/api/mappers.js +30 -0
- package/dist/api/posts.d.ts +44 -0
- package/dist/api/posts.js +104 -0
- package/dist/api/translations.d.ts +16 -0
- package/dist/api/translations.js +28 -0
- package/dist/api/types.d.ts +30 -0
- package/dist/api/types.js +1 -0
- package/dist/api/wpClient.d.ts +31 -0
- package/dist/api/wpClient.js +23 -0
- package/dist/app/config.d.ts +5 -0
- package/dist/app/config.js +28 -0
- package/dist/app/globals.css +326 -0
- package/dist/base/category/index.d.ts +38 -0
- package/dist/base/category/index.js +104 -0
- package/dist/base/category/pagination.d.ts +44 -0
- package/dist/base/category/pagination.js +114 -0
- package/dist/base/home/index.d.ts +13 -0
- package/dist/base/home/index.js +20 -0
- package/dist/base/home/pagination.d.ts +36 -0
- package/dist/base/home/pagination.js +83 -0
- package/dist/base/index.d.ts +7 -0
- package/dist/base/index.js +7 -0
- package/dist/base/layout/RootLayoutBase.d.ts +22 -0
- package/dist/base/layout/RootLayoutBase.js +15 -0
- package/dist/base/legal/index.d.ts +31 -0
- package/dist/base/legal/index.js +167 -0
- package/dist/base/post/index.d.ts +31 -0
- package/dist/base/post/index.js +97 -0
- package/dist/components/Breadcrumbs/index.d.ts +9 -0
- package/dist/components/Breadcrumbs/index.js +11 -0
- package/dist/components/Breadcrumbs/index.module.css +39 -0
- package/dist/components/Paginator/index.d.ts +10 -0
- package/dist/components/Paginator/index.js +16 -0
- package/dist/components/Paginator/index.module.css +13 -0
- package/dist/components/Paginator/index.utils.d.ts +7 -0
- package/dist/components/Paginator/index.utils.js +19 -0
- package/dist/components/PostCard/index.d.ts +10 -0
- package/dist/components/PostCard/index.js +22 -0
- package/dist/components/PostCard/index.module.css +56 -0
- package/dist/components/PostCard/index.types.d.ts +7 -0
- package/dist/components/PostCard/index.types.js +1 -0
- package/dist/components/PostCard/index.utils.d.ts +8 -0
- package/dist/components/PostCard/index.utils.js +23 -0
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.js +5 -0
- package/dist/components/layout/Footer/index.d.ts +12 -0
- package/dist/components/layout/Footer/index.js +36 -0
- package/dist/components/layout/Footer/index.module.css +61 -0
- package/dist/components/layout/Header/index.d.ts +13 -0
- package/dist/components/layout/Header/index.js +36 -0
- package/dist/components/layout/Header/index.module.css +48 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/index.js +1 -0
- package/dist/plugins/yoast/index.d.ts +16 -0
- package/dist/plugins/yoast/index.js +44 -0
- package/dist/utils/hreflang.d.ts +37 -0
- package/dist/utils/hreflang.js +95 -0
- package/dist/utils/html.d.ts +4 -0
- package/dist/utils/html.js +8 -0
- package/dist/utils/i18n.d.ts +12 -0
- package/dist/utils/i18n.js +161 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/routing.d.ts +40 -0
- package/dist/utils/routing.js +84 -0
- package/dist/utils/seo.d.ts +75 -0
- package/dist/utils/seo.js +97 -0
- package/dist/utils/sitemap.d.ts +26 -0
- package/dist/utils/sitemap.js +327 -0
- package/dist/views/CategoryListView/index.d.ts +12 -0
- package/dist/views/CategoryListView/index.js +17 -0
- package/dist/views/CategoryListView/index.module.css +39 -0
- package/dist/views/CategoryListView/index.utils.d.ts +5 -0
- package/dist/views/CategoryListView/index.utils.js +14 -0
- package/dist/views/CategoryPaginationView/index.d.ts +13 -0
- package/dist/views/CategoryPaginationView/index.js +18 -0
- package/dist/views/CategoryPaginationView/index.module.css +39 -0
- package/dist/views/CategoryPaginationView/index.utils.d.ts +5 -0
- package/dist/views/CategoryPaginationView/index.utils.js +4 -0
- package/dist/views/HomePaginationView/index.d.ts +13 -0
- package/dist/views/HomePaginationView/index.js +9 -0
- package/dist/views/HomePaginationView/index.module.css +38 -0
- package/dist/views/HomePaginationView/index.utils.d.ts +7 -0
- package/dist/views/HomePaginationView/index.utils.js +10 -0
- package/dist/views/HomeView/index.d.ts +13 -0
- package/dist/views/HomeView/index.js +21 -0
- package/dist/views/HomeView/index.module.css +78 -0
- package/dist/views/HomeView/index.utils.d.ts +23 -0
- package/dist/views/HomeView/index.utils.js +44 -0
- package/dist/views/LegalPageView/index.d.ts +8 -0
- package/dist/views/LegalPageView/index.js +12 -0
- package/dist/views/LegalPageView/index.module.css +39 -0
- package/dist/views/LegalPageView/index.utils.d.ts +11 -0
- package/dist/views/LegalPageView/index.utils.js +16 -0
- package/dist/views/PostView/index.d.ts +10 -0
- package/dist/views/PostView/index.js +26 -0
- package/dist/views/PostView/index.module.css +80 -0
- package/dist/views/PostView/index.types.d.ts +2 -0
- package/dist/views/PostView/index.types.js +1 -0
- package/dist/views/PostView/index.utils.d.ts +15 -0
- package/dist/views/PostView/index.utils.js +22 -0
- package/dist/views/index.d.ts +6 -0
- package/dist/views/index.js +6 -0
- package/package.json +45 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { WPCategory, WPPost } from "wpjsapi-lib";
|
|
2
|
+
import type { ApiResult } from "./posts";
|
|
3
|
+
export type CategoryClientConfig = {
|
|
4
|
+
baseApiUrl: string;
|
|
5
|
+
username?: string;
|
|
6
|
+
appPassword?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function listCategoryStaticParams({ baseApiUrl, lang, }: CategoryClientConfig & {
|
|
9
|
+
lang: string;
|
|
10
|
+
}): Promise<ApiResult<Array<{
|
|
11
|
+
category: string;
|
|
12
|
+
}>>>;
|
|
13
|
+
export declare function getCategoryBySlug({ baseApiUrl, slug, lang, }: CategoryClientConfig & {
|
|
14
|
+
slug: string;
|
|
15
|
+
lang: string;
|
|
16
|
+
}): Promise<ApiResult<WPCategory | null>>;
|
|
17
|
+
export declare function listCategoryPosts({ baseApiUrl, categoryId, perPage, langId, }: CategoryClientConfig & {
|
|
18
|
+
categoryId: number;
|
|
19
|
+
perPage: number;
|
|
20
|
+
langId: string;
|
|
21
|
+
}): Promise<ApiResult<{
|
|
22
|
+
posts: WPPost[];
|
|
23
|
+
totalPages: number;
|
|
24
|
+
}>>;
|
|
25
|
+
export declare function getCategoryByTranslationKey({ baseApiUrl, translationKey, lang, perPage, }: CategoryClientConfig & {
|
|
26
|
+
translationKey: string;
|
|
27
|
+
lang: string;
|
|
28
|
+
perPage?: number;
|
|
29
|
+
}): Promise<ApiResult<WPCategory | null>>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createWpClient } from "./wpClient";
|
|
2
|
+
import { fetchCategoriesByTranslationKey } from "./translations";
|
|
3
|
+
export async function listCategoryStaticParams({ baseApiUrl, lang, }) {
|
|
4
|
+
try {
|
|
5
|
+
const { categoriesApi } = createWpClient({
|
|
6
|
+
baseUrl: baseApiUrl,
|
|
7
|
+
});
|
|
8
|
+
const categories = await categoriesApi.listAll({ lang });
|
|
9
|
+
return {
|
|
10
|
+
ok: true,
|
|
11
|
+
data: categories.map((category) => ({ category: category.slug })),
|
|
12
|
+
error: null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return {
|
|
17
|
+
ok: false,
|
|
18
|
+
data: [],
|
|
19
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function getCategoryBySlug({ baseApiUrl, slug, lang, }) {
|
|
24
|
+
try {
|
|
25
|
+
const { categoriesApi } = createWpClient({
|
|
26
|
+
baseUrl: baseApiUrl,
|
|
27
|
+
});
|
|
28
|
+
const response = await categoriesApi.list({
|
|
29
|
+
slug,
|
|
30
|
+
per_page: 1,
|
|
31
|
+
lang,
|
|
32
|
+
_fields: ["id", "slug", "name", "description", "yoast_head_json", "meta"],
|
|
33
|
+
});
|
|
34
|
+
return { ok: true, data: response.items[0] ?? null, error: null };
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
return {
|
|
38
|
+
ok: false,
|
|
39
|
+
data: null,
|
|
40
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export async function listCategoryPosts({ baseApiUrl, categoryId, perPage, langId, }) {
|
|
45
|
+
try {
|
|
46
|
+
const { postsApi } = createWpClient({
|
|
47
|
+
baseUrl: baseApiUrl,
|
|
48
|
+
});
|
|
49
|
+
const response = await postsApi.list({
|
|
50
|
+
categories: [categoryId],
|
|
51
|
+
per_page: perPage,
|
|
52
|
+
orderby: "date",
|
|
53
|
+
order: "desc",
|
|
54
|
+
_embed: true,
|
|
55
|
+
taxonomies: { language: [langId] },
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
ok: true,
|
|
59
|
+
data: {
|
|
60
|
+
posts: response.items,
|
|
61
|
+
totalPages: response.pagination.totalPages,
|
|
62
|
+
},
|
|
63
|
+
error: null,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
return {
|
|
68
|
+
ok: false,
|
|
69
|
+
data: { posts: [], totalPages: 0 },
|
|
70
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export async function getCategoryByTranslationKey({ baseApiUrl, translationKey, lang, perPage = 1, }) {
|
|
75
|
+
try {
|
|
76
|
+
const categories = await fetchCategoriesByTranslationKey({
|
|
77
|
+
baseApiUrl,
|
|
78
|
+
translationKey,
|
|
79
|
+
lang,
|
|
80
|
+
perPage,
|
|
81
|
+
});
|
|
82
|
+
return { ok: true, data: categories[0] ?? null, error: null };
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
data: null,
|
|
88
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { WPCategory, WPPost, WPPage } from "wpjsapi-lib";
|
|
2
|
+
import { type CategoryEntity, type PostEntity, type PageEntity } from "./types";
|
|
3
|
+
export declare function mapPostEntity(post: WPPost): PostEntity;
|
|
4
|
+
export declare function mapCategoryEntity(category: WPCategory): CategoryEntity;
|
|
5
|
+
export declare function mapPageEntity(page: WPPage): PageEntity<WPPage>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getEntityLanguageId, getTranslationKey } from "../utils/hreflang";
|
|
2
|
+
const getEmbeddedCategories = (post) => (post._embedded?.["wp:term"]?.[0] || []).filter(Boolean);
|
|
3
|
+
export function mapPostEntity(post) {
|
|
4
|
+
return {
|
|
5
|
+
post,
|
|
6
|
+
meta: {
|
|
7
|
+
translationKey: getTranslationKey(post),
|
|
8
|
+
languageId: getEntityLanguageId(post),
|
|
9
|
+
categories: getEmbeddedCategories(post),
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function mapCategoryEntity(category) {
|
|
14
|
+
return {
|
|
15
|
+
category,
|
|
16
|
+
meta: {
|
|
17
|
+
translationKey: getTranslationKey(category),
|
|
18
|
+
languageId: getEntityLanguageId(category),
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function mapPageEntity(page) {
|
|
23
|
+
return {
|
|
24
|
+
page,
|
|
25
|
+
meta: {
|
|
26
|
+
translationKey: getTranslationKey(page),
|
|
27
|
+
languageId: getEntityLanguageId(page),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { WPPost, WPPaginatedResponse, WPPostParameters } from "wpjsapi-lib";
|
|
2
|
+
export type ApiResult<T> = {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
data: T | null;
|
|
5
|
+
error: string | null;
|
|
6
|
+
};
|
|
7
|
+
export type ClientConfig = {
|
|
8
|
+
baseApiUrl: string;
|
|
9
|
+
username?: string;
|
|
10
|
+
appPassword?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function getPostBySlug({ baseApiUrl, slug, langId, username, appPassword, }: ClientConfig & {
|
|
13
|
+
slug: string;
|
|
14
|
+
langId: string;
|
|
15
|
+
}): Promise<ApiResult<WPPost | null>>;
|
|
16
|
+
export declare function listPostStaticParams({ baseApiUrl, lang, langId, }: ClientConfig & {
|
|
17
|
+
lang: string;
|
|
18
|
+
langId: string;
|
|
19
|
+
}): Promise<ApiResult<Array<{
|
|
20
|
+
category: string;
|
|
21
|
+
post: string;
|
|
22
|
+
}>>>;
|
|
23
|
+
export declare function listPostsByTranslationKey({ baseApiUrl, translationKey, username, appPassword, perPage, }: ClientConfig & {
|
|
24
|
+
translationKey: string;
|
|
25
|
+
perPage?: number;
|
|
26
|
+
}): Promise<ApiResult<WPPost[]>>;
|
|
27
|
+
type ListPostsParams = ClientConfig & {
|
|
28
|
+
perPage: number;
|
|
29
|
+
page?: number;
|
|
30
|
+
langId: string;
|
|
31
|
+
categories?: number[];
|
|
32
|
+
embed?: boolean;
|
|
33
|
+
fields?: string[];
|
|
34
|
+
orderby?: WPPostParameters["orderby"];
|
|
35
|
+
order?: WPPostParameters["order"];
|
|
36
|
+
};
|
|
37
|
+
export declare function listPosts({ baseApiUrl, perPage, page, langId, categories, embed, fields, order, orderby, }: ListPostsParams): Promise<ApiResult<WPPaginatedResponse<WPPost>>>;
|
|
38
|
+
export declare function listPostsWithEmbeds(params: Omit<ListPostsParams, "embed">): Promise<ApiResult<WPPaginatedResponse<WPPost>>>;
|
|
39
|
+
export declare function getPostsTotalPages({ baseApiUrl, perPage, langId, categories, }: ClientConfig & {
|
|
40
|
+
perPage: number;
|
|
41
|
+
langId: string;
|
|
42
|
+
categories?: number[];
|
|
43
|
+
}): Promise<ApiResult<number>>;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createWpClient } from "./wpClient";
|
|
2
|
+
import { fetchPostsByTranslationKey } from "./translations";
|
|
3
|
+
const toMessage = (err) => err instanceof Error ? err.message : "Unknown error";
|
|
4
|
+
export async function getPostBySlug({ baseApiUrl, slug, langId, username, appPassword, }) {
|
|
5
|
+
try {
|
|
6
|
+
const { postsApi } = createWpClient({
|
|
7
|
+
baseUrl: baseApiUrl,
|
|
8
|
+
username,
|
|
9
|
+
appPassword,
|
|
10
|
+
});
|
|
11
|
+
const response = await postsApi.list({
|
|
12
|
+
slug,
|
|
13
|
+
per_page: 1,
|
|
14
|
+
_embed: true,
|
|
15
|
+
taxonomies: { language: [langId] },
|
|
16
|
+
});
|
|
17
|
+
return { ok: true, data: response.items[0] ?? null, error: null };
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
return { ok: false, data: null, error: toMessage(error) };
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function listPostStaticParams({ baseApiUrl, lang, langId, }) {
|
|
24
|
+
try {
|
|
25
|
+
const { postsApi, categoriesApi } = createWpClient({ baseUrl: baseApiUrl });
|
|
26
|
+
const categories = await categoriesApi.listAll({ lang });
|
|
27
|
+
const response = await postsApi.list({
|
|
28
|
+
per_page: 100,
|
|
29
|
+
_fields: ["slug", "categories"],
|
|
30
|
+
taxonomies: { language: [langId] },
|
|
31
|
+
});
|
|
32
|
+
const paths = [];
|
|
33
|
+
for (const post of response.items) {
|
|
34
|
+
for (const categoryId of post.categories) {
|
|
35
|
+
const category = categories.find((cat) => cat.id === categoryId);
|
|
36
|
+
if (category) {
|
|
37
|
+
paths.push({
|
|
38
|
+
category: category.slug,
|
|
39
|
+
post: post.slug,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { ok: true, data: paths, error: null };
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
return { ok: false, data: [], error: toMessage(error) };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export async function listPostsByTranslationKey({ baseApiUrl, translationKey, username, appPassword, perPage = 20, }) {
|
|
51
|
+
try {
|
|
52
|
+
const posts = await fetchPostsByTranslationKey({
|
|
53
|
+
baseApiUrl,
|
|
54
|
+
translationKey,
|
|
55
|
+
perPage,
|
|
56
|
+
username,
|
|
57
|
+
appPassword,
|
|
58
|
+
});
|
|
59
|
+
return { ok: true, data: posts, error: null };
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return { ok: false, data: [], error: toMessage(error) };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export async function listPosts({ baseApiUrl, perPage, page, langId, categories, embed = false, fields, order, orderby, }) {
|
|
66
|
+
try {
|
|
67
|
+
const { postsApi } = createWpClient({
|
|
68
|
+
baseUrl: baseApiUrl,
|
|
69
|
+
});
|
|
70
|
+
const response = await postsApi.list({
|
|
71
|
+
per_page: perPage,
|
|
72
|
+
page,
|
|
73
|
+
...(orderby ? { orderby } : {}),
|
|
74
|
+
...(order ? { order } : {}),
|
|
75
|
+
...(categories ? { categories } : {}),
|
|
76
|
+
...(embed ? { _embed: true } : {}),
|
|
77
|
+
...(fields ? { _fields: fields } : {}),
|
|
78
|
+
taxonomies: { language: [langId] },
|
|
79
|
+
});
|
|
80
|
+
return { ok: true, data: response, error: null };
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return { ok: false, data: null, error: toMessage(error) };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export async function listPostsWithEmbeds(params) {
|
|
87
|
+
return listPosts({ ...params, embed: true, fields: undefined });
|
|
88
|
+
}
|
|
89
|
+
export async function getPostsTotalPages({ baseApiUrl, perPage, langId, categories, }) {
|
|
90
|
+
const response = await listPosts({
|
|
91
|
+
baseApiUrl,
|
|
92
|
+
perPage: 1,
|
|
93
|
+
page: 1,
|
|
94
|
+
langId,
|
|
95
|
+
categories,
|
|
96
|
+
fields: ["id"],
|
|
97
|
+
embed: false,
|
|
98
|
+
});
|
|
99
|
+
if (!response.ok || !response.data) {
|
|
100
|
+
return { ok: false, data: 0, error: response.error };
|
|
101
|
+
}
|
|
102
|
+
const totalPosts = response.data.pagination.total;
|
|
103
|
+
return { ok: true, data: Math.ceil(totalPosts / perPage), error: null };
|
|
104
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WPPost, WPCategory } from "wpjsapi-lib";
|
|
2
|
+
type ClientConfig = {
|
|
3
|
+
baseApiUrl: string;
|
|
4
|
+
username?: string;
|
|
5
|
+
appPassword?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function fetchPostsByTranslationKey({ baseApiUrl, translationKey, username, appPassword, perPage, }: ClientConfig & {
|
|
8
|
+
translationKey: string;
|
|
9
|
+
perPage?: number;
|
|
10
|
+
}): Promise<WPPost[]>;
|
|
11
|
+
export declare function fetchCategoriesByTranslationKey({ baseApiUrl, translationKey, lang, username, appPassword, perPage, }: ClientConfig & {
|
|
12
|
+
translationKey: string;
|
|
13
|
+
lang?: string;
|
|
14
|
+
perPage?: number;
|
|
15
|
+
}): Promise<WPCategory[]>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createWpClient } from "./wpClient";
|
|
2
|
+
export async function fetchPostsByTranslationKey({ baseApiUrl, translationKey, username, appPassword, perPage = 20, }) {
|
|
3
|
+
const { postsApi } = createWpClient({
|
|
4
|
+
baseUrl: baseApiUrl,
|
|
5
|
+
username,
|
|
6
|
+
appPassword,
|
|
7
|
+
});
|
|
8
|
+
const response = await postsApi.list({
|
|
9
|
+
per_page: perPage,
|
|
10
|
+
_embed: true,
|
|
11
|
+
translation_key_v2: translationKey,
|
|
12
|
+
});
|
|
13
|
+
return response.items;
|
|
14
|
+
}
|
|
15
|
+
export async function fetchCategoriesByTranslationKey({ baseApiUrl, translationKey, lang, username, appPassword, perPage = 20, }) {
|
|
16
|
+
const { categoriesApi } = createWpClient({
|
|
17
|
+
baseUrl: baseApiUrl,
|
|
18
|
+
username,
|
|
19
|
+
appPassword,
|
|
20
|
+
});
|
|
21
|
+
const response = await categoriesApi.list({
|
|
22
|
+
translation_key_v2: translationKey,
|
|
23
|
+
per_page: perPage,
|
|
24
|
+
lang,
|
|
25
|
+
_fields: ["id", "slug", "name", "description", "yoast_head_json", "meta"],
|
|
26
|
+
});
|
|
27
|
+
return response.items;
|
|
28
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WPPost, WPCategory } from "wpjsapi-lib";
|
|
2
|
+
export type EmbeddedCategory = NonNullable<NonNullable<WPPost["_embedded"]>["wp:term"]>[number][number];
|
|
3
|
+
export type EmbeddedLanguageMeta = {
|
|
4
|
+
translationKey?: string;
|
|
5
|
+
languageId?: LanguageId;
|
|
6
|
+
};
|
|
7
|
+
export type LanguageId = string;
|
|
8
|
+
export type PostTranslationMeta = {
|
|
9
|
+
translationKey?: string;
|
|
10
|
+
languageId?: LanguageId;
|
|
11
|
+
categories: EmbeddedCategory[];
|
|
12
|
+
};
|
|
13
|
+
export type CategoryTranslationMeta = {
|
|
14
|
+
translationKey?: string;
|
|
15
|
+
languageId?: LanguageId;
|
|
16
|
+
};
|
|
17
|
+
export type PostEntity = {
|
|
18
|
+
post: WPPost;
|
|
19
|
+
meta: PostTranslationMeta;
|
|
20
|
+
};
|
|
21
|
+
export type CategoryEntity = {
|
|
22
|
+
category: WPCategory;
|
|
23
|
+
meta: CategoryTranslationMeta;
|
|
24
|
+
};
|
|
25
|
+
export type PageEntity<T extends {
|
|
26
|
+
meta?: unknown;
|
|
27
|
+
}> = {
|
|
28
|
+
page: T;
|
|
29
|
+
meta: EmbeddedLanguageMeta;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type WpClientOptions = {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
username?: string;
|
|
4
|
+
appPassword?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function createWpClient({ baseUrl, username, appPassword, }: WpClientOptions): {
|
|
7
|
+
postsApi: {
|
|
8
|
+
list: (params?: import("wpjsapi-lib").WPPostParameters, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPaginatedResponse<import("wpjsapi-lib").WPPost>>;
|
|
9
|
+
listAll: (params?: Omit<import("wpjsapi-lib").WPPostParameters, "page" | "per_page">, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost[]>;
|
|
10
|
+
pages: (params?: import("wpjsapi-lib").WPPostParameters, options?: import("wpjsapi-lib").RequestOptions) => {
|
|
11
|
+
[Symbol.asyncIterator](): AsyncGenerator<import("wpjsapi-lib").WPPaginatedResponse<import("wpjsapi-lib").WPPost>, void, unknown>;
|
|
12
|
+
};
|
|
13
|
+
get: (id: number, context?: "view" | "embed" | "edit", embed?: boolean, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost>;
|
|
14
|
+
create: (data: import("wpjsapi-lib").WPPostCreate, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost>;
|
|
15
|
+
update: (id: number, data: import("wpjsapi-lib").WPPostUpdate, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost>;
|
|
16
|
+
delete: (id: number, force?: boolean, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost>;
|
|
17
|
+
getRevisions: (id: number, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPost[]>;
|
|
18
|
+
};
|
|
19
|
+
categoriesApi: {
|
|
20
|
+
list: (params?: import("wpjsapi-lib").WPCategoryParameters, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPPaginatedResponse<import("wpjsapi-lib").WPCategory>>;
|
|
21
|
+
listAll: (params?: Omit<import("wpjsapi-lib").WPCategoryParameters, "page" | "per_page">, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPCategory[]>;
|
|
22
|
+
pages: (params?: import("wpjsapi-lib").WPCategoryParameters, options?: import("wpjsapi-lib").RequestOptions) => {
|
|
23
|
+
[Symbol.asyncIterator](): AsyncGenerator<import("wpjsapi-lib").WPPaginatedResponse<import("wpjsapi-lib").WPCategory>, void, unknown>;
|
|
24
|
+
};
|
|
25
|
+
get: (id: number, context?: "view" | "embed" | "edit", embed?: boolean, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPCategory>;
|
|
26
|
+
create: (data: import("wpjsapi-lib").WPCategoryCreate, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPCategory>;
|
|
27
|
+
update: (id: number, data: import("wpjsapi-lib").WPCategoryUpdate, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPCategory>;
|
|
28
|
+
delete: (id: number, force?: boolean, options?: import("wpjsapi-lib").RequestOptions) => Promise<import("wpjsapi-lib").WPCategory>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createAuth, createCategoryEndpoints, createPostsEndpoints, } from "wpjsapi-lib";
|
|
2
|
+
export function createWpClient({ baseUrl, username, appPassword, }) {
|
|
3
|
+
const hasAuth = username && appPassword;
|
|
4
|
+
const auth = hasAuth
|
|
5
|
+
? createAuth({
|
|
6
|
+
method: "basic",
|
|
7
|
+
credentials: {
|
|
8
|
+
username: username,
|
|
9
|
+
password: appPassword,
|
|
10
|
+
},
|
|
11
|
+
})
|
|
12
|
+
: undefined;
|
|
13
|
+
const sharedOptions = {
|
|
14
|
+
baseUrl,
|
|
15
|
+
...(auth ? { auth } : {}),
|
|
16
|
+
};
|
|
17
|
+
const postsApi = createPostsEndpoints(sharedOptions);
|
|
18
|
+
const categoriesApi = createCategoryEndpoints(sharedOptions);
|
|
19
|
+
return {
|
|
20
|
+
postsApi,
|
|
21
|
+
categoriesApi,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const parseLanguagesFromEnv = () => {
|
|
2
|
+
const raw = process.env.WP_LANGUAGES;
|
|
3
|
+
if (!raw)
|
|
4
|
+
return [];
|
|
5
|
+
try {
|
|
6
|
+
const parsed = JSON.parse(raw);
|
|
7
|
+
if (!Array.isArray(parsed))
|
|
8
|
+
return [];
|
|
9
|
+
return parsed
|
|
10
|
+
.map((item) => {
|
|
11
|
+
if (!item || typeof item !== "object")
|
|
12
|
+
return null;
|
|
13
|
+
const { code, basePath, langId } = item;
|
|
14
|
+
if (typeof code !== "string" || typeof basePath !== "string")
|
|
15
|
+
return null;
|
|
16
|
+
return { code, basePath, langId: typeof langId === "string" ? langId : undefined };
|
|
17
|
+
})
|
|
18
|
+
.filter(Boolean);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.warn("Unable to parse WP_LANGUAGES env var", error);
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export const languages = parseLanguagesFromEnv();
|
|
26
|
+
export const runtimeConfig = {
|
|
27
|
+
postsPerPagePagination: Number.parseInt(process.env.WP_POSTS_PER_PAGE_PAGINATION ?? "", 10) || 10,
|
|
28
|
+
};
|