@rytass/secret-adapter-vault-nestjs 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,7 +23,7 @@ class TestService {
23
23
  }
24
24
 
25
25
  @Module({
26
- imports: [VaultModule],
26
+ imports: [VaultModule.forRoot({ path: '/', isGlobal: true })],
27
27
  providers: [TestService],
28
28
  })
29
29
  class TestModule {};
package/constants.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const VAULT_PATH_TOKEN: symbol;
package/constants.js ADDED
@@ -0,0 +1,3 @@
1
+ const VAULT_PATH_TOKEN = Symbol('VAULT_PATH_TOKEN');
2
+
3
+ export { VAULT_PATH_TOKEN };
package/index.cjs.js CHANGED
@@ -4,47 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var common = require('@nestjs/common');
6
6
  var config = require('@nestjs/config');
7
- var regeneratorRuntime = require('regenerator-runtime');
8
7
  var secretAdapterVault = require('@rytass/secret-adapter-vault');
9
8
 
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
9
+ const VAULT_PATH_TOKEN = Symbol('VAULT_PATH_TOKEN');
11
10
 
12
- var regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(regeneratorRuntime);
13
-
14
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
15
- try {
16
- var info = gen[key](arg);
17
- var value = info.value;
18
- } catch (error) {
19
- reject(error);
20
- return;
21
- }
22
- if (info.done) {
23
- resolve(value);
24
- } else {
25
- Promise.resolve(value).then(_next, _throw);
26
- }
27
- }
28
- function _asyncToGenerator(fn) {
29
- return function() {
30
- var self = this, args = arguments;
31
- return new Promise(function(resolve, reject) {
32
- var gen = fn.apply(self, args);
33
- function _next(value) {
34
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
35
- }
36
- function _throw(err) {
37
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
38
- }
39
- _next(undefined);
40
- });
41
- };
42
- }
43
- function _classCallCheck$1(instance, Constructor) {
44
- if (!(instance instanceof Constructor)) {
45
- throw new TypeError("Cannot call a class as a function");
46
- }
47
- }
48
11
  var __decorate$1 = undefined && undefined.__decorate || function(decorators, target, key, desc) {
49
12
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
50
13
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -54,88 +17,90 @@ var __decorate$1 = undefined && undefined.__decorate || function(decorators, tar
54
17
  var __metadata = undefined && undefined.__metadata || function(k, v) {
55
18
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
56
19
  };
57
- exports.VaultService = /*#__PURE__*/ function() {
58
- function VaultService(config) {
59
- var _this = this;
60
- _classCallCheck$1(this, VaultService);
20
+ var __param = undefined && undefined.__param || function(paramIndex, decorator) {
21
+ return function(target, key) {
22
+ decorator(target, key, paramIndex);
23
+ };
24
+ };
25
+ exports.VaultService = class VaultService {
26
+ constructor(config, path){
61
27
  this.config = config;
62
28
  this.onReadyCallbacks = [];
63
- var host = config.get("VAULT_HOST");
64
- var user = config.get("VAULT_ACCOUNT");
65
- var pass = config.get("VAULT_PASSWORD");
66
- var path = config.get("VAULT_PATH");
29
+ this.fallbackToEnvFile = false;
30
+ const host = config.get('VAULT_HOST');
31
+ const user = config.get('VAULT_ACCOUNT');
32
+ const pass = config.get('VAULT_PASSWORD');
67
33
  this.manager = new secretAdapterVault.VaultSecret(path, {
68
- host: host,
34
+ host,
69
35
  auth: {
70
36
  account: user,
71
37
  password: pass
72
38
  },
73
- onReady: function() {
74
- _this.onReadyCallbacks.forEach(function(done) {
75
- return done();
76
- });
39
+ onError: (err)=>{
40
+ this.fallbackToEnvFile = true;
41
+ this.onReadyCallbacks.forEach((done)=>done(config));
42
+ },
43
+ onReady: ()=>{
44
+ this.onReadyCallbacks.forEach((done)=>done());
77
45
  }
78
46
  });
79
47
  }
80
- var _proto = VaultService.prototype;
81
- _proto.get = function get(key) {
82
- var _this = this;
83
- return _asyncToGenerator(regeneratorRuntime__default.mark(function _callee() {
84
- return regeneratorRuntime__default.wrap(function _callee$(_ctx) {
85
- while(1)switch(_ctx.prev = _ctx.next){
86
- case 0:
87
- if (!(_this.manager.state === secretAdapterVault.VaultSecretState.READY)) {
88
- _ctx.next = 2;
89
- break;
90
- }
91
- return _ctx.abrupt("return", _this.manager.get(key));
92
- case 2:
93
- return _ctx.abrupt("return", new Promise(function(resolve) {
94
- _this.onReadyCallbacks.push(function() {
95
- resolve(_this.manager.get(key));
96
- });
97
- }));
98
- case 3:
99
- case "end":
100
- return _ctx.stop();
101
- }
102
- }, _callee);
103
- }))();
104
- };
105
- return VaultService;
106
- }();
48
+ async get(key) {
49
+ if (this.fallbackToEnvFile) {
50
+ return Promise.resolve(this.config.get(key) || '');
51
+ }
52
+ if (this.manager.state === secretAdapterVault.VaultSecretState.READY) {
53
+ return this.manager.get(key);
54
+ }
55
+ return new Promise((resolve)=>{
56
+ this.onReadyCallbacks.push((dataSource = this.manager)=>{
57
+ resolve(dataSource.get(key));
58
+ });
59
+ });
60
+ }
61
+ };
107
62
  exports.VaultService = __decorate$1([
108
63
  common.Injectable(),
64
+ __param(1, common.Inject(VAULT_PATH_TOKEN)),
109
65
  __metadata("design:type", Function),
110
66
  __metadata("design:paramtypes", [
111
- typeof config.ConfigService === "undefined" ? Object : config.ConfigService
67
+ typeof config.ConfigService === "undefined" ? Object : config.ConfigService,
68
+ String
112
69
  ])
113
70
  ], exports.VaultService);
114
71
 
115
- function _classCallCheck(instance, Constructor) {
116
- if (!(instance instanceof Constructor)) {
117
- throw new TypeError("Cannot call a class as a function");
118
- }
119
- }
120
72
  var __decorate = undefined && undefined.__decorate || function(decorators, target, key, desc) {
121
73
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
122
74
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
123
75
  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;
124
76
  return c > 3 && r && Object.defineProperty(target, key, r), r;
125
77
  };
126
- exports.VaultModule = function VaultModule() {
127
- _classCallCheck(this, VaultModule);
78
+ exports.VaultModule = class VaultModule1 {
79
+ static forRoot(options = {
80
+ path: '/'
81
+ }) {
82
+ return {
83
+ imports: [
84
+ config.ConfigModule.forRoot({
85
+ envFilePath: options.cacheFile
86
+ }),
87
+ ],
88
+ module: exports.VaultModule,
89
+ providers: [
90
+ config.ConfigService,
91
+ {
92
+ provide: VAULT_PATH_TOKEN,
93
+ useValue: options.path
94
+ },
95
+ exports.VaultService,
96
+ ],
97
+ exports: [
98
+ exports.VaultService
99
+ ]
100
+ };
101
+ }
128
102
  };
129
103
  exports.VaultModule = __decorate([
130
- common.Module({
131
- imports: [
132
- config.ConfigModule.forRoot()
133
- ],
134
- providers: [
135
- exports.VaultService
136
- ],
137
- exports: [
138
- exports.VaultService
139
- ]
140
- })
104
+ common.Global(),
105
+ common.Module({})
141
106
  ], exports.VaultModule);
@@ -0,0 +1,4 @@
1
+ export interface VaultModuleOptions {
2
+ path: string;
3
+ cacheFile?: string;
4
+ }
package/module.d.ts CHANGED
@@ -1,2 +1,5 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { VaultModuleOptions } from './interfaces';
1
3
  export declare class VaultModule {
4
+ static forRoot(options?: VaultModuleOptions): DynamicModule;
2
5
  }
package/module.js CHANGED
@@ -1,33 +1,42 @@
1
- import { Module } from '@nestjs/common';
2
- import { ConfigModule } from '@nestjs/config';
1
+ import { Global, Module } from '@nestjs/common';
2
+ import { ConfigModule, ConfigService } from '@nestjs/config';
3
+ import { VAULT_PATH_TOKEN } from './constants.js';
3
4
  import { VaultService } from './service.js';
4
5
 
5
- function _classCallCheck(instance, Constructor) {
6
- if (!(instance instanceof Constructor)) {
7
- throw new TypeError("Cannot call a class as a function");
8
- }
9
- }
10
6
  var __decorate = undefined && undefined.__decorate || function(decorators, target, key, desc) {
11
7
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12
8
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
13
9
  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;
14
10
  return c > 3 && r && Object.defineProperty(target, key, r), r;
15
11
  };
16
- var VaultModule = function VaultModule() {
17
- _classCallCheck(this, VaultModule);
12
+ let VaultModule = class VaultModule1 {
13
+ static forRoot(options = {
14
+ path: '/'
15
+ }) {
16
+ return {
17
+ imports: [
18
+ ConfigModule.forRoot({
19
+ envFilePath: options.cacheFile
20
+ }),
21
+ ],
22
+ module: VaultModule,
23
+ providers: [
24
+ ConfigService,
25
+ {
26
+ provide: VAULT_PATH_TOKEN,
27
+ useValue: options.path
28
+ },
29
+ VaultService,
30
+ ],
31
+ exports: [
32
+ VaultService
33
+ ]
34
+ };
35
+ }
18
36
  };
19
37
  VaultModule = __decorate([
20
- Module({
21
- imports: [
22
- ConfigModule.forRoot()
23
- ],
24
- providers: [
25
- VaultService
26
- ],
27
- exports: [
28
- VaultService
29
- ]
30
- })
38
+ Global(),
39
+ Module({})
31
40
  ], VaultModule);
32
41
 
33
42
  export { VaultModule };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rytass/secret-adapter-vault-nestjs",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "Rytass Sceret Vault nestjs adapter",
5
5
  "keywords": [
6
6
  "rytass",
@@ -24,13 +24,14 @@
24
24
  "reflect-metadata": "^0.1.13"
25
25
  },
26
26
  "dependencies": {
27
- "@rytass/secret-adapter-vault": "^0.1.2",
28
- "regenerator-runtime": "^0.13.9"
27
+ "@rytass/secret-adapter-vault": "^0.2.0",
28
+ "regenerator-runtime": "^0.13.11"
29
29
  },
30
30
  "devDependencies": {
31
- "@nestjs/common": "^9.0.11",
32
- "@nestjs/config": "^2.2.0",
33
- "axios": "^0.27.2",
31
+ "@nestjs/common": "^9.4.1",
32
+ "@nestjs/config": "^2.3.2",
33
+ "@nestjs/core": "^9.4.1",
34
+ "axios": "^1.4.0",
34
35
  "reflect-metadata": "^0.1.13"
35
36
  },
36
37
  "main": "./index.cjs.js",
package/service.d.ts CHANGED
@@ -3,6 +3,7 @@ export declare class VaultService {
3
3
  private readonly config;
4
4
  private readonly manager;
5
5
  private readonly onReadyCallbacks;
6
- constructor(config: ConfigService);
6
+ private fallbackToEnvFile;
7
+ constructor(config: ConfigService, path: string);
7
8
  get<T = string>(key: string): Promise<T>;
8
9
  }
package/service.js CHANGED
@@ -1,42 +1,8 @@
1
- import regeneratorRuntime from 'regenerator-runtime';
2
- import { Injectable } from '@nestjs/common';
1
+ import { Injectable, Inject } from '@nestjs/common';
3
2
  import { ConfigService } from '@nestjs/config';
4
- import { VaultSecretState, VaultSecret } from '@rytass/secret-adapter-vault';
3
+ import { VaultSecret, VaultSecretState } from '@rytass/secret-adapter-vault';
4
+ import { VAULT_PATH_TOKEN } from './constants.js';
5
5
 
6
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
7
- try {
8
- var info = gen[key](arg);
9
- var value = info.value;
10
- } catch (error) {
11
- reject(error);
12
- return;
13
- }
14
- if (info.done) {
15
- resolve(value);
16
- } else {
17
- Promise.resolve(value).then(_next, _throw);
18
- }
19
- }
20
- function _asyncToGenerator(fn) {
21
- return function() {
22
- var self = this, args = arguments;
23
- return new Promise(function(resolve, reject) {
24
- var gen = fn.apply(self, args);
25
- function _next(value) {
26
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
27
- }
28
- function _throw(err) {
29
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
30
- }
31
- _next(undefined);
32
- });
33
- };
34
- }
35
- function _classCallCheck(instance, Constructor) {
36
- if (!(instance instanceof Constructor)) {
37
- throw new TypeError("Cannot call a class as a function");
38
- }
39
- }
40
6
  var __decorate = undefined && undefined.__decorate || function(decorators, target, key, desc) {
41
7
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
42
8
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -46,61 +12,55 @@ var __decorate = undefined && undefined.__decorate || function(decorators, targe
46
12
  var __metadata = undefined && undefined.__metadata || function(k, v) {
47
13
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
48
14
  };
49
- var VaultService = /*#__PURE__*/ function() {
50
- function VaultService(config) {
51
- var _this = this;
52
- _classCallCheck(this, VaultService);
15
+ var __param = undefined && undefined.__param || function(paramIndex, decorator) {
16
+ return function(target, key) {
17
+ decorator(target, key, paramIndex);
18
+ };
19
+ };
20
+ let VaultService = class VaultService {
21
+ constructor(config, path){
53
22
  this.config = config;
54
23
  this.onReadyCallbacks = [];
55
- var host = config.get("VAULT_HOST");
56
- var user = config.get("VAULT_ACCOUNT");
57
- var pass = config.get("VAULT_PASSWORD");
58
- var path = config.get("VAULT_PATH");
24
+ this.fallbackToEnvFile = false;
25
+ const host = config.get('VAULT_HOST');
26
+ const user = config.get('VAULT_ACCOUNT');
27
+ const pass = config.get('VAULT_PASSWORD');
59
28
  this.manager = new VaultSecret(path, {
60
- host: host,
29
+ host,
61
30
  auth: {
62
31
  account: user,
63
32
  password: pass
64
33
  },
65
- onReady: function() {
66
- _this.onReadyCallbacks.forEach(function(done) {
67
- return done();
68
- });
34
+ onError: (err)=>{
35
+ this.fallbackToEnvFile = true;
36
+ this.onReadyCallbacks.forEach((done)=>done(config));
37
+ },
38
+ onReady: ()=>{
39
+ this.onReadyCallbacks.forEach((done)=>done());
69
40
  }
70
41
  });
71
42
  }
72
- var _proto = VaultService.prototype;
73
- _proto.get = function get(key) {
74
- var _this = this;
75
- return _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
76
- return regeneratorRuntime.wrap(function _callee$(_ctx) {
77
- while(1)switch(_ctx.prev = _ctx.next){
78
- case 0:
79
- if (!(_this.manager.state === VaultSecretState.READY)) {
80
- _ctx.next = 2;
81
- break;
82
- }
83
- return _ctx.abrupt("return", _this.manager.get(key));
84
- case 2:
85
- return _ctx.abrupt("return", new Promise(function(resolve) {
86
- _this.onReadyCallbacks.push(function() {
87
- resolve(_this.manager.get(key));
88
- });
89
- }));
90
- case 3:
91
- case "end":
92
- return _ctx.stop();
93
- }
94
- }, _callee);
95
- }))();
96
- };
97
- return VaultService;
98
- }();
43
+ async get(key) {
44
+ if (this.fallbackToEnvFile) {
45
+ return Promise.resolve(this.config.get(key) || '');
46
+ }
47
+ if (this.manager.state === VaultSecretState.READY) {
48
+ return this.manager.get(key);
49
+ }
50
+ return new Promise((resolve)=>{
51
+ this.onReadyCallbacks.push((dataSource = this.manager)=>{
52
+ resolve(dataSource.get(key));
53
+ });
54
+ });
55
+ }
56
+ };
99
57
  VaultService = __decorate([
100
58
  Injectable(),
59
+ __param(1, Inject(VAULT_PATH_TOKEN)),
101
60
  __metadata("design:type", Function),
102
61
  __metadata("design:paramtypes", [
103
- typeof ConfigService === "undefined" ? Object : ConfigService
62
+ typeof ConfigService === "undefined" ? Object : ConfigService,
63
+ String
104
64
  ])
105
65
  ], VaultService);
106
66