Skip to content

Commit

Permalink
fix: gets rid of dist imported types which cannot be resolved at runt…
Browse files Browse the repository at this point in the history
…ime (#895)
  • Loading branch information
stalniy authored Mar 27, 2024
1 parent 095bea5 commit 29f2d90
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/casl-mongoose/src/accessibleBy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AnyMongoAbility, Generics, SubjectType } from '@casl/ability';
import { ToAbilityTypes } from '@casl/ability/dist/types/types';
import { AnyMongoAbility, Generics, SubjectType, Abilities, AbilityTuple, ExtractSubjectType } from '@casl/ability';
import { rulesToQuery } from '@casl/ability/extra';

function convertToMongoQuery(rule: AnyMongoAbility['rules'][number]) {
Expand All @@ -23,12 +22,16 @@ export class AccessibleRecords<T extends SubjectType> {
}
}

type SubjectTypes<T extends Abilities> = T extends AbilityTuple
? ExtractSubjectType<T[1]>
: never;

/**
* Returns accessible records Mongo query per record type (i.e., entity type) based on provided Ability and action.
*/
export function accessibleBy<T extends AnyMongoAbility>(
ability: T,
action: Parameters<T['rulesFor']>[0] = 'read'
): AccessibleRecords<ToAbilityTypes<Generics<T>['abilities']>[1]> {
): AccessibleRecords<SubjectTypes<Generics<T>['abilities']>> {
return new AccessibleRecords(ability, action);
}

0 comments on commit 29f2d90

Please sign in to comment.