-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from movinsilva/js-ui-core--branding
feat(core): add branding
- Loading branch information
Showing
31 changed files
with
2,102 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,73 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {AuthClient} from 'src/auth-client'; | ||
import AsgardeoUIException from 'src/exception'; | ||
import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-response'; | ||
|
||
/** | ||
* Fetch the branding preference text from the server. | ||
* | ||
* @param locale - The locale of the branding text. | ||
* @param name - The name of the branding text. | ||
* @param screen - The screen of the branding text. | ||
* @param type - The type of the branding text. | ||
* @returns A Promise that resolves to the response from the server. | ||
* @throws {AsgardeoUIException} If the API call fails or when the response is not successful. | ||
*/ | ||
export const getBrandingPreferenceText = async ( | ||
locale: string, | ||
name: string, | ||
screen: string, | ||
type: string, | ||
): Promise<BrandingPreferenceTextAPIResponse> => { | ||
const headers: Headers = new Headers(); | ||
headers.append('Accept', 'application/json'); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const requestOptions: RequestInit = { | ||
headers, | ||
method: 'GET', | ||
}; | ||
|
||
const params: URLSearchParams = new URLSearchParams(); | ||
params.append('locale', locale); | ||
params.append('name', name); | ||
params.append('screen', screen); | ||
params.append('type', type); | ||
|
||
const {baseUrl} = await AuthClient.getInstance().getDataLayer().getConfigData(); | ||
const textUrl: string = `${baseUrl}/api/server/v1/branding-preference/text/resolve`; | ||
const urlWithParams: string = `${textUrl}?${params.toString()}`; | ||
let response: Response; | ||
|
||
try { | ||
response = await fetch(new Request(urlWithParams, requestOptions)); | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-BT-GBPT-NE', 'Branding Text API call failed', error.stack); | ||
} | ||
|
||
if (response.ok) { | ||
return (await response.json()) as BrandingPreferenceTextAPIResponse; | ||
} | ||
|
||
throw new AsgardeoUIException( | ||
'JS_UI_CORE-BT-GBPT-HE', | ||
'Failed to receive a successful response from the branding text endpoint', | ||
); | ||
}; |
This file contains 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,54 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {AuthClient} from '../auth-client'; | ||
import AsgardeoUIException from '../exception'; | ||
import {BrandingPreferenceAPIResponseInterface, BrandingPreferenceTypes} from '../models/branding-api-response'; | ||
|
||
/** | ||
* Fetch branding preferences from the server. | ||
* | ||
* @returns {Promise<BrandingPreferenceAPIResponseInterface>} A promise that resolves with the branding preferences. | ||
* @throws {AsgardeoUIException} If an error occurs while fetching the branding preferences or when the response is unsuccessful. | ||
*/ | ||
const getBrandingPreference = async (): Promise<BrandingPreferenceAPIResponseInterface> => { | ||
const { | ||
baseUrl, | ||
type = BrandingPreferenceTypes.Org, | ||
name = 'WSO2', | ||
} = await AuthClient.getInstance().getDataLayer().getConfigData(); | ||
const brandingUrl: string = `${baseUrl}/api/server/v1/branding-preference?type=${type}&name=${name}`; | ||
let response: Response; | ||
|
||
try { | ||
response = await fetch(brandingUrl); | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-BR-GBP-NE', 'Error while fetching branding data.', error.stack); | ||
} | ||
|
||
if (response.ok) { | ||
return (await response.json()) as Promise<BrandingPreferenceAPIResponseInterface>; | ||
} | ||
|
||
throw new AsgardeoUIException( | ||
'JS_UI_CORE-BR-GBP-HE', | ||
'Failed to receive a successful response from the branding API.', | ||
); | ||
}; | ||
|
||
export default getBrandingPreference; |
This file contains 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 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,57 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import merge from 'lodash.merge'; | ||
import getBrandingPreference from 'src/api/branding-preference'; | ||
import {AuthClient} from 'src/auth-client'; | ||
import {BrandingPreferenceAPIResponseInterface} from 'src/models/branding-api-response'; | ||
import DEFAULT_BRANDING from './default-branding/default-branding'; | ||
import {Customization, GetBranding} from '../models/customization'; | ||
|
||
/** | ||
* Fetch and merge branding properties. | ||
* | ||
* @param {GetBranding} props - Branding properties. | ||
* @returns {Promise<Customization>} A promise that resolves with the merged branding properties. | ||
*/ | ||
export const getBranding = async (props: GetBranding): Promise<Customization> => { | ||
const {customization, merged} = props; | ||
let mergedBranding: Customization; | ||
|
||
/** | ||
* If the `merged` prop is not provided, fetch the branding from the console and merge it with the default branding. | ||
* If the `merged` prop is provided, merge it with the branding props. | ||
*/ | ||
if (!merged) { | ||
let brandingFromConsole: BrandingPreferenceAPIResponseInterface; | ||
|
||
if ((await AuthClient.getInstance().getDataLayer().getConfigData()).enableConsoleBranding ?? true) { | ||
brandingFromConsole = await getBrandingPreference(); | ||
} | ||
|
||
if (brandingFromConsole?.preference?.configs?.isBrandingEnabled) { | ||
mergedBranding = await merge(DEFAULT_BRANDING, brandingFromConsole ?? {}, customization ?? {}); | ||
} else { | ||
mergedBranding = await merge(DEFAULT_BRANDING, customization ?? {}); | ||
} | ||
} else { | ||
mergedBranding = await merge(merged ?? {}, customization ?? {}); | ||
} | ||
|
||
return mergedBranding; | ||
}; |
Oops, something went wrong.