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

Fix response payload handling #1179

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/dsync/CreateDirectory/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
33 changes: 17 additions & 16 deletions src/dsync/DirectoryList/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,30 @@ 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;
typeof props.errorCallback === 'function' && props.errorCallback(response.error.message);
} 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;
}
Expand Down
16 changes: 9 additions & 7 deletions src/dsync/EditDirectory/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand All @@ -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' });
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions src/dsync/ToggleConnectionStatus/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function ToggleConnectionStatus(props: ToggleDirectoryStatusProps
deactivated: status,
};

const response = await sendHTTPRequest<Directory>(props.urls.patch, {
const response = await sendHTTPRequest<{ data: Directory }>(props.urls.patch, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/dsync/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
48 changes: 26 additions & 22 deletions src/sso/connections/ConnectionList/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,38 @@ export default function ConnectionList(props: ConnectionListProps) {
async function getFieldsData(url: string) {
state.isConnectionListLoading = true;
type ConnectionList = ConnectionData<SAMLSSORecord | OIDCSSORecord>[];
const data = await sendHTTPRequest<ConnectionList | { data: ConnectionList; pageToken: PageToken }>(url);
const response = await sendHTTPRequest<ConnectionList | { data: ConnectionList; pageToken: PageToken }>(
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<any>) => {
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<any>) => {
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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sso/connections/CreateConnection/oidc/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/sso/connections/CreateConnection/saml/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/sso/connections/EditConnection/oidc/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -143,7 +143,7 @@ export default function EditOIDCConnection(props: EditOIDCConnectionProps) {
clientId: state.oidcConnection.clientID!,
clientSecret: state.oidcConnection.clientSecret!,
callback: async (data: ApiResponse<undefined>) => {
if (data && 'error' in data) {
if (data?.error) {
typeof props.errorCallback === 'function' && props.errorCallback(data.error.message);
} else {
typeof props.successCallback === 'function' &&
Expand Down Expand Up @@ -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 = {
Expand Down
10 changes: 5 additions & 5 deletions src/sso/connections/EditConnection/saml/index.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand All @@ -118,7 +118,7 @@ export default function EditSAMLConnection(props: EditSAMLConnectionProps) {
clientId: state.samlConnection.clientID!,
clientSecret: state.samlConnection.clientSecret!,
callback: async (data: ApiResponse<undefined>) => {
if (data && 'error' in data) {
if (data?.error) {
typeof props.errorCallback === 'function' && props.errorCallback(data.error.message);
} else {
typeof props.successCallback === 'function' &&
Expand Down Expand Up @@ -149,10 +149,10 @@ export default function EditSAMLConnection(props: EditSAMLConnectionProps) {
const data = await sendHTTPRequest<SAMLSSORecord[]>(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 = {
Expand Down