forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inferencer): Ability to transform/ignore fields in inferencer co…
…mponents (refinedev#3173) * refactor: update component naming convention * refactor(inferencer): log relation fetch attempts * chore(examples): update multi tenancy example * feat(inferencer): add `fieldTransformer` prop to inferencers * chore(inferencer): remove unused param * feat(inferencer): try to fetch relation from basic fields * chore: add changeset * chore: update snapshots
- Loading branch information
Showing
21 changed files
with
1,052 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@pankod/refine-inferencer": minor | ||
--- | ||
|
||
- Added `fieldTransformer` prop to inferencer components to let users transform or hide the field to be rendered. | ||
- Hide networks errors caused by the relation detection process. | ||
- Added the ability to detect relations from basic types like `"text"` and `"number"`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/inferencer/src/field-transformers/basic-to-relation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { FieldTransformer, InferField } from "@/types"; | ||
|
||
export const basicToRelation: FieldTransformer = ( | ||
fields, | ||
resources, | ||
resource, | ||
record, | ||
) => { | ||
const mapped: Array<InferField> = fields.map((field) => { | ||
if ( | ||
!field.relation && | ||
(field.type === "text" || | ||
field.type === "richtext" || | ||
field.type === "number") && | ||
!field.canRelation | ||
) { | ||
// check if value is a valid id (regex) | ||
// if multiple, check value by value | ||
// take accessor into account (should be single only) | ||
// valid id should be a valid uuid (meaning, lowercase alphanumeric with dashes) | ||
const validUUIdRegex = /^[a-z0-9-]+$/; | ||
|
||
const isValidUUID = (value: unknown) => { | ||
return validUUIdRegex.test(`${value}`); | ||
}; | ||
|
||
const isNotSelf = field.key.toLowerCase() !== "id"; | ||
|
||
const singleOrNoAccessor = | ||
!field.accessor || typeof field.accessor === "string"; | ||
|
||
// in case of multiple accessors, we can't infer a relation | ||
// or if the field is the id field | ||
if (!singleOrNoAccessor || !isNotSelf) { | ||
return field; | ||
} | ||
|
||
const valuesToCheck = field.multiple | ||
? (record[field.key] as unknown[]) | ||
: [record[field.key]]; | ||
|
||
const allValid = valuesToCheck.every((value) => { | ||
return isValidUUID( | ||
field.accessor | ||
? (value as Record<string, unknown>)[ | ||
field.accessor as string | ||
] | ||
: value, | ||
); | ||
}); | ||
|
||
if (allValid) { | ||
return { | ||
...field, | ||
canRelation: true, | ||
}; | ||
} | ||
|
||
return field; | ||
} | ||
return field; | ||
}); | ||
|
||
return mapped; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.