1313 * @since 2023-12-27
1414 */
1515
16+ import { fetchJson } from './fetch-wrapper' ;
1617import { Response } from '../interfaces' ;
1718import { FaqList } from '../interfaces' ;
1819
@@ -37,7 +38,7 @@ export const fetchAllFaqsByCategory = async (
3738 if ( onlyNew ) {
3839 url . searchParams . set ( 'only-new' , onlyNew as unknown as string ) ;
3940 }
40- const response = await fetch ( url . toString ( ) , {
41+ return await fetchJson ( url . toString ( ) , {
4142 method : 'GET' ,
4243 cache : 'no-cache' ,
4344 headers : {
@@ -46,12 +47,10 @@ export const fetchAllFaqsByCategory = async (
4647 redirect : 'follow' ,
4748 referrerPolicy : 'no-referrer' ,
4849 } ) ;
49-
50- return await response . json ( ) ;
5150} ;
5251
5352export const fetchFaqsByAutocomplete = async ( searchTerm : string , csrfToken : string ) : Promise < Response | undefined > => {
54- const response = await fetch ( `./api/faq/search` , {
53+ return await fetchJson ( `./api/faq/search` , {
5554 method : 'POST' ,
5655 headers : {
5756 Accept : 'application/json, text/plain, */*' ,
@@ -62,16 +61,10 @@ export const fetchFaqsByAutocomplete = async (searchTerm: string, csrfToken: str
6261 csrf : csrfToken ,
6362 } ) ,
6463 } ) ;
65-
66- if ( response . status === 200 ) {
67- return await response . json ( ) ;
68- }
69-
70- throw new Error ( 'Network response was not ok.' ) ;
7164} ;
7265
7366export const deleteFaq = async ( faqId : string , faqLanguage : string , token : string ) : Promise < Response | undefined > => {
74- const response = await fetch ( './api/faq/delete' , {
67+ return await fetchJson ( './api/faq/delete' , {
7568 method : 'DELETE' ,
7669 headers : {
7770 Accept : 'application/json, text/plain, */*' ,
@@ -83,16 +76,10 @@ export const deleteFaq = async (faqId: string, faqLanguage: string, token: strin
8376 faqLanguage : faqLanguage ,
8477 } ) ,
8578 } ) ;
86-
87- if ( response . status === 200 ) {
88- return await response . json ( ) ;
89- }
90-
91- throw new Error ( 'Network response was not ok.' ) ;
9279} ;
9380
9481export const create = async ( formData : unknown ) : Promise < Response | undefined > => {
95- const response = await fetch ( './api/faq/create' , {
82+ return await fetchJson ( './api/faq/create' , {
9683 method : 'POST' ,
9784 headers : {
9885 Accept : 'application/json, text/plain, */*' ,
@@ -102,12 +89,10 @@ export const create = async (formData: unknown): Promise<Response | undefined> =
10289 data : formData ,
10390 } ) ,
10491 } ) ;
105-
106- return await response . json ( ) ;
10792} ;
10893
10994export const update = async ( formData : unknown ) : Promise < Response | undefined > => {
110- const response = await fetch ( './api/faq/update' , {
95+ return await fetchJson ( './api/faq/update' , {
11196 method : 'PUT' ,
11297 headers : {
11398 Accept : 'application/json, text/plain, */*' ,
@@ -117,6 +102,4 @@ export const update = async (formData: unknown): Promise<Response | undefined> =
117102 data : formData ,
118103 } ) ,
119104 } ) ;
120-
121- return await response . json ( ) ;
122105} ;
0 commit comments