Skip to content

Commit 2eeac36

Browse files
committed
feat: add getUrl function
1 parent 3fb200c commit 2eeac36

File tree

1 file changed

+56
-38
lines changed

1 file changed

+56
-38
lines changed

lib/wordpress.ts

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Used to fetch data from a WordPress site using the WordPress REST API
33
// Types are imported from `wp.d.ts`
44

5+
import querystring from 'query-string'
6+
57
import {
68
Post,
79
Category,
@@ -13,133 +15,149 @@ import {
1315

1416
// WordPress Config
1517

16-
const url = process.env.WORDPRESS_URL;
18+
const baseUrl = process.env.WORDPRESS_URL;
19+
20+
function getUrl(path: string, query?: Record<string, any>) {
21+
const params = query ? querystring.stringify(query) : null
22+
23+
return `${baseUrl}${path}${params ? `?${params}` : ""}`
24+
}
1725

1826
// WordPress Functions
1927

2028
export async function getAllPosts(filterParams?: {
2129
author?: string;
2230
tag?: string;
2331
category?: string;
24-
}): Promise<Post[]> {
25-
let filterUrl = "";
26-
if (filterParams) {
27-
const { author, tag, category } = filterParams;
28-
filterUrl = `${author ? `&author=${author}` : ""}${
29-
tag ? `&tags=${tag}` : ""
30-
}${category ? `&categories=${category}` : ""}`;
31-
}
32-
const response = await fetch(`${url}/wp-json/wp/v2/posts?${filterUrl}`);
32+
}): Promise<Post[]> {
33+
const url = getUrl("/wp-json/wp/v2/posts", { author: filterParams?.author, tags: filterParams?.tag, categories: filterParams?.category });
34+
const response = await fetch(url);
3335
const posts: Post[] = await response.json();
3436
return posts;
3537
}
3638

3739
export async function getPostById(id: number): Promise<Post> {
38-
const response = await fetch(`${url}/wp-json/wp/v2/posts/${id}`);
40+
const url = getUrl(`/wp-json/wp/v2/posts/${id}`);
41+
const response = await fetch(url);
3942
const post: Post = await response.json();
4043
return post;
4144
}
4245

4346
export async function getPostBySlug(slug: string): Promise<Post> {
44-
const response = await fetch(`${url}/wp-json/wp/v2/posts?slug=${slug}`);
47+
const url = getUrl("/wp-json/wp/v2/posts", { slug });
48+
const response = await fetch(url);
4549
const post: Post[] = await response.json();
4650
return post[0];
4751
}
4852

4953
export async function getAllCategories(): Promise<Category[]> {
50-
const response = await fetch(`${url}/wp-json/wp/v2/categories`);
54+
const url = getUrl("/wp-json/wp/v2/categories");
55+
const response = await fetch(url);
5156
const categories: Category[] = await response.json();
5257
return categories;
5358
}
5459

5560
export async function getCategoryById(id: number): Promise<Category> {
56-
const response = await fetch(`${url}/wp-json/wp/v2/categories/${id}`);
61+
const url = getUrl(`/wp-json/wp/v2/categories/${id}`);
62+
const response = await fetch(url);
5763
const category: Category = await response.json();
5864
return category;
5965
}
6066

6167
export async function getCategoryBySlug(slug: string): Promise<Category> {
62-
const response = await fetch(`${url}/wp-json/wp/v2/categories?slug=${slug}`);
68+
const url = getUrl("/wp-json/wp/v2/categories", { slug });
69+
const response = await fetch(url);
6370
const category: Category[] = await response.json();
6471
return category[0];
6572
}
6673

6774
export async function getPostsByCategory(categoryId: number): Promise<Post[]> {
68-
const response = await fetch(
69-
`${url}/wp-json/wp/v2/posts?categories=${categoryId}`
70-
);
75+
const url = getUrl("/wp-json/wp/v2/posts", { categories: categoryId });
76+
const response = await fetch(url);
7177
const posts: Post[] = await response.json();
7278
return posts;
7379
}
7480

7581
export async function getPostsByTag(tagId: number): Promise<Post[]> {
76-
const response = await fetch(`${url}/wp-json/wp/v2/posts?tags=${tagId}`);
82+
const url = getUrl("/wp-json/wp/v2/posts", { tags: tagId });
83+
const response = await fetch(url);
7784
const posts: Post[] = await response.json();
7885
return posts;
7986
}
8087

8188
export async function getTagsByPost(postId: number): Promise<Tag[]> {
82-
const response = await fetch(`${url}/wp-json/wp/v2/tags?post=${postId}`);
89+
const url = getUrl("/wp-json/wp/v2/tags", { post: postId });
90+
const response = await fetch(url);
8391
const tags: Tag[] = await response.json();
8492
return tags;
8593
}
8694

8795
export async function getAllTags(): Promise<Tag[]> {
88-
const response = await fetch(`${url}/wp-json/wp/v2/tags`);
96+
const url = getUrl("/wp-json/wp/v2/tags");
97+
const response = await fetch(url);
8998
const tags: Tag[] = await response.json();
9099
return tags;
91100
}
92101

93102
export async function getTagById(id: number): Promise<Tag> {
94-
const response = await fetch(`${url}/wp-json/wp/v2/tags/${id}`);
103+
const url = getUrl(`/wp-json/wp/v2/tags/${id}`);
104+
const response = await fetch(url);
95105
const tag: Tag = await response.json();
96106
return tag;
97107
}
98108

99109
export async function getTagBySlug(slug: string): Promise<Tag> {
100-
const response = await fetch(`${url}/wp-json/wp/v2/tags?slug=${slug}`);
110+
const url = getUrl("/wp-json/wp/v2/tags", { slug });
111+
const response = await fetch(url);
101112
const tag: Tag[] = await response.json();
102113
return tag[0];
103114
}
104115

105116
export async function getAllPages(): Promise<Page[]> {
106-
const response = await fetch(`${url}/wp-json/wp/v2/pages`);
117+
const url = getUrl("/wp-json/wp/v2/pages");
118+
const response = await fetch(url);
107119
const pages: Page[] = await response.json();
108120
return pages;
109121
}
110122

111123
export async function getPageById(id: number): Promise<Page> {
112-
const response = await fetch(`${url}/wp-json/wp/v2/pages/${id}`);
124+
const url = getUrl(`/wp-json/wp/v2/pages/${id}`);
125+
const response = await fetch(url);
113126
const page: Page = await response.json();
114127
return page;
115128
}
116129

117130
export async function getPageBySlug(slug: string): Promise<Page> {
118-
const response = await fetch(`${url}/wp-json/wp/v2/pages?slug=${slug}`);
131+
const url = getUrl("/wp-json/wp/v2/pages", { slug });
132+
const response = await fetch(url);
119133
const page: Page[] = await response.json();
120134
return page[0];
121135
}
122136

123137
export async function getAllAuthors(): Promise<Author[]> {
124-
const response = await fetch(`${url}/wp-json/wp/v2/users`);
138+
const url = getUrl("/wp-json/wp/v2/users");
139+
const response = await fetch(url);
125140
const authors: Author[] = await response.json();
126141
return authors;
127142
}
128143

129144
export async function getAuthorById(id: number): Promise<Author> {
130-
const response = await fetch(`${url}/wp-json/wp/v2/users/${id}`);
145+
const url = getUrl(`/wp-json/wp/v2/users/${id}`);
146+
const response = await fetch(url);
131147
const author: Author = await response.json();
132148
return author;
133149
}
134150

135151
export async function getAuthorBySlug(slug: string): Promise<Author> {
136-
const response = await fetch(`${url}/wp-json/wp/v2/users?slug=${slug}`);
152+
const url = getUrl("/wp-json/wp/v2/users", { slug });
153+
const response = await fetch(url);
137154
const author: Author[] = await response.json();
138155
return author[0];
139156
}
140157

141158
export async function getPostsByAuthor(authorId: number): Promise<Post[]> {
142-
const response = await fetch(`${url}/wp-json/wp/v2/posts?author=${authorId}`);
159+
const url = getUrl("/wp-json/wp/v2/posts", { author: authorId });
160+
const response = await fetch(url);
143161
const posts: Post[] = await response.json();
144162
return posts;
145163
}
@@ -148,9 +166,8 @@ export async function getPostsByAuthorSlug(
148166
authorSlug: string
149167
): Promise<Post[]> {
150168
const author = await getAuthorBySlug(authorSlug);
151-
const response = await fetch(
152-
`${url}/wp-json/wp/v2/posts?author=${author.id}`
153-
);
169+
const url = getUrl("/wp-json/wp/v2/posts", { author: author.id });
170+
const response = await fetch(url);
154171
const posts: Post[] = await response.json();
155172
return posts;
156173
}
@@ -159,22 +176,23 @@ export async function getPostsByCategorySlug(
159176
categorySlug: string
160177
): Promise<Post[]> {
161178
const category = await getCategoryBySlug(categorySlug);
162-
const response = await fetch(
163-
`${url}/wp-json/wp/v2/posts?categories=${category.id}`
164-
);
179+
const url = getUrl("/wp-json/wp/v2/posts", { categories: category.id });
180+
const response = await fetch(url);
165181
const posts: Post[] = await response.json();
166182
return posts;
167183
}
168184

169185
export async function getPostsByTagSlug(tagSlug: string): Promise<Post[]> {
170186
const tag = await getTagBySlug(tagSlug);
171-
const response = await fetch(`${url}/wp-json/wp/v2/posts?tags=${tag.id}`);
187+
const url = getUrl("/wp-json/wp/v2/posts", { tags: tag.id });
188+
const response = await fetch(url);
172189
const posts: Post[] = await response.json();
173190
return posts;
174191
}
175192

176193
export async function getFeaturedMediaById(id: number): Promise<FeaturedMedia> {
177-
const response = await fetch(`${url}/wp-json/wp/v2/media/${id}`);
194+
const url = getUrl(`/wp-json/wp/v2/media/${id}`);
195+
const response = await fetch(url);
178196
const featuredMedia: FeaturedMedia = await response.json();
179197
return featuredMedia;
180198
}

0 commit comments

Comments
 (0)