Skip to content

Commit

Permalink
Protect: Convert useProtectData to TypeScript (#39778)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller authored Oct 15, 2024
1 parent 3e271cf commit 98c584f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix expected types returned from API response


4 changes: 2 additions & 2 deletions projects/plugins/protect/src/js/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ const API = {
data: { step_ids: stepIds },
} ),

getScanHistory: (): Promise< ScanStatus > =>
getScanHistory: (): Promise< ScanStatus | false > =>
apiFetch( {
path: 'jetpack-protect/v1/scan-history',
method: 'GET',
} ).then( camelize ) as Promise< ScanStatus >,
} ).then( camelize ),

scan: () =>
apiFetch( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { useQuery, UseQueryResult } from '@tanstack/react-query';
import camelize from 'camelize';
import API from '../../api';
import { QUERY_HISTORY_KEY } from '../../constants';
import { ScanStatus } from '../../types/scans';

/**
* Use History Query
*
* @return {UseQueryResult} useQuery result.
*/
export default function useHistoryQuery(): UseQueryResult {
export default function useHistoryQuery(): UseQueryResult< ScanStatus | false > {
const { isRegistered } = useConnection( {
autoTrigger: false,
from: 'protect',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { useMemo } from 'react';
import useHistoryQuery from '../../data/scan/use-history-query';
import useScanStatusQuery from '../../data/scan/use-scan-status-query';
import useProductDataQuery from '../../data/use-product-data-query';
import { ExtensionStatus } from '../../types/scans';
import { Threat, ThreatStatus } from '../../types/threats';

type ThreatFilterKey = 'all' | 'core' | 'files' | 'database' | string;

type Filter = { key: ThreatFilterKey; status: ThreatStatus | 'all' };

// Valid "key" values for filtering.
const KEY_FILTERS = [ 'all', 'core', 'plugins', 'themes', 'files', 'database' ];
Expand All @@ -12,13 +18,13 @@ const KEY_FILTERS = [ 'all', 'core', 'plugins', 'themes', 'files', 'database' ];
*
* @param {Array} threats - The threats to filter.
* @param {object} filter - The filter to apply to the data.
* @param {string} filter.status - The status to filter: 'all', 'fixed', or 'ignored'.
* @param {string} filter.status - The status to filter: 'all', 'current', 'fixed', or 'ignored'.
* @param {string} filter.key - The key to filter: 'all', 'core', 'files', 'database', or an extension name.
* @param {string} key - The threat's key: 'all', 'core', 'files', 'database', or an extension name.
*
* @return {Array} The filtered threats.
*/
const filterThreats = ( threats, filter, key ) => {
const filterThreats = ( threats: Threat[], filter: Filter, key: ThreatFilterKey ): Threat[] => {
if ( ! Array.isArray( threats ) ) {
return [];
}
Expand Down Expand Up @@ -92,7 +98,7 @@ export default function useProtectData(
};

// Loop through the provided extensions, and update the result object.
const processExtensions = ( extensions, key ) => {
const processExtensions = ( extensions: Array< ExtensionStatus >, key: ThreatFilterKey ) => {
if ( ! Array.isArray( extensions ) ) {
return [];
}
Expand All @@ -103,7 +109,7 @@ export default function useProtectData(

// Filter the extension's threats based on the current filters.
const filteredThreats = filterThreats(
extension?.threats,
extension?.threats || [],
filter,
KEY_FILTERS.includes( filter.key ) ? key : extension?.name
);
Expand All @@ -118,7 +124,7 @@ export default function useProtectData(
};

// Loop through the provided threats, and update the result object.
const processThreats = ( threatsToProcess, key ) => {
const processThreats = ( threatsToProcess: Threat[], key: ThreatFilterKey ) => {
if ( ! Array.isArray( threatsToProcess ) ) {
return [];
}
Expand All @@ -135,7 +141,7 @@ export default function useProtectData(

// Core data may be either a single object or an array of multiple objects.
let cores = Array.isArray( data.core ) ? data.core : [];
if ( data.core.threats ) {
if ( data?.core?.threats ) {
cores = [ data.core ];
}

Expand Down

0 comments on commit 98c584f

Please sign in to comment.