npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nest-lab/or-guard

v2.4.1

Published

A mixin guard that allows for one of N guards to pass for the request to be considers authenticated.

Downloads

20,075

Readme

@nest-lab/or-guard

This library contains a two guards that allows for checking multiple guards and creating complex logical statements based on the results of those guards for if the request should be completed or not.

Installation

Pick your favorite package manager and install. Should be straight forward.

npm i @nest-lab/or-guard
yarn add @nest-lab/or-guard
pnpm i @nest-lab/or-guard

OrGuard

To use the OrGuard, there are a couple of things that need to happen, due to how the guard resolves the guards it's going to be using.

First, make sure to add all the guards the OrGuard will be using to the current module's providers array. Enhancer in Nest are just specialized providers after all. This will allow the OrGuard to use a ModuleRef to get these guards. The guards can either be registered directly as providers, or set up as custom providers and you may use an injection token reference. Make sure, that if you use a custom provider, the instance of the guard is what is tied to the token, not the reference to the class.

Second, make sure none of these guards are REQUEST or TRANSIENT scoped, as this will make the OrGuard throw an error.

Third, make use of it! The OrGuard takes in an array of guard to use for the first parameter, and an optional second parameter for options as described below.

important: for Nest v7, use @nest-lab/[email protected], for Nest v8, please use v2

OrGuard(guards: Array<Type<CanActivate> | InjectionToken>, orGuardOptions?: OrGuardOptions): CanActivate
  • guards: an array of guards or injection tokens for the OrGuard to resolve and test
  • orGuardOptions: an optional object with properties to modify how the OrGuard functions
interface OrGuardOptions {
  throwOnFirstError?: boolean;
}
  • throwOnFirstError: a boolean to tell the OrGuard whether to throw if an error is encountered or if the error should be considered a return false. The default value is false. If this is set to true, the first error encountered will lead to the same error being thrown.

Note: guards are ran in a non-deterministic order. All guard returns are transformed into Observables and ran concurrently to ensure the fastest response time possible.

AndGuard

Just like the OrGuard, you can create a logic grouping of situations that should pass. This is Nest's default when there are multiple guards passed to the @UseGuards() decorator; however, there are situations where it would be useful to use an AndGuard inside of an OrGuard to be able to create logic like (A && B) || C. With using an AndGuard inside of an OrGuard, you'll most likely want to create a dedicated custom provider for the guard like so:

{
  provide: AndGuardToken,
  useClass: AndGuard([GuardA, GuardB])
}

With this added to the module's providers where you plan to use the related OrGuard you can use the following in a controller or resolve:

@UseGuards(OrGuard([AndGuardToken, GuardC]))

And this library will set up the handling of the logic for (GuardA && GuardB) || GuardC without having to worry about the complexities under the hood.

AndGuard(guards: Array<Type<CanActivate> | InjectionToken>, andGuardOptions?: AndGuardOptions): CanActivate
  • guards: an array of guards or injection tokens for the AndGuard to resolve and test
  • andGuardOptions: an optional object with properties to modify how the AndGuard functions
interface AndGuardOptions {
  // immediately stop all other guards and throw an error
  throwOnFirstError?: boolean;
  // run the guards in order they are declared in the array rather than in parallel
  sequential?: boolean;
}

Local Development

Feel free to pull down the repository and work locally. If any changes are made, please make sure tests are added to ensure the functionality works and nothing is broken.

Running unit tests

Run nx test or-guard to execute the unit tests via Jest.