From 3966d5e50fc2e977d9e2e190f197cb2ebc9707d3 Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Sun, 26 Nov 2023 17:58:23 +0200 Subject: [PATCH] cleaning up --- ui/src/app/segments/segmentsApi.ts | 25 +++---------------------- ui/src/utils/helpers.ts | 1 + ui/src/utils/redux-rtk.ts | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 ui/src/utils/redux-rtk.ts diff --git a/ui/src/app/segments/segmentsApi.ts b/ui/src/app/segments/segmentsApi.ts index a0f514f0b8..b0d1d2bff6 100644 --- a/ui/src/app/segments/segmentsApi.ts +++ b/ui/src/app/segments/segmentsApi.ts @@ -1,30 +1,11 @@ -import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; -import { apiURL, checkResponse, defaultHeaders } from '~/data/api'; +import { createApi } from '@reduxjs/toolkit/query/react'; import { IConstraintBase } from '~/types/Constraint'; import { ISegment, ISegmentBase, ISegmentList } from '~/types/Segment'; - -type CustomFetchFn = ( - url: RequestInfo, - options: RequestInit | undefined -) => Promise; - -const customFetchFn: CustomFetchFn = async (url, options) => { - const headers = defaultHeaders(); - - const response = await fetch(url, { - ...options, - headers - }); - checkResponse(response); - return response; -}; +import { baseQuery } from '~/utils/redux-rtk'; export const segmentsApi = createApi({ reducerPath: 'segments', - baseQuery: fetchBaseQuery({ - baseUrl: apiURL, - fetchFn: customFetchFn - }), + baseQuery, tagTypes: ['Segment'], endpoints: (builder) => ({ // get list of segments in this namespace diff --git a/ui/src/utils/helpers.ts b/ui/src/utils/helpers.ts index a1ed3bf069..0ac59a8aba 100644 --- a/ui/src/utils/helpers.ts +++ b/ui/src/utils/helpers.ts @@ -63,6 +63,7 @@ function isFetchBaseQueryError(error: unknown): error is ErrorWithMessage { typeof error === 'object' && error !== null && 'data' in error && + error.data !== null && typeof (error.data as Record).message === 'string' ); } diff --git a/ui/src/utils/redux-rtk.ts b/ui/src/utils/redux-rtk.ts new file mode 100644 index 0000000000..d02e3c8501 --- /dev/null +++ b/ui/src/utils/redux-rtk.ts @@ -0,0 +1,23 @@ +import { fetchBaseQuery } from '@reduxjs/toolkit/query/react'; +import { apiURL, checkResponse, defaultHeaders } from '~/data/api'; + +type CustomFetchFn = ( + url: RequestInfo, + options: RequestInit | undefined +) => Promise; + +const customFetchFn: CustomFetchFn = async (url, options) => { + const headers = defaultHeaders(); + + const response = await fetch(url, { + ...options, + headers + }); + checkResponse(response); + return response; +}; + +export const baseQuery = fetchBaseQuery({ + baseUrl: apiURL, + fetchFn: customFetchFn +});