11import type { PageResponse } from "../post-type"
22import type { DgSettings , Media } from "../types"
33
4- const API_ROOT = process . env . VITE_REACT_APP_WP_API ?? '/wp/wp-json'
5- const URL_MENU = API_ROOT + '/menus/v1/menus/'
4+ // @ts -ignore Types not available
5+ 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/'
67
7- const URL_API_BASE = API_ROOT + '/wp/v2/'
8+ // const URL_API_BASE = API_ROOT + '/wp/v2/'
89
9- const URL_PAGE = API_ROOT + '/wp/v2/pages'
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')
1013
11- const URL_SEARCH = API_ROOT + ( process . env . VITE_REACT_APP_WP_SEARCH_END_POINT ?? '/wp/v2/search' )
14+ // const URL_MEDIA = API_ROOT + '/wp/v2/media'
1215
13- const URL_MEDIA = API_ROOT + '/wp/v2/media'
16+ // const URL_SETTINGS = API_ROOT + '/dg/v1/settings';
1417
15- const URL_SETTINGS = API_ROOT + '/dg/v1/settings' ;
18+ // const URL_CATEGORIES = API_ROOT + '/wp/v2/categories'
1619
17- const URL_CATEGORIES = API_ROOT + '/wp/v2/categories'
18-
19- const URL_YEAR_RANGE = API_ROOT + '/util-api/v1/year-range'
20+ // const URL_YEAR_RANGE = API_ROOT + '/util-api/v1/year-range'
2021
2122
2223export const post = ( url : string , params : Record < string , unknown > , isBlob ?: boolean ) => {
@@ -59,6 +60,7 @@ export const post = (url: string, params: Record<string, unknown>, isBlob?: bool
5960 } )
6061}
6162export const get = ( url : string , _params : Record < string , unknown > = { } ) => {
63+ console . log ( "get" , url ) ;
6264 return new Promise ( ( resolve , reject ) => {
6365
6466 fetch ( url , { credentials : 'include' } )
@@ -91,25 +93,29 @@ export const queryParams = (params: Record<string, any>) => {
9193}
9294
9395
94- export const getTaxonomy = ( name : string , locale : string ) => {
95- 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' )
9699
97100}
98101
99102//TODO:make a unique getPost method
100103export const getPostsByTypeAndTaxonomy = (
101- { type, category, value, locale, page = 1 , perPage = 1 } :
102- { type : string , category : string , value : string , locale : string , page ?: number , perPage ?: number } ) => {
103- 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 )
104108}
105109
106110
107- export const getSettings = ( locale : string , changeUUID ?: string ) : Promise < DgSettings > => {
108- 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 >
109114}
110115
111- export const getMenu = ( name : string , locale : string ) => {
112- 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 )
113119}
114120
115121export const getPosts = ( {
@@ -125,7 +131,8 @@ export const getPosts = ({
125131 previewNonce,
126132 previewId,
127133 search,
128- after
134+ after,
135+ apiBaseUrl
129136} : {
130137 slug ?: string ;
131138 type ?: string ;
@@ -140,9 +147,11 @@ export const getPosts = ({
140147 previewId ?: string ;
141148 search ?: string ;
142149 after ?: Date ;
150+ apiBaseUrl ?: string ;
143151} ) => {
144152
145- let url = URL_API_BASE + ( type ?? 'posts' )
153+ let url = apiBaseUrl ? apiBaseUrl + "/wp/v2/" : API_ROOT + '/wp/v2/' ;
154+ url += ( type ?? 'posts' )
146155
147156 if ( previewId ) {
148157 url += '/' + previewId + '/revisions'
@@ -190,7 +199,8 @@ export const getPages = ({
190199 previewNonce,
191200 previewId,
192201 search,
193- noCache
202+ noCache,
203+ apiBaseUrl
194204} : {
195205 before ?: Date ;
196206 perPage ?: number ;
@@ -203,8 +213,9 @@ export const getPages = ({
203213 previewId ?: string ;
204214 search ?: string ;
205215 noCache ?: boolean ;
216+ apiBaseUrl ?: string ;
206217} ) => {
207- let url = URL_PAGE
218+ let url = apiBaseUrl ? apiBaseUrl + "/wp/v2/pages" : API_ROOT + '/wp/v2/pages'
208219
209220 if ( previewId ) {
210221 url += '/' + previewId + '/revisions'
@@ -224,6 +235,7 @@ export const getPages = ({
224235 + ( search ? '&search=' + search : '' )
225236 + ( noCache ? '&cacheBust=' + ( ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 7 ) ) : '' )
226237 }
238+ console . log ( "url==>" , url ) ;
227239 return get ( url ) as Promise < PageResponse >
228240}
229241
@@ -234,8 +246,10 @@ export const search = (
234246 search ?: string ,
235247 type ?: string ,
236248 subtype ?: string ,
237- locale ?: string ) => {
238- let url = URL_SEARCH + '?lang=' + locale
249+ locale ?: string ,
250+ apiBaseUrl ?: string ) => {
251+ let url = apiBaseUrl ? apiBaseUrl + "/dg/v1/search" : API_ROOT + '/dg/v1/search' ;
252+ url += '?lang=' + locale
239253 + ( context ? "&context=" + context : '' )
240254 + ( perPage ? '&per_page=' + perPage : '' )
241255 + ( page ? '&page=' + page : '' )
@@ -246,8 +260,9 @@ export const search = (
246260 return get ( url )
247261}
248262
249- export const getMedia = ( slug : string , locale : string ) : Promise < Media > => {
250- 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 > ;
251266}
252267
253268export const getCategories = ( {
@@ -263,7 +278,8 @@ export const getCategories = ({
263278 parent,
264279 post,
265280 slug,
266- locale
281+ locale,
282+ apiBaseUrl
267283} : {
268284 context ?: string ;
269285 page ?: number ;
@@ -278,8 +294,10 @@ export const getCategories = ({
278294 post ?: string ;
279295 slug ?: string ;
280296 locale ?: string ;
297+ apiBaseUrl ?: string ;
281298} ) => {
282- 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
283301 + ( context ? '&context=' + context : '' )
284302 + ( page ? '&page=' + page : '' )
285303 + ( perPage ? '&per_page=' + perPage : '' )
@@ -295,6 +313,8 @@ export const getCategories = ({
295313 return get ( url )
296314}
297315
298- export const getYearRange = ( ) => {
299- 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 )
300320}
0 commit comments