Skip to content

Commit a5fae9f

Browse files
chore (DVIZ-42): enhance API structure and type definitions
1 parent 646fa0f commit a5fae9f

File tree

6 files changed

+104
-48
lines changed

6 files changed

+104
-48
lines changed

wp-react-lib/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
"types": "./dist/types/index.d.ts",
6969
"import": "./dist/esm/index.js",
7070
"require": "./dist/cjs/index.js"
71+
},
72+
"./api": {
73+
"types": "./dist/types/api/index.d.ts",
74+
"import": "./dist/esm/api/index.js",
75+
"require": "./dist/cjs/api/index.js"
7176
}
7277
}
7378
}

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

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Media } from "../types"
1+
import type { PageResponse } from "../post-type"
2+
import type { DgSettings, Media } from "../types"
23

34
const API_ROOT = process.env.VITE_REACT_APP_WP_API ?? '/wp/wp-json'
45
const 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

104107
export 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

184212
export 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 : '')

wp-react-lib/src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import SearchProvider from "./providers/SearchProvider";
3333
import utils from "./util";
3434
import wordpress from "./reducers/wordpress";
3535

36-
import { SettingsContext } from './providers/Context'
36+
import { SettingsContext, PageContext, AppContext, PostContext } from './providers/Context'
3737

3838
export {
3939
Post,
@@ -63,7 +63,11 @@ export {
6363
PostIcon,
6464
SearchConsumer,
6565
SearchProvider,
66-
SettingsContext
66+
SettingsContext,
67+
PageContext,
68+
AppContext,
69+
PostContext
6770
}
6871

6972
export * from './types';
73+
export * from './post-type';

wp-react-lib/src/post-type.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface Post {
3434
_links: PostLinks;
3535
}
3636

37+
export type PostType = Post;
38+
3739
export interface PostLinks {
3840
self: Self[];
3941
collection: About[];
@@ -107,7 +109,9 @@ export interface PostYoastHeadJSON {
107109
twitter_site?: string;
108110
twitter_misc?: PostTwitterMisc;
109111
schema?: PostSchema;
110-
og_description?: string;
112+
og_description?: string;
113+
description?: string;
114+
twitter_image?: string;
111115
}
112116

113117

@@ -216,4 +220,18 @@ export interface TargetClass {
216220
export interface PostTwitterMisc {
217221
"Written by": string;
218222
"Est. reading time"?: string;
223+
}
224+
225+
226+
export interface PageMetaResponse {
227+
"content-type": string;
228+
"link": string;
229+
"x-wp-total": string;
230+
"x-wp-totalpages": string;
231+
}
232+
233+
234+
export interface PageResponse {
235+
data: Post[];
236+
meta: PageMetaResponse;
219237
}

wp-react-lib/src/reducers/actions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const getPostByTaxonomy = ({
5353

5454
dispatch({type: LOAD_CUSTOM_POSTS_BY_TAXONOMY, ...payLoad})
5555

56-
wp.getPostsByTypeAndTaxonomy(wpType, taxonomy, categoryId, locale, page, perPage)
56+
wp.getPostsByTypeAndTaxonomy({type: wpType, category: taxonomy, value: categoryId, locale, page, perPage})
5757
.then(response => {
5858
const {data, meta} = response
5959
dispatch({type: LOAD_CUSTOM_POSTS_BY_TAXONOMY_DONE, data, meta, ...payLoad})
@@ -81,7 +81,7 @@ export const getPosts = ({
8181

8282
dispatch({type: LOAD_POSTS, slug, taxonomy, categories, before, perPage, page, fields, store, locale})
8383

84-
wp.getPosts(slug, type, taxonomy, categories, before, perPage, page, fields, locale, previewNonce, previewId, search)
84+
wp.getPosts({slug, type, taxonomy, categories, before, perPage, page, fields, locale, previewNonce, previewId, search})
8585
.then(response => {
8686
const {data, meta} = response
8787
dispatch({
@@ -151,7 +151,7 @@ export const getPages = ({
151151
}) => (dispatch, getState) => {
152152

153153
dispatch({type: LOAD_PAGES, store})
154-
wp.getPages(before, perPage, page, fields, parent, slug, store, locale, previewNonce, previewId, search)
154+
wp.getPages({before, perPage, page, fields, parent, slug, locale, previewNonce, previewId, search})
155155
.then(response => {
156156
const {data, meta} = response
157157
dispatch({

wp-react-lib/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default defineConfig({
3939
],
4040
input: {
4141
index: resolve(__dirname, 'src/index.js'),
42+
'api/index': resolve(__dirname, 'src/api/index.js'),
4243
},
4344
plugins: [preserveDirectives()],
4445
output: [

0 commit comments

Comments
 (0)