Skip to content

Commit

Permalink
feat(profile): change profile api endpoint (carbon-design-system#4909)
Browse files Browse the repository at this point in the history
### Related Ticket(s)

Change profile endpoint on Carbon Masthead for signed in state carbon-design-system#4701

### Description

Change profile endpoint which no longer allows for jsonp to get around CORS errors. Set the new preprod profile endpoint as default. 

### Changelog

**Changed**

- use `axios` for the profile calls
- set default host endpoint to preprod url

**Removed**

- `jsonp` package - no longer needed

<!-- React and Web Component deploy previews are enabled by default. -->
<!-- To enable additional available deploy previews, apply the following -->
<!-- labels for the corresponding package: -->
<!-- *** "package: services": Services -->
<!-- *** "package: utilities": Utilities -->
<!-- *** "package: styles": Carbon Expressive -->
<!-- *** "RTL": React / Web Components (RTL) -->
<!-- *** "feature flag": React / Web Components (experimental) -->
  • Loading branch information
annawen1 authored and IgnacioBecerra committed Feb 22, 2021
1 parent 9ac7c98 commit 09c6b09
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 149 deletions.
Binary file removed .yarn/offline-mirror/jsonp-0.2.1.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/react/src/components/Masthead/Masthead.js
Expand Up @@ -88,7 +88,7 @@ const Masthead = ({
(async () => {
const status = await ProfileAPI.getUserStatus();
if (!unmounted) {
setStatus(status.user === 'Authenticated');
setStatus(status.user !== 'Unauthenticated');
}
})();
return () => {
Expand Down
73 changes: 35 additions & 38 deletions packages/services-store/src/actions/__tests__/profileAPI.test.ts
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -10,10 +10,10 @@
import configureMockStore from 'redux-mock-store';
import { AnyAction } from 'redux';
import thunk, { ThunkDispatch } from 'redux-thunk';
import ProfileAPI from '@carbon/ibmdotcom-services/es/services/Profile/Profile.js';
import { USER_AUTHENTICATION_STATUS, PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import convertValue from '../../../tests/utils/convert-value';
import { setUserStatus, monitorUserStatus } from '../profileAPI';
// import ProfileAPI from '@carbon/ibmdotcom-services/es/services/Profile/Profile.js';
import { PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
// import convertValue from '../../../tests/utils/convert-value';
import { setUserStatus } from '../profileAPI';

jest.mock('@carbon/ibmdotcom-services/es/services/Profile/Profile');

Expand All @@ -25,45 +25,42 @@ const mockStore = configureMockStore<
describe('Redux actions for `ProfileAPI`', () => {
it('dispatches the action to set user authentication status', () => {
const store = mockStore();
store.dispatch(setUserStatus({ user: USER_AUTHENTICATION_STATUS.AUTHENTICATED }));
store.dispatch(setUserStatus({ user: 'test.user@ibm.com' }));
expect(store.getActions()).toEqual([
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
},
]);
});

it('dispatches the action to monitor user authentication status', () => {
ProfileAPI.monitorUserStatus.mockImplementation(callback => {
callback(null, { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED });
callback(null, { user: USER_AUTHENTICATION_STATUS.UNAUTHENTICATED });
});
const store = mockStore();
store.dispatch(monitorUserStatus());
expect(convertValue(store.getActions())).toEqual([
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
},
{
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.UNAUTHENTICATED },
},
]);
});
// it('dispatches the action to get user authentication status', async () => {
// ProfileAPI.getUserStatus.mockResolvedValue({ user: 'Unauthenticated' });
// const store = mockStore();
// await store.dispatch(ProfileAPI.getUserStatus());
// expect(convertValue(store.getActions())).toEqual([
// {
// type: PROFILE_API_ACTION.SET_REQUEST_USER_STATUS_IN_PROGRESS,
// request: 'PROMISE',
// },
// {
// type: PROFILE_API_ACTION.SET_USER_STATUS,
// status: { user: 'Unauthenticated' },
// },
// ]);
// });

it('dispatches the action of error in monitoring user authentication status', () => {
ProfileAPI.monitorUserStatus.mockImplementation(callback => {
callback(new Error('error-monitoruserstatus'));
});
const store = mockStore();
store.dispatch(monitorUserStatus());
expect(convertValue(store.getActions())).toEqual([
{
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
error: 'error-monitoruserstatus',
},
]);
});
// it('dispatches the action of error in monitoring user authentication status', () => {
// ProfileAPI.getUserStatus.mockImplementation(callback => {
// callback(new Error('error-getuserstatus'));
// });
// const store = mockStore();
// store.dispatch(getUserStatus());
// expect(convertValue(store.getActions())).toEqual([
// {
// type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
// error: 'error-getuserstatus',
// },
// ]);
// });
});
45 changes: 31 additions & 14 deletions packages/services-store/src/actions/profileAPI.ts
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -16,13 +16,25 @@ import { UserStatus, PROFILE_API_ACTION, ProfileAPIState } from '../types/profil
* @returns A Redux action to set the state that the JSONP call for user authentication status failed.
* @private
*/
export function setMonitorUserStatusError(error: Error) {
export function setErrorRequestUserStatus(error: Error) {
return {
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
error,
};
}

/**
* @param status The promise of the REST call for user status that is in progress.
* @returns A Redux action to set the state that the REST call for user status is in progress.
* @private
*/
export function setRequestUserStatusInProgress(status: Promise<UserStatus>) {
return {
type: PROFILE_API_ACTION.SET_REQUEST_USER_STATUS_IN_PROGRESS,
status,
};
}

/**
* @param status The user authentication status from the JSONP call.
* @returns A Redux action to set the given user authentication status.
Expand All @@ -34,19 +46,24 @@ export function setUserStatus(status: UserStatus) {
};
}

export type ProfileAPIActions = ReturnType<typeof setMonitorUserStatusError> | ReturnType<typeof setUserStatus>;
export type ProfileAPIActions =
| ReturnType<typeof setErrorRequestUserStatus>
| ReturnType<typeof setRequestUserStatusInProgress>
| ReturnType<typeof setUserStatus>;

/**
* @returns A Redux action that monitors user authentication status.
* @returns A Redux action that gets user authentication status.
*/
export function monitorUserStatus(): ThunkAction<void, { profileAPI: ProfileAPIState }, void, ProfileAPIActions> {
return dispatch => {
ProfileAPI.monitorUserStatus((error: Error, status: UserStatus) => {
if (error) {
dispatch(setMonitorUserStatusError(error));
} else {
dispatch(setUserStatus(status));
}
});
export function getUserStatus(): ThunkAction<Promise<UserStatus>, { profileAPI: ProfileAPIState }, void, ProfileAPIActions> {
return async dispatch => {
const promiseStatus: Promise<UserStatus> = ProfileAPI.getUserStatus();
dispatch(setRequestUserStatusInProgress(promiseStatus));
try {
dispatch(setUserStatus(await promiseStatus));
} catch (error) {
dispatch(setErrorRequestUserStatus(error));
throw error;
}
return promiseStatus;
};
}
@@ -1,33 +1,33 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { USER_AUTHENTICATION_STATUS, PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import { PROFILE_API_ACTION, ProfileAPIState } from '../../types/profileAPI';
import { ProfileAPIActions } from '../../actions/profileAPI';
import convertValue from '../../../tests/utils/convert-value';
import reducer from '../profileAPI';

describe('Redux reducers for `ProfileAPI`', () => {
it('should return the state unmodified for unknown action', () => {
const state = { status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED } };
const state = { status: { user: 'test.user@ibm.com' } };
expect(reducer(state, {} as ProfileAPIActions)).toEqual(state);
});

it('should support setting error in monitoring user authentication status', () => {
expect(
convertValue(
reducer({} as ProfileAPIState, {
type: PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS,
type: PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS,
error: new Error('error-user-status'),
})
)
).toEqual({
errorMonitorUserStatus: 'error-user-status',
errorGetUserStatus: 'error-user-status',
});
});

Expand All @@ -36,11 +36,11 @@ describe('Redux reducers for `ProfileAPI`', () => {
convertValue(
reducer({} as ProfileAPIState, {
type: PROFILE_API_ACTION.SET_USER_STATUS,
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
})
)
).toEqual({
status: { user: USER_AUTHENTICATION_STATUS.AUTHENTICATED },
status: { user: 'test.user@ibm.com' },
});
});
});
10 changes: 5 additions & 5 deletions packages/services-store/src/reducers/profileAPI.ts
@@ -1,14 +1,14 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { PROFILE_API_ACTION, ProfileAPIState } from '../types/profileAPI';
import { setMonitorUserStatusError, setUserStatus, ProfileAPIActions } from '../actions/profileAPI';
import { setErrorRequestUserStatus, setUserStatus, ProfileAPIActions } from '../actions/profileAPI';

/**
* @param state The state for profile API.
Expand All @@ -17,11 +17,11 @@ import { setMonitorUserStatusError, setUserStatus, ProfileAPIActions } from '../
*/
export default function reducer(state: ProfileAPIState = {}, action: ProfileAPIActions): ProfileAPIState {
switch (action.type) {
case PROFILE_API_ACTION.SET_ERROR_MONITOR_USER_STATUS: {
const { error: errorMonitorUserStatus } = action as ReturnType<typeof setMonitorUserStatusError>;
case PROFILE_API_ACTION.SET_ERROR_REQUEST_USER_STATUS: {
const { error: errorGetUserStatus } = action as ReturnType<typeof setErrorRequestUserStatus>;
return {
...state,
errorMonitorUserStatus,
errorGetUserStatus,
};
}
case PROFILE_API_ACTION.SET_USER_STATUS: {
Expand Down
31 changes: 10 additions & 21 deletions packages/services-store/src/types/profileAPI.ts
@@ -1,45 +1,34 @@
/**
* @license
*
* Copyright IBM Corp. 2020
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* The user authentication status.
*/
export enum USER_AUTHENTICATION_STATUS {
/**
* Authenticated.
*/
AUTHENTICATED = 'Authenticated',

/**
* Unauthenticated.
*/
UNAUTHENTICATED = 'Unauthenticated',
}

/**
* The user authentication status, etc.
*/
export interface UserStatus {
/**
* The user authentication status.
*/
user: USER_AUTHENTICATION_STATUS;
user: string;
}

/**
* The Redux action ID for `ProfileAPI`.
*/
export enum PROFILE_API_ACTION {
/**
* One to set the state that the JSONP call for user authentication status failed.
* One to set the state that the REST call for user status is in progress or not.
*/
SET_REQUEST_USER_STATUS_IN_PROGRESS = 'SET_REQUEST_USER_STATUS_IN_PROGRESS',
/**
* One to set the state that the call user authentication status failed.
*/
SET_ERROR_MONITOR_USER_STATUS = 'SET_ERROR_MONITOR_USER_STATUS',
SET_ERROR_REQUEST_USER_STATUS = 'SET_ERROR_REQUEST_USER_STATUS',

/**
* One to set the given user authentication status.
Expand All @@ -52,9 +41,9 @@ export enum PROFILE_API_ACTION {
*/
export interface ProfileAPIState {
/**
* The error from the JSONP call for the user authentication status.
* The error from the call for the user authentication status.
*/
errorMonitorUserStatus?: Error;
errorGetUserStatus?: Error;

/**
* The user authentication status.
Expand Down
1 change: 0 additions & 1 deletion packages/services/package.json
Expand Up @@ -45,7 +45,6 @@
"@babel/runtime": "^7.5.0",
"@carbon/ibmdotcom-utilities": "1.14.0",
"axios": "^0.21.1",
"jsonp": "^0.2.1",
"window-or-global": "^1.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 09c6b09

Please sign in to comment.