From bee6002ebb7eac2f892a2a8be8e23511fcb87e1a Mon Sep 17 00:00:00 2001 From: Aswin V Date: Mon, 22 Apr 2024 18:27:31 +0530 Subject: [PATCH 1/4] Fix dsync response handling --- src/dsync/CreateDirectory/index.lite.tsx | 4 ++-- src/dsync/EditDirectory/index.lite.tsx | 16 +++++++++------- src/dsync/ToggleConnectionStatus/index.lite.tsx | 14 ++++++++------ src/dsync/types.ts | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/dsync/CreateDirectory/index.lite.tsx b/src/dsync/CreateDirectory/index.lite.tsx index 1ac6aa108..91f187151 100644 --- a/src/dsync/CreateDirectory/index.lite.tsx +++ b/src/dsync/CreateDirectory/index.lite.tsx @@ -96,10 +96,10 @@ export default function CreateDirectory(props: CreateDirectoryProps) { body: JSON.stringify(body), }); state.isSaving = false; - if (response) { + if (response && typeof response === 'object') { if ('error' in response && response.error) { typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); - } else if ('data' in response) { + } else if ('data' in response && response.data) { typeof props.successCallback === 'function' && props.successCallback({ operation: 'CREATE', connection: response.data }); } diff --git a/src/dsync/EditDirectory/index.lite.tsx b/src/dsync/EditDirectory/index.lite.tsx index a830a6475..416fb9452 100644 --- a/src/dsync/EditDirectory/index.lite.tsx +++ b/src/dsync/EditDirectory/index.lite.tsx @@ -76,10 +76,10 @@ export default function EditDirectory(props: EditDirectoryProps) { body: JSON.stringify(state.directoryUpdated), }); state.isSaving = false; - if (response) { + if (response && typeof response === 'object') { if ('error' in response && response.error) { typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); - } else if ('data' in response) { + } else if ('data' in response && response.data) { typeof props.successCallback === 'function' && props.successCallback({ operation: 'UPDATE', connection: response.data }); } @@ -93,10 +93,12 @@ export default function EditDirectory(props: EditDirectoryProps) { method: 'DELETE', }); - if (response && 'error' in response && response.error) { - typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); - } else { - typeof props.successCallback === 'function' && props.successCallback({ operation: 'DELETE' }); + if (response && typeof response === 'object') { + if (response.error) { + typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); + } else { + typeof props.successCallback === 'function' && props.successCallback({ operation: 'DELETE' }); + } } } @@ -131,7 +133,7 @@ export default function EditDirectory(props: EditDirectoryProps) { const response = await sendHTTPRequest<{ data: Directory }>(url); state.isDirectoryLoading = false; - if (response) { + if (response && typeof response === 'object') { if ('error' in response && response.error) { typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); } else if ('data' in response) { diff --git a/src/dsync/ToggleConnectionStatus/index.lite.tsx b/src/dsync/ToggleConnectionStatus/index.lite.tsx index 2e02fe955..f74d293ec 100644 --- a/src/dsync/ToggleConnectionStatus/index.lite.tsx +++ b/src/dsync/ToggleConnectionStatus/index.lite.tsx @@ -36,7 +36,7 @@ export default function ToggleConnectionStatus(props: ToggleDirectoryStatusProps deactivated: status, }; - const response = await sendHTTPRequest(props.urls.patch, { + const response = await sendHTTPRequest<{ data: Directory }>(props.urls.patch, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -45,11 +45,13 @@ export default function ToggleConnectionStatus(props: ToggleDirectoryStatusProps }); state.displayPrompt = false; - - if (response && 'error' in response && response.error) { - typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); - } else { - typeof props.successCallback === 'function' && props.successCallback({ operation: 'UPDATE' }); + if (response && typeof response === 'object') { + if ('error' in response && response.error) { + typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); + } else if ('data' in response && response.data) { + typeof props.successCallback === 'function' && + props.successCallback({ operation: 'UPDATE', connection: response.data }); + } } } toggle(); diff --git a/src/dsync/types.ts b/src/dsync/types.ts index bc8ca670c..cc5f19758 100644 --- a/src/dsync/types.ts +++ b/src/dsync/types.ts @@ -85,7 +85,7 @@ export interface ToggleDirectoryStatusProps { patch: string; }; errorCallback?: (errMsg: string) => void; - successCallback?: (info: { operation: 'UPDATE' }) => void; + successCallback?: (info: { operation: 'UPDATE'; connection?: Directory }) => void; classNames?: { container?: string; confirmationPrompt?: ConfirmationPromptProps['classNames']; From 85655fd32f9d4aca0de44803578bc3a754b5da67 Mon Sep 17 00:00:00 2001 From: Aswin V Date: Mon, 22 Apr 2024 20:42:20 +0530 Subject: [PATCH 2/4] Check if response is object and data is array before using them --- src/dsync/DirectoryList/index.lite.tsx | 33 ++++++------- .../connections/ConnectionList/index.lite.tsx | 48 ++++++++++--------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/dsync/DirectoryList/index.lite.tsx b/src/dsync/DirectoryList/index.lite.tsx index 740b3990d..337243c6a 100644 --- a/src/dsync/DirectoryList/index.lite.tsx +++ b/src/dsync/DirectoryList/index.lite.tsx @@ -129,7 +129,7 @@ export default function DirectoryList(props: DirectoryListProps) { { data: Directory[] } | { data: { data: Directory[] }; pageToken: PageToken } >(directoryListUrl); state.isDirectoryListLoading = false; - if (response) { + if (response && typeof response === 'object') { if ('error' in response && response.error) { state.showErrorComponent = true; state.errorMessage = response.error.message; @@ -137,21 +137,22 @@ export default function DirectoryList(props: DirectoryListProps) { } else if ('data' in response) { const isTokenizedPagination = 'pageToken' in response; const _data = isTokenizedPagination ? response.data.data : response.data; - const directoriesListData = _data.map((directory: Directory) => { - return { - ...directory, - id: directory.id, - name: directory.name, - tenant: directory.tenant, - product: directory.product, - type: state.providers.find(({ value }) => value === directory.type)?.text as DirectoryType, - status: directory.deactivated ? 'Inactive' : 'Active', - }; - }); - state.directoryListData = directoriesListData; - typeof props.handleListFetchComplete === 'function' && - props.handleListFetchComplete(directoriesListData); - + if (Array.isArray(_data)) { + const directoriesListData = _data.map((directory: Directory) => { + return { + ...directory, + id: directory.id, + name: directory.name, + tenant: directory.tenant, + product: directory.product, + type: state.providers.find(({ value }) => value === directory.type)?.text as DirectoryType, + status: directory.deactivated ? 'Inactive' : 'Active', + }; + }); + state.directoryListData = directoriesListData; + typeof props.handleListFetchComplete === 'function' && + props.handleListFetchComplete(directoriesListData); + } if (isTokenizedPagination) { return response.pageToken; } diff --git a/src/sso/connections/ConnectionList/index.lite.tsx b/src/sso/connections/ConnectionList/index.lite.tsx index 667a4f30d..7d47c2da3 100644 --- a/src/sso/connections/ConnectionList/index.lite.tsx +++ b/src/sso/connections/ConnectionList/index.lite.tsx @@ -140,34 +140,38 @@ export default function ConnectionList(props: ConnectionListProps) { async function getFieldsData(url: string) { state.isConnectionListLoading = true; type ConnectionList = ConnectionData[]; - const data = await sendHTTPRequest(url); + const response = await sendHTTPRequest( + url + ); state.isConnectionListLoading = false; - if (data) { - if ('error' in data) { + if (response && typeof response === 'object') { + if ('error' in response && response.error) { state.showErrorComponent = true; - state.errorMessage = data.error.message; - typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); + state.errorMessage = response.error.message; + typeof props.errorCallback === 'function' && props.errorCallback(response.error.message); } else { - const isTokenizedPagination = typeof data === 'object' && 'pageToken' in data; - const _data = isTokenizedPagination ? data.data : data; - const _connectionsListData = _data.map((connection: ConnectionData) => { - return { - ...connection, - provider: state.connectionProviderName(connection), - type: 'oidcProvider' in connection ? 'OIDC' : 'SAML', - status: connection.deactivated ? 'Inactive' : 'Active', - isSystemSSO: connection.isSystemSSO, - }; - }); - - state.connectionListData = _connectionsListData; - - typeof props.handleListFetchComplete === 'function' && - props.handleListFetchComplete(_connectionsListData); + const isTokenizedPagination = typeof response === 'object' && 'pageToken' in response; + const _data = isTokenizedPagination ? response.data : response; + if (Array.isArray(_data)) { + const _connectionsListData = _data.map((connection: ConnectionData) => { + return { + ...connection, + provider: state.connectionProviderName(connection), + type: 'oidcProvider' in connection ? 'OIDC' : 'SAML', + status: connection.deactivated ? 'Inactive' : 'Active', + isSystemSSO: connection.isSystemSSO, + }; + }); + + state.connectionListData = _connectionsListData; + + typeof props.handleListFetchComplete === 'function' && + props.handleListFetchComplete(_connectionsListData); + } if (isTokenizedPagination) { - return data.pageToken; + return response.pageToken; } } } From d958a508add75fe62dac09a2df5b1c35d5ccca36 Mon Sep 17 00:00:00 2001 From: Aswin V Date: Mon, 22 Apr 2024 23:04:14 +0530 Subject: [PATCH 3/4] Tweak response data handling --- src/sso/connections/EditConnection/oidc/index.lite.tsx | 10 +++++----- src/sso/connections/EditConnection/saml/index.lite.tsx | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/sso/connections/EditConnection/oidc/index.lite.tsx b/src/sso/connections/EditConnection/oidc/index.lite.tsx index 1b00190c0..8922a1f38 100644 --- a/src/sso/connections/EditConnection/oidc/index.lite.tsx +++ b/src/sso/connections/EditConnection/oidc/index.lite.tsx @@ -123,7 +123,7 @@ export default function EditOIDCConnection(props: EditOIDCConnectionProps) { connectionIsOIDC: true, callback: async (data) => { state.isSaving = false; - if (data && 'error' in data) { + if (data?.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else { if (state.oidcConnection.oidcDiscoveryUrl) { @@ -143,7 +143,7 @@ export default function EditOIDCConnection(props: EditOIDCConnectionProps) { clientId: state.oidcConnection.clientID!, clientSecret: state.oidcConnection.clientSecret!, callback: async (data: ApiResponse) => { - if (data && 'error' in data) { + if (data?.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else { typeof props.successCallback === 'function' && @@ -175,10 +175,10 @@ export default function EditOIDCConnection(props: EditOIDCConnectionProps) { state.isConnectionLoading = false; - if (data) { - if ('error' in data) { + if (data && typeof data === 'object') { + if ('error' in data && data.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); - } else { + } else if (Array.isArray(data)) { const _connection = data[0]; if (_connection) { state.oidcConnection = { diff --git a/src/sso/connections/EditConnection/saml/index.lite.tsx b/src/sso/connections/EditConnection/saml/index.lite.tsx index bf4403cd9..11fe8e581 100644 --- a/src/sso/connections/EditConnection/saml/index.lite.tsx +++ b/src/sso/connections/EditConnection/saml/index.lite.tsx @@ -101,7 +101,7 @@ export default function EditSAMLConnection(props: EditSAMLConnectionProps) { connectionIsSAML: true, callback: async (data) => { state.isSaving = false; - if (data && 'error' in data) { + if (data?.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else { typeof props.successCallback === 'function' && @@ -118,7 +118,7 @@ export default function EditSAMLConnection(props: EditSAMLConnectionProps) { clientId: state.samlConnection.clientID!, clientSecret: state.samlConnection.clientSecret!, callback: async (data: ApiResponse) => { - if (data && 'error' in data) { + if (data?.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else { typeof props.successCallback === 'function' && @@ -149,10 +149,10 @@ export default function EditSAMLConnection(props: EditSAMLConnectionProps) { const data = await sendHTTPRequest(url); state.isConnectionLoading = false; - if (data) { - if ('error' in data) { + if (data && typeof data === 'object') { + if ('error' in data && data.error) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); - } else { + } else if (Array.isArray(data)) { const _connection = data[0]; if (_connection) { state.samlConnection = { From 7b39a4b974608fcb673e250f5ccf1f74653a72da Mon Sep 17 00:00:00 2001 From: Aswin V Date: Mon, 22 Apr 2024 23:22:55 +0530 Subject: [PATCH 4/4] Check if data is object before usage --- src/sso/connections/CreateConnection/oidc/index.lite.tsx | 2 +- src/sso/connections/CreateConnection/saml/index.lite.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sso/connections/CreateConnection/oidc/index.lite.tsx b/src/sso/connections/CreateConnection/oidc/index.lite.tsx index 93eaf3445..c236ff443 100644 --- a/src/sso/connections/CreateConnection/oidc/index.lite.tsx +++ b/src/sso/connections/CreateConnection/oidc/index.lite.tsx @@ -80,7 +80,7 @@ export default function CreateOIDCConnection(props: CreateConnectionProps) { connectionIsOIDC: true, callback: async (data) => { state.isSaving = false; - if (data) { + if (data && typeof data === 'object') { if ('error' in data) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else { diff --git a/src/sso/connections/CreateConnection/saml/index.lite.tsx b/src/sso/connections/CreateConnection/saml/index.lite.tsx index d44005b8d..1ed7917a7 100644 --- a/src/sso/connections/CreateConnection/saml/index.lite.tsx +++ b/src/sso/connections/CreateConnection/saml/index.lite.tsx @@ -67,7 +67,7 @@ export default function CreateSAMLConnection(props: CreateConnectionProps) { connectionIsSAML: true, callback: async (data) => { state.isSaving = false; - if (data) { + if (data && typeof data === 'object') { if ('error' in data) { typeof props.errorCallback === 'function' && props.errorCallback(data.error.message); } else {