voltenv-sdk 1.0.0 → 1.1.1
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 +62 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +257 -11
- package/dist/index.js.map +1 -1
- package/dist/utilities/cacheService.d.ts +21 -6
- package/dist/utilities/cacheService.d.ts.map +1 -1
- package/dist/utilities/cacheService.js +36 -9
- package/dist/utilities/cacheService.js.map +1 -1
- package/dist/utilities/errorHandler.d.ts +2 -0
- package/dist/utilities/errorHandler.d.ts.map +1 -1
- package/dist/utilities/errorHandler.js.map +1 -1
- package/index.ts +261 -11
- package/jest.config.js +9 -0
- package/package.json +36 -29
- package/utilities/cacheService.ts +49 -12
- package/utilities/errorHandler.ts +3 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/verify.ts +0 -18
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# VoltEnv Node.js SDK
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for VoltEnv - The Environment Variable Management System.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install voltenv-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Initialization
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import VoltEnv from 'voltenv-sdk';
|
|
17
|
+
|
|
18
|
+
const voltenv = new VoltEnv({
|
|
19
|
+
apiKey: 'your_api_key',
|
|
20
|
+
// Optional
|
|
21
|
+
baseURL: 'https://api.voltenv.com', // Defaults to official API
|
|
22
|
+
cache: true, // Enable local caching (default: true)
|
|
23
|
+
timeout: 5000 // Request timeout in ms
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Get a Variable
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// Get a specific variable from an environment
|
|
31
|
+
const port = await voltenv.get('development', 'PORT', '3000');
|
|
32
|
+
console.log(port); // Returns value or default '3000'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Get All Variables
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// Get all variables for an environment
|
|
39
|
+
const variables = await voltenv.all('development');
|
|
40
|
+
console.log(variables);
|
|
41
|
+
// { PORT: "3000", DB_HOST: "localhost", ... }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Set a Variable
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
await voltenv.set('development', 'API_KEY', 'secret-value');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Delete a Variable
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
await voltenv.delete('development', 'API_KEY');
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Caching
|
|
57
|
+
The SDK includes a built-in LRU cache to minimize API requests. You can manually sync/refresh the cache:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Force refresh all variables for an environment from the server
|
|
61
|
+
await voltenv.sync('development');
|
|
62
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +2,24 @@ import { VoltEnvOptions } from './utilities/errorHandler';
|
|
|
2
2
|
export declare class VoltEnv {
|
|
3
3
|
private httpClient;
|
|
4
4
|
private cacheService;
|
|
5
|
+
private resolvedPrefix;
|
|
5
6
|
/**
|
|
6
7
|
* Initialize the VoltEnv SDK
|
|
7
8
|
* @param options Configuration options or API key string
|
|
8
9
|
*/
|
|
9
10
|
constructor(options: VoltEnvOptions | string);
|
|
11
|
+
/**
|
|
12
|
+
* Resolve the prefix based on framework or explicit option
|
|
13
|
+
*/
|
|
14
|
+
private resolvePrefix;
|
|
15
|
+
/**
|
|
16
|
+
* Try to detect the framework from package.json
|
|
17
|
+
*/
|
|
18
|
+
private detectFramework;
|
|
19
|
+
/**
|
|
20
|
+
* Apply prefix to a key if it's missing
|
|
21
|
+
*/
|
|
22
|
+
private applyPrefix;
|
|
10
23
|
/**
|
|
11
24
|
* Get a variable from a specific environment
|
|
12
25
|
* @param environment The environment name (e.g., 'production', 'development')
|
|
@@ -28,6 +41,21 @@ export declare class VoltEnv {
|
|
|
28
41
|
* @param value The variable value
|
|
29
42
|
*/
|
|
30
43
|
set(environment: string, key: string, value: string): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Set a secret variable in a specific environment
|
|
46
|
+
* @param environment The environment name
|
|
47
|
+
* @param key The variable key
|
|
48
|
+
* @param value The secret value
|
|
49
|
+
*/
|
|
50
|
+
setSecret(environment: string, key: string, value: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Watch for changes in an environment
|
|
53
|
+
* @param environment The environment name
|
|
54
|
+
* @param callback Function to call when changes occur
|
|
55
|
+
* @param interval Polling interval in milliseconds (default: 30000)
|
|
56
|
+
* @returns A function to stop watching
|
|
57
|
+
*/
|
|
58
|
+
watch(environment: string, callback: (variables: Record<string, string>) => void, interval?: number): () => void;
|
|
31
59
|
/**
|
|
32
60
|
* Set multiple variables at once
|
|
33
61
|
* @param environment The environment name
|
|
@@ -46,11 +74,52 @@ export declare class VoltEnv {
|
|
|
46
74
|
* @param keys Array of keys to delete
|
|
47
75
|
*/
|
|
48
76
|
deleteMany(environment: string, keys: string[]): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Get metadata for a specific variable
|
|
79
|
+
* @param environment The environment name
|
|
80
|
+
* @param key The variable key
|
|
81
|
+
*/
|
|
82
|
+
getMetadata(environment: string, key: string): Promise<Record<string, any>>;
|
|
83
|
+
/**
|
|
84
|
+
* Get the audit history of a variable
|
|
85
|
+
* @param environment The environment name
|
|
86
|
+
* @param key The variable key
|
|
87
|
+
*/
|
|
88
|
+
getHistory(environment: string, key: string): Promise<any[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Export all variables in an environment to a .env file
|
|
91
|
+
* @param environment The environment name
|
|
92
|
+
* @param filePath Path to the .env file to create/overwrite
|
|
93
|
+
*/
|
|
94
|
+
exportToEnvFile(environment: string, filePath: string): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Load variables from a .env file and set them in an environment
|
|
97
|
+
* @param environment The environment name
|
|
98
|
+
* @param filePath Path to the .env file to read from
|
|
99
|
+
*/
|
|
100
|
+
loadFromEnvFile(environment: string, filePath: string): Promise<void>;
|
|
49
101
|
/**
|
|
50
102
|
* List all environments accessible by this API key
|
|
51
103
|
* @returns Array of environment names
|
|
52
104
|
*/
|
|
53
105
|
listEnvironments(): Promise<string[]>;
|
|
106
|
+
/**
|
|
107
|
+
* Create a new environment
|
|
108
|
+
* @param name The name of the environment
|
|
109
|
+
* @param options Optional configuration for the environment
|
|
110
|
+
*/
|
|
111
|
+
createEnvironment(name: string, options?: Record<string, any>): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Update an existing environment
|
|
114
|
+
* @param name The current name of the environment
|
|
115
|
+
* @param update Data to update (e.g., { name: 'new-name' })
|
|
116
|
+
*/
|
|
117
|
+
updateEnvironment(name: string, update: Record<string, any>): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Delete an environment
|
|
120
|
+
* @param name The name of the environment to delete
|
|
121
|
+
*/
|
|
122
|
+
deleteEnvironment(name: string): Promise<void>;
|
|
54
123
|
/**
|
|
55
124
|
* Sync/refresh variables from server (bypassing cache)
|
|
56
125
|
*If an environment is provided, it only syncs that environment.
|
|
@@ -58,6 +127,17 @@ export declare class VoltEnv {
|
|
|
58
127
|
* @param environment Optional environment to sync
|
|
59
128
|
*/
|
|
60
129
|
sync(environment?: string): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Get all cached keys (for debugging)
|
|
132
|
+
*/
|
|
133
|
+
getCacheKeys(): string[];
|
|
134
|
+
/**
|
|
135
|
+
* Get cache statistics
|
|
136
|
+
*/
|
|
137
|
+
getCacheStats(): {
|
|
138
|
+
size: number;
|
|
139
|
+
keys: string[];
|
|
140
|
+
};
|
|
61
141
|
}
|
|
62
142
|
export default VoltEnv;
|
|
63
143
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAIxE,qBAAa,OAAO;IAChB,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,cAAc,CAAc;IAEpC;;;OAGG;gBACS,OAAO,EAAE,cAAc,GAAG,MAAM;IA0B5C;;OAEG;IACH,OAAO,CAAC,aAAa;IAarB;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAMnB;;;;;;OAMG;IACG,GAAG,CACL,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,YAAY,CAAC,EAAE,MAAM,GACtB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA4B9B;;;;OAIG;IACG,MAAM,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAgBlE;;;;;OAKG;IACG,GAAG,CACL,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC;IAWhB;;;;;OAKG;IACG,SAAS,CACX,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC;IAYhB;;;;;;OAMG;IACH,KAAK,CACD,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM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CAAC;IAIjF;;;OAGG;IACG,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQpD;;;;;OAKG;IACG,IAAI,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoB/C;;OAEG;IACH,YAAY,IAAI,MAAM,EAAE;IAOxB;;OAEG;IACH,aAAa,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE;CASpD;AAED,eAAe,OAAO,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.VoltEnv = void 0;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
4
39
|
const errorHandler_1 = require("./utilities/errorHandler");
|
|
5
40
|
const httpClient_1 = require("./utilities/httpClient");
|
|
6
41
|
const cacheService_1 = require("./utilities/cacheService");
|
|
@@ -10,10 +45,11 @@ class VoltEnv {
|
|
|
10
45
|
* @param options Configuration options or API key string
|
|
11
46
|
*/
|
|
12
47
|
constructor(options) {
|
|
48
|
+
this.resolvedPrefix = '';
|
|
13
49
|
const config = typeof options === 'string'
|
|
14
50
|
? { apiKey: options }
|
|
15
51
|
: options;
|
|
16
|
-
const { apiKey, baseURL = 'https://api.voltenv.com', timeout = 5000, cache = true, onError } = config;
|
|
52
|
+
const { apiKey, baseURL = 'https://api.voltenv.com', timeout = 5000, cache = true, onError, framework, prefix } = config;
|
|
17
53
|
this.httpClient = new httpClient_1.HttpClient({
|
|
18
54
|
apiKey,
|
|
19
55
|
baseURL,
|
|
@@ -21,6 +57,53 @@ class VoltEnv {
|
|
|
21
57
|
onError
|
|
22
58
|
});
|
|
23
59
|
this.cacheService = cache ? new cacheService_1.CacheService() : null;
|
|
60
|
+
this.resolvedPrefix = this.resolvePrefix(framework, prefix);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolve the prefix based on framework or explicit option
|
|
64
|
+
*/
|
|
65
|
+
resolvePrefix(framework, explicitPrefix) {
|
|
66
|
+
if (explicitPrefix !== undefined)
|
|
67
|
+
return explicitPrefix;
|
|
68
|
+
const fw = framework || this.detectFramework();
|
|
69
|
+
switch (fw) {
|
|
70
|
+
case 'vite': return 'VITE_';
|
|
71
|
+
case 'nextjs': return 'NEXT_PUBLIC_';
|
|
72
|
+
case 'cra': return 'REACT_APP_';
|
|
73
|
+
default: return '';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Try to detect the framework from package.json
|
|
78
|
+
*/
|
|
79
|
+
detectFramework() {
|
|
80
|
+
try {
|
|
81
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
82
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
83
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
84
|
+
const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
|
|
85
|
+
if (deps['vite'])
|
|
86
|
+
return 'vite';
|
|
87
|
+
if (deps['next'])
|
|
88
|
+
return 'nextjs';
|
|
89
|
+
if (deps['react-scripts'])
|
|
90
|
+
return 'cra';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
// Silently fail and return empty string if package.json cannot be read/parsed
|
|
95
|
+
}
|
|
96
|
+
return '';
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Apply prefix to a key if it's missing
|
|
100
|
+
*/
|
|
101
|
+
applyPrefix(key) {
|
|
102
|
+
if (!this.resolvedPrefix)
|
|
103
|
+
return key;
|
|
104
|
+
if (key.startsWith(this.resolvedPrefix))
|
|
105
|
+
return key;
|
|
106
|
+
return `${this.resolvedPrefix}${key}`;
|
|
24
107
|
}
|
|
25
108
|
/**
|
|
26
109
|
* Get a variable from a specific environment
|
|
@@ -30,12 +113,13 @@ class VoltEnv {
|
|
|
30
113
|
* @returns The variable value or the default value
|
|
31
114
|
*/
|
|
32
115
|
async get(environment, key, defaultValue) {
|
|
33
|
-
const
|
|
116
|
+
const prefixedKey = this.applyPrefix(key);
|
|
117
|
+
const cacheKey = `${environment}:${prefixedKey}`;
|
|
34
118
|
if (this.cacheService?.has(cacheKey)) {
|
|
35
119
|
return this.cacheService.get(cacheKey);
|
|
36
120
|
}
|
|
37
121
|
try {
|
|
38
|
-
const data = await this.httpClient.get(`/v1/variables/${environment}/${
|
|
122
|
+
const data = await this.httpClient.get(`/v1/variables/${environment}/${prefixedKey}`);
|
|
39
123
|
const value = data.value;
|
|
40
124
|
if (value && this.cacheService) {
|
|
41
125
|
this.cacheService.set(cacheKey, value);
|
|
@@ -71,24 +155,72 @@ class VoltEnv {
|
|
|
71
155
|
* @param value The variable value
|
|
72
156
|
*/
|
|
73
157
|
async set(environment, key, value) {
|
|
74
|
-
|
|
158
|
+
const prefixedKey = this.applyPrefix(key);
|
|
159
|
+
await this.httpClient.put(`/v1/variables/${environment}/${prefixedKey}`, {
|
|
160
|
+
value,
|
|
161
|
+
});
|
|
162
|
+
if (this.cacheService) {
|
|
163
|
+
this.cacheService.set(`${environment}:${prefixedKey}`, value);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Set a secret variable in a specific environment
|
|
168
|
+
* @param environment The environment name
|
|
169
|
+
* @param key The variable key
|
|
170
|
+
* @param value The secret value
|
|
171
|
+
*/
|
|
172
|
+
async setSecret(environment, key, value) {
|
|
173
|
+
const prefixedKey = this.applyPrefix(key);
|
|
174
|
+
await this.httpClient.put(`/v1/variables/${environment}/${prefixedKey}`, {
|
|
75
175
|
value,
|
|
176
|
+
isSecret: true
|
|
76
177
|
});
|
|
77
178
|
if (this.cacheService) {
|
|
78
|
-
this.cacheService.set(`${environment}:${
|
|
179
|
+
this.cacheService.set(`${environment}:${prefixedKey}`, value);
|
|
79
180
|
}
|
|
80
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Watch for changes in an environment
|
|
184
|
+
* @param environment The environment name
|
|
185
|
+
* @param callback Function to call when changes occur
|
|
186
|
+
* @param interval Polling interval in milliseconds (default: 30000)
|
|
187
|
+
* @returns A function to stop watching
|
|
188
|
+
*/
|
|
189
|
+
watch(environment, callback, interval = 30000) {
|
|
190
|
+
let previousVariables = '';
|
|
191
|
+
const fetchAndCompare = async () => {
|
|
192
|
+
try {
|
|
193
|
+
const variables = await this.getAll(environment);
|
|
194
|
+
const currentVariables = JSON.stringify(variables);
|
|
195
|
+
if (currentVariables !== previousVariables) {
|
|
196
|
+
previousVariables = currentVariables;
|
|
197
|
+
callback(variables);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
console.error(`Error in VoltEnv watcher for ${environment}:`, error);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
// Initial fetch
|
|
205
|
+
fetchAndCompare();
|
|
206
|
+
const timer = setInterval(fetchAndCompare, interval);
|
|
207
|
+
return () => clearInterval(timer);
|
|
208
|
+
}
|
|
81
209
|
/**
|
|
82
210
|
* Set multiple variables at once
|
|
83
211
|
* @param environment The environment name
|
|
84
212
|
* @param variables A record of key-value pairs to set
|
|
85
213
|
*/
|
|
86
214
|
async setMany(environment, variables) {
|
|
215
|
+
const prefixedVariables = {};
|
|
216
|
+
Object.entries(variables).forEach(([key, value]) => {
|
|
217
|
+
prefixedVariables[this.applyPrefix(key)] = value;
|
|
218
|
+
});
|
|
87
219
|
await this.httpClient.post(`/v1/variables/${environment}/batch`, {
|
|
88
|
-
variables,
|
|
220
|
+
variables: prefixedVariables,
|
|
89
221
|
});
|
|
90
222
|
if (this.cacheService) {
|
|
91
|
-
Object.entries(
|
|
223
|
+
Object.entries(prefixedVariables).forEach(([key, value]) => {
|
|
92
224
|
this.cacheService.set(`${environment}:${key}`, value);
|
|
93
225
|
});
|
|
94
226
|
}
|
|
@@ -99,9 +231,10 @@ class VoltEnv {
|
|
|
99
231
|
* @param key The variable key
|
|
100
232
|
*/
|
|
101
233
|
async delete(environment, key) {
|
|
102
|
-
|
|
234
|
+
const prefixedKey = this.applyPrefix(key);
|
|
235
|
+
await this.httpClient.delete(`/v1/variables/${environment}/${prefixedKey}`);
|
|
103
236
|
if (this.cacheService) {
|
|
104
|
-
this.cacheService.delete(`${environment}:${
|
|
237
|
+
this.cacheService.delete(`${environment}:${prefixedKey}`);
|
|
105
238
|
}
|
|
106
239
|
}
|
|
107
240
|
/**
|
|
@@ -110,15 +243,78 @@ class VoltEnv {
|
|
|
110
243
|
* @param keys Array of keys to delete
|
|
111
244
|
*/
|
|
112
245
|
async deleteMany(environment, keys) {
|
|
246
|
+
const prefixedKeys = keys.map(k => this.applyPrefix(k));
|
|
113
247
|
await this.httpClient.post(`/v1/variables/${environment}/batch-delete`, {
|
|
114
|
-
keys,
|
|
248
|
+
keys: prefixedKeys,
|
|
115
249
|
});
|
|
116
250
|
if (this.cacheService) {
|
|
117
|
-
|
|
251
|
+
prefixedKeys.forEach(key => {
|
|
118
252
|
this.cacheService.delete(`${environment}:${key}`);
|
|
119
253
|
});
|
|
120
254
|
}
|
|
121
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Get metadata for a specific variable
|
|
258
|
+
* @param environment The environment name
|
|
259
|
+
* @param key The variable key
|
|
260
|
+
*/
|
|
261
|
+
async getMetadata(environment, key) {
|
|
262
|
+
const prefixedKey = this.applyPrefix(key);
|
|
263
|
+
const data = await this.httpClient.get(`/v1/variables/${environment}/${prefixedKey}/metadata`);
|
|
264
|
+
return data;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get the audit history of a variable
|
|
268
|
+
* @param environment The environment name
|
|
269
|
+
* @param key The variable key
|
|
270
|
+
*/
|
|
271
|
+
async getHistory(environment, key) {
|
|
272
|
+
const prefixedKey = this.applyPrefix(key);
|
|
273
|
+
const data = await this.httpClient.get(`/v1/variables/${environment}/${prefixedKey}/history`);
|
|
274
|
+
return data.history;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Export all variables in an environment to a .env file
|
|
278
|
+
* @param environment The environment name
|
|
279
|
+
* @param filePath Path to the .env file to create/overwrite
|
|
280
|
+
*/
|
|
281
|
+
async exportToEnvFile(environment, filePath) {
|
|
282
|
+
const variables = await this.getAll(environment);
|
|
283
|
+
const content = Object.entries(variables)
|
|
284
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
285
|
+
.join('\n');
|
|
286
|
+
const absolutePath = path.isAbsolute(filePath)
|
|
287
|
+
? filePath
|
|
288
|
+
: path.join(process.cwd(), filePath);
|
|
289
|
+
fs.writeFileSync(absolutePath, content, 'utf8');
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Load variables from a .env file and set them in an environment
|
|
293
|
+
* @param environment The environment name
|
|
294
|
+
* @param filePath Path to the .env file to read from
|
|
295
|
+
*/
|
|
296
|
+
async loadFromEnvFile(environment, filePath) {
|
|
297
|
+
const absolutePath = path.isAbsolute(filePath)
|
|
298
|
+
? filePath
|
|
299
|
+
: path.join(process.cwd(), filePath);
|
|
300
|
+
if (!fs.existsSync(absolutePath)) {
|
|
301
|
+
throw new Error(`File not found: ${absolutePath}`);
|
|
302
|
+
}
|
|
303
|
+
const content = fs.readFileSync(absolutePath, 'utf8');
|
|
304
|
+
const variables = {};
|
|
305
|
+
content.split(/\r?\n/).forEach(line => {
|
|
306
|
+
const trimmedLine = line.trim();
|
|
307
|
+
if (trimmedLine && !trimmedLine.startsWith('#')) {
|
|
308
|
+
const [key, ...valueParts] = trimmedLine.split('=');
|
|
309
|
+
if (key) {
|
|
310
|
+
variables[key.trim()] = valueParts.join('=').trim();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
if (Object.keys(variables).length > 0) {
|
|
315
|
+
await this.setMany(environment, variables);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
122
318
|
/**
|
|
123
319
|
* List all environments accessible by this API key
|
|
124
320
|
* @returns Array of environment names
|
|
@@ -127,6 +323,35 @@ class VoltEnv {
|
|
|
127
323
|
const data = await this.httpClient.get('/v1/environments');
|
|
128
324
|
return data.environments;
|
|
129
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Create a new environment
|
|
328
|
+
* @param name The name of the environment
|
|
329
|
+
* @param options Optional configuration for the environment
|
|
330
|
+
*/
|
|
331
|
+
async createEnvironment(name, options) {
|
|
332
|
+
await this.httpClient.post('/v1/environments', {
|
|
333
|
+
name,
|
|
334
|
+
...options
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Update an existing environment
|
|
339
|
+
* @param name The current name of the environment
|
|
340
|
+
* @param update Data to update (e.g., { name: 'new-name' })
|
|
341
|
+
*/
|
|
342
|
+
async updateEnvironment(name, update) {
|
|
343
|
+
await this.httpClient.put(`/v1/environments/${name}`, update);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Delete an environment
|
|
347
|
+
* @param name The name of the environment to delete
|
|
348
|
+
*/
|
|
349
|
+
async deleteEnvironment(name) {
|
|
350
|
+
await this.httpClient.delete(`/v1/environments/${name}`);
|
|
351
|
+
if (this.cacheService) {
|
|
352
|
+
this.cacheService.deleteByPrefix(`${name}:`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
130
355
|
/**
|
|
131
356
|
* Sync/refresh variables from server (bypassing cache)
|
|
132
357
|
*If an environment is provided, it only syncs that environment.
|
|
@@ -150,6 +375,27 @@ class VoltEnv {
|
|
|
150
375
|
}
|
|
151
376
|
}
|
|
152
377
|
}
|
|
378
|
+
/**
|
|
379
|
+
* Get all cached keys (for debugging)
|
|
380
|
+
*/
|
|
381
|
+
getCacheKeys() {
|
|
382
|
+
if (!this.cacheService) {
|
|
383
|
+
return [];
|
|
384
|
+
}
|
|
385
|
+
return Array.from(this.cacheService.keys());
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Get cache statistics
|
|
389
|
+
*/
|
|
390
|
+
getCacheStats() {
|
|
391
|
+
if (!this.cacheService) {
|
|
392
|
+
return { size: 0, keys: [] };
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
size: this.cacheService.size,
|
|
396
|
+
keys: Array.from(this.cacheService.keys())
|
|
397
|
+
};
|
|
398
|
+
}
|
|
153
399
|
}
|
|
154
400
|
exports.VoltEnv = VoltEnv;
|
|
155
401
|
exports.default = VoltEnv;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,uCAAyB;AACzB,2CAA6B;AAC7B,2DAAwE;AACxE,uDAAoD;AACpD,2DAAwD;AAExD,MAAa,OAAO;IAKhB;;;OAGG;IACH,YAAY,OAAgC;QANpC,mBAAc,GAAW,EAAE,CAAC;QAOhC,MAAM,MAAM,GAAG,OAAO,OAAO,KAAK,QAAQ;YACtC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE;YACrB,CAAC,CAAC,OAAO,CAAC;QAEd,MAAM,EACF,MAAM,EACN,OAAO,GAAG,yBAAyB,EACnC,OAAO,GAAG,IAAI,EACd,KAAK,GAAG,IAAI,EACZ,OAAO,EACP,SAAS,EACT,MAAM,EACT,GAAG,MAAM,CAAC;QAEX,IAAI,CAAC,UAAU,GAAG,IAAI,uBAAU,CAAC;YAC7B,MAAM;YACN,OAAO;YACP,OAAO;YACP,OAAO;SACV,CAAC,CAAC;QAEH,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,2BAAY,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;QACtD,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAChE,CAAC;IAED;;OAEG;IACK,aAAa,CAAC,SAAkB,EAAE,cAAuB;QAC7D,IAAI,cAAc,KAAK,SAAS;YAAE,OAAO,cAAc,CAAC;QAExD,MAAM,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;;OAEG;IACH,YAAY;QACR,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACrB,OAAO,EAAE,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,aAAa;QACT,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACrB,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACjC,CAAC;QACD,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI;YAC5B,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;SAC7C,CAAC;IACN,CAAC;CACJ;AA5bD,0BA4bC;AAED,kBAAe,OAAO,CAAC"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export interface CacheServiceOptions {
|
|
2
|
+
max?: number;
|
|
3
|
+
ttl?: number;
|
|
4
|
+
maxSize?: number;
|
|
5
|
+
sizeCalculation?: (value: any, key: string) => number;
|
|
6
|
+
}
|
|
4
7
|
export declare class CacheService {
|
|
5
8
|
private cache;
|
|
6
|
-
constructor();
|
|
9
|
+
constructor(options?: CacheServiceOptions);
|
|
7
10
|
/**
|
|
8
11
|
* Get a value from the cache
|
|
9
12
|
*/
|
|
@@ -11,7 +14,7 @@ export declare class CacheService {
|
|
|
11
14
|
/**
|
|
12
15
|
* Set a value in the cache
|
|
13
16
|
*/
|
|
14
|
-
set(key: string, value: any): void;
|
|
17
|
+
set(key: string, value: any, ttl?: number): void;
|
|
15
18
|
/**
|
|
16
19
|
* Check if a key exists in the cache
|
|
17
20
|
*/
|
|
@@ -19,7 +22,7 @@ export declare class CacheService {
|
|
|
19
22
|
/**
|
|
20
23
|
* Delete a value from the cache
|
|
21
24
|
*/
|
|
22
|
-
delete(key: string):
|
|
25
|
+
delete(key: string): boolean;
|
|
23
26
|
/**
|
|
24
27
|
* Clear all values from the cache
|
|
25
28
|
*/
|
|
@@ -28,5 +31,17 @@ export declare class CacheService {
|
|
|
28
31
|
* Delete all keys starting with a prefix
|
|
29
32
|
*/
|
|
30
33
|
deleteByPrefix(prefix: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Get the current size of the cache
|
|
36
|
+
*/
|
|
37
|
+
get size(): number;
|
|
38
|
+
/**
|
|
39
|
+
* Get all keys in the cache
|
|
40
|
+
*/
|
|
41
|
+
keys(): IterableIterator<string>;
|
|
42
|
+
/**
|
|
43
|
+
* Get all values in the cache
|
|
44
|
+
*/
|
|
45
|
+
values(): IterableIterator<any>;
|
|
31
46
|
}
|
|
32
47
|
//# sourceMappingURL=cacheService.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cacheService.d.ts","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cacheService.d.ts","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IAChC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CACzD;AAED,qBAAa,YAAY;IACrB,OAAO,CAAC,KAAK,CAAwB;gBAEzB,OAAO,GAAE,mBAAwB;IAS7C;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAIlC;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAQhD;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAI5B;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IASpC;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,IAAI,gBAAgB,CAAC,MAAM,CAAC;IAIhC;;OAEG;IACH,MAAM,IAAI,gBAAgB,CAAC,GAAG,CAAC;CAGlC"}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CacheService = void 0;
|
|
4
|
-
|
|
5
|
-
* Simple in-memory cache service implementation
|
|
6
|
-
*/
|
|
4
|
+
const lru_cache_1 = require("lru-cache");
|
|
7
5
|
class CacheService {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.cache = new
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.cache = new lru_cache_1.LRUCache({
|
|
8
|
+
max: options.max ?? 500, // Default: 500 items
|
|
9
|
+
ttl: options.ttl, // Optional TTL
|
|
10
|
+
maxSize: options.maxSize, // Optional size limit
|
|
11
|
+
sizeCalculation: options.sizeCalculation,
|
|
12
|
+
});
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
15
|
* Get a value from the cache
|
|
@@ -17,8 +20,13 @@ class CacheService {
|
|
|
17
20
|
/**
|
|
18
21
|
* Set a value in the cache
|
|
19
22
|
*/
|
|
20
|
-
set(key, value) {
|
|
21
|
-
|
|
23
|
+
set(key, value, ttl) {
|
|
24
|
+
if (ttl !== undefined) {
|
|
25
|
+
this.cache.set(key, value, { ttl });
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.cache.set(key, value);
|
|
29
|
+
}
|
|
22
30
|
}
|
|
23
31
|
/**
|
|
24
32
|
* Check if a key exists in the cache
|
|
@@ -30,7 +38,7 @@ class CacheService {
|
|
|
30
38
|
* Delete a value from the cache
|
|
31
39
|
*/
|
|
32
40
|
delete(key) {
|
|
33
|
-
this.cache.delete(key);
|
|
41
|
+
return this.cache.delete(key);
|
|
34
42
|
}
|
|
35
43
|
/**
|
|
36
44
|
* Clear all values from the cache
|
|
@@ -42,12 +50,31 @@ class CacheService {
|
|
|
42
50
|
* Delete all keys starting with a prefix
|
|
43
51
|
*/
|
|
44
52
|
deleteByPrefix(prefix) {
|
|
45
|
-
|
|
53
|
+
const keys = Array.from(this.cache.keys());
|
|
54
|
+
for (const key of keys) {
|
|
46
55
|
if (key.startsWith(prefix)) {
|
|
47
56
|
this.cache.delete(key);
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the current size of the cache
|
|
62
|
+
*/
|
|
63
|
+
get size() {
|
|
64
|
+
return this.cache.size;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get all keys in the cache
|
|
68
|
+
*/
|
|
69
|
+
keys() {
|
|
70
|
+
return this.cache.keys();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get all values in the cache
|
|
74
|
+
*/
|
|
75
|
+
values() {
|
|
76
|
+
return this.cache.values();
|
|
77
|
+
}
|
|
51
78
|
}
|
|
52
79
|
exports.CacheService = CacheService;
|
|
53
80
|
//# sourceMappingURL=cacheService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cacheService.js","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"cacheService.js","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":";;;AAAA,yCAAqC;AASrC,MAAa,YAAY;IAGrB,YAAY,UAA+B,EAAE;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,oBAAQ,CAAC;YACt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AAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC3B,CAAC;QACL,CAAC;IACL,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI;QACA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,MAAM;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;IAC/B,CAAC;CACJ;AAnFD,oCAmFC"}
|
|
@@ -5,6 +5,8 @@ export interface VoltEnvOptions {
|
|
|
5
5
|
timeout?: number;
|
|
6
6
|
cache?: boolean;
|
|
7
7
|
onError?: (error: VoltEnvError) => void;
|
|
8
|
+
framework?: 'vite' | 'nextjs' | 'cra' | 'custom';
|
|
9
|
+
prefix?: string;
|
|
8
10
|
}
|
|
9
11
|
export type VoltEnvErrorCode = 'INVALID_API_KEY' | 'PERMISSION_DENIED' | 'NOT_FOUND' | 'RATE_LIMIT_EXCEEDED' | 'SERVER_ERROR' | 'NETWORK_ERROR' | 'REQUEST_ERROR' | 'UNKNOWN_ERROR';
|
|
10
12
|
export declare class VoltEnvError extends Error {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../utilities/errorHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../utilities/errorHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;IACxC,SAAS,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,CAAC;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,gBAAgB,GACtB,iBAAiB,GACjB,mBAAmB,GACnB,WAAW,GACX,qBAAqB,GACrB,cAAc,GACd,eAAe,GACf,eAAe,GACf,eAAe,CAAC;AAEtB,qBAAa,YAAa,SAAQ,KAAK;IAGxB,IAAI,EAAE,gBAAgB;IACtB,UAAU,CAAC,EAAE,MAAM;IACnB,aAAa,CAAC,EAAE,GAAG;gBAH1B,OAAO,EAAE,MAAM,EACR,IAAI,EAAE,gBAAgB,EACtB,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,aAAa,CAAC,EAAE,GAAG,YAAA;CAMjC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CA0ChE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorHandler.js","sourceRoot":"","sources":["../../utilities/errorHandler.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"errorHandler.js","sourceRoot":"","sources":["../../utilities/errorHandler.ts"],"names":[],"mappings":";;;AAsCA,4CA0CC;AA1DD,MAAa,YAAa,SAAQ,KAAK;IACnC,YACI,OAAe,EACR,IAAsB,EACtB,UAAmB,EACnB,aAAmB;QAE1B,KAAK,CAAC,OAAO,CAAC,CAAC;QAJR,SAAI,GAAJ,IAAI,CAA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CAAC;AACL,CAAC"}
|
package/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
2
4
|
import { VoltEnvError, VoltEnvOptions } from './utilities/errorHandler';
|
|
3
5
|
import { HttpClient } from './utilities/httpClient';
|
|
4
6
|
import { CacheService } from './utilities/cacheService';
|
|
@@ -6,6 +8,7 @@ import { CacheService } from './utilities/cacheService';
|
|
|
6
8
|
export class VoltEnv {
|
|
7
9
|
private httpClient: HttpClient;
|
|
8
10
|
private cacheService: CacheService | null;
|
|
11
|
+
private resolvedPrefix: string = '';
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* Initialize the VoltEnv SDK
|
|
@@ -21,7 +24,9 @@ export class VoltEnv {
|
|
|
21
24
|
baseURL = 'https://api.voltenv.com',
|
|
22
25
|
timeout = 5000,
|
|
23
26
|
cache = true,
|
|
24
|
-
onError
|
|
27
|
+
onError,
|
|
28
|
+
framework,
|
|
29
|
+
prefix
|
|
25
30
|
} = config;
|
|
26
31
|
|
|
27
32
|
this.httpClient = new HttpClient({
|
|
@@ -32,6 +37,52 @@ export class VoltEnv {
|
|
|
32
37
|
});
|
|
33
38
|
|
|
34
39
|
this.cacheService = cache ? new CacheService() : null;
|
|
40
|
+
this.resolvedPrefix = this.resolvePrefix(framework, prefix);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the prefix based on framework or explicit option
|
|
45
|
+
*/
|
|
46
|
+
private resolvePrefix(framework?: string, explicitPrefix?: string): string {
|
|
47
|
+
if (explicitPrefix !== undefined) return explicitPrefix;
|
|
48
|
+
|
|
49
|
+
const fw = framework || this.detectFramework();
|
|
50
|
+
|
|
51
|
+
switch (fw) {
|
|
52
|
+
case 'vite': return 'VITE_';
|
|
53
|
+
case 'nextjs': return 'NEXT_PUBLIC_';
|
|
54
|
+
case 'cra': return 'REACT_APP_';
|
|
55
|
+
default: return '';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Try to detect the framework from package.json
|
|
61
|
+
*/
|
|
62
|
+
private detectFramework(): string {
|
|
63
|
+
try {
|
|
64
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
65
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
66
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
67
|
+
const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
|
|
68
|
+
|
|
69
|
+
if (deps['vite']) return 'vite';
|
|
70
|
+
if (deps['next']) return 'nextjs';
|
|
71
|
+
if (deps['react-scripts']) return 'cra';
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// Silently fail and return empty string if package.json cannot be read/parsed
|
|
75
|
+
}
|
|
76
|
+
return '';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Apply prefix to a key if it's missing
|
|
81
|
+
*/
|
|
82
|
+
private applyPrefix(key: string): string {
|
|
83
|
+
if (!this.resolvedPrefix) return key;
|
|
84
|
+
if (key.startsWith(this.resolvedPrefix)) return key;
|
|
85
|
+
return `${this.resolvedPrefix}${key}`;
|
|
35
86
|
}
|
|
36
87
|
|
|
37
88
|
/**
|
|
@@ -46,7 +97,8 @@ export class VoltEnv {
|
|
|
46
97
|
key: string,
|
|
47
98
|
defaultValue?: string
|
|
48
99
|
): Promise<string | undefined> {
|
|
49
|
-
const
|
|
100
|
+
const prefixedKey = this.applyPrefix(key);
|
|
101
|
+
const cacheKey = `${environment}:${prefixedKey}`;
|
|
50
102
|
|
|
51
103
|
if (this.cacheService?.has(cacheKey)) {
|
|
52
104
|
return this.cacheService.get<string>(cacheKey);
|
|
@@ -54,7 +106,7 @@ export class VoltEnv {
|
|
|
54
106
|
|
|
55
107
|
try {
|
|
56
108
|
const data = await this.httpClient.get<{ value: string }>(
|
|
57
|
-
`/v1/variables/${environment}/${
|
|
109
|
+
`/v1/variables/${environment}/${prefixedKey}`
|
|
58
110
|
);
|
|
59
111
|
|
|
60
112
|
const value = data.value;
|
|
@@ -104,15 +156,74 @@ export class VoltEnv {
|
|
|
104
156
|
key: string,
|
|
105
157
|
value: string
|
|
106
158
|
): Promise<void> {
|
|
107
|
-
|
|
159
|
+
const prefixedKey = this.applyPrefix(key);
|
|
160
|
+
await this.httpClient.put(`/v1/variables/${environment}/${prefixedKey}`, {
|
|
161
|
+
value,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (this.cacheService) {
|
|
165
|
+
this.cacheService.set(`${environment}:${prefixedKey}`, value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Set a secret variable in a specific environment
|
|
171
|
+
* @param environment The environment name
|
|
172
|
+
* @param key The variable key
|
|
173
|
+
* @param value The secret value
|
|
174
|
+
*/
|
|
175
|
+
async setSecret(
|
|
176
|
+
environment: string,
|
|
177
|
+
key: string,
|
|
178
|
+
value: string
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
const prefixedKey = this.applyPrefix(key);
|
|
181
|
+
await this.httpClient.put(`/v1/variables/${environment}/${prefixedKey}`, {
|
|
108
182
|
value,
|
|
183
|
+
isSecret: true
|
|
109
184
|
});
|
|
110
185
|
|
|
111
186
|
if (this.cacheService) {
|
|
112
|
-
this.cacheService.set(`${environment}:${
|
|
187
|
+
this.cacheService.set(`${environment}:${prefixedKey}`, value);
|
|
113
188
|
}
|
|
114
189
|
}
|
|
115
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Watch for changes in an environment
|
|
193
|
+
* @param environment The environment name
|
|
194
|
+
* @param callback Function to call when changes occur
|
|
195
|
+
* @param interval Polling interval in milliseconds (default: 30000)
|
|
196
|
+
* @returns A function to stop watching
|
|
197
|
+
*/
|
|
198
|
+
watch(
|
|
199
|
+
environment: string,
|
|
200
|
+
callback: (variables: Record<string, string>) => void,
|
|
201
|
+
interval: number = 30000
|
|
202
|
+
): () => void {
|
|
203
|
+
let previousVariables: string = '';
|
|
204
|
+
|
|
205
|
+
const fetchAndCompare = async () => {
|
|
206
|
+
try {
|
|
207
|
+
const variables = await this.getAll(environment);
|
|
208
|
+
const currentVariables = JSON.stringify(variables);
|
|
209
|
+
|
|
210
|
+
if (currentVariables !== previousVariables) {
|
|
211
|
+
previousVariables = currentVariables;
|
|
212
|
+
callback(variables);
|
|
213
|
+
}
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error(`Error in VoltEnv watcher for ${environment}:`, error);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// Initial fetch
|
|
220
|
+
fetchAndCompare();
|
|
221
|
+
|
|
222
|
+
const timer = setInterval(fetchAndCompare, interval);
|
|
223
|
+
|
|
224
|
+
return () => clearInterval(timer);
|
|
225
|
+
}
|
|
226
|
+
|
|
116
227
|
/**
|
|
117
228
|
* Set multiple variables at once
|
|
118
229
|
* @param environment The environment name
|
|
@@ -122,12 +233,17 @@ export class VoltEnv {
|
|
|
122
233
|
environment: string,
|
|
123
234
|
variables: Record<string, string>
|
|
124
235
|
): Promise<void> {
|
|
236
|
+
const prefixedVariables: Record<string, string> = {};
|
|
237
|
+
Object.entries(variables).forEach(([key, value]) => {
|
|
238
|
+
prefixedVariables[this.applyPrefix(key)] = value;
|
|
239
|
+
});
|
|
240
|
+
|
|
125
241
|
await this.httpClient.post(`/v1/variables/${environment}/batch`, {
|
|
126
|
-
variables,
|
|
242
|
+
variables: prefixedVariables,
|
|
127
243
|
});
|
|
128
244
|
|
|
129
245
|
if (this.cacheService) {
|
|
130
|
-
Object.entries(
|
|
246
|
+
Object.entries(prefixedVariables).forEach(([key, value]) => {
|
|
131
247
|
this.cacheService!.set(`${environment}:${key}`, value);
|
|
132
248
|
});
|
|
133
249
|
}
|
|
@@ -139,10 +255,11 @@ export class VoltEnv {
|
|
|
139
255
|
* @param key The variable key
|
|
140
256
|
*/
|
|
141
257
|
async delete(environment: string, key: string): Promise<void> {
|
|
142
|
-
|
|
258
|
+
const prefixedKey = this.applyPrefix(key);
|
|
259
|
+
await this.httpClient.delete(`/v1/variables/${environment}/${prefixedKey}`);
|
|
143
260
|
|
|
144
261
|
if (this.cacheService) {
|
|
145
|
-
this.cacheService.delete(`${environment}:${
|
|
262
|
+
this.cacheService.delete(`${environment}:${prefixedKey}`);
|
|
146
263
|
}
|
|
147
264
|
}
|
|
148
265
|
|
|
@@ -152,17 +269,94 @@ export class VoltEnv {
|
|
|
152
269
|
* @param keys Array of keys to delete
|
|
153
270
|
*/
|
|
154
271
|
async deleteMany(environment: string, keys: string[]): Promise<void> {
|
|
272
|
+
const prefixedKeys = keys.map(k => this.applyPrefix(k));
|
|
155
273
|
await this.httpClient.post(`/v1/variables/${environment}/batch-delete`, {
|
|
156
|
-
keys,
|
|
274
|
+
keys: prefixedKeys,
|
|
157
275
|
});
|
|
158
276
|
|
|
159
277
|
if (this.cacheService) {
|
|
160
|
-
|
|
278
|
+
prefixedKeys.forEach(key => {
|
|
161
279
|
this.cacheService!.delete(`${environment}:${key}`);
|
|
162
280
|
});
|
|
163
281
|
}
|
|
164
282
|
}
|
|
165
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Get metadata for a specific variable
|
|
286
|
+
* @param environment The environment name
|
|
287
|
+
* @param key The variable key
|
|
288
|
+
*/
|
|
289
|
+
async getMetadata(environment: string, key: string): Promise<Record<string, any>> {
|
|
290
|
+
const prefixedKey = this.applyPrefix(key);
|
|
291
|
+
const data = await this.httpClient.get<Record<string, any>>(
|
|
292
|
+
`/v1/variables/${environment}/${prefixedKey}/metadata`
|
|
293
|
+
);
|
|
294
|
+
return data;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Get the audit history of a variable
|
|
299
|
+
* @param environment The environment name
|
|
300
|
+
* @param key The variable key
|
|
301
|
+
*/
|
|
302
|
+
async getHistory(environment: string, key: string): Promise<any[]> {
|
|
303
|
+
const prefixedKey = this.applyPrefix(key);
|
|
304
|
+
const data = await this.httpClient.get<{ history: any[] }>(
|
|
305
|
+
`/v1/variables/${environment}/${prefixedKey}/history`
|
|
306
|
+
);
|
|
307
|
+
return data.history;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Export all variables in an environment to a .env file
|
|
312
|
+
* @param environment The environment name
|
|
313
|
+
* @param filePath Path to the .env file to create/overwrite
|
|
314
|
+
*/
|
|
315
|
+
async exportToEnvFile(environment: string, filePath: string): Promise<void> {
|
|
316
|
+
const variables = await this.getAll(environment);
|
|
317
|
+
const content = Object.entries(variables)
|
|
318
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
319
|
+
.join('\n');
|
|
320
|
+
|
|
321
|
+
const absolutePath = path.isAbsolute(filePath)
|
|
322
|
+
? filePath
|
|
323
|
+
: path.join(process.cwd(), filePath);
|
|
324
|
+
|
|
325
|
+
fs.writeFileSync(absolutePath, content, 'utf8');
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Load variables from a .env file and set them in an environment
|
|
330
|
+
* @param environment The environment name
|
|
331
|
+
* @param filePath Path to the .env file to read from
|
|
332
|
+
*/
|
|
333
|
+
async loadFromEnvFile(environment: string, filePath: string): Promise<void> {
|
|
334
|
+
const absolutePath = path.isAbsolute(filePath)
|
|
335
|
+
? filePath
|
|
336
|
+
: path.join(process.cwd(), filePath);
|
|
337
|
+
|
|
338
|
+
if (!fs.existsSync(absolutePath)) {
|
|
339
|
+
throw new Error(`File not found: ${absolutePath}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const content = fs.readFileSync(absolutePath, 'utf8');
|
|
343
|
+
const variables: Record<string, string> = {};
|
|
344
|
+
|
|
345
|
+
content.split(/\r?\n/).forEach(line => {
|
|
346
|
+
const trimmedLine = line.trim();
|
|
347
|
+
if (trimmedLine && !trimmedLine.startsWith('#')) {
|
|
348
|
+
const [key, ...valueParts] = trimmedLine.split('=');
|
|
349
|
+
if (key) {
|
|
350
|
+
variables[key.trim()] = valueParts.join('=').trim();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
if (Object.keys(variables).length > 0) {
|
|
356
|
+
await this.setMany(environment, variables);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
166
360
|
/**
|
|
167
361
|
* List all environments accessible by this API key
|
|
168
362
|
* @returns Array of environment names
|
|
@@ -174,6 +368,39 @@ export class VoltEnv {
|
|
|
174
368
|
return data.environments;
|
|
175
369
|
}
|
|
176
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Create a new environment
|
|
373
|
+
* @param name The name of the environment
|
|
374
|
+
* @param options Optional configuration for the environment
|
|
375
|
+
*/
|
|
376
|
+
async createEnvironment(name: string, options?: Record<string, any>): Promise<void> {
|
|
377
|
+
await this.httpClient.post('/v1/environments', {
|
|
378
|
+
name,
|
|
379
|
+
...options
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Update an existing environment
|
|
385
|
+
* @param name The current name of the environment
|
|
386
|
+
* @param update Data to update (e.g., { name: 'new-name' })
|
|
387
|
+
*/
|
|
388
|
+
async updateEnvironment(name: string, update: Record<string, any>): Promise<void> {
|
|
389
|
+
await this.httpClient.put(`/v1/environments/${name}`, update);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Delete an environment
|
|
394
|
+
* @param name The name of the environment to delete
|
|
395
|
+
*/
|
|
396
|
+
async deleteEnvironment(name: string): Promise<void> {
|
|
397
|
+
await this.httpClient.delete(`/v1/environments/${name}`);
|
|
398
|
+
|
|
399
|
+
if (this.cacheService) {
|
|
400
|
+
this.cacheService.deleteByPrefix(`${name}:`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
177
404
|
/**
|
|
178
405
|
* Sync/refresh variables from server (bypassing cache)
|
|
179
406
|
*If an environment is provided, it only syncs that environment.
|
|
@@ -199,6 +426,29 @@ export class VoltEnv {
|
|
|
199
426
|
}
|
|
200
427
|
}
|
|
201
428
|
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Get all cached keys (for debugging)
|
|
432
|
+
*/
|
|
433
|
+
getCacheKeys(): string[] {
|
|
434
|
+
if (!this.cacheService) {
|
|
435
|
+
return [];
|
|
436
|
+
}
|
|
437
|
+
return Array.from(this.cacheService.keys());
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Get cache statistics
|
|
442
|
+
*/
|
|
443
|
+
getCacheStats(): { size: number; keys: string[] } {
|
|
444
|
+
if (!this.cacheService) {
|
|
445
|
+
return { size: 0, keys: [] };
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
size: this.cacheService.size,
|
|
449
|
+
keys: Array.from(this.cacheService.keys())
|
|
450
|
+
};
|
|
451
|
+
}
|
|
202
452
|
}
|
|
203
453
|
|
|
204
454
|
export default VoltEnv;
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
testMatch: ['**/tests/**/*.test.ts'],
|
|
6
|
+
collectCoverage: true,
|
|
7
|
+
coverageDirectory: 'coverage',
|
|
8
|
+
coveragePathIgnorePatterns: ['/node_modules/', '/dist/'],
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "voltenv-sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "TypeScript SDK for VoltEnv",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"prepublishOnly": "npm run build",
|
|
10
|
-
"test": "
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "voltenv-sdk",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "TypeScript SDK for VoltEnv",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch",
|
|
12
|
+
"test:coverage": "jest --coverage"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"voltenv",
|
|
16
|
+
"sdk",
|
|
17
|
+
"environment-variables"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"type": "commonjs",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"axios": "^1.13.2",
|
|
24
|
+
"lru-cache": "^11.2.4"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/express": "^5.0.6",
|
|
28
|
+
"@types/jest": "^30.0.0",
|
|
29
|
+
"@types/node": "^25.0.3",
|
|
30
|
+
"jest": "^30.2.0",
|
|
31
|
+
"jest-environment-node": "^30.2.0",
|
|
32
|
+
"ts-jest": "^29.4.6",
|
|
33
|
+
"ts-node": "^10.9.2",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { LRUCache } from "lru-cache";
|
|
2
|
+
|
|
3
|
+
export interface CacheServiceOptions {
|
|
4
|
+
max?: number; // Maximum number of items
|
|
5
|
+
ttl?: number; // Time to live in milliseconds
|
|
6
|
+
maxSize?: number; // Maximum size in bytes (optional)
|
|
7
|
+
sizeCalculation?: (value: any, key: string) => number;
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
export class CacheService {
|
|
5
|
-
private cache:
|
|
11
|
+
private cache: LRUCache<string, any>;
|
|
6
12
|
|
|
7
|
-
constructor() {
|
|
8
|
-
this.cache = new
|
|
13
|
+
constructor(options: CacheServiceOptions = {}) {
|
|
14
|
+
this.cache = new LRUCache({
|
|
15
|
+
max: options.max ?? 500, // Default: 500 items
|
|
16
|
+
ttl: options.ttl, // Optional TTL
|
|
17
|
+
maxSize: options.maxSize, // Optional size limit
|
|
18
|
+
sizeCalculation: options.sizeCalculation,
|
|
19
|
+
});
|
|
9
20
|
}
|
|
10
21
|
|
|
11
22
|
/**
|
|
@@ -18,8 +29,12 @@ export class CacheService {
|
|
|
18
29
|
/**
|
|
19
30
|
* Set a value in the cache
|
|
20
31
|
*/
|
|
21
|
-
set(key: string, value: any): void {
|
|
22
|
-
|
|
32
|
+
set(key: string, value: any, ttl?: number): void {
|
|
33
|
+
if (ttl !== undefined) {
|
|
34
|
+
this.cache.set(key, value, { ttl });
|
|
35
|
+
} else {
|
|
36
|
+
this.cache.set(key, value);
|
|
37
|
+
}
|
|
23
38
|
}
|
|
24
39
|
|
|
25
40
|
/**
|
|
@@ -32,8 +47,8 @@ export class CacheService {
|
|
|
32
47
|
/**
|
|
33
48
|
* Delete a value from the cache
|
|
34
49
|
*/
|
|
35
|
-
delete(key: string):
|
|
36
|
-
this.cache.delete(key);
|
|
50
|
+
delete(key: string): boolean {
|
|
51
|
+
return this.cache.delete(key);
|
|
37
52
|
}
|
|
38
53
|
|
|
39
54
|
/**
|
|
@@ -47,10 +62,32 @@ export class CacheService {
|
|
|
47
62
|
* Delete all keys starting with a prefix
|
|
48
63
|
*/
|
|
49
64
|
deleteByPrefix(prefix: string): void {
|
|
50
|
-
|
|
65
|
+
const keys = Array.from(this.cache.keys());
|
|
66
|
+
for (const key of keys) {
|
|
51
67
|
if (key.startsWith(prefix)) {
|
|
52
68
|
this.cache.delete(key);
|
|
53
69
|
}
|
|
54
70
|
}
|
|
55
71
|
}
|
|
56
|
-
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Get the current size of the cache
|
|
75
|
+
*/
|
|
76
|
+
get size(): number {
|
|
77
|
+
return this.cache.size;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get all keys in the cache
|
|
82
|
+
*/
|
|
83
|
+
keys(): IterableIterator<string> {
|
|
84
|
+
return this.cache.keys();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get all values in the cache
|
|
89
|
+
*/
|
|
90
|
+
values(): IterableIterator<any> {
|
|
91
|
+
return this.cache.values();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -6,9 +6,11 @@ export interface VoltEnvOptions {
|
|
|
6
6
|
timeout?: number;
|
|
7
7
|
cache?: boolean;
|
|
8
8
|
onError?: (error: VoltEnvError) => void;
|
|
9
|
+
framework?: 'vite' | 'nextjs' | 'cra' | 'custom';
|
|
10
|
+
prefix?: string;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
export type VoltEnvErrorCode =
|
|
13
|
+
export type VoltEnvErrorCode =
|
|
12
14
|
| 'INVALID_API_KEY'
|
|
13
15
|
| 'PERMISSION_DENIED'
|
|
14
16
|
| 'NOT_FOUND'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../index.ts","../utilities/cacheservice.ts","../utilities/errorhandler.ts","../utilities/httpclient.ts"],"version":"5.9.3"}
|
package/verify.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { VoltEnv } from './index';
|
|
2
|
-
|
|
3
|
-
async function verify() {
|
|
4
|
-
try {
|
|
5
|
-
console.log('Initializing VoltEnv...');
|
|
6
|
-
const voltEnv = new VoltEnv({
|
|
7
|
-
apiKey: 'test-api-key',
|
|
8
|
-
cache: true
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
console.log('VoltEnv initialized successfully.');
|
|
12
|
-
console.log('Methods available:', Object.getOwnPropertyNames(Object.getPrototypeOf(voltEnv)));
|
|
13
|
-
} catch (error) {
|
|
14
|
-
console.error('Verification failed:', error);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
verify();
|