Skip to content

Commit

Permalink
Fix build bugs, issues with types
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarsh867 committed Nov 5, 2023
1 parent 4d07a5e commit c2722ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
12 changes: 6 additions & 6 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import "reflect-metadata";
import { ArgsDictionary, ResolverData } from "type-graphql";
export interface ResolverDataWithArgs<TArgsType extends ArgsDictionary, TContextType = {}> extends ResolverData<TContextType> {
export interface ResolverDataWithArgs<TArgsType extends ArgsDictionary, TContextType extends object = {}> extends ResolverData<TContextType> {
args: TArgsType;
context: TContextType;
}
export declare type Rule<TContextType = {}, TArgsType extends ArgsDictionary = {}> = (D: ResolverDataWithArgs<TArgsType, TContextType>) => boolean | Promise<boolean>;
export declare type Rules<TContextType = {}> = Rule<TContextType> | RuleObject<TContextType> | Rules<TContextType>[];
export interface RuleObject<TContextType = {}> {
export type Rule<TContextType extends object = {}, TArgsType extends ArgsDictionary = {}> = (D: ResolverDataWithArgs<TArgsType, TContextType>) => boolean | Promise<boolean>;
export type Rules<TContextType extends object = {}> = Rule<TContextType> | RuleObject<TContextType> | Rules<TContextType>[];
export interface RuleObject<TContextType extends object = {}> {
OR?: Rules<TContextType>[];
AND?: Rules<TContextType>[];
NOT?: Rules<TContextType>[];
}
export declare type AuthCheckerFn<TContextType = {}, TRoleType = Rules<TContextType>> = (resolverData: ResolverData<TContextType>, roles: TRoleType) => boolean | Promise<boolean>;
export declare type FAuthChecker<TContextType = {}, TRoleType = Rules<TContextType>> = AuthCheckerFn<TContextType, TRoleType>;
export type AuthCheckerFn<TContextType extends object = {}, TRoleType = Rules<TContextType>> = (resolverData: ResolverData<TContextType>, roles: TRoleType) => boolean | Promise<boolean>;
export type FAuthChecker<TContextType extends object = {}, TRoleType = Rules<TContextType>> = AuthCheckerFn<TContextType, TRoleType>;
export declare const authResolver: FAuthChecker;
declare const authChecker: FAuthChecker;
export default authChecker;
10 changes: 5 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ const authResolver = ({ root, args, context, info }, rules) => __awaiter(void 0,
}
else if (isRulesArray(rules)) {
return yield rules.reduce((acc, rule) => __awaiter(void 0, void 0, void 0, function* () {
return (yield exports.authResolver({ root, args, context, info }, rule)) && acc;
return (yield (0, exports.authResolver)({ root, args, context, info }, rule)) && acc;
}), Promise.resolve(true));
}
else if (isRulesObject(rules)) {
const andRules = rules.AND
? yield rules.AND.reduce((andAcc, rule) => __awaiter(void 0, void 0, void 0, function* () {
return ((yield exports.authResolver({ root, args, context, info }, rule)) &&
return ((yield (0, exports.authResolver)({ root, args, context, info }, rule)) &&
andAcc);
}), Promise.resolve(true))
: true;
const notRules = rules.NOT
? yield rules.NOT.reduce((notAcc, rule) => __awaiter(void 0, void 0, void 0, function* () {
return (!(yield exports.authResolver({ root, args, context, info }, rule)) &&
return (!(yield (0, exports.authResolver)({ root, args, context, info }, rule)) &&
notAcc);
}), Promise.resolve(true))
: true;
const orRules = rules.OR
? yield rules.OR.reduce((andAcc, rule) => __awaiter(void 0, void 0, void 0, function* () {
return ((yield exports.authResolver({ root, args, context, info }, rule)) ||
return ((yield (0, exports.authResolver)({ root, args, context, info }, rule)) ||
andAcc);
}), Promise.resolve(false))
: true;
Expand All @@ -71,6 +71,6 @@ const authResolver = ({ root, args, context, info }, rules) => __awaiter(void 0,
});
exports.authResolver = authResolver;
const authChecker = ({ root, args, context, info }, rules) => __awaiter(void 0, void 0, void 0, function* () {
return yield exports.authResolver({ root, args, context, info }, rules);
return yield (0, exports.authResolver)({ root, args, context, info }, rules);
});
exports.default = authChecker;
60 changes: 30 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ import { ArgsDictionary, ResolverData } from "type-graphql";

export interface ResolverDataWithArgs<
TArgsType extends ArgsDictionary,
TContextType = {}
TContextType extends object = {}
> extends ResolverData<TContextType> {
args: TArgsType;
context: TContextType;
}

export type Rule<TContextType = {}, TArgsType extends ArgsDictionary = {}> = (
export type Rule<TContextType extends object = {}, TArgsType extends ArgsDictionary = {}> = (
D: ResolverDataWithArgs<TArgsType, TContextType>
) => boolean | Promise<boolean>;

export type Rules<TContextType = {}> =
export type Rules<TContextType extends object = {}> =
| Rule<TContextType>
| RuleObject<TContextType>
| Rules<TContextType>[];

export interface RuleObject<TContextType = {}> {
export interface RuleObject<TContextType extends object = {}> {
OR?: Rules<TContextType>[];
AND?: Rules<TContextType>[];
NOT?: Rules<TContextType>[];
}

export type AuthCheckerFn<
TContextType = {},
TContextType extends object = {},
TRoleType = Rules<TContextType>
> = (
resolverData: ResolverData<TContextType>,
roles: TRoleType
) => boolean | Promise<boolean>;

export type FAuthChecker<
TContextType = {},
TContextType extends object = {},
TRoleType = Rules<TContextType>
> = AuthCheckerFn<TContextType, TRoleType>;

Expand Down Expand Up @@ -78,36 +78,36 @@ export const authResolver: FAuthChecker = async (
} else if (isRulesObject(rules)) {
const andRules = rules.AND
? await rules.AND.reduce<boolean | Promise<boolean>>(
async (andAcc, rule) => {
return (
(await authResolver({ root, args, context, info }, rule)) &&
andAcc
);
},
Promise.resolve(true)
)
async (andAcc, rule) => {
return (
(await authResolver({ root, args, context, info }, rule)) &&
andAcc
);
},
Promise.resolve(true)
)
: true;
const notRules = rules.NOT
? await rules.NOT.reduce<boolean | Promise<boolean>>(
async (notAcc, rule) => {
return (
!(await authResolver({ root, args, context, info }, rule)) &&
notAcc
);
},
Promise.resolve(true)
)
async (notAcc, rule) => {
return (
!(await authResolver({ root, args, context, info }, rule)) &&
notAcc
);
},
Promise.resolve(true)
)
: true;
const orRules = rules.OR
? await rules.OR.reduce<boolean | Promise<boolean>>(
async (andAcc, rule) => {
return (
(await authResolver({ root, args, context, info }, rule)) ||
andAcc
);
},
Promise.resolve(false)
)
async (andAcc, rule) => {
return (
(await authResolver({ root, args, context, info }, rule)) ||
andAcc
);
},
Promise.resolve(false)
)
: true;
return andRules && orRules && notRules;
}
Expand Down

0 comments on commit c2722ec

Please sign in to comment.