2
2
// Used to fetch data from a WordPress site using the WordPress REST API
3
3
// Types are imported from `wp.d.ts`
4
4
5
+ import querystring from 'query-string'
6
+
5
7
import {
6
8
Post ,
7
9
Category ,
@@ -13,133 +15,149 @@ import {
13
15
14
16
// WordPress Config
15
17
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
+ }
17
25
18
26
// WordPress Functions
19
27
20
28
export async function getAllPosts ( filterParams ?: {
21
29
author ?: string ;
22
30
tag ?: string ;
23
31
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 ) ;
33
35
const posts : Post [ ] = await response . json ( ) ;
34
36
return posts ;
35
37
}
36
38
37
39
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 ) ;
39
42
const post : Post = await response . json ( ) ;
40
43
return post ;
41
44
}
42
45
43
46
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 ) ;
45
49
const post : Post [ ] = await response . json ( ) ;
46
50
return post [ 0 ] ;
47
51
}
48
52
49
53
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 ) ;
51
56
const categories : Category [ ] = await response . json ( ) ;
52
57
return categories ;
53
58
}
54
59
55
60
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 ) ;
57
63
const category : Category = await response . json ( ) ;
58
64
return category ;
59
65
}
60
66
61
67
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 ) ;
63
70
const category : Category [ ] = await response . json ( ) ;
64
71
return category [ 0 ] ;
65
72
}
66
73
67
74
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 ) ;
71
77
const posts : Post [ ] = await response . json ( ) ;
72
78
return posts ;
73
79
}
74
80
75
81
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 ) ;
77
84
const posts : Post [ ] = await response . json ( ) ;
78
85
return posts ;
79
86
}
80
87
81
88
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 ) ;
83
91
const tags : Tag [ ] = await response . json ( ) ;
84
92
return tags ;
85
93
}
86
94
87
95
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 ) ;
89
98
const tags : Tag [ ] = await response . json ( ) ;
90
99
return tags ;
91
100
}
92
101
93
102
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 ) ;
95
105
const tag : Tag = await response . json ( ) ;
96
106
return tag ;
97
107
}
98
108
99
109
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 ) ;
101
112
const tag : Tag [ ] = await response . json ( ) ;
102
113
return tag [ 0 ] ;
103
114
}
104
115
105
116
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 ) ;
107
119
const pages : Page [ ] = await response . json ( ) ;
108
120
return pages ;
109
121
}
110
122
111
123
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 ) ;
113
126
const page : Page = await response . json ( ) ;
114
127
return page ;
115
128
}
116
129
117
130
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 ) ;
119
133
const page : Page [ ] = await response . json ( ) ;
120
134
return page [ 0 ] ;
121
135
}
122
136
123
137
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 ) ;
125
140
const authors : Author [ ] = await response . json ( ) ;
126
141
return authors ;
127
142
}
128
143
129
144
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 ) ;
131
147
const author : Author = await response . json ( ) ;
132
148
return author ;
133
149
}
134
150
135
151
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 ) ;
137
154
const author : Author [ ] = await response . json ( ) ;
138
155
return author [ 0 ] ;
139
156
}
140
157
141
158
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 ) ;
143
161
const posts : Post [ ] = await response . json ( ) ;
144
162
return posts ;
145
163
}
@@ -148,9 +166,8 @@ export async function getPostsByAuthorSlug(
148
166
authorSlug : string
149
167
) : Promise < Post [ ] > {
150
168
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 ) ;
154
171
const posts : Post [ ] = await response . json ( ) ;
155
172
return posts ;
156
173
}
@@ -159,22 +176,23 @@ export async function getPostsByCategorySlug(
159
176
categorySlug : string
160
177
) : Promise < Post [ ] > {
161
178
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 ) ;
165
181
const posts : Post [ ] = await response . json ( ) ;
166
182
return posts ;
167
183
}
168
184
169
185
export async function getPostsByTagSlug ( tagSlug : string ) : Promise < Post [ ] > {
170
186
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 ) ;
172
189
const posts : Post [ ] = await response . json ( ) ;
173
190
return posts ;
174
191
}
175
192
176
193
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 ) ;
178
196
const featuredMedia : FeaturedMedia = await response . json ( ) ;
179
197
return featuredMedia ;
180
198
}
0 commit comments