Giter Club home page Giter Club logo

Comments (5)

cmanrique-surtec avatar cmanrique-surtec commented on June 16, 2024 2

Use middleware

function Access(value) {
	return (req, res, next) => {
		// your logic
	}
}
@Post("/", [Access('CAN_POST_PERMISSION')])
getPost(){
  return "POST ALLOWED"
}

from node-decorators.

serhiisol avatar serhiisol commented on June 16, 2024

I believe you can, you can wrap original function with custom and add extra logic on top, something like:

function Access(rule: string) {
  return (target: object, key: string, descriptor: any) => {
    const handler = descriptor.value;

    descriptor.value = (...args) => {
      if (ALLOWED_RULES.includes(rule) {
        return handler.apply(target, args);
      }
      
      throw new Unauthorized();
    };

    return descriptor;
  };
}

from node-decorators.

vikaskandari avatar vikaskandari commented on June 16, 2024

but how do i access the request and response objects in the access decorator factory ? so i can directory send the response or use next function if rule exists

from node-decorators.

vikaskandari avatar vikaskandari commented on June 16, 2024

Currently i am using middleware method but i wanted to use decorators to keep code neat ,

Currently i have two decorators in my mind

Validate(DtoClass) this decorator will take class-validator as input and validate the request and send error if validation fails

and

Access("PERMISSION_NAME") this will validate the request if user have permission to access this api or not

from node-decorators.

serhiisol avatar serhiisol commented on June 16, 2024

Probably there must be a customDecorator function to create a decorator. I'll take a look.

from node-decorators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.