Skip to content

Commit 4d06f7d

Browse files
Merge pull request #64 from devgateway/task/TCDICORE-340/add-config-layer
2 parents 71c3342 + e5b7db3 commit 4d06f7d

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devgateway/wp-react-lib": minor
3+
---
4+
5+
update API endpoint handling to support dynamic base URLs for SSR contexts

wp-react-lib/src/api/index.ts

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import type { DgSettings, Media } from "../types"
33

44
// @ts-ignore Types not available
55
const API_ROOT = import.meta.env.VITE_REACT_APP_WP_API ?? process.env.VITE_REACT_APP_WP_API ?? '/wp/wp-json'
6-
const URL_MENU = API_ROOT + '/menus/v1/menus/'
6+
// const URL_MENU = API_ROOT + '/menus/v1/menus/'
77

8-
const URL_API_BASE = API_ROOT + '/wp/v2/'
8+
// const URL_API_BASE = API_ROOT + '/wp/v2/'
99

10-
const URL_PAGE = API_ROOT + '/wp/v2/pages'
11-
// @ts-ignore
12-
const URL_SEARCH = API_ROOT + (import.meta.env.VITE_REACT_APP_WP_SEARCH_END_POINT ?? process.env.VITE_REACT_APP_WP_SEARCH_END_POINT ?? '/wp/v2/search')
10+
// const URL_PAGE = API_ROOT + '/wp/v2/pages'
11+
// // @ts-ignore
12+
// const URL_SEARCH = API_ROOT + (import.meta.env.VITE_REACT_APP_WP_SEARCH_END_POINT ?? process.env.VITE_REACT_APP_WP_SEARCH_END_POINT ?? '/wp/v2/search')
1313

14-
const URL_MEDIA = API_ROOT + '/wp/v2/media'
14+
// const URL_MEDIA = API_ROOT + '/wp/v2/media'
1515

16-
const URL_SETTINGS = API_ROOT + '/dg/v1/settings';
16+
// const URL_SETTINGS = API_ROOT + '/dg/v1/settings';
1717

18-
const URL_CATEGORIES = API_ROOT + '/wp/v2/categories'
18+
// const URL_CATEGORIES = API_ROOT + '/wp/v2/categories'
1919

20-
const URL_YEAR_RANGE = API_ROOT + '/util-api/v1/year-range'
20+
// const URL_YEAR_RANGE = API_ROOT + '/util-api/v1/year-range'
2121

2222

2323
export const post = (url: string, params: Record<string, unknown>, isBlob?: boolean) => {
@@ -60,6 +60,7 @@ export const post = (url: string, params: Record<string, unknown>, isBlob?: bool
6060
})
6161
}
6262
export const get = (url: string, _params: Record<string, unknown> = {}) => {
63+
console.log("get", url);
6364
return new Promise((resolve, reject) => {
6465

6566
fetch(url, {credentials: 'include'})
@@ -92,25 +93,29 @@ export const queryParams = (params: Record<string, any>) => {
9293
}
9394

9495

95-
export const getTaxonomy = (name: string, locale: string) => {
96-
return get(URL_API_BASE + "" + name + '?lang=' + locale + '&per_page=100')
96+
export const getTaxonomy = (name: string, locale: string, apiBaseUrl: string = API_ROOT + '/wp/v2/') => {
97+
const apiUrl = apiBaseUrl + "/wp/v2/"
98+
return get(apiUrl + "" + name + '?lang=' + locale + '&per_page=100')
9799

98100
}
99101

100102
//TODO:make a unique getPost method
101103
export const getPostsByTypeAndTaxonomy = (
102-
{type, category, value, locale, page = 1, perPage = 1}:
103-
{type: string, category: string, value: string, locale: string, page?: number, perPage?: number}) => {
104-
return get(URL_API_BASE + type + "?_embed&" + category + '=' + value + '&lang=' + locale + '&per_page=' + perPage + '&page=' + page)
104+
{type, category, value, locale, page = 1, perPage = 1, apiBaseUrl}:
105+
{type: string, category: string, value: string, locale: string, page?: number, perPage?: number, apiBaseUrl?: string}) => {
106+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/wp/v2/" : API_ROOT + '/wp/v2/';
107+
return get(apiUrl + type + "?_embed&" + category + '=' + value + '&lang=' + locale + '&per_page=' + perPage + '&page=' + page)
105108
}
106109

107110

108-
export const getSettings=(locale: string,changeUUID?: string) : Promise<DgSettings> =>{
109-
return get(URL_SETTINGS+'?cacheBust='+((Math.random() + 1).toString(36).substring(7))+'&lang='+locale+(changeUUID?'&customize_changeset_uuid='+changeUUID:'')) as Promise<DgSettings>
111+
export const getSettings=(locale: string,changeUUID?: string, apiBaseUrl?: string) : Promise<DgSettings> =>{
112+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/dg/v1/settings" : API_ROOT + '/dg/v1/settings';
113+
return get(apiUrl+'?cacheBust='+((Math.random() + 1).toString(36).substring(7))+'&lang='+locale+(changeUUID?'&customize_changeset_uuid='+changeUUID:'')) as Promise<DgSettings>
110114
}
111115

112-
export const getMenu = (name: string, locale: string) => {
113-
return get(URL_MENU + name + '?lang=' + locale)
116+
export const getMenu = (name: string, locale: string, apiBaseUrl?: string) => {
117+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/menus/v1/menus/" : API_ROOT + '/menus/v1/menus/';
118+
return get(apiUrl + name + '?lang=' + locale)
114119
}
115120

116121
export const getPosts = ({
@@ -126,7 +131,8 @@ export const getPosts = ({
126131
previewNonce,
127132
previewId,
128133
search,
129-
after
134+
after,
135+
apiBaseUrl
130136
}: {
131137
slug?: string;
132138
type?: string;
@@ -141,9 +147,11 @@ export const getPosts = ({
141147
previewId?: string;
142148
search?: string;
143149
after?: Date;
150+
apiBaseUrl?: string;
144151
}) => {
145152

146-
let url = URL_API_BASE + (type ?? 'posts')
153+
let url = apiBaseUrl ? apiBaseUrl + "/wp/v2/" : API_ROOT + '/wp/v2/';
154+
url += (type ?? 'posts')
147155

148156
if (previewId) {
149157
url += '/' + previewId + '/revisions'
@@ -191,7 +199,8 @@ export const getPages = ({
191199
previewNonce,
192200
previewId,
193201
search,
194-
noCache
202+
noCache,
203+
apiBaseUrl
195204
}: {
196205
before?: Date;
197206
perPage?: number;
@@ -204,8 +213,9 @@ export const getPages = ({
204213
previewId?: string;
205214
search?: string;
206215
noCache?: boolean;
216+
apiBaseUrl?: string;
207217
}) => {
208-
let url = URL_PAGE
218+
let url = apiBaseUrl ? apiBaseUrl + "/wp/v2/pages" : API_ROOT + '/wp/v2/pages'
209219

210220
if (previewId) {
211221
url += '/' + previewId + '/revisions'
@@ -236,8 +246,10 @@ export const search = (
236246
search?: string,
237247
type?: string,
238248
subtype?: string,
239-
locale?: string) => {
240-
let url = URL_SEARCH + '?lang=' + locale
249+
locale?: string,
250+
apiBaseUrl?: string) => {
251+
let url = apiBaseUrl ? apiBaseUrl + "/wp/v2/search" : API_ROOT + '/wp/v2/search';
252+
url += '?lang=' + locale
241253
+ (context ? "&context=" + context : '')
242254
+ (perPage ? '&per_page=' + perPage : '')
243255
+ (page ? '&page=' + page : '')
@@ -248,8 +260,9 @@ export const search = (
248260
return get(url)
249261
}
250262

251-
export const getMedia = (slug: string, locale: string) : Promise<Media> => {
252-
return get(URL_MEDIA + '/' + slug + '?lang=' + locale) as Promise<Media>;
263+
export const getMedia = (slug: string, locale: string, apiBaseUrl?: string) : Promise<Media> => {
264+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/wp/v2/media" : API_ROOT + '/wp/v2/media';
265+
return get(apiUrl + '/' + slug + '?lang=' + locale) as Promise<Media>;
253266
}
254267

255268
export const getCategories = ({
@@ -265,7 +278,8 @@ export const getCategories = ({
265278
parent,
266279
post,
267280
slug,
268-
locale
281+
locale,
282+
apiBaseUrl
269283
}: {
270284
context?: string;
271285
page?: number;
@@ -280,8 +294,10 @@ export const getCategories = ({
280294
post?: string;
281295
slug?: string;
282296
locale?: string;
297+
apiBaseUrl?: string;
283298
})=> {
284-
let url = URL_CATEGORIES + '?lang=' + locale
299+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/wp/v2/categories" : API_ROOT + '/wp/v2/categories';
300+
let url = apiUrl + '?lang=' + locale
285301
+ (context ? '&context=' + context : '')
286302
+ (page ? '&page=' + page : '')
287303
+ (perPage ? '&per_page=' + perPage : '')
@@ -297,6 +313,8 @@ export const getCategories = ({
297313
return get(url)
298314
}
299315

300-
export const getYearRange = () => {
301-
return get(URL_YEAR_RANGE)
316+
export const getYearRange = (apiBaseUrl?: string) => {
317+
const apiUrl = apiBaseUrl ? apiBaseUrl + "/util-api/v1/year-range" : API_ROOT + '/util-api/v1/year-range';
318+
319+
return get(apiUrl)
302320
}

0 commit comments

Comments
 (0)