type-graphql
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -1,4 +0,4 @@ | ||
import { ReturnTypeFunc, TypeOptions, DescriptionOptions, ValidateOptions } from "../types/decorators"; | ||
export declare type Options = TypeOptions & DescriptionOptions & ValidateOptions; | ||
import { ReturnTypeFunc, DecoratorTypeOptions, DescriptionOptions, ValidateOptions } from "../types/decorators"; | ||
export declare type Options = DecoratorTypeOptions & DescriptionOptions & ValidateOptions; | ||
export declare function Arg(name: string, options?: Options): ParameterDecorator; | ||
export declare function Arg(name: string, returnTypeFunc: ReturnTypeFunc, options?: Options): ParameterDecorator; |
@@ -5,10 +5,5 @@ "use strict"; | ||
const errors_1 = require("../errors"); | ||
const decorators_1 = require("../helpers/decorators"); | ||
function Authorized(...rolesOrRolesArray) { | ||
let roles; | ||
if (Array.isArray(rolesOrRolesArray[0])) { | ||
roles = rolesOrRolesArray[0]; | ||
} | ||
else { | ||
roles = rolesOrRolesArray; | ||
} | ||
const roles = decorators_1.getArrayFromOverloadedRest(rolesOrRolesArray); | ||
return (prototype, propertyKey) => { | ||
@@ -15,0 +10,0 @@ if (typeof propertyKey === "symbol") { |
export { Arg } from "./Arg"; | ||
export { Args } from "./Args"; | ||
export { ArgsType, ArgsType as GraphQLArgsType } from "./ArgsType"; | ||
export { ArgsType } from "./ArgsType"; | ||
export { Authorized } from "./Authorized"; | ||
@@ -10,11 +10,12 @@ export { Ctx } from "./Ctx"; | ||
export { Info } from "./Info"; | ||
export { InputType, InputType as GraphQLInputType } from "./InputType"; | ||
export { InterfaceType, InterfaceType as GraphQLInterfaceType } from "./InterfaceType"; | ||
export { InputType } from "./InputType"; | ||
export { InterfaceType } from "./InterfaceType"; | ||
export { Mutation } from "./Mutation"; | ||
export { ObjectType, ObjectType as GraphQLObjectType } from "./ObjectType"; | ||
export { ObjectType } from "./ObjectType"; | ||
export { PubSub } from "./PubSub"; | ||
export { Query } from "./Query"; | ||
export { Resolver, Resolver as GraphQLResolver } from "./Resolver"; | ||
export { Resolver } from "./Resolver"; | ||
export { Root } from "./Root"; | ||
export { Subscription } from "./Subscription"; | ||
export { createUnionType } from "./unions"; | ||
export { UseMiddleware } from "./UseMiddleware"; |
@@ -7,6 +7,4 @@ "use strict"; | ||
exports.Args = Args_1.Args; | ||
// export { ArgsType } from "./ArgsType"; | ||
var ArgsType_1 = require("./ArgsType"); | ||
exports.ArgsType = ArgsType_1.ArgsType; | ||
exports.GraphQLArgsType = ArgsType_1.ArgsType; | ||
var Authorized_1 = require("./Authorized"); | ||
@@ -24,16 +22,10 @@ exports.Authorized = Authorized_1.Authorized; | ||
exports.Info = Info_1.Info; | ||
// export { InputType } from "./InputType"; | ||
var InputType_1 = require("./InputType"); | ||
exports.InputType = InputType_1.InputType; | ||
exports.GraphQLInputType = InputType_1.InputType; | ||
// export { InterfaceType } from "./InterfaceType"; | ||
var InterfaceType_1 = require("./InterfaceType"); | ||
exports.InterfaceType = InterfaceType_1.InterfaceType; | ||
exports.GraphQLInterfaceType = InterfaceType_1.InterfaceType; | ||
var Mutation_1 = require("./Mutation"); | ||
exports.Mutation = Mutation_1.Mutation; | ||
// export { ObjectType } from "./ObjectType"; | ||
var ObjectType_1 = require("./ObjectType"); | ||
exports.ObjectType = ObjectType_1.ObjectType; | ||
exports.GraphQLObjectType = ObjectType_1.ObjectType; | ||
var PubSub_1 = require("./PubSub"); | ||
@@ -43,6 +35,4 @@ exports.PubSub = PubSub_1.PubSub; | ||
exports.Query = Query_1.Query; | ||
// export { Resolver } from "./Resolver"; | ||
var Resolver_1 = require("./Resolver"); | ||
exports.Resolver = Resolver_1.Resolver; | ||
exports.GraphQLResolver = Resolver_1.Resolver; | ||
var Root_1 = require("./Root"); | ||
@@ -54,1 +44,3 @@ exports.Root = Root_1.Root; | ||
exports.createUnionType = unions_1.createUnionType; | ||
var UseMiddleware_1 = require("./UseMiddleware"); | ||
exports.UseMiddleware = UseMiddleware_1.UseMiddleware; |
@@ -14,1 +14,2 @@ import { TypeOptions, ReturnTypeFunc, DescriptionOptions } from "../types/decorators"; | ||
}; | ||
export declare function getArrayFromOverloadedRest<T>(overloadedArray: Array<T | T[]>): T[]; |
@@ -31,1 +31,12 @@ "use strict"; | ||
exports.getNameDecoratorParams = getNameDecoratorParams; | ||
function getArrayFromOverloadedRest(overloadedArray) { | ||
let items; | ||
if (Array.isArray(overloadedArray[0])) { | ||
items = overloadedArray[0]; | ||
} | ||
else { | ||
items = overloadedArray; | ||
} | ||
return items; | ||
} | ||
exports.getArrayFromOverloadedRest = getArrayFromOverloadedRest; |
@@ -7,3 +7,3 @@ export * from "./decorators"; | ||
export { useContainer } from "./utils/container"; | ||
export { AuthChecker, ActionData, FilterActionData } from "./types"; | ||
export { ActionData, FilterActionData } from "./types"; | ||
export { PubSubEngine } from "graphql-subscriptions"; |
@@ -0,2 +1,4 @@ | ||
export { MiddlewareFn, NextFn, MiddlewareInterface } from "./Middleware"; | ||
export * from "./auth-checker"; | ||
export * from "./Publisher"; | ||
export * from "./ResolverInterface"; |
import { ParamMetadata } from "./param-metadata"; | ||
import { TypeValueThunk, TypeOptions } from "../../types/decorators"; | ||
import { Middleware } from "../../interfaces/Middleware"; | ||
export interface FieldMetadata { | ||
@@ -12,2 +13,3 @@ target: Function; | ||
roles?: string[]; | ||
middlewares?: Array<Middleware<any>>; | ||
} |
@@ -5,4 +5,5 @@ export * from "./authorized-metadata"; | ||
export * from "./field-metadata"; | ||
export * from "./middleware-metadata"; | ||
export * from "./param-metadata"; | ||
export * from "./resolver-metadata"; | ||
export * from "./union-metadata"; |
import { TypeValueThunk, TypeOptions, ClassTypeResolver, SubscriptionFilterFunc } from "../../types/decorators"; | ||
import { ParamMetadata } from "./param-metadata"; | ||
import { Middleware } from "../../interfaces/Middleware"; | ||
export interface BaseResolverMetadata { | ||
@@ -9,2 +10,3 @@ methodName: string; | ||
roles?: string[]; | ||
middlewares?: Array<Middleware<any>>; | ||
} | ||
@@ -11,0 +13,0 @@ export interface ResolverMetadata extends BaseResolverMetadata { |
@@ -1,2 +0,2 @@ | ||
import { ResolverMetadata, ClassMetadata, FieldMetadata, ParamMetadata, FieldResolverMetadata, AuthorizedMetadata, EnumMetadata, UnionMetadata, UnionMetadataWithSymbol, ResolverClassMetadata, SubscriptionResolverMetadata } from "./definitions"; | ||
import { ResolverMetadata, ClassMetadata, FieldMetadata, ParamMetadata, FieldResolverMetadata, AuthorizedMetadata, EnumMetadata, UnionMetadata, UnionMetadataWithSymbol, ResolverClassMetadata, SubscriptionResolverMetadata, MiddlewareMetadata } from "./definitions"; | ||
export declare abstract class MetadataStorage { | ||
@@ -14,2 +14,3 @@ static queries: ResolverMetadata[]; | ||
static unions: UnionMetadataWithSymbol[]; | ||
static middlewares: MiddlewareMetadata[]; | ||
private static resolvers; | ||
@@ -29,2 +30,3 @@ private static fields; | ||
static collectUnionMetadata(definition: UnionMetadata): symbol; | ||
static collectMiddlewareMetadata(definition: MiddlewareMetadata): void; | ||
static collectResolverClassMetadata(definition: ResolverClassMetadata): void; | ||
@@ -39,2 +41,3 @@ static collectClassFieldMetadata(definition: FieldMetadata): void; | ||
private static findFieldRoles(target, fieldName); | ||
private static mapMetadatasToMiddlewares(metadata); | ||
} |
@@ -39,2 +39,5 @@ "use strict"; | ||
} | ||
static collectMiddlewareMetadata(definition) { | ||
this.middlewares.push(definition); | ||
} | ||
static collectResolverClassMetadata(definition) { | ||
@@ -72,2 +75,3 @@ this.resolvers.push(definition); | ||
this.unions = []; | ||
this.middlewares = []; | ||
this.resolvers = []; | ||
@@ -83,2 +87,3 @@ this.fields = []; | ||
field.params = this.params.filter(param => param.target === field.target && field.name === param.methodName); | ||
field.middlewares = this.mapMetadatasToMiddlewares(this.middlewares.filter(middleware => middleware.target === field.target && middleware.fieldName === field.name)); | ||
if (field.params.length === 0) { | ||
@@ -99,2 +104,3 @@ // no params = try to get params from field resolver | ||
def.roles = this.findFieldRoles(def.target, def.methodName); | ||
def.middlewares = this.mapMetadatasToMiddlewares(this.middlewares.filter(middleware => middleware.target === def.target && def.methodName === middleware.fieldName)); | ||
}); | ||
@@ -119,2 +125,7 @@ } | ||
} | ||
static mapMetadatasToMiddlewares(metadata) { | ||
return metadata | ||
.map(m => m.middlewares) | ||
.reduce((middlewares, resultArray) => resultArray.concat(middlewares), []); | ||
} | ||
} | ||
@@ -132,2 +143,3 @@ MetadataStorage.queries = []; | ||
MetadataStorage.unions = []; | ||
MetadataStorage.middlewares = []; | ||
MetadataStorage.resolvers = []; | ||
@@ -134,0 +146,0 @@ MetadataStorage.fields = []; |
{ | ||
"name": "type-graphql", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"main": "./index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -17,8 +17,11 @@ "use strict"; | ||
const targetInstance = container_1.IOCContainer.getInstance(resolverMetadata.target); | ||
const { validate: globalValidate, authChecker, pubSub } = build_context_1.BuildContext; | ||
const { validate: globalValidate, authChecker, pubSub, globalMiddlewares } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(resolverMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, resolverMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
const actionData = { root, args, context, info }; | ||
yield helpers_1.checkForAccess(actionData, authChecker, resolverMetadata.roles); | ||
const params = yield helpers_1.getParams(resolverMetadata.params, actionData, globalValidate, pubSub); | ||
return resolverMetadata.handler.apply(targetInstance, params); | ||
return helpers_1.applyMiddlewares(actionData, middlewares, () => __awaiter(this, void 0, void 0, function* () { | ||
const params = yield helpers_1.getParams(resolverMetadata.params, actionData, globalValidate, pubSub); | ||
return resolverMetadata.handler.apply(targetInstance, params); | ||
})); | ||
}); | ||
@@ -32,14 +35,17 @@ } | ||
const targetType = fieldResolverMetadata.getParentType(); | ||
const { validate: globalValidate, authChecker, pubSub } = build_context_1.BuildContext; | ||
const { validate: globalValidate, authChecker, pubSub, globalMiddlewares } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(fieldResolverMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, fieldResolverMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
const actionData = { root, args, context, info }; | ||
yield helpers_1.checkForAccess(actionData, authChecker, fieldResolverMetadata.roles); | ||
const targetInstance = types_1.convertToType(targetType, root); | ||
// method | ||
if (fieldResolverMetadata.handler) { | ||
const params = yield helpers_1.getParams(fieldResolverMetadata.params, actionData, globalValidate, pubSub); | ||
return fieldResolverMetadata.handler.apply(targetInstance, params); | ||
} | ||
// getter | ||
return targetInstance[fieldResolverMetadata.methodName]; | ||
return helpers_1.applyMiddlewares(actionData, middlewares, () => __awaiter(this, void 0, void 0, function* () { | ||
// method | ||
if (fieldResolverMetadata.handler) { | ||
const params = yield helpers_1.getParams(fieldResolverMetadata.params, actionData, globalValidate, pubSub); | ||
return fieldResolverMetadata.handler.apply(targetInstance, params); | ||
} | ||
// getter | ||
return targetInstance[fieldResolverMetadata.methodName]; | ||
})); | ||
}); | ||
@@ -49,9 +55,10 @@ } | ||
function createSimpleFieldResolver(fieldMetadata) { | ||
const authChecker = build_context_1.BuildContext.authChecker; | ||
const { authChecker, globalMiddlewares } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(fieldMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, fieldMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
const actionData = { root, args, context, info }; | ||
yield helpers_1.checkForAccess(actionData, authChecker, fieldMetadata.roles); | ||
return root[fieldMetadata.name]; | ||
return yield helpers_1.applyMiddlewares(actionData, middlewares, () => root[fieldMetadata.name]); | ||
}); | ||
} | ||
exports.createSimpleFieldResolver = createSimpleFieldResolver; |
import { PubSubEngine } from "graphql-subscriptions"; | ||
import { ValidatorOptions } from "class-validator"; | ||
import { ParamMetadata } from "../metadata/definitions"; | ||
import { AuthChecker, ActionData } from "../types"; | ||
import { AuthChecker } from "../interfaces"; | ||
import { ActionData } from "../types"; | ||
import { Middleware } from "../interfaces/Middleware"; | ||
export declare function getParams(params: ParamMetadata[], {root, args, context, info}: ActionData<any>, globalValidate: boolean | ValidatorOptions, pubSub: PubSubEngine): Promise<any[]>; | ||
export declare function checkForAccess(action: ActionData, authChecker?: AuthChecker<any>, roles?: string[]): Promise<void>; | ||
export declare function applyAuthChecker(middlewares: Array<Middleware<any>>, authChecker?: AuthChecker, roles?: string[]): void; | ||
export declare function applyMiddlewares(actionData: ActionData<any>, middlewares: Array<Middleware<any>>, resolverHandlerFunction: () => any): Promise<any>; |
@@ -13,3 +13,4 @@ "use strict"; | ||
const validate_arg_1 = require("./validate-arg"); | ||
const errors_1 = require("../errors"); | ||
const container_1 = require("../utils/container"); | ||
const auth_middleware_1 = require("../helpers/auth-middleware"); | ||
function getParams(params, { root, args, context, info }, globalValidate, pubSub) { | ||
@@ -46,12 +47,43 @@ return __awaiter(this, void 0, void 0, function* () { | ||
exports.getParams = getParams; | ||
function checkForAccess(action, authChecker, roles) { | ||
function applyAuthChecker(middlewares, authChecker, roles) { | ||
if (authChecker && roles) { | ||
middlewares.unshift(auth_middleware_1.AuthMiddleware(authChecker, roles)); | ||
} | ||
} | ||
exports.applyAuthChecker = applyAuthChecker; | ||
function applyMiddlewares(actionData, middlewares, resolverHandlerFunction) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (roles && authChecker) { | ||
const accessGranted = yield authChecker(action, roles); | ||
if (!accessGranted) { | ||
throw roles.length === 0 ? new errors_1.UnauthorizedError() : new errors_1.ForbiddenError(); | ||
} | ||
let middlewaresIndex = -1; | ||
function dispatchHandler(currentIndex) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (currentIndex <= middlewaresIndex) { | ||
throw new Error("next() called multiple times"); | ||
} | ||
middlewaresIndex = currentIndex; | ||
let handlerFn; | ||
if (currentIndex === middlewares.length) { | ||
handlerFn = resolverHandlerFunction; | ||
} | ||
else { | ||
const currentMiddleware = middlewares[currentIndex]; | ||
// arrow function or class | ||
if (currentMiddleware.prototype !== undefined) { | ||
const middlewareClassInstance = container_1.IOCContainer.getInstance(currentMiddleware); | ||
handlerFn = middlewareClassInstance.use.bind(middlewareClassInstance); | ||
} | ||
else { | ||
handlerFn = currentMiddleware; | ||
} | ||
} | ||
let nextResult; | ||
const result = yield handlerFn(actionData, () => __awaiter(this, void 0, void 0, function* () { | ||
nextResult = yield dispatchHandler(currentIndex + 1); | ||
return nextResult; | ||
})); | ||
return result !== undefined ? result : nextResult; | ||
}); | ||
} | ||
return dispatchHandler(0); | ||
}); | ||
} | ||
exports.checkForAccess = checkForAccess; | ||
exports.applyMiddlewares = applyMiddlewares; |
import { GraphQLScalarType } from "graphql"; | ||
import { ValidatorOptions } from "class-validator"; | ||
import { PubSubEngine, PubSubOptions } from "graphql-subscriptions"; | ||
import { AuthChecker } from "../types/auth-checker"; | ||
import { AuthChecker } from "../interfaces/auth-checker"; | ||
import { Middleware } from "../interfaces/Middleware"; | ||
export declare type DateScalarMode = "isoDate" | "timestamp"; | ||
@@ -20,2 +21,3 @@ export interface ScalarsTypeMap { | ||
pubSub?: PubSubEngine | PubSubOptions; | ||
globalMiddlewares?: Array<Middleware<any>>; | ||
} | ||
@@ -28,2 +30,3 @@ export declare abstract class BuildContext { | ||
static pubSub: PubSubEngine; | ||
static globalMiddlewares: Array<Middleware<any>>; | ||
/** | ||
@@ -30,0 +33,0 @@ * Set static fields with current building context data |
@@ -29,2 +29,5 @@ "use strict"; | ||
} | ||
if (options.globalMiddlewares) { | ||
this.globalMiddlewares = options.globalMiddlewares; | ||
} | ||
} | ||
@@ -40,2 +43,3 @@ /** | ||
this.pubSub = new graphql_subscriptions_1.PubSub(); | ||
this.globalMiddlewares = []; | ||
} | ||
@@ -42,0 +46,0 @@ } |
@@ -10,10 +10,8 @@ import { GraphQLScalarType } from "graphql"; | ||
export declare type SubscriptionFilterFunc = (actionData: FilterActionData<any, any, any>) => boolean | Promise<boolean>; | ||
export interface TypeOptions { | ||
/** | ||
* Used to annotate the type as an Array (TS reflection system limitation) | ||
* @deprecated use array syntax `type => [ItemType]` notation instead | ||
*/ | ||
array?: boolean; | ||
export interface DecoratorTypeOptions { | ||
nullable?: boolean; | ||
} | ||
export interface TypeOptions extends DecoratorTypeOptions { | ||
array?: boolean; | ||
} | ||
export interface DescriptionOptions { | ||
@@ -28,3 +26,3 @@ description?: string; | ||
} | ||
export declare type BasicOptions = TypeOptions & DescriptionOptions; | ||
export declare type BasicOptions = DecoratorTypeOptions & DescriptionOptions; | ||
export declare type AdvancedOptions = BasicOptions & DepreciationOptions; | ||
@@ -31,0 +29,0 @@ export interface ClassType<T = any> { |
export * from "./action-data"; | ||
export * from "./auth-checker"; | ||
export * from "./filter-action-data"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111894
144
2326