Skip to content

Commit be57f11

Browse files
authored
Improve type safety of tool definitions (Stirling-Tools#6895)
# Description of Changes Followup work requested in review of Stirling-Tools#6867. Currently, there is nothing enforcing that the endpoint chosen in the tool config is the correct mapping for `toApiParams`, so theoretically it's possible for a tool to be set up to call an endpoint with the wrong API params for it. There's also nothing currently enforcing that `toApiParams` and `fromApiParams` are compatible with each other (using the same types). This PR changes it so that instead of creating the config object directly, tools create it via a generic function, which enforces that all of the relevant mappings are using compatible types.
1 parent 8ba8f69 commit be57f11

46 files changed

Lines changed: 239 additions & 249 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

frontend/editor/src/core/components/tools/addPageNumbers/useAddPageNumbersOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
43
useToolOperation,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -73,15 +73,14 @@ export const buildAddPageNumbersFormData = (
7373
): FormData =>
7474
objectToFormData(addPageNumbersToApiParams(parameters), { fileInput: file });
7575

76-
export const addPageNumbersOperationConfig = {
77-
toolType: ToolType.singleFile,
76+
export const addPageNumbersOperationConfig = defineSingleFileTool({
7877
buildFormData: buildAddPageNumbersFormData,
7978
toApiParams: addPageNumbersToApiParams,
8079
fromApiParams: addPageNumbersFromApiParams,
8180
operationType: "addPageNumbers",
8281
endpoint: ENDPOINT,
8382
defaultParameters,
84-
} as const;
83+
});
8584

8685
export const useAddPageNumbersOperation = () => {
8786
const { t } = useTranslation();

frontend/editor/src/core/components/tools/addStamp/useAddStampOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
43
useToolOperation,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -87,15 +87,14 @@ export const buildAddStampFormData = (
8787
: { fileInput: file },
8888
);
8989

90-
export const addStampOperationConfig = {
91-
toolType: ToolType.singleFile,
90+
export const addStampOperationConfig = defineSingleFileTool({
9291
buildFormData: buildAddStampFormData,
9392
toApiParams: addStampToApiParams,
9493
fromApiParams: addStampFromApiParams,
9594
operationType: "addStamp",
9695
endpoint: ENDPOINT,
9796
defaultParameters,
98-
} as const;
97+
});
9998

10099
export const useAddStampOperation = () => {
101100
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/addAttachments/useAddAttachmentsOperation.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
33
useToolOperation,
4-
ToolOperationConfig,
5-
ToolType,
4+
defineSingleFileTool,
65
} from "@app/hooks/tools/shared/useToolOperation";
76
import {
87
objectToFormData,
@@ -48,16 +47,14 @@ const buildFormData = (
4847
});
4948

5049
// Operation configuration for automation
51-
export const addAttachmentsOperationConfig: ToolOperationConfig<AddAttachmentsParameters> =
52-
{
53-
toolType: ToolType.singleFile,
54-
buildFormData,
55-
toApiParams: addAttachmentsToApiParams,
56-
fromApiParams: addAttachmentsFromApiParams,
57-
operationType: "addAttachments",
58-
endpoint: ENDPOINT,
59-
defaultParameters: DEFAULT_ADD_ATTACHMENTS_PARAMETERS,
60-
};
50+
export const addAttachmentsOperationConfig = defineSingleFileTool({
51+
buildFormData,
52+
toApiParams: addAttachmentsToApiParams,
53+
fromApiParams: addAttachmentsFromApiParams,
54+
operationType: "addAttachments",
55+
endpoint: ENDPOINT,
56+
defaultParameters: DEFAULT_ADD_ATTACHMENTS_PARAMETERS,
57+
});
6158

6259
export const useAddAttachmentsOperation = () => {
6360
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/addPassword/useAddPasswordOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
43
useToolOperation,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -85,15 +85,14 @@ const fullDefaultParameters: AddPasswordFullParameters = {
8585
};
8686

8787
// Static configuration object
88-
export const addPasswordOperationConfig = {
89-
toolType: ToolType.singleFile,
88+
export const addPasswordOperationConfig = defineSingleFileTool({
9089
buildFormData: buildAddPasswordFormData,
9190
toApiParams: addPasswordToApiParams,
9291
fromApiParams: addPasswordFromApiParams,
9392
operationType: "addPassword",
9493
endpoint: ENDPOINT,
9594
defaultParameters: fullDefaultParameters,
96-
} as const;
95+
});
9796

9897
export const useAddPasswordOperation = () => {
9998
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/addWatermark/useAddWatermarkOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
43
useToolOperation,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -87,15 +87,14 @@ export const buildAddWatermarkFormData = (
8787
);
8888

8989
// Static configuration object
90-
export const addWatermarkOperationConfig = {
91-
toolType: ToolType.singleFile,
90+
export const addWatermarkOperationConfig = defineSingleFileTool({
9291
buildFormData: buildAddWatermarkFormData,
9392
toApiParams: addWatermarkToApiParams,
9493
fromApiParams: addWatermarkFromApiParams,
9594
operationType: "watermark",
9695
endpoint: ENDPOINT,
9796
defaultParameters,
98-
} as const;
97+
});
9998

10099
export const useAddWatermarkOperation = () => {
101100
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/adjustContrast/useAdjustContrastOperation.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
3+
defineCustomTool,
44
useToolOperation,
55
CustomProcessorResult,
66
} from "@app/hooks/tools/shared/useToolOperation";
@@ -195,14 +195,11 @@ async function processPdfClientSide(
195195
};
196196
}
197197

198-
export const adjustContrastOperationConfig = {
199-
toolType: ToolType.custom,
198+
export const adjustContrastOperationConfig = defineCustomTool({
200199
customProcessor: processPdfClientSide,
201200
operationType: "adjustContrast",
202201
defaultParameters,
203-
settingsComponentPath:
204-
"components/tools/adjustContrast/AdjustContrastSingleStepSettings",
205-
} as const;
202+
});
206203

207204
export const useAdjustContrastOperation = () => {
208205
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/adjustPageScale/useAdjustPageScaleOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
33
useToolOperation,
4-
ToolType,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import { createStandardErrorHandler } from "@app/utils/toolErrorHandler";
77
import {
@@ -21,15 +21,14 @@ export {
2121
adjustPageScaleFromApiParams,
2222
};
2323

24-
export const adjustPageScaleOperationConfig = {
25-
toolType: ToolType.singleFile,
24+
export const adjustPageScaleOperationConfig = defineSingleFileTool({
2625
buildFormData: buildAdjustPageScaleFormData,
2726
toApiParams: adjustPageScaleToApiParams,
2827
fromApiParams: adjustPageScaleFromApiParams,
2928
operationType: "scalePages",
3029
endpoint: ADJUST_PAGE_SCALE_ENDPOINT,
3130
defaultParameters,
32-
} as const;
31+
});
3332

3433
export const useAdjustPageScaleOperation = () => {
3534
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/autoRename/useAutoRenameOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
3-
ToolType,
43
useToolOperation,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -44,16 +44,15 @@ export const buildAutoRenameFormData = (
4444
objectToFormData(autoRenameToApiParams(parameters), { fileInput: file });
4545

4646
// Static configuration object
47-
export const autoRenameOperationConfig = {
48-
toolType: ToolType.singleFile,
47+
export const autoRenameOperationConfig = defineSingleFileTool({
4948
buildFormData: buildAutoRenameFormData,
5049
toApiParams: autoRenameToApiParams,
5150
fromApiParams: autoRenameFromApiParams,
5251
operationType: "autoRename",
5352
endpoint: ENDPOINT,
5453
preserveBackendFilename: true, // Use filename from backend response headers
5554
defaultParameters,
56-
} as const;
55+
});
5756

5857
export const useAutoRenameOperation = () => {
5958
const { t } = useTranslation();

frontend/editor/src/core/hooks/tools/automate/useAutomateOperation.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
ToolType,
2+
defineCustomTool,
33
useToolOperation,
44
} from "@app/hooks/tools/shared/useToolOperation";
55
import { useCallback } from "react";
@@ -55,10 +55,11 @@ export function useAutomateOperation() {
5555
[toolRegistry],
5656
);
5757

58-
return useToolOperation<AutomateParameters>({
59-
toolType: ToolType.custom,
60-
operationType: "automate",
61-
customProcessor,
62-
consumesAllInputs: true,
63-
});
58+
return useToolOperation<AutomateParameters>(
59+
defineCustomTool({
60+
operationType: "automate",
61+
customProcessor,
62+
consumesAllInputs: true,
63+
}),
64+
);
6465
}

frontend/editor/src/core/hooks/tools/bookletImposition/useBookletImpositionOperation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTranslation } from "react-i18next";
22
import {
33
useToolOperation,
4-
ToolType,
4+
defineSingleFileTool,
55
} from "@app/hooks/tools/shared/useToolOperation";
66
import {
77
objectToFormData,
@@ -59,15 +59,14 @@ export const buildBookletImpositionFormData = (
5959
});
6060

6161
// Static configuration object
62-
export const bookletImpositionOperationConfig = {
63-
toolType: ToolType.singleFile,
62+
export const bookletImpositionOperationConfig = defineSingleFileTool({
6463
buildFormData: buildBookletImpositionFormData,
6564
toApiParams: bookletImpositionToApiParams,
6665
fromApiParams: bookletImpositionFromApiParams,
6766
operationType: "bookletImposition",
6867
endpoint: ENDPOINT,
6968
defaultParameters,
70-
} as const;
69+
});
7170

7271
export const useBookletImpositionOperation = () => {
7372
const { t } = useTranslation();

0 commit comments

Comments
 (0)