Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
skunight committed Dec 6, 2018
0 parents commit f84377f
Show file tree
Hide file tree
Showing 31 changed files with 933 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# upload
uploads/

.DS_Store
.gulp-cache
src/test
.vscode
logs/
*.thrift
*.sql
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 KnowYourself

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

# Nestjs Rabbitmq
Rabbitmq component for NestJs.


### Installation

**Yarn**
```bash
yarn add nestjs-rabbitmq
```

**NPM**
```bash
npm install nestjs-rabbitmq --save
```

### Getting Started
Let's import the RabbitMqModule in `app.module.ts`

```typescript
import { Module } from '@nestjs/common';
import { RabbitMqModule} from 'nestjs-rabbitmq'

@Module({
imports: [
RabbitMqModule.forRoot(options)
],
})
export class AppModule {}
```
With Async
```typescript
import { Module } from '@nestjs/common';
import { RabbitMqModule} from 'nestjs-rabbitmq'

@Module({
imports: [
RabbitMqModule.forRootAsync({
useFactory: (configService: ConfigService) => configService.get('rabbitmq'), // or use async method
//useFactory: async (configService: ConfigService) => configService.get('rabbitmq'),
inject:[ConfigService]
}),
],
})
export class AppModule {}
```
Options
```typescript
interface RabbitMqModuleOptions {
user?: string, //Default guest
passwd?: string, //Default guest
host?: string, //Default localhost
port?: number //Default 5672
}
```
That's it!
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './rabbitmq.interface';
export * from './rabbitmq.module';
export * from './rabbitmq.service';
7 changes: 7 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./rabbitmq.module"));
__export(require("./rabbitmq.service"));
12 changes: 12 additions & 0 deletions dist/rabbitmq-client.provider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RabbitMqModuleOptions, RabbitMqModuleAsyncOptions } from "./rabbitmq.interface";
import * as amqp from 'amqp-connection-manager';
export declare const createClient: () => {
provide: symbol;
useFactory: (options: RabbitMqModuleOptions) => amqp.AmqpConnectionManager;
inject: symbol[];
};
export declare const createAsyncClientOptions: (options: RabbitMqModuleAsyncOptions) => {
provide: symbol;
useFactory: (...args: any[]) => RabbitMqModuleOptions | Promise<RabbitMqModuleOptions>;
inject: any[];
};
16 changes: 16 additions & 0 deletions dist/rabbitmq-client.provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rabbitmq_constants_1 = require("./rabbitmq.constants");
const amqp = require("amqp-connection-manager");
exports.createClient = () => ({
provide: rabbitmq_constants_1.RABBITMQ_CLIENT,
useFactory: (options) => {
return amqp.connect([`amqp://${options.user || 'guest'}:${options.passwd || 'guest'}@${options.host || 'localhost'}:${options.port || 5672}`]);
},
inject: [rabbitmq_constants_1.RABBITMQ_MODULE_OPTIONS]
});
exports.createAsyncClientOptions = (options) => ({
provide: rabbitmq_constants_1.RABBITMQ_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject
});
2 changes: 2 additions & 0 deletions dist/rabbitmq.constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const RABBITMQ_MODULE_OPTIONS: unique symbol;
export declare const RABBITMQ_CLIENT: unique symbol;
4 changes: 4 additions & 0 deletions dist/rabbitmq.constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RABBITMQ_MODULE_OPTIONS = Symbol('RABBITMQ_MODULE_OPTIONS');
exports.RABBITMQ_CLIENT = Symbol('RABBITMQ_CLIENT');
11 changes: 11 additions & 0 deletions dist/rabbitmq.interface.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ModuleMetadata } from "@nestjs/common/interfaces";
export interface RabbitMqModuleOptions {
user?: string;
passwd?: string;
host?: string;
port?: number;
}
export interface RabbitMqModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RabbitMqModuleOptions | Promise<RabbitMqModuleOptions>;
inject?: any[];
}
2 changes: 2 additions & 0 deletions dist/rabbitmq.interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
6 changes: 6 additions & 0 deletions dist/rabbitmq.module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DynamicModule } from '@nestjs/common';
import { RabbitMqModuleOptions, RabbitMqModuleAsyncOptions } from './rabbitmq.interface';
export declare class RabbitMqModule {
static forRoot(options: RabbitMqModuleOptions): DynamicModule;
static forRootSync(options: RabbitMqModuleAsyncOptions): DynamicModule;
}
46 changes: 46 additions & 0 deletions dist/rabbitmq.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var RabbitMqModule_1;
const common_1 = require("@nestjs/common");
const rabbitmq_constants_1 = require("./rabbitmq.constants");
const rabbitmq_service_1 = require("./rabbitmq.service");
const rabbitmq_client_provider_1 = require("./rabbitmq-client.provider");
let RabbitMqModule = RabbitMqModule_1 = class RabbitMqModule {
static forRoot(options) {
return {
module: RabbitMqModule_1,
providers: [
rabbitmq_client_provider_1.createClient(),
{
provide: rabbitmq_constants_1.RABBITMQ_MODULE_OPTIONS,
useValue: options
}
],
exports: [rabbitmq_service_1.RabbitMqService]
};
}
static forRootSync(options) {
return {
module: RabbitMqModule_1,
providers: [
rabbitmq_client_provider_1.createClient(),
rabbitmq_client_provider_1.createAsyncClientOptions(options),
],
exports: [rabbitmq_service_1.RabbitMqService]
};
}
};
RabbitMqModule = RabbitMqModule_1 = __decorate([
common_1.Global(),
common_1.Module({
providers: [rabbitmq_service_1.RabbitMqService],
exports: [rabbitmq_service_1.RabbitMqService]
})
], RabbitMqModule);
exports.RabbitMqModule = RabbitMqModule;
6 changes: 6 additions & 0 deletions dist/rabbitmq.service.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as amqp from 'amqp-connection-manager';
export declare class RabbitMqService {
private readonly client;
constructor(client: amqp.AmqpConnectionManager);
getClient(): amqp.AmqpConnectionManager;
}
31 changes: 31 additions & 0 deletions dist/rabbitmq.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const rabbitmq_constants_1 = require("./rabbitmq.constants");
const amqp = require("amqp-connection-manager");
let RabbitMqService = class RabbitMqService {
constructor(client) {
this.client = client;
}
getClient() {
return this.client;
}
};
RabbitMqService = __decorate([
common_1.Injectable(),
__param(0, common_1.Inject(rabbitmq_constants_1.RABBITMQ_CLIENT)),
__metadata("design:paramtypes", [Object])
], RabbitMqService);
exports.RabbitMqService = RabbitMqService;
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist";
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("./dist"));
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist'
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './rabbitmq.interface'
export * from './rabbitmq.module'
export * from './rabbitmq.service'
16 changes: 16 additions & 0 deletions lib/rabbitmq-client.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RabbitMqModuleOptions, RabbitMqModuleAsyncOptions } from "./rabbitmq.interface";
import { RABBITMQ_CLIENT, RABBITMQ_MODULE_OPTIONS } from './rabbitmq.constants';
import * as amqp from 'amqp-connection-manager'
export const createClient = () => ({
provide: RABBITMQ_CLIENT,
useFactory:(options: RabbitMqModuleOptions) => {
return amqp.connect([`amqp://${options.user || 'guest'}:${options.passwd || 'guest'}@${options.host || 'localhost'}:${options.port || 5672}`])
},
inject:[RABBITMQ_MODULE_OPTIONS]
})

export const createAsyncClientOptions = (options: RabbitMqModuleAsyncOptions) => ({
provide: RABBITMQ_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject
})
2 changes: 2 additions & 0 deletions lib/rabbitmq.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const RABBITMQ_MODULE_OPTIONS = Symbol('RABBITMQ_MODULE_OPTIONS')
export const RABBITMQ_CLIENT = Symbol('RABBITMQ_CLIENT')
13 changes: 13 additions & 0 deletions lib/rabbitmq.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ModuleMetadata } from "@nestjs/common/interfaces";

export interface RabbitMqModuleOptions {
user?: string,
passwd?: string,
host?: string,
port?: number
}

export interface RabbitMqModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RabbitMqModuleOptions | Promise<RabbitMqModuleOptions>,
inject?: any[];
}
Loading

0 comments on commit f84377f

Please sign in to comment.