-
Notifications
You must be signed in to change notification settings - Fork 20
Integrate users/me/meta endpoint into the React and NextJs sdks #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/javascript/src/api/__tests__/getUsersMeMeta.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright 2025-2026 The ThunderID Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import {describe, it, expect, vi} from 'vitest'; | ||
| import getUsersMeMeta from '../getUsersMeMeta'; | ||
| import ThunderIDAPIError from '../../errors/ThunderIDAPIError'; | ||
|
|
||
| describe('getUsersMeMeta', () => { | ||
| it('fetches user schema metadata successfully with custom fetcher', async () => { | ||
| const mockSchema = { | ||
| schema: { | ||
| givenName: { | ||
| displayName: 'First Name', | ||
| type: 'STRING', | ||
| required: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const mockFetcher = vi.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockSchema, | ||
| } as Response); | ||
|
|
||
| const result = await getUsersMeMeta({ | ||
| baseUrl: 'https://api.example.com', | ||
| fetcher: mockFetcher, | ||
| }); | ||
|
|
||
| expect(mockFetcher).toHaveBeenCalledWith( | ||
| 'https://api.example.com/users/me/meta', | ||
| expect.objectContaining({ | ||
| method: 'GET', | ||
| }), | ||
| ); | ||
| expect(result).toEqual(mockSchema); | ||
| }); | ||
|
|
||
| it('throws ThunderIDAPIError for invalid URL', async () => { | ||
| await expect( | ||
| getUsersMeMeta({ | ||
| baseUrl: 'invalid-url', | ||
| fetcher: vi.fn(), | ||
| }), | ||
| ).rejects.toThrow(ThunderIDAPIError); | ||
| }); | ||
|
|
||
| it('throws ThunderIDAPIError when server returns non-ok response', async () => { | ||
| const mockFetcher = vi.fn().mockResolvedValue({ | ||
| ok: false, | ||
| status: 500, | ||
| statusText: 'Internal Server Error', | ||
| text: async () => 'Server error', | ||
| } as Response); | ||
|
|
||
| await expect( | ||
| getUsersMeMeta({ | ||
| baseUrl: 'https://api.example.com', | ||
| fetcher: mockFetcher, | ||
| }), | ||
| ).rejects.toThrow(ThunderIDAPIError); | ||
| }); | ||
|
|
||
| it('handles network failure', async () => { | ||
| const mockFetcher = vi.fn().mockRejectedValue(new Error('Network error')); | ||
|
|
||
| await expect( | ||
| getUsersMeMeta({ | ||
| baseUrl: 'https://api.example.com', | ||
| fetcher: mockFetcher, | ||
| }), | ||
| ).rejects.toThrow(ThunderIDAPIError); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright 2025-2026 The ThunderID Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import ThunderIDAPIError from '../errors/ThunderIDAPIError'; | ||
|
|
||
| /** | ||
| * Attribute schema metadata returned by GET /users/me/meta | ||
| */ | ||
| export interface AttributeSchema { | ||
| credential?: boolean; | ||
| description?: string; | ||
| displayName?: string; | ||
| mutability?: string; | ||
| readOnly?: boolean; | ||
| regex?: string; | ||
| required?: boolean; | ||
| subAttributes?: AttributeSchema[]; | ||
| type?: string; | ||
| unique?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Configuration for the getUsersMeMeta request | ||
| */ | ||
| export interface GetUsersMeMetaConfig extends Omit<RequestInit, 'method' | 'headers'> { | ||
| /** | ||
| * The base path of the API endpoint. | ||
| */ | ||
| baseUrl?: string; | ||
| /** | ||
| * Optional custom fetcher function. | ||
| * If not provided, native fetch will be used. | ||
| */ | ||
| fetcher?: (url: string, config: RequestInit) => Promise<Response>; | ||
| /** | ||
| * Custom HTTP headers as a plain object. | ||
| */ | ||
| headers?: Record<string, string>; | ||
| /** | ||
| * The absolute API endpoint. | ||
| */ | ||
| url?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Response structure for GET /users/me/meta | ||
| */ | ||
| export interface UsersMeMetaResponse { | ||
| schema?: Record<string, AttributeSchema>; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the user schema metadata from the specified /users/me/meta endpoint. | ||
| * | ||
| * @param config - Request configuration object. | ||
| * @returns A promise that resolves with the user schema metadata. | ||
| */ | ||
| const getUsersMeMeta = async ({ | ||
| baseUrl, | ||
| fetcher, | ||
| url, | ||
| ...requestConfig | ||
| }: GetUsersMeMetaConfig): Promise<UsersMeMetaResponse> => { | ||
| try { | ||
| // eslint-disable-next-line no-new | ||
| new URL((url ?? baseUrl)!); | ||
| } catch (error) { | ||
| throw new ThunderIDAPIError( | ||
| `Invalid URL provided. ${error instanceof Error ? error.message : String(error)}`, | ||
| 'getUsersMeMeta-ValidationError-001', | ||
| 'javascript', | ||
| 400, | ||
| 'The provided `url` or `baseUrl` path does not adhere to the URL schema.', | ||
| ); | ||
| } | ||
|
|
||
| const fetchFn: typeof fetch = fetcher ?? fetch; | ||
| const resolvedUrl: string = url ?? `${baseUrl?.replace(/\/$/, '')}/users/me/meta`; | ||
|
|
||
| const requestInit: RequestInit = { | ||
| ...requestConfig, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| ...requestConfig.headers, | ||
| }, | ||
| method: 'GET', | ||
| }; | ||
|
|
||
| try { | ||
| const response: Response = await fetchFn(resolvedUrl, requestInit); | ||
|
|
||
| if (!response?.ok) { | ||
| const errorText: string = await response.text(); | ||
|
|
||
| throw new ThunderIDAPIError( | ||
| errorText, | ||
| 'getUsersMeMeta-ResponseError-001', | ||
| 'javascript', | ||
| response.status, | ||
| response.statusText, | ||
| 'Failed to fetch user schema metadata', | ||
| ); | ||
| } | ||
|
|
||
| return (await response.json()) as UsersMeMetaResponse; | ||
| } catch (error) { | ||
| if (error instanceof ThunderIDAPIError) { | ||
| throw error; | ||
| } | ||
|
|
||
| throw new ThunderIDAPIError( | ||
| `Network or parsing error: ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
| 'getUsersMeMeta-NetworkError-001', | ||
| 'javascript', | ||
| 0, | ||
| 'Network Error', | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| export default getUsersMeMeta; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2025-2026 The ThunderID Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| FetchHttpClient, | ||
| HttpRequestConfig, | ||
| HttpResponse, | ||
| getUsersMeMeta as baseGetUsersMeMeta, | ||
| GetUsersMeMetaConfig as BaseGetUsersMeMetaConfig, | ||
| UsersMeMetaResponse, | ||
| AttributeSchema, | ||
| } from '@thunderid/browser'; | ||
|
|
||
| export type {AttributeSchema, UsersMeMetaResponse}; | ||
|
|
||
| /** | ||
| * Configuration for the getUsersMeMeta request (React-specific) | ||
| */ | ||
| export interface GetUsersMeMetaConfig extends Omit<BaseGetUsersMeMetaConfig, 'fetcher'> { | ||
| /** | ||
| * Optional custom fetcher function. If not provided, the ThunderID SPA client's httpClient will be used | ||
| */ | ||
| fetcher?: (url: string, config: RequestInit) => Promise<Response>; | ||
| /** | ||
| * Optional instance ID for multi-instance support. Defaults to 0. | ||
| */ | ||
| instanceId?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the user schema metadata from the specified /users/me/meta endpoint. | ||
| * Uses ThunderID SPA client FetchHttpClient by default with multi-instance support. | ||
| */ | ||
| const getUsersMeMeta = async ({ | ||
| fetcher, | ||
| instanceId = 0, | ||
| ...requestConfig | ||
| }: GetUsersMeMetaConfig): Promise<UsersMeMetaResponse> => { | ||
| const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => { | ||
| const httpClient: FetchHttpClient = FetchHttpClient.getInstance(instanceId); | ||
| const response: HttpResponse<UsersMeMetaResponse> = await httpClient.request({ | ||
| headers: config.headers as Record<string, string>, | ||
| method: config.method ?? 'GET', | ||
| url, | ||
| } as HttpRequestConfig); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return { | ||
| json: () => Promise.resolve(response.data), | ||
| ok: response.status >= 200 && response.status < 300, | ||
| status: response.status, | ||
| statusText: response.statusText || '', | ||
| text: () => Promise.resolve(typeof response.data === 'string' ? response.data : JSON.stringify(response.data)), | ||
| } as Response; | ||
| }; | ||
|
|
||
| return baseGetUsersMeMeta({ | ||
| ...requestConfig, | ||
| fetcher: fetcher ?? defaultFetcher, | ||
| }); | ||
| }; | ||
|
|
||
| export default getUsersMeMeta; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.