|
7 | 7 |
|
8 | 8 | import { createSelector } from '@reduxjs/toolkit'; |
9 | 9 | import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'; |
10 | | -import { fetchWellknownConfiguration } from '@forgerock/sdk-oidc'; |
| 10 | +import { initWellknownQuery } from '@forgerock/sdk-oidc'; |
11 | 11 |
|
12 | | -import type { WellknownResponse, GenericError } from '@forgerock/sdk-types'; |
13 | | -import type { BaseQueryFn, FetchBaseQueryError } from '@reduxjs/toolkit/query'; |
14 | | - |
15 | | -function isObject(value: unknown): value is Record<string, unknown> { |
16 | | - return typeof value === 'object' && value !== null; |
17 | | -} |
18 | | - |
19 | | -/** |
20 | | - * Converts FetchBaseQueryError to GenericError. |
21 | | - */ |
22 | | -function toGenericError(error: FetchBaseQueryError): GenericError { |
23 | | - const status = error.status; |
24 | | - let message = `HTTP error ${String(status)}`; |
25 | | - |
26 | | - if ('error' in error) { |
27 | | - message = error.error; |
28 | | - } else if ('data' in error && isObject(error.data)) { |
29 | | - if (typeof error.data['message'] === 'string') message = error.data['message']; |
30 | | - else if (typeof error.data['error'] === 'string') message = error.data['error']; |
31 | | - else if (typeof error.data['error_description'] === 'string') |
32 | | - message = error.data['error_description']; |
33 | | - else message = JSON.stringify(error.data); |
34 | | - } |
35 | | - |
36 | | - return { |
37 | | - error: 'Well-known configuration fetch failed', |
38 | | - message, |
39 | | - type: 'wellknown_error', |
40 | | - status, |
41 | | - }; |
42 | | -} |
43 | | - |
44 | | -/** |
45 | | - * Configured fetchBaseQuery that sets `Accept: application/json` headers. |
46 | | - */ |
47 | | -const innerBaseQuery = fetchBaseQuery({ |
48 | | - prepareHeaders: (headers) => { |
49 | | - headers.set('Accept', 'application/json'); |
50 | | - return headers; |
51 | | - }, |
52 | | -}); |
53 | | - |
54 | | -/** |
55 | | - * BaseQuery wrapper that normalizes FetchBaseQueryError to GenericError. |
56 | | - * |
57 | | - * This allows the wellknownApi to use GenericError as its error type |
58 | | - * while the actual HTTP transport goes through RTK Query's pipeline. |
59 | | - */ |
60 | | -const wellknownBaseQuery: BaseQueryFn<string, unknown, GenericError> = async ( |
61 | | - args, |
62 | | - api, |
63 | | - extraOptions, |
64 | | -) => { |
65 | | - const result = await innerBaseQuery(args, api, extraOptions); |
66 | | - if (result.error) { |
67 | | - return { ...result, error: toGenericError(result.error) }; |
68 | | - } |
69 | | - return result; |
70 | | -}; |
| 12 | +import type { WellknownResponse } from '@forgerock/sdk-types'; |
| 13 | +import type { |
| 14 | + FetchBaseQueryError, |
| 15 | + FetchBaseQueryMeta, |
| 16 | + QueryReturnValue, |
| 17 | +} from '@reduxjs/toolkit/query'; |
71 | 18 |
|
72 | 19 | /** |
73 | 20 | * RTK Query API for well-known endpoint discovery. |
74 | 21 | * |
75 | | - * Uses `queryFn` to pass the baseQuery into the framework-agnostic |
76 | | - * `fetchWellknownConfiguration` effect, which handles response validation. |
77 | | - * The baseQuery handles HTTP transport. |
| 22 | + * Uses the `initWellknownQuery` builder pattern from `@forgerock/sdk-oidc`. |
| 23 | + * The builder constructs the request and validates the response; |
| 24 | + * `fetchBaseQuery` handles the HTTP transport through RTK Query's pipeline. |
78 | 25 | */ |
79 | 26 | export const wellknownApi = createApi({ |
80 | 27 | reducerPath: 'wellknown', |
81 | | - baseQuery: wellknownBaseQuery, |
| 28 | + baseQuery: fetchBaseQuery(), |
82 | 29 | endpoints: (builder) => ({ |
83 | 30 | configuration: builder.query<WellknownResponse, string>({ |
84 | 31 | queryFn: async (url, _api, _extra, baseQuery) => { |
85 | | - const result = await fetchWellknownConfiguration(url, baseQuery); |
86 | | - return result.success ? { data: result.data } : { error: result.error }; |
| 32 | + const result = await initWellknownQuery(url).applyQuery(async (req) => { |
| 33 | + const queryResult = await baseQuery(req); |
| 34 | + return queryResult as QueryReturnValue<unknown, FetchBaseQueryError, FetchBaseQueryMeta>; |
| 35 | + }); |
| 36 | + return result as QueryReturnValue< |
| 37 | + WellknownResponse, |
| 38 | + FetchBaseQueryError, |
| 39 | + FetchBaseQueryMeta |
| 40 | + >; |
87 | 41 | }, |
88 | 42 | }), |
89 | 43 | }), |
|
0 commit comments