@stlib/guard
v1.0.1
Published
Guardian library for node.js
Downloads
2
Maintainers
Readme
@stlib/guard
Table of contents
About
@stlib/guard - is a library which provides guards to node.js. You simply set metadata somewhere, and checks for existing of this metadata somewhere else to deny access to something.
Getting started
Prerequisites
- yarn
npm i -g yarn
orcorepack enable
[!IMPORTANT] Node.js 18.x+ version must be installed in your OS.
Installation
$ yarn add @stlib/guard
Usage
- Set metadata:
import { Guardian } from '@stlib/guard';
Guardian.setMetadata('Key', ['some', 'data']);
// Also you can remove metadata
Guardian.removeMetadata('Key');
- Use guard somewhere in the code
import { Guardian, MetaGuard } from '@stlib/guard';
Guardian.useGuard(new MetaGuard('Key'));
Guardian.useGuard(new MetaGuard('NotExistingKey')); // Will throw MetaException
// Also you can get all metas
MetaGuard.getMetas();
Creating your own Guards and Exceptions
Example:
import { Guard, GuardException } from '@stlib/guard';
export class NewGuard extends Guard {
constructor(key: string) {
super(key);
}
protected checkData {
const metas = Guard.getMetas();
if (metas.has(this.metaKey)) {
throw new NewException('Bad metadata exists');
}
}
}
export class NewException extends GuardException {
readonly code: number = 400;
constructor(message: string, options?: { [key: string]: string | number }) {
super(message, options);
}
}
[!NOTE] You can provide any personal implementations you want
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Changelog
Project changes are writen in changelog, see the CHANGELOG.md.
We use SemVer for versioning. For the versions available, see the tags on this repository.
For the supported and unsupported versions, see the SECURITY.md.
Authors
License
This project is licensed under the MIT License - see the LICENSE.md