|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | + |
| 8 | +import { createSelector } from '@reduxjs/toolkit'; |
| 9 | +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'; |
| 10 | +import { fetchWellknownConfiguration } from '@forgerock/sdk-oidc'; |
| 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 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * RTK Query API for well-known endpoint discovery. |
| 74 | + * |
| 75 | + * Uses `queryFn` to pass the baseQuery into the framework-agnostic |
| 76 | + * `fetchWellknownConfiguration` effect, which handles response validation. |
| 77 | + * The baseQuery handles HTTP transport. |
| 78 | + */ |
| 79 | +export const wellknownApi = createApi({ |
| 80 | + reducerPath: 'wellknown', |
| 81 | + baseQuery: wellknownBaseQuery, |
| 82 | + endpoints: (builder) => ({ |
| 83 | + configuration: builder.query<WellknownResponse, string>({ |
| 84 | + queryFn: async (url, _api, _extra, baseQuery) => { |
| 85 | + const result = await fetchWellknownConfiguration(url, baseQuery); |
| 86 | + return result.success ? { data: result.data } : { error: result.error }; |
| 87 | + }, |
| 88 | + }), |
| 89 | + }), |
| 90 | +}); |
| 91 | + |
| 92 | +/** |
| 93 | + * Creates a memoized selector for cached well-known data. |
| 94 | + * |
| 95 | + * @param wellknownUrl - The well-known endpoint URL used as the cache key |
| 96 | + * @returns A memoized selector that extracts the WellknownResponse from state, or undefined if not yet fetched |
| 97 | + */ |
| 98 | +export function createWellknownSelector(wellknownUrl: string) { |
| 99 | + return createSelector( |
| 100 | + wellknownApi.endpoints.configuration.select(wellknownUrl), |
| 101 | + (result) => result?.data, |
| 102 | + ); |
| 103 | +} |
0 commit comments