Skip to content

Commit

Permalink
feat(common): injectable decorator factory
Browse files Browse the repository at this point in the history
fix #120
  • Loading branch information
WonderPanda committed Mar 28, 2020
1 parent 8a9d6cb commit 42b2f34
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A collection of Badass modules and utilities to help you level up your NestJS ap

</span>

| Project | Description | Version | Changelog |
| Package | Description | Version | Changelog |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [`@golevelup/nestjs-common`](./packages/common) | Common types, mixins | [![version](https://img.shields.io/npm/v/@golevelup/nestjs-common.svg)](https://www.npmjs.com/package/@golevelup/nestjs-common) | [changelog](./packages/common/CHANGELOG.md) |
| [`@golevelup/nestjs-discovery`](./packages/discovery) | `DiscoveryModule` for finding providers, controllers and method handlers from your NestJS app that have certain metadata | [![version](https://img.shields.io/npm/v/@golevelup/nestjs-discovery.svg)](https://www.npmjs.com/package/@golevelup/nestjs-discovery) | [changelog](./packages/discovery/CHANGELOG.md) |
Expand Down
7 changes: 7 additions & 0 deletions packages/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

Utility functions and low level reusable modules that provide building blocks for the @levelup-nestjs and @nestjs ecosystem.

## Inject Decorator Factory (makeInjectableDecorator)

Creates a decorator that can be used as a convenience to inject a specific token

Instead of using `@Inject(SOME_THING_TOKEN)` this can be used to create a new named Decorator such as `@InjectSomeThing()` which will hide the token details from users making APIs easier
to consume

## Mixins

The mixin pattern is particularly useful with NestJS components like `Interceptors` as a mechanism to provide both configuration while still allowing the component to participate with Nest's `Dependency Injection`.
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './injectDecoratorFactory';
export * from './mixins';
export * from './options';
14 changes: 14 additions & 0 deletions packages/common/src/injectDecoratorFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Inject } from '@nestjs/common';

/**
* Creates a decorator that can be used as a convenience to inject a specific token
*
* Instead of using @Inject(SOME_THING_TOKEN) this can be used to create a new named Decorator
* such as @InjectSomeThing() which will hide the token details from users making APIs easier
* to consume
* @param token
*/
export const makeInjectableDecorator = (
token: string | symbol
): (() => ParameterDecorator) => () => (target, key, descriptor) =>
Inject(token)(target, key, descriptor);
1 change: 1 addition & 0 deletions packages/hasura/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"access": "public"
},
"dependencies": {
"@golevelup/nestjs-common": "^1.3.1",
"@golevelup/nestjs-discovery": "^2.3.0",
"@golevelup/nestjs-modules": "^0.4.0"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/hasura/src/hasura.decorators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { makeInjectableDecorator } from '@golevelup/nestjs-common';
import { SetMetadata } from '@nestjs/common';
import { HASURA_EVENT_HANDLER } from './hasura.constants';
import { HASURA_EVENT_HANDLER, HASURA_MODULE_CONFIG } from './hasura.constants';
import { HasuraEventHandlerConfig } from './hasura.interfaces';

export const HasuraEventHandler = (config: HasuraEventHandlerConfig) => (
target,
key,
descriptor
) => SetMetadata(HASURA_EVENT_HANDLER, config)(target, key, descriptor);

export const InjectHasuraConfig = makeInjectableDecorator(HASURA_MODULE_CONFIG);
11 changes: 3 additions & 8 deletions packages/hasura/src/hasura.event-handler.guard.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
CanActivate,
ExecutionContext,
Inject,
Injectable,
} from '@nestjs/common';
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Request } from 'express';
import { Observable } from 'rxjs';
import { HASURA_MODULE_CONFIG } from './hasura.constants';
import { InjectHasuraConfig } from './hasura.decorators';
import { HasuraModuleConfig } from './hasura.interfaces';

@Injectable()
export class HasuraEventHandlerHeaderGuard implements CanActivate {
private readonly apiSecret: string;
constructor(
@Inject(HASURA_MODULE_CONFIG)
@InjectHasuraConfig()
private readonly hasuraConfig: HasuraModuleConfig
) {
this.apiSecret =
Expand Down
4 changes: 2 additions & 2 deletions packages/hasura/src/hasura.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { DiscoveryModule, DiscoveryService } from '@golevelup/nestjs-discovery';
import { createConfigurableDynamicRootModule } from '@golevelup/nestjs-modules';
import {
BadRequestException,
Inject,
Logger,
Module,
OnModuleInit,
} from '@nestjs/common';
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
import { flatten, groupBy } from 'lodash';
import { HASURA_EVENT_HANDLER, HASURA_MODULE_CONFIG } from './hasura.constants';
import { InjectHasuraConfig } from './hasura.decorators';
import { EventHandlerController } from './hasura.event-handler.controller';
import { HasuraEventHandlerHeaderGuard } from './hasura.event-handler.guard';
import { EventHandlerService } from './hasura.event-handler.service';
Expand All @@ -36,7 +36,7 @@ export class HasuraModule
constructor(
private readonly discover: DiscoveryService,
private readonly externalContextCreator: ExternalContextCreator,
@Inject(HASURA_MODULE_CONFIG)
@InjectHasuraConfig()
private readonly hasuraModuleConfig: HasuraModuleConfig
) {
super();
Expand Down
2 changes: 1 addition & 1 deletion packages/hasura/src/tests/hasura.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ describe('Hasura Module (e2e)', () => {
.set(secretHeader, secret)
.send(eventPayload)
.expect(202)
.then((x) => expect(eventHandlerFn).toHaveBeenCalledTimes(1));
.then(() => expect(eventHandlerFn).toHaveBeenCalledTimes(1));
});
});
1 change: 1 addition & 0 deletions packages/rabbitmq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"url": "https://github.com/golevelup/nestjs/issues"
},
"dependencies": {
"@golevelup/nestjs-common": "^1.3.1",
"@golevelup/nestjs-discovery": "^2.3.0",
"@golevelup/nestjs-modules": "^0.4.0",
"amqp-connection-manager": "^3.0.0",
Expand Down
7 changes: 6 additions & 1 deletion packages/rabbitmq/src/rabbitmq.decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeInjectableDecorator } from '@golevelup/nestjs-common';
import { SetMetadata } from '@nestjs/common';
import { RABBIT_HANDLER } from './rabbitmq.constants';
import { RABBIT_CONFIG_TOKEN, RABBIT_HANDLER } from './rabbitmq.constants';
import { RabbitHandlerConfig } from './rabbitmq.interfaces';

export const makeRabbitDecorator = <T extends Partial<RabbitHandlerConfig>>(
Expand All @@ -18,3 +19,7 @@ export const RabbitHandler = (config: RabbitHandlerConfig) => (
export const RabbitSubscribe = makeRabbitDecorator({ type: 'subscribe' });

export const RabbitRPC = makeRabbitDecorator({ type: 'rpc' });

export const InjectRabbitMQConfig = makeInjectableDecorator(
RABBIT_CONFIG_TOKEN
);

0 comments on commit 42b2f34

Please sign in to comment.