|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import AsgardeoAPIError from '../../errors/AsgardeoAPIError'; |
| 20 | +import {FlowMetadataResponse, GetFlowMetaRequestConfig} from '../../models/v2/flow-meta-v2'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Fetches aggregated flow metadata from the `GET /flow/meta` endpoint. |
| 24 | + * |
| 25 | + * The response includes: |
| 26 | + * - Application or OU details depending on the `type` parameter |
| 27 | + * - Resolved design configuration (theme and layout) |
| 28 | + * - i18n translations filtered by `language` and `namespace` |
| 29 | + * - Registration flow enablement status |
| 30 | + * |
| 31 | + * @param config - Request configuration including `baseUrl`/`url`, `type`, `id`, |
| 32 | + * and optional `language` / `namespace` filters. |
| 33 | + * @returns A promise that resolves to the {@link FlowMetadataResponse}. |
| 34 | + * |
| 35 | + * @throws {AsgardeoAPIError} When required parameters are missing or the server |
| 36 | + * returns a non-OK response. |
| 37 | + * |
| 38 | + * @example |
| 39 | + * ```typescript |
| 40 | + * import getFlowMetaV2 from './api/v2/getFlowMetaV2'; |
| 41 | + * import { FlowMetaType } from './models/v2/flow-meta-v2'; |
| 42 | + * |
| 43 | + * const meta = await getFlowMetaV2({ |
| 44 | + * baseUrl: 'https://localhost:8090', |
| 45 | + * type: FlowMetaType.App, |
| 46 | + * id: '60a9b38b-6eba-9f9e-55f9-267067de4680', |
| 47 | + * language: 'en', |
| 48 | + * namespace: 'auth', |
| 49 | + * }); |
| 50 | + * |
| 51 | + * console.log(meta.application?.name); |
| 52 | + * console.log(meta.i18n.translations); |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * @experimental This function targets the Asgardeo V2 platform API |
| 56 | + */ |
| 57 | +const getFlowMetaV2 = async ({ |
| 58 | + url, |
| 59 | + baseUrl, |
| 60 | + type, |
| 61 | + id, |
| 62 | + language, |
| 63 | + namespace, |
| 64 | + ...requestConfig |
| 65 | +}: GetFlowMetaRequestConfig): Promise<FlowMetadataResponse> => { |
| 66 | + if (!type) { |
| 67 | + throw new AsgardeoAPIError( |
| 68 | + 'The "type" parameter is required', |
| 69 | + 'getFlowMetaV2-ValidationError-001', |
| 70 | + 'javascript', |
| 71 | + 400, |
| 72 | + 'The "type" query parameter must be either "APP" or "OU".', |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + if (!id) { |
| 77 | + throw new AsgardeoAPIError( |
| 78 | + 'The "id" parameter is required', |
| 79 | + 'getFlowMetaV2-ValidationError-002', |
| 80 | + 'javascript', |
| 81 | + 400, |
| 82 | + 'The "id" query parameter must be a valid UUID of the target application or organization unit.', |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + const queryParams: URLSearchParams = new URLSearchParams({ |
| 87 | + id, |
| 88 | + type, |
| 89 | + ...(language ? {language} : {}), |
| 90 | + ...(namespace ? {namespace} : {}), |
| 91 | + }); |
| 92 | + |
| 93 | + const baseEndpoint: string = url ?? `${baseUrl}/flow/meta`; |
| 94 | + const endpoint: string = `${baseEndpoint}?${queryParams.toString()}`; |
| 95 | + |
| 96 | + const response: Response = await fetch(endpoint, { |
| 97 | + ...requestConfig, |
| 98 | + headers: { |
| 99 | + Accept: 'application/json', |
| 100 | + ...requestConfig.headers, |
| 101 | + }, |
| 102 | + method: 'GET', |
| 103 | + }); |
| 104 | + |
| 105 | + if (!response.ok) { |
| 106 | + const errorText: string = await response.text(); |
| 107 | + |
| 108 | + throw new AsgardeoAPIError( |
| 109 | + `Flow metadata request failed: ${errorText}`, |
| 110 | + 'getFlowMetaV2-ResponseError-001', |
| 111 | + 'javascript', |
| 112 | + response.status, |
| 113 | + response.statusText, |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + const flowMetadata: FlowMetadataResponse = await response.json(); |
| 118 | + |
| 119 | + return flowMetadata; |
| 120 | +}; |
| 121 | + |
| 122 | +export default getFlowMetaV2; |
0 commit comments