mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-23 03:56:20 +00:00

# Description of Changes The `FileId` type in V2 currently is just defined to be a string. This makes it really easy to accidentally pass strings into things accepting file IDs (such as file names). This PR makes the `FileId` type [an opaque type](https://www.geeksforgeeks.org/typescript/opaque-types-in-typescript/), so it is compatible with things accepting strings (arguably not ideal for this...) but strings are not compatible with it without explicit conversion. The PR also includes changes to use `FileId` consistently throughout the project (everywhere I could find uses of `fileId: string`), so that we have the maximum benefit from the type safety. > [!note] > I've marked quite a few things as `FIX ME` where we're passing names in as IDs. If that is intended behaviour, I'm happy to remove the fix me and insert a cast instead, but they probably need comments explaining why we're using a file name as an ID.
30 lines
939 B
TypeScript
30 lines
939 B
TypeScript
import { FileId } from '../types/file';
|
|
import { FileOperation } from '../types/fileContext';
|
|
|
|
/**
|
|
* Creates operation tracking data for FileContext integration
|
|
*/
|
|
export const createOperation = <TParams = void>(
|
|
operationType: string,
|
|
params: TParams,
|
|
selectedFiles: File[]
|
|
): { operation: FileOperation; operationId: string; fileId: FileId } => {
|
|
const operationId = `${operationType}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
const fileId = selectedFiles.map(f => f.name).join(',') as FileId;
|
|
|
|
const operation: FileOperation = {
|
|
id: operationId,
|
|
type: operationType,
|
|
timestamp: Date.now(),
|
|
fileIds: selectedFiles.map(f => f.name),
|
|
status: 'pending',
|
|
metadata: {
|
|
originalFileName: selectedFiles[0]?.name,
|
|
parameters: params,
|
|
fileSize: selectedFiles.reduce((sum, f) => sum + f.size, 0)
|
|
}
|
|
} as any /* FIX ME*/;
|
|
|
|
return { operation, operationId, fileId };
|
|
};
|