Skip to content
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

feat(core): add branding #3

Merged
merged 25 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
158d04d
feat(core): :sparkles: add branding function to retrieve branding pre…
movinsilva Apr 19, 2024
bbb4d6c
style(core): :art: update naming for enums in branding
movinsilva Apr 19, 2024
3e8daa4
feat(core): :label: add recursivePartial type & customization interface
movinsilva Apr 22, 2024
88a053d
feat(core): :sparkles: add default translations for en-US & fr-FR
movinsilva Apr 22, 2024
95e37f4
feat(core): :sparkles: add default branding objects
movinsilva Apr 22, 2024
5f71aca
feat(core): :sparkles: add getBranding function
movinsilva Apr 22, 2024
bdb44e4
feat(core): :technologist: improve branding function
movinsilva Apr 22, 2024
8e979f7
feat(core): :sparkles: add brandingText function
movinsilva Apr 22, 2024
32d8480
refactor(core): :art: update error codes
movinsilva Apr 22, 2024
d0cfea9
feat(core): :sparkles: add getLocalization function
movinsilva Apr 22, 2024
7219dfd
fix(core): :bug: modify getBranding function
movinsilva Apr 22, 2024
b47a515
refactor(core): :label: add types in getLocalization function
movinsilva Apr 22, 2024
89f9529
docs(core): :memo: update internal header
movinsilva Apr 26, 2024
936e4fb
fix(core): :art: fix formatting issues
movinsilva Apr 26, 2024
39532ad
refactor(core): :recycle: relocate GetLocalization interface
movinsilva Apr 26, 2024
e10a6e9
refactor(core): :art: reorganize variables
movinsilva Apr 26, 2024
3df960e
docs(core): :memo: update js docs for functions
movinsilva Apr 26, 2024
db71f90
refactor(core): :recycle: relocate & rename GetBranding props interface
movinsilva Apr 26, 2024
3249e99
docs(core): :memo: add comments for interfaces and types in customiza…
movinsilva Apr 26, 2024
72d1d33
chore(core): :wrench: override lint rule
movinsilva Apr 26, 2024
d78cd70
docs(core): :art: improve comments in getLocalization function
movinsilva Apr 26, 2024
556a906
fix(core): :bug: update copyright string
movinsilva Apr 26, 2024
4a57977
docs(core): :memo: add comments for BrandingPreferenceTypes enum
movinsilva Apr 26, 2024
adb5461
refactor(core): :recycle: rename branding function and file
movinsilva Apr 29, 2024
3951760
refactor(core): :recycle: rename brandingText function, file and the …
movinsilva Apr 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/core/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ module.exports = {
project: [path.resolve(__dirname, 'tsconfig.eslint.json')],
},
plugins: ['@wso2'],
rules: {
'@typescript-eslint/no-empty-function': [
'error',
{
allow: ['constructors'],
},
],
},
};
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@asgardeo/auth-js": "^5.0.1",
"@asgardeo/js-ui-core": "link:"
"csstype": "^3.1.3",
"lodash.merge": "^4.6.2"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/api/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const authenticate = async (props: AuthApiRequestBody): Promise<AuthApiResponse>
}

throw new AsgardeoUIException(
'JS_UI_CORE-AUTHN-AN-HE',
'JS_UI_CORE-AUTHN-A-HE',
'Failed to receive a successful response from the authentication endpoint',
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const authorize = async (): Promise<AuthApiResponse> => {
}

throw new AsgardeoUIException(
'UI_CORE-AUTHZ-A-HE',
'JS_UI_CORE-AUTHZ-A-HE',
'Failed to receive a successful response from the authorization server',
);
};
Expand Down
73 changes: 73 additions & 0 deletions packages/core/src/api/branding-text.ts
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';
movinsilva marked this conversation as resolved.
Show resolved Hide resolved
import AsgardeoUIException from 'src/exception';
import {BrandingTextAPIResponse} from 'src/models/branding-text-api-response';

/**
* Fetch the branding 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 brandingText = async (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const brandingText = async (
export const getBrandingPreferenceText = async (

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with 3951760

locale: string,
name: string,
screen: string,
type: string,
): Promise<BrandingTextAPIResponse> => {
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-BT-NE', 'Branding Text API call failed', error.stack);
}

if (response.ok) {
return (await response.json()) as BrandingTextAPIResponse;
}

throw new AsgardeoUIException(
'JS_UI_CORE-BT-BT-HE',
dasuni-30 marked this conversation as resolved.
Show resolved Hide resolved
'Failed to receive a successful response from the branding text endpoint',
);
};
51 changes: 51 additions & 0 deletions packages/core/src/api/branding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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 branding = async (): Promise<BrandingPreferenceAPIResponseInterface> => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const branding = async (): Promise<BrandingPreferenceAPIResponseInterface> => {
const getBrandingPreference = async (): Promise<BrandingPreferenceAPIResponseInterface> => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with adb5461

const {
baseUrl,
type = BrandingPreferenceTypes.Org,
name = 'WSO2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes
since they are optional properties in the config, added these as a fallback.

} = 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-B-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-B-HE', 'Failed to receive a successful response from the branding API.');
};

export default branding;
1 change: 0 additions & 1 deletion packages/core/src/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class AuthClient {
* This is necessary because this is a singleton class.
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}

/**
Expand Down
57 changes: 57 additions & 0 deletions packages/core/src/branding/branding.ts
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 branding from 'src/api/branding';
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 branding();
}

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;
};
Loading