|
| 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 ThunderIDAPIError from '../errors/ThunderIDAPIError'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Attribute schema metadata returned by GET /users/me/meta |
| 23 | + */ |
| 24 | +export interface AttributeSchema { |
| 25 | + credential?: boolean; |
| 26 | + description?: string; |
| 27 | + displayName?: string; |
| 28 | + mutability?: string; |
| 29 | + readOnly?: boolean; |
| 30 | + regex?: string; |
| 31 | + required?: boolean; |
| 32 | + subAttributes?: AttributeSchema[]; |
| 33 | + type?: string; |
| 34 | + unique?: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Configuration for the getUsersMeMeta request |
| 39 | + */ |
| 40 | +export interface GetUsersMeMetaConfig extends Omit<RequestInit, 'method'> { |
| 41 | + /** |
| 42 | + * The base path of the API endpoint. |
| 43 | + */ |
| 44 | + baseUrl?: string; |
| 45 | + /** |
| 46 | + * Optional custom fetcher function. |
| 47 | + * If not provided, native fetch will be used. |
| 48 | + */ |
| 49 | + fetcher?: (url: string, config: RequestInit) => Promise<Response>; |
| 50 | + /** |
| 51 | + * The absolute API endpoint. |
| 52 | + */ |
| 53 | + url?: string; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Response structure for GET /users/me/meta |
| 58 | + */ |
| 59 | +export interface UsersMeMetaResponse { |
| 60 | + schema?: Record<string, AttributeSchema>; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Retrieves the user schema metadata from the specified /users/me/meta endpoint. |
| 65 | + * |
| 66 | + * @param config - Request configuration object. |
| 67 | + * @returns A promise that resolves with the user schema metadata. |
| 68 | + */ |
| 69 | +const getUsersMeMeta = async ({ |
| 70 | + baseUrl, |
| 71 | + fetcher, |
| 72 | + url, |
| 73 | + ...requestConfig |
| 74 | +}: GetUsersMeMetaConfig): Promise<UsersMeMetaResponse> => { |
| 75 | + try { |
| 76 | + // eslint-disable-next-line no-new |
| 77 | + new URL((url ?? baseUrl)!); |
| 78 | + } catch (error) { |
| 79 | + throw new ThunderIDAPIError( |
| 80 | + `Invalid URL provided. ${error instanceof Error ? error.message : String(error)}`, |
| 81 | + 'getUsersMeMeta-ValidationError-001', |
| 82 | + 'javascript', |
| 83 | + 400, |
| 84 | + 'The provided `url` or `baseUrl` path does not adhere to the URL schema.', |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + const fetchFn: typeof fetch = fetcher ?? fetch; |
| 89 | + const resolvedUrl: string = url ?? `${baseUrl?.replace(/\/$/, '')}/users/me/meta`; |
| 90 | + |
| 91 | + const requestInit: RequestInit = { |
| 92 | + ...requestConfig, |
| 93 | + headers: { |
| 94 | + Accept: 'application/json', |
| 95 | + 'Content-Type': 'application/json', |
| 96 | + ...requestConfig.headers, |
| 97 | + }, |
| 98 | + method: 'GET', |
| 99 | + }; |
| 100 | + |
| 101 | + try { |
| 102 | + const response: Response = await fetchFn(resolvedUrl, requestInit); |
| 103 | + |
| 104 | + if (!response?.ok) { |
| 105 | + const errorText: string = await response.text(); |
| 106 | + |
| 107 | + throw new ThunderIDAPIError( |
| 108 | + errorText, |
| 109 | + 'getUsersMeMeta-ResponseError-001', |
| 110 | + 'javascript', |
| 111 | + response.status, |
| 112 | + response.statusText, |
| 113 | + 'Failed to fetch user schema metadata', |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return (await response.json()) as UsersMeMetaResponse; |
| 118 | + } catch (error) { |
| 119 | + if (error instanceof ThunderIDAPIError) { |
| 120 | + throw error; |
| 121 | + } |
| 122 | + |
| 123 | + throw new ThunderIDAPIError( |
| 124 | + `Network or parsing error: ${error instanceof Error ? error.message : 'Unknown error'}`, |
| 125 | + 'getUsersMeMeta-NetworkError-001', |
| 126 | + 'javascript', |
| 127 | + 0, |
| 128 | + 'Network Error', |
| 129 | + ); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +export default getUsersMeMeta; |
0 commit comments