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

@magnetmlm/common

v3.2.39

Published

### Файл app.module.ts ```import { CacheModule, Global, Module } from '@nestjs/common'; import { AuthModule as MainAuthModule } from '@magnetmlm/common';

Downloads

527

Readme

1. Подключение

Файл app.module.ts

import { AuthModule as MainAuthModule } from '@magnetmlm/common';

@Global()
@Module({
  imports: [
    CacheModule.register(),
    MainAuthModule,
    ...
    ],
  providers: [
    ...
    LoggerService,
    { useExisting: ConfigService, provide: 'ConfigService' },
  ],
  exports: [..., 'ConfigService', CacheModule],
})
export class AppModule {}

Только в основном проекте (указаны только нужные для этой библиотеки зависимости)

user.module.ts

import { UserService } from './user.service';
import { EndpointSigStrategy } from '@magnetmlm/common';

@Global()
@Module({
  providers: [
   ...
    { provide: 'UserService', useExisting: UserService },
    EndpointSigStrategy,
  ],
  exports: [..., 'UserService'],
})
export class UserModule {}

2. Роли

Наличие роли проверяется в RolesGuard из roles.guard.ts Чтобы в проекте использовать роли нужно повесить UseGuards декоратор на контроллер, только после этого в ендпоинтах можно будет проверять роль:

user.controller.ts

import {
  JwtAuthGuard, RolesGuard
} from '@magnetmlm/common';

@UseGuards(JwtAuthGuard, new RolesGuard(new Reflector()))
export class UserController {...}

Чтобы для определенного ендпоинта проверялась роль, надо на него повесить декоратор с нужной ролью:

import {
  SetRoles,
  UserRole,
} from '@magnetmlm/common';

  @Post('/grant-role')
  @SetRoles(UserRole.Admin)
  [...other decorators]
  async grantRoles(@Body() body: GrantRoleDto): Promise<void> {
    return this.userService.grantRoles(body);
  }

3. Обязательная подпись для любого ендпоинта

Чтобы повесить обязательную подпись на ендпоинт, нужно повесить на ендпоинт стратегию endpointSignature (чтобы можно было добавлять подпись в свагере - @ApiHeader({ name: 'signature' }))

import {
  CommonAuthStrategies,
} from '@magnetmlm/common';

@Controller('user')
@ApiTags('User')
@UseGuards(JwtAuthGuard, new RolesGuard(new Reflector()))
@ApiBearerAuth()
export class UserController {

  @Post('/grant-role')
  @UseGuards(AuthGuard(CommonAuthStrategies.endpointSignature))
  @ApiHeader({ name: 'signature' })
  async grantRoles(@Body() body: GrantRoleDto): Promise<void> {
    return this.userService.grantRoles(body);
  }
}

По дефолту при подключении библиотеки уже есть ендпоинт /sign/endpoint-message (реализация в auth.service.ts) который отправляет сообщение для подписи для конкретного ендпоинта. В данном случае передаваемые параметры будут такими: method: MethodType.POST path: 'user/grant-role' Сообщение нужно подписать через метамаск и результат передать в хедер signature