Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the folder patch operation type into a discriminated union #138

Closed
JiriLojda opened this issue Feb 2, 2024 · 1 comment · Fixed by #142
Closed

Change the folder patch operation type into a discriminated union #138

JiriLojda opened this issue Feb 2, 2024 · 1 comment · Fixed by #142
Assignees

Comments

@JiriLojda
Copy link
Member

Motivation

When I want to modify asset folders, I have to specify patch operations. The SDK provides the AssetFolderModels.IModifyAssetFoldersData type for the operations. The problem is that this type doesn't guide me as to what properties are needed for different patch operations. I always have to check with the docs for what kind of properties I have to provide. It is unnecessarily difficult and opens up opportunities for mistakes. For example, when removing a folder I only have to specify the reference property, but when renaming, I also have to specify the value property.
Properties also can have different types based on the operation type. For example, when renaming a folder, the value property has to be of type string (the new name), but when adding a folder, the same property value has to be an object specifying the folder object.
Example:

// ...
.withData([
  {
    op: "addInto",
    value: "new folder name", // there is no error here, even though it is obviously wrong
  },
]);

Proposed solution

Change the AssetFolderModels.IModifyAssetFoldersData to be a discriminated union of available operations. Based on the op property, TypeScript would be able to identify what operation am I writing and would properly type-check them and offer me relevant IntelliSense. That would resolve both of the problems.
The type would look something like this:

export type ModifyAssetFoldersData =
  AddIntoOperation
  | RemoveOperation
  | RenameOperation;

type RemoveOperation = {
  op: "remove";
  // ...
};
// define the rest of the operations
@JiriLojda
Copy link
Member Author

There even is a bug, because the the of the value property is defined as IAddOrModifyAssetFolderData, which is an object with folder properties like name or folders. However, when I want to define the rename patch operation, I need to assign a string (the new name) into the value property (as per the documentation), but the type doesn't allow me to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants