Skip to content

Commit

Permalink
Merge pull request #3 from movinsilva/js-ui-core--branding
Browse files Browse the repository at this point in the history
feat(core): add branding
  • Loading branch information
dasuni-30 committed Apr 29, 2024
2 parents a7c28d7 + 3951760 commit a385ab1
Show file tree
Hide file tree
Showing 31 changed files with 2,102 additions and 8 deletions.
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-preference-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';
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',
);
};
54 changes: 54 additions & 0 deletions packages/core/src/api/branding-preference.ts
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;
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 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;
};
Loading

0 comments on commit a385ab1

Please sign in to comment.