Skip to content

Commit

Permalink
Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Oct 2, 2024
1 parent 3c2d4f3 commit a2c7b37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export default function useFixersMutation(): UseMutationResult {
throw new Error( data.error );
}

const isThreatLevelError = Object.values( data.threats ).every(
( threat: { error?: string } ) => Boolean( threat.error )
);
const isThreatLevelError = Object.values( data.threats ).every( threat => 'error' in threat );

// Handle a threat level error
if ( isThreatLevelError ) {
Expand Down
58 changes: 28 additions & 30 deletions projects/plugins/protect/src/js/types/fixers.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
export type FixerStatus = 'not_started' | 'in_progress' | 'fixed' | 'not_fixed';

// Discriminated union for top-level error
export type FixersStatusTopLevelError = {
ok: false; // Discriminator for overall failure
error: string; // When `ok` is false, top-level error is required
/**
* Threat Fix Status
*
* Individual fixer status for a threat.
*/
export type ThreatFixStatusError = {
error: string;
};

// Discriminated union for threat-level errors
export type FixersStatusThreatError = {
ok: true; // Discriminator for overall success
threats: {
[ key: number ]: ThreatFixError; // At least one threat has an error
};
export type ThreatFixStatusSuccess = {
status: FixerStatus;
last_updated: string;
};

// Discriminated union for success scenario
export type FixersStatusSuccess = {
ok: true; // Discriminator for overall success
threats: {
[ key: number ]: ThreatFixStatusSuccess; // Threats with successful statuses
};
};
export type ThreatFixStatus = ThreatFixStatusError | ThreatFixStatusSuccess;

// Union type for fixers status (top-level or threat-level error, or success)
export type FixersStatus =
| FixersStatusTopLevelError
| FixersStatusThreatError
| FixersStatusSuccess;
/**
* Fixers Status
*
* Overall status of all fixers.
*/
type FixersStatusBase = {
ok: boolean; // Discriminator for overall success
};

// Threat-level error (discriminated)
export type ThreatFixError = {
error: string; // Discriminator for threat-level error
export type FixersStatusError = FixersStatusBase & {
ok: false;
error: string;
};

// Threat-level success (discriminated)
export type ThreatFixStatusSuccess = {
status: FixerStatus; // Threat fix status (one of 'not_started', 'in_progress', etc.)
last_updated: string; // Last updated timestamp
export type FixersStatusSuccess = FixersStatusBase & {
ok: true;
threats: {
[ key: number ]: ThreatFixStatus;
};
};

export type ThreatFixStatus = ThreatFixError | ThreatFixStatusSuccess;
export type FixersStatus = FixersStatusSuccess | FixersStatusError;

0 comments on commit a2c7b37

Please sign in to comment.