1- import type { Media } from "../types"
1+ import type { PageResponse } from "../post-type"
2+ import type { DgSettings , Media } from "../types"
23
34const API_ROOT = process . env . VITE_REACT_APP_WP_API ?? '/wp/wp-json'
45const URL_MENU = API_ROOT + '/menus/v1/menus/'
@@ -92,37 +93,52 @@ export const getTaxonomy = (name: string, locale: string) => {
9293}
9394
9495//TODO:make a unique getPost method
95- export const getPostsByTypeAndTaxonomy = ( type : string , category : string , value : string , locale : string , page = 1 , perPage = 1 ) => {
96+ export const getPostsByTypeAndTaxonomy = (
97+ { type, category, value, locale, page = 1 , perPage = 1 } :
98+ { type : string , category : string , value : string , locale : string , page ?: number , perPage ?: number } ) => {
9699 return get ( URL_API_BASE + type + "?_embed&" + category + '=' + value + '&lang=' + locale + '&per_page=' + perPage + '&page=' + page )
97100}
98101
99102
100- export const getSettings = ( locale : string , changeUUID : string ) => {
101- return get ( URL_SETTINGS + '?cacheBust=' + ( ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 7 ) ) + '&lang=' + locale + ( changeUUID ?'&customize_changeset_uuid=' + changeUUID :'' ) )
103+ export const getSettings = ( locale : string , changeUUID ? : string ) : Promise < DgSettings > => {
104+ return get ( URL_SETTINGS + '?cacheBust=' + ( ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 7 ) ) + '&lang=' + locale + ( changeUUID ?'&customize_changeset_uuid=' + changeUUID :'' ) ) as Promise < DgSettings >
102105}
103106
104107export const getMenu = ( name : string , locale : string ) => {
105108 return get ( URL_MENU + name + '?lang=' + locale )
106109}
107110
108- export const getPosts = (
109- slug : string ,
110- type : string ,
111- taxonomy : string ,
112- categories : string ,
113- before : Date ,
114- perPage : number ,
115- page : number ,
116- fields : string ,
117- locale : string ,
118- previewNonce : string ,
119- previewId : string ,
120- search : string
121- ) => {
111+ export const getPosts = ( {
112+ slug,
113+ type,
114+ taxonomy,
115+ categories,
116+ before,
117+ perPage,
118+ page,
119+ fields,
120+ locale,
121+ previewNonce,
122+ previewId,
123+ search
124+ } : {
125+ slug ?: string ;
126+ type ?: string ;
127+ taxonomy ?: string ;
128+ categories ?: string ;
129+ before ?: Date ;
130+ perPage ?: number ;
131+ page ?: number ;
132+ fields ?: string ;
133+ locale ?: string ;
134+ previewNonce ?: string ;
135+ previewId ?: string ;
136+ search ?: string ;
137+ } ) => {
122138 //language , categories id, date before, record per page, number of page, fields to be included, post type
123139 //const {lang, slug, wType: type, taxonomy, categories, before, perPage, page, fields} = params
124140
125- let url = URL_API_BASE + ( type ? type : 'posts' )
141+ let url = URL_API_BASE + ( type ?? 'posts' )
126142 if ( previewId ) {
127143 url += '/' + previewId + '/revisions'
128144 + ( previewNonce ? '?_wpnonce=' + previewNonce + '&' : '' )
@@ -133,7 +149,7 @@ export const getPosts = (
133149 + ( slug ? '&slug=' + slug : '' )
134150 if ( ! slug ) {
135151 url += ( categories ? ( taxonomy ? '&' + taxonomy : '&categories' )
136- + "=" + ( categories ? categories : "" ) : '' ) //ids
152+ + "=" + ( categories ?? "" ) : '' ) //ids
137153 + ( before ? "&before=" + before . toISOString ( ) : "" )
138154 + ( perPage ? '&per_page=' + perPage : '' )
139155 + ( page ? '&page=' + page : '' )
@@ -142,22 +158,34 @@ export const getPosts = (
142158 }
143159
144160 //url += "&lang=" + locale
145- return get ( url )
161+ return get ( url ) ;
146162}
147163
148- export const getPages = (
149- before : Date ,
150- perPage : number ,
151- page : number ,
152- fields : string ,
153- parent : string ,
154- slug : string ,
155- locale : string ,
156- previewNonce : string ,
157- previewId : string ,
158- search : string ,
159- noCache : boolean ) => {
160-
164+ export const getPages = ( {
165+ before,
166+ perPage,
167+ page,
168+ fields,
169+ parent,
170+ slug,
171+ locale,
172+ previewNonce,
173+ previewId,
174+ search,
175+ noCache
176+ } : {
177+ before ?: Date ;
178+ perPage ?: number ;
179+ page ?: number ;
180+ fields ?: string ;
181+ parent ?: string ;
182+ slug ?: string ;
183+ locale ?: string ;
184+ previewNonce ?: string ;
185+ previewId ?: string ;
186+ search ?: string ;
187+ noCache ?: boolean ;
188+ } ) => {
161189 let url = URL_PAGE
162190
163191 if ( previewId ) {
@@ -178,17 +206,17 @@ export const getPages = (
178206 + ( search ? '&search=' + search : '' )
179207 + ( noCache ? '&cacheBust=' + ( ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 7 ) ) : '' )
180208 }
181- return get ( url )
209+ return get ( url ) as Promise < PageResponse >
182210}
183211
184212export const search = (
185- context : string ,
186- page : number ,
187- perPage : number ,
188- search : string ,
189- type : string ,
190- subtype : string ,
191- locale : string ) => {
213+ context ? : string ,
214+ page ? : number ,
215+ perPage ? : number ,
216+ search ? : string ,
217+ type ? : string ,
218+ subtype ? : string ,
219+ locale ? : string ) => {
192220 let url = URL_SEARCH + '?lang=' + locale
193221 + ( context ? "&context=" + context : '' )
194222 + ( perPage ? '&per_page=' + perPage : '' )
0 commit comments