mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-28 07:09:23 +00:00

# Description of Changes Currently, the `tsconfig.json` file enforces strict type checking, but nothing in CI checks that the code is actually correctly typed. [Vite only transpiles TypeScript code](https://vite.dev/guide/features.html#transpile-only) so doesn't ensure that the TS code we're running is correct. This PR adds running of the type checker to CI and fixes the type errors that have already crept into the codebase. Note that many of the changes I've made to 'fix the types' are just using `any` to disable the type checker because the code is under too much churn to fix anything properly at the moment. I still think enabling the type checker now is the best course of action though because otherwise we'll never be able to fix all of them, and it should at least help us not break things when adding new code. Co-authored-by: James <james@crosscourtanalytics.com>
87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import { useCallback, useRef } from 'react';
|
|
import axios, { CancelTokenSource } from 'axios';
|
|
import { processResponse, ResponseHandler } from '../../../utils/toolResponseProcessor';
|
|
import type { ProcessingProgress } from './useToolState';
|
|
|
|
export interface ApiCallsConfig<TParams = void> {
|
|
endpoint: string | ((params: TParams) => string);
|
|
buildFormData: (file: File, params: TParams) => FormData;
|
|
filePrefix: string;
|
|
responseHandler?: ResponseHandler;
|
|
}
|
|
|
|
export const useToolApiCalls = <TParams = void>() => {
|
|
const cancelTokenRef = useRef<CancelTokenSource | null>(null);
|
|
|
|
const processFiles = useCallback(async (
|
|
params: TParams,
|
|
validFiles: File[],
|
|
config: ApiCallsConfig<TParams>,
|
|
onProgress: (progress: ProcessingProgress) => void,
|
|
onStatus: (status: string) => void
|
|
): Promise<File[]> => {
|
|
const processedFiles: File[] = [];
|
|
const failedFiles: string[] = [];
|
|
const total = validFiles.length;
|
|
|
|
// Create cancel token for this operation
|
|
cancelTokenRef.current = axios.CancelToken.source();
|
|
|
|
for (let i = 0; i < validFiles.length; i++) {
|
|
const file = validFiles[i];
|
|
|
|
onProgress({ current: i + 1, total, currentFileName: file.name });
|
|
onStatus(`Processing ${file.name} (${i + 1}/${total})`);
|
|
|
|
try {
|
|
const formData = config.buildFormData(file, params);
|
|
const endpoint = typeof config.endpoint === 'function' ? config.endpoint(params) : config.endpoint;
|
|
const response = await axios.post(endpoint, formData, {
|
|
responseType: 'blob',
|
|
cancelToken: cancelTokenRef.current.token,
|
|
});
|
|
|
|
// Forward to shared response processor (uses tool-specific responseHandler if provided)
|
|
const responseFiles = await processResponse(
|
|
response.data,
|
|
[file],
|
|
config.filePrefix,
|
|
config.responseHandler
|
|
);
|
|
processedFiles.push(...responseFiles);
|
|
|
|
} catch (error) {
|
|
if (axios.isCancel(error)) {
|
|
throw new Error('Operation was cancelled');
|
|
}
|
|
console.error(`Failed to process ${file.name}:`, error);
|
|
failedFiles.push(file.name);
|
|
}
|
|
}
|
|
|
|
if (failedFiles.length > 0 && processedFiles.length === 0) {
|
|
throw new Error(`Failed to process all files: ${failedFiles.join(', ')}`);
|
|
}
|
|
|
|
if (failedFiles.length > 0) {
|
|
onStatus(`Processed ${processedFiles.length}/${total} files. Failed: ${failedFiles.join(', ')}`);
|
|
} else {
|
|
onStatus(`Successfully processed ${processedFiles.length} file${processedFiles.length === 1 ? '' : 's'}`);
|
|
}
|
|
|
|
return processedFiles;
|
|
}, []);
|
|
|
|
const cancelOperation = useCallback(() => {
|
|
if (cancelTokenRef.current) {
|
|
cancelTokenRef.current.cancel('Operation cancelled by user');
|
|
cancelTokenRef.current = null;
|
|
}
|
|
}, []);
|
|
|
|
return {
|
|
processFiles,
|
|
cancelOperation,
|
|
};
|
|
};
|