javascript - How to build the url with filter params in RTK Query of this format? - Stack Overflow

I struggle making the url to fetch data with filters. Backend REST api is built in .Net.The url to fil

I struggle making the url to fetch data with filters. Backend REST api is built in .Net. The url to filter items has this format:

BASE_URL/ENDPOINT?Technologies=some-id&Complexities=0&Complexities=1&page=1&pageSize=3

Technologies and Complexities can repeat x times with different values.

RTK Query provides a query function, but it doesn't build up the url in the way i need.

export const apiService = createApi({
  reducerPath: 'apiService',
  baseQuery: fetchBaseQuery({ baseUrl: BASE_URL }),
  endpoints: (build) => ({
    getAllQuizzes: build.query<ApiResponse, QueryProps>({
      query: ({ Technologies, Complexities, page, pageSize }) => {
        return ({
          url: ENDPOINT,
          params: {
            Technologies,
            Complexities,
            page,
            pageSize, 
          }
        })
      },
    }),
  }),
});

I struggle making the url to fetch data with filters. Backend REST api is built in .Net. The url to filter items has this format:

BASE_URL/ENDPOINT?Technologies=some-id&Complexities=0&Complexities=1&page=1&pageSize=3

Technologies and Complexities can repeat x times with different values.

RTK Query provides a query function, but it doesn't build up the url in the way i need.

export const apiService = createApi({
  reducerPath: 'apiService',
  baseQuery: fetchBaseQuery({ baseUrl: BASE_URL }),
  endpoints: (build) => ({
    getAllQuizzes: build.query<ApiResponse, QueryProps>({
      query: ({ Technologies, Complexities, page, pageSize }) => {
        return ({
          url: ENDPOINT,
          params: {
            Technologies,
            Complexities,
            page,
            pageSize, 
          }
        })
      },
    }),
  }),
});

Im using mui async autoplete ponent to pick multiple filters. Any ideas?

Share Improve this question asked Dec 30, 2022 at 11:11 DorinDorin 651 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

RTK Query only uses the object signature of the URLSearchParams constructor function by default.

For you, that will not work.

Theoretically, you could pass in your own pre-populated URLSearchParams object in there (but this is not a guarantee for future versions and not indicated by the TypeScript types), or could just omit the params step altogether, building the full url yourself:

   getAllQuizzes: build.query<ApiResponse, QueryProps>({
      query: ({ Technologies, Complexities, page, pageSize }) => {
        const params = new URLSearchParams({ page, pageSize })
        for (const tech of Technologies) {
          params.append('Technologies', tech)
        }
        for (const p of Complexities) {
          params.append('Complexities', p )
        }
        return ({
          url: ENDPOINT+'?'+params.toString(),
        })
      },
    }),

Alternatively, you could also keep your existing code and specify a custom paramsSerializer function - for example the query-string library:

import queryString from 'query-string'

//...
const baseQuery = fetchBaseQuery({
    baseUrl,
    paramsSerializer: (params: Record<string, unknown>) =>
        queryString.stringify(params, { arrayFormat: 'none' }),
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745494030a4630114.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信