voltenv-sdk 1.0.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.
@@ -0,0 +1,63 @@
1
+ import { VoltEnvOptions } from './utilities/errorHandler';
2
+ export declare class VoltEnv {
3
+ private httpClient;
4
+ private cacheService;
5
+ /**
6
+ * Initialize the VoltEnv SDK
7
+ * @param options Configuration options or API key string
8
+ */
9
+ constructor(options: VoltEnvOptions | string);
10
+ /**
11
+ * Get a variable from a specific environment
12
+ * @param environment The environment name (e.g., 'production', 'development')
13
+ * @param key The variable key
14
+ * @param defaultValue Optional default value if not found
15
+ * @returns The variable value or the default value
16
+ */
17
+ get(environment: string, key: string, defaultValue?: string): Promise<string | undefined>;
18
+ /**
19
+ * Get all variables from an environment
20
+ * @param environment The environment name
21
+ * @returns A record of key-value pairs
22
+ */
23
+ getAll(environment: string): Promise<Record<string, string>>;
24
+ /**
25
+ * Set a variable in a specific environment
26
+ * @param environment The environment name
27
+ * @param key The variable key
28
+ * @param value The variable value
29
+ */
30
+ set(environment: string, key: string, value: string): Promise<void>;
31
+ /**
32
+ * Set multiple variables at once
33
+ * @param environment The environment name
34
+ * @param variables A record of key-value pairs to set
35
+ */
36
+ setMany(environment: string, variables: Record<string, string>): Promise<void>;
37
+ /**
38
+ * Delete a variable from a specific environment
39
+ * @param environment The environment name
40
+ * @param key The variable key
41
+ */
42
+ delete(environment: string, key: string): Promise<void>;
43
+ /**
44
+ * Delete multiple variables at once
45
+ * @param environment The environment name
46
+ * @param keys Array of keys to delete
47
+ */
48
+ deleteMany(environment: string, keys: string[]): Promise<void>;
49
+ /**
50
+ * List all environments accessible by this API key
51
+ * @returns Array of environment names
52
+ */
53
+ listEnvironments(): Promise<string[]>;
54
+ /**
55
+ * Sync/refresh variables from server (bypassing cache)
56
+ *If an environment is provided, it only syncs that environment.
57
+ * If no environment is provided, it lists all environments and syncs them all.
58
+ * @param environment Optional environment to sync
59
+ */
60
+ sync(environment?: string): Promise<void>;
61
+ }
62
+ export default VoltEnv;
63
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAIxE,qBAAa,OAAO;IAChB,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,YAAY,CAAsB;IAE1C;;;OAGG;gBACS,OAAO,EAAE,cAAc,GAAG,MAAM;IAuB5C;;;;;;OAMG;IACG,GAAG,CACL,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,YAAY,CAAC,EAAE,MAAM,GACtB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA2B9B;;;;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;IAUhB;;;;OAIG;IACG,OAAO,CACT,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAClC,OAAO,CAAC,IAAI,CAAC;IAYhB;;;;OAIG;IACG,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7D;;;;OAIG;IACG,UAAU,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAYpE;;;OAGG;IACG,gBAAgB,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAO3C;;;;;OAKG;IACG,IAAI,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAmBlD;AAED,eAAe,OAAO,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VoltEnv = void 0;
4
+ const errorHandler_1 = require("./utilities/errorHandler");
5
+ const httpClient_1 = require("./utilities/httpClient");
6
+ const cacheService_1 = require("./utilities/cacheService");
7
+ class VoltEnv {
8
+ /**
9
+ * Initialize the VoltEnv SDK
10
+ * @param options Configuration options or API key string
11
+ */
12
+ constructor(options) {
13
+ const config = typeof options === 'string'
14
+ ? { apiKey: options }
15
+ : options;
16
+ const { apiKey, baseURL = 'https://api.voltenv.com', timeout = 5000, cache = true, onError } = config;
17
+ this.httpClient = new httpClient_1.HttpClient({
18
+ apiKey,
19
+ baseURL,
20
+ timeout,
21
+ onError
22
+ });
23
+ this.cacheService = cache ? new cacheService_1.CacheService() : null;
24
+ }
25
+ /**
26
+ * Get a variable from a specific environment
27
+ * @param environment The environment name (e.g., 'production', 'development')
28
+ * @param key The variable key
29
+ * @param defaultValue Optional default value if not found
30
+ * @returns The variable value or the default value
31
+ */
32
+ async get(environment, key, defaultValue) {
33
+ const cacheKey = `${environment}:${key}`;
34
+ if (this.cacheService?.has(cacheKey)) {
35
+ return this.cacheService.get(cacheKey);
36
+ }
37
+ try {
38
+ const data = await this.httpClient.get(`/v1/variables/${environment}/${key}`);
39
+ const value = data.value;
40
+ if (value && this.cacheService) {
41
+ this.cacheService.set(cacheKey, value);
42
+ }
43
+ return value || defaultValue;
44
+ }
45
+ catch (error) {
46
+ if (error instanceof errorHandler_1.VoltEnvError && error.code === 'NOT_FOUND') {
47
+ return defaultValue;
48
+ }
49
+ throw error;
50
+ }
51
+ }
52
+ /**
53
+ * Get all variables from an environment
54
+ * @param environment The environment name
55
+ * @returns A record of key-value pairs
56
+ */
57
+ async getAll(environment) {
58
+ const data = await this.httpClient.get(`/v1/variables/${environment}`);
59
+ const variables = data.variables;
60
+ if (this.cacheService) {
61
+ Object.entries(variables).forEach(([key, value]) => {
62
+ this.cacheService.set(`${environment}:${key}`, value);
63
+ });
64
+ }
65
+ return variables;
66
+ }
67
+ /**
68
+ * Set a variable in a specific environment
69
+ * @param environment The environment name
70
+ * @param key The variable key
71
+ * @param value The variable value
72
+ */
73
+ async set(environment, key, value) {
74
+ await this.httpClient.put(`/v1/variables/${environment}/${key}`, {
75
+ value,
76
+ });
77
+ if (this.cacheService) {
78
+ this.cacheService.set(`${environment}:${key}`, value);
79
+ }
80
+ }
81
+ /**
82
+ * Set multiple variables at once
83
+ * @param environment The environment name
84
+ * @param variables A record of key-value pairs to set
85
+ */
86
+ async setMany(environment, variables) {
87
+ await this.httpClient.post(`/v1/variables/${environment}/batch`, {
88
+ variables,
89
+ });
90
+ if (this.cacheService) {
91
+ Object.entries(variables).forEach(([key, value]) => {
92
+ this.cacheService.set(`${environment}:${key}`, value);
93
+ });
94
+ }
95
+ }
96
+ /**
97
+ * Delete a variable from a specific environment
98
+ * @param environment The environment name
99
+ * @param key The variable key
100
+ */
101
+ async delete(environment, key) {
102
+ await this.httpClient.delete(`/v1/variables/${environment}/${key}`);
103
+ if (this.cacheService) {
104
+ this.cacheService.delete(`${environment}:${key}`);
105
+ }
106
+ }
107
+ /**
108
+ * Delete multiple variables at once
109
+ * @param environment The environment name
110
+ * @param keys Array of keys to delete
111
+ */
112
+ async deleteMany(environment, keys) {
113
+ await this.httpClient.post(`/v1/variables/${environment}/batch-delete`, {
114
+ keys,
115
+ });
116
+ if (this.cacheService) {
117
+ keys.forEach(key => {
118
+ this.cacheService.delete(`${environment}:${key}`);
119
+ });
120
+ }
121
+ }
122
+ /**
123
+ * List all environments accessible by this API key
124
+ * @returns Array of environment names
125
+ */
126
+ async listEnvironments() {
127
+ const data = await this.httpClient.get('/v1/environments');
128
+ return data.environments;
129
+ }
130
+ /**
131
+ * Sync/refresh variables from server (bypassing cache)
132
+ *If an environment is provided, it only syncs that environment.
133
+ * If no environment is provided, it lists all environments and syncs them all.
134
+ * @param environment Optional environment to sync
135
+ */
136
+ async sync(environment) {
137
+ if (environment) {
138
+ const variables = await this.getAll(environment);
139
+ if (this.cacheService) {
140
+ this.cacheService.deleteByPrefix(`${environment}:`);
141
+ Object.entries(variables).forEach(([key, value]) => {
142
+ this.cacheService.set(`${environment}:${key}`, value);
143
+ });
144
+ }
145
+ }
146
+ else {
147
+ const environments = await this.listEnvironments();
148
+ for (const env of environments) {
149
+ await this.sync(env);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ exports.VoltEnv = VoltEnv;
155
+ exports.default = VoltEnv;
156
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AACA,2DAAwE;AACxE,uDAAoD;AACpD,2DAAwD;AAExD,MAAa,OAAO;IAIhB;;;OAGG;IACH,YAAY,OAAgC;QACxC,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,EACV,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;IAC1D,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,GAAG,CACL,WAAmB,EACnB,GAAW,EACX,YAAqB;QAErB,MAAM,QAAQ,GAAG,GAAG,WAAW,IAAI,GAAG,EAAE,CAAC;QAEzC,IAAI,IAAI,CAAC,YAAY,EAAE,GAAG,CAAC,QAAQ,CAAC,EA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ities/errorhandler.ts","../utilities/httpclient.ts"],"version":"5.9.3"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Simple in-memory cache service implementation
3
+ */
4
+ export declare class CacheService {
5
+ private cache;
6
+ constructor();
7
+ /**
8
+ * Get a value from the cache
9
+ */
10
+ get<T>(key: string): T | undefined;
11
+ /**
12
+ * Set a value in the cache
13
+ */
14
+ set(key: string, value: any): void;
15
+ /**
16
+ * Check if a key exists in the cache
17
+ */
18
+ has(key: string): boolean;
19
+ /**
20
+ * Delete a value from the cache
21
+ */
22
+ delete(key: string): void;
23
+ /**
24
+ * Clear all values from the cache
25
+ */
26
+ clear(): void;
27
+ /**
28
+ * Delete all keys starting with a prefix
29
+ */
30
+ deleteByPrefix(prefix: string): void;
31
+ }
32
+ //# sourceMappingURL=cacheService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cacheService.d.ts","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,YAAY;IACrB,OAAO,CAAC,KAAK,CAAmB;;IAMhC;;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,GAAG,IAAI;IAIlC;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAOvC"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheService = void 0;
4
+ /**
5
+ * Simple in-memory cache service implementation
6
+ */
7
+ class CacheService {
8
+ constructor() {
9
+ this.cache = new Map();
10
+ }
11
+ /**
12
+ * Get a value from the cache
13
+ */
14
+ get(key) {
15
+ return this.cache.get(key);
16
+ }
17
+ /**
18
+ * Set a value in the cache
19
+ */
20
+ set(key, value) {
21
+ this.cache.set(key, value);
22
+ }
23
+ /**
24
+ * Check if a key exists in the cache
25
+ */
26
+ has(key) {
27
+ return this.cache.has(key);
28
+ }
29
+ /**
30
+ * Delete a value from the cache
31
+ */
32
+ delete(key) {
33
+ this.cache.delete(key);
34
+ }
35
+ /**
36
+ * Clear all values from the cache
37
+ */
38
+ clear() {
39
+ this.cache.clear();
40
+ }
41
+ /**
42
+ * Delete all keys starting with a prefix
43
+ */
44
+ deleteByPrefix(prefix) {
45
+ for (const key of this.cache.keys()) {
46
+ if (key.startsWith(prefix)) {
47
+ this.cache.delete(key);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ exports.CacheService = CacheService;
53
+ //# sourceMappingURL=cacheService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cacheService.js","sourceRoot":"","sources":["../../utilities/cacheService.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,YAAY;IAGrB;QACI,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,GAAG,CAAI,GAAW;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW,EAAE,KAAU;QACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAW;QACd,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,cAAc,CAAC,MAAc;QACzB,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;YAClC,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;gBACzB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC3B,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AApDD,oCAoDC"}
@@ -0,0 +1,20 @@
1
+ import { AxiosError } from 'axios';
2
+ export interface VoltEnvOptions {
3
+ apiKey: string;
4
+ baseURL?: string;
5
+ timeout?: number;
6
+ cache?: boolean;
7
+ onError?: (error: VoltEnvError) => void;
8
+ }
9
+ export type VoltEnvErrorCode = 'INVALID_API_KEY' | 'PERMISSION_DENIED' | 'NOT_FOUND' | 'RATE_LIMIT_EXCEEDED' | 'SERVER_ERROR' | 'NETWORK_ERROR' | 'REQUEST_ERROR' | 'UNKNOWN_ERROR';
10
+ export declare class VoltEnvError extends Error {
11
+ code: VoltEnvErrorCode;
12
+ statusCode?: number | undefined;
13
+ originalError?: any | undefined;
14
+ constructor(message: string, code: VoltEnvErrorCode, statusCode?: number | undefined, originalError?: any | undefined);
15
+ }
16
+ /**
17
+ * Helper function to transform Axios errors into VoltEnvErrors
18
+ */
19
+ export declare function handleAxiosError(error: AxiosError): VoltEnvError;
20
+ //# sourceMappingURL=errorHandler.d.ts.map
@@ -0,0 +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;CAC3C;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"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VoltEnvError = void 0;
4
+ exports.handleAxiosError = handleAxiosError;
5
+ class VoltEnvError extends Error {
6
+ constructor(message, code, statusCode, originalError) {
7
+ super(message);
8
+ this.code = code;
9
+ this.statusCode = statusCode;
10
+ this.originalError = originalError;
11
+ this.name = 'VoltEnvError';
12
+ Object.setPrototypeOf(this, VoltEnvError.prototype);
13
+ }
14
+ }
15
+ exports.VoltEnvError = VoltEnvError;
16
+ /**
17
+ * Helper function to transform Axios errors into VoltEnvErrors
18
+ */
19
+ function handleAxiosError(error) {
20
+ if (error.response) {
21
+ const { status, data } = error.response;
22
+ const message = data?.message || error.message;
23
+ let code = 'UNKNOWN_ERROR';
24
+ switch (status) {
25
+ case 401:
26
+ code = 'INVALID_API_KEY';
27
+ break;
28
+ case 403:
29
+ code = 'PERMISSION_DENIED';
30
+ break;
31
+ case 404:
32
+ code = 'NOT_FOUND';
33
+ break;
34
+ case 429:
35
+ code = 'RATE_LIMIT_EXCEEDED';
36
+ break;
37
+ case 500:
38
+ case 502:
39
+ case 503:
40
+ code = 'SERVER_ERROR';
41
+ break;
42
+ }
43
+ return new VoltEnvError(message, code, status, error);
44
+ }
45
+ else if (error.request) {
46
+ return new VoltEnvError('No response from server', 'NETWORK_ERROR', undefined, error);
47
+ }
48
+ else {
49
+ return new VoltEnvError(error.message, 'REQUEST_ERROR', undefined, error);
50
+ }
51
+ }
52
+ //# sourceMappingURL=errorHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorHandler.js","sourceRoot":"","sources":["../../utilities/errorHandler.ts"],"names":[],"mappings":";;;AAoCA,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,CAAkB;QACtB,eAAU,GAAV,UAAU,CAAS;QACnB,kBAAa,GAAb,aAAa,CAAM;QAG1B,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;IACxD,CAAC;CACJ;AAXD,oCAWC;AAED;;GAEG;AACH,SAAgB,gBAAgB,CAAC,KAAiB;IAC9C,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;QACjB,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC;QACxC,MAAM,OAAO,GAAI,IAAY,EAAE,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC;QACxD,IAAI,IAAI,GAAqB,eAAe,CAAC;QAE7C,QAAQ,MAAM,EAAE,CAAC;YACb,KAAK,GAAG;gBACJ,IAAI,GAAG,iBAAiB,CAAC;gBACzB,MAAM;YACV,KAAK,GAAG;gBACJ,IAAI,GAAG,mBAAmB,CAAC;gBAC3B,MAAM;YACV,KAAK,GAAG;gBACJ,IAAI,GAAG,WAAW,CAAC;gBACnB,MAAM;YACV,KAAK,GAAG;gBACJ,IAAI,GAAG,qBAAqB,CAAC;gBAC7B,MAAM;YACV,KAAK,GAAG,CAAC;YACT,KAAK,GAAG,CAAC;YACT,KAAK,GAAG;gBACJ,IAAI,GAAG,cAAc,CAAC;gBACtB,MAAM;QACd,CAAC;QAED,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAC1D,CAAC;SAAM,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QACvB,OAAO,IAAI,YAAY,CACnB,yBAAyB,EACzB,eAAe,EACf,SAAS,EACT,KAAK,CACR,CAAC;IACN,CAAC;SAAM,CAAC;QACJ,OAAO,IAAI,YAAY,CACnB,KAAK,CAAC,OAAO,EACb,eAAe,EACf,SAAS,EACT,KAAK,CACR,CAAC;IACN,CAAC;AACL,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { VoltEnvError } from './errorHandler';
2
+ export interface HttpClientOptions {
3
+ baseURL: string;
4
+ apiKey: string;
5
+ timeout?: number;
6
+ onError?: (error: VoltEnvError) => void;
7
+ }
8
+ export declare class HttpClient {
9
+ private client;
10
+ private options;
11
+ constructor(options: HttpClientOptions);
12
+ private setupInterceptors;
13
+ get<T>(url: string): Promise<T>;
14
+ post<T>(url: string, data?: any): Promise<T>;
15
+ put<T>(url: string, data?: any): Promise<T>;
16
+ delete<T>(url: string): Promise<T>;
17
+ }
18
+ //# sourceMappingURL=httpClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpClient.d.ts","sourceRoot":"","sources":["../../utilities/httpClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAoB,MAAM,gBAAgB,CAAC;AAEhE,MAAM,WAAW,iBAAiB;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;CAC3C;AAED,qBAAa,UAAU;IACnB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,OAAO,CAAoB;gBAEvB,OAAO,EAAE,iBAAiB;IActC,OAAO,CAAC,iBAAiB;IA0BnB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAK/B,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC;IAK5C,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC;IAK3C,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;CAI3C"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HttpClient = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const errorHandler_1 = require("./errorHandler");
9
+ class HttpClient {
10
+ constructor(options) {
11
+ this.options = options;
12
+ this.client = axios_1.default.create({
13
+ baseURL: options.baseURL,
14
+ timeout: options.timeout,
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ },
18
+ });
19
+ this.setupInterceptors();
20
+ }
21
+ setupInterceptors() {
22
+ // Request interceptor - Add API key to all requests
23
+ this.client.interceptors.request.use((config) => {
24
+ if (!config.headers) {
25
+ config.headers = {};
26
+ }
27
+ config.headers['Authorization'] = `Bearer ${this.options.apiKey}`;
28
+ return config;
29
+ }, (error) => Promise.reject(error));
30
+ // Response interceptor - Handle errors
31
+ this.client.interceptors.response.use((response) => response, (error) => {
32
+ const voltError = (0, errorHandler_1.handleAxiosError)(error);
33
+ if (this.options.onError) {
34
+ this.options.onError(voltError);
35
+ }
36
+ return Promise.reject(voltError);
37
+ });
38
+ }
39
+ async get(url) {
40
+ const response = await this.client.get(url);
41
+ return response.data;
42
+ }
43
+ async post(url, data) {
44
+ const response = await this.client.post(url, data);
45
+ return response.data;
46
+ }
47
+ async put(url, data) {
48
+ const response = await this.client.put(url, data);
49
+ return response.data;
50
+ }
51
+ async delete(url) {
52
+ const response = await this.client.delete(url);
53
+ return response.data;
54
+ }
55
+ }
56
+ exports.HttpClient = HttpClient;
57
+ //# sourceMappingURL=httpClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../utilities/httpClient.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAqF;AACrF,iDAAgE;AAShE,MAAa,UAAU;IAInB,YAAY,OAA0B;QAClC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,CAAC,MAAM,GAAG,eAAK,CAAC,MAAM,CAAC;YACvB,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,OAAO,EAAE;gBACL,cAAc,EAAE,kBAAkB;aACrC;SACJ,CAAC,CAAC;QAEH,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAEO,iBAAiB;QACrB,oDAAoD;QACpD,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAChC,CAAC,MAAkC,EAAE,EAAE;YACnC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBAClB,MAAM,CAAC,OAAO,GAAG,EAAS,CAAC;YAC/B,CAAC;YACD,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAClE,OAAO,MAAM,CAAC;QAClB,CAAC,EACD,CAAC,KAAK,EA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DD,gCA+DC"}
package/index.ts ADDED
@@ -0,0 +1,204 @@
1
+
2
+ import { VoltEnvError, VoltEnvOptions } from './utilities/errorHandler';
3
+ import { HttpClient } from './utilities/httpClient';
4
+ import { CacheService } from './utilities/cacheService';
5
+
6
+ export class VoltEnv {
7
+ private httpClient: HttpClient;
8
+ private cacheService: CacheService | null;
9
+
10
+ /**
11
+ * Initialize the VoltEnv SDK
12
+ * @param options Configuration options or API key string
13
+ */
14
+ constructor(options: VoltEnvOptions | string) {
15
+ const config = typeof options === 'string'
16
+ ? { apiKey: options }
17
+ : options;
18
+
19
+ const {
20
+ apiKey,
21
+ baseURL = 'https://api.voltenv.com',
22
+ timeout = 5000,
23
+ cache = true,
24
+ onError
25
+ } = config;
26
+
27
+ this.httpClient = new HttpClient({
28
+ apiKey,
29
+ baseURL,
30
+ timeout,
31
+ onError
32
+ });
33
+
34
+ this.cacheService = cache ? new CacheService() : null;
35
+ }
36
+
37
+ /**
38
+ * Get a variable from a specific environment
39
+ * @param environment The environment name (e.g., 'production', 'development')
40
+ * @param key The variable key
41
+ * @param defaultValue Optional default value if not found
42
+ * @returns The variable value or the default value
43
+ */
44
+ async get(
45
+ environment: string,
46
+ key: string,
47
+ defaultValue?: string
48
+ ): Promise<string | undefined> {
49
+ const cacheKey = `${environment}:${key}`;
50
+
51
+ if (this.cacheService?.has(cacheKey)) {
52
+ return this.cacheService.get<string>(cacheKey);
53
+ }
54
+
55
+ try {
56
+ const data = await this.httpClient.get<{ value: string }>(
57
+ `/v1/variables/${environment}/${key}`
58
+ );
59
+
60
+ const value = data.value;
61
+
62
+ if (value && this.cacheService) {
63
+ this.cacheService.set(cacheKey, value);
64
+ }
65
+
66
+ return value || defaultValue;
67
+ } catch (error) {
68
+ if (error instanceof VoltEnvError && error.code === 'NOT_FOUND') {
69
+ return defaultValue;
70
+ }
71
+ throw error;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Get all variables from an environment
77
+ * @param environment The environment name
78
+ * @returns A record of key-value pairs
79
+ */
80
+ async getAll(environment: string): Promise<Record<string, string>> {
81
+ const data = await this.httpClient.get<{ variables: Record<string, string> }>(
82
+ `/v1/variables/${environment}`
83
+ );
84
+
85
+ const variables = data.variables;
86
+
87
+ if (this.cacheService) {
88
+ Object.entries(variables).forEach(([key, value]) => {
89
+ this.cacheService!.set(`${environment}:${key}`, value);
90
+ });
91
+ }
92
+
93
+ return variables;
94
+ }
95
+
96
+ /**
97
+ * Set a variable in a specific environment
98
+ * @param environment The environment name
99
+ * @param key The variable key
100
+ * @param value The variable value
101
+ */
102
+ async set(
103
+ environment: string,
104
+ key: string,
105
+ value: string
106
+ ): Promise<void> {
107
+ await this.httpClient.put(`/v1/variables/${environment}/${key}`, {
108
+ value,
109
+ });
110
+
111
+ if (this.cacheService) {
112
+ this.cacheService.set(`${environment}:${key}`, value);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Set multiple variables at once
118
+ * @param environment The environment name
119
+ * @param variables A record of key-value pairs to set
120
+ */
121
+ async setMany(
122
+ environment: string,
123
+ variables: Record<string, string>
124
+ ): Promise<void> {
125
+ await this.httpClient.post(`/v1/variables/${environment}/batch`, {
126
+ variables,
127
+ });
128
+
129
+ if (this.cacheService) {
130
+ Object.entries(variables).forEach(([key, value]) => {
131
+ this.cacheService!.set(`${environment}:${key}`, value);
132
+ });
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Delete a variable from a specific environment
138
+ * @param environment The environment name
139
+ * @param key The variable key
140
+ */
141
+ async delete(environment: string, key: string): Promise<void> {
142
+ await this.httpClient.delete(`/v1/variables/${environment}/${key}`);
143
+
144
+ if (this.cacheService) {
145
+ this.cacheService.delete(`${environment}:${key}`);
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Delete multiple variables at once
151
+ * @param environment The environment name
152
+ * @param keys Array of keys to delete
153
+ */
154
+ async deleteMany(environment: string, keys: string[]): Promise<void> {
155
+ await this.httpClient.post(`/v1/variables/${environment}/batch-delete`, {
156
+ keys,
157
+ });
158
+
159
+ if (this.cacheService) {
160
+ keys.forEach(key => {
161
+ this.cacheService!.delete(`${environment}:${key}`);
162
+ });
163
+ }
164
+ }
165
+
166
+ /**
167
+ * List all environments accessible by this API key
168
+ * @returns Array of environment names
169
+ */
170
+ async listEnvironments(): Promise<string[]> {
171
+ const data = await this.httpClient.get<{ environments: string[] }>(
172
+ '/v1/environments'
173
+ );
174
+ return data.environments;
175
+ }
176
+
177
+ /**
178
+ * Sync/refresh variables from server (bypassing cache)
179
+ *If an environment is provided, it only syncs that environment.
180
+ * If no environment is provided, it lists all environments and syncs them all.
181
+ * @param environment Optional environment to sync
182
+ */
183
+ async sync(environment?: string): Promise<void> {
184
+ if (environment) {
185
+ const variables = await this.getAll(environment);
186
+
187
+ if (this.cacheService) {
188
+ this.cacheService.deleteByPrefix(`${environment}:`);
189
+
190
+ Object.entries(variables).forEach(([key, value]) => {
191
+ this.cacheService!.set(`${environment}:${key}`, value);
192
+ });
193
+ }
194
+ } else {
195
+ const environments = await this.listEnvironments();
196
+
197
+ for (const env of environments) {
198
+ await this.sync(env);
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ export default VoltEnv;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "voltenv-sdk",
3
+ "version": "1.0.0",
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": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [
13
+ "voltenv",
14
+ "sdk",
15
+ "environment-variables"
16
+ ],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "type": "commonjs",
20
+ "dependencies": {
21
+ "axios": "^1.13.2"
22
+ },
23
+ "devDependencies": {
24
+ "@types/express": "^5.0.6",
25
+ "@types/node": "^25.0.3",
26
+ "ts-node": "^10.9.2",
27
+ "typescript": "^5.9.3"
28
+ }
29
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "CommonJS",
5
+ "lib": [
6
+ "ES2020"
7
+ ],
8
+ "declaration": true,
9
+ "declarationMap": true,
10
+ "sourceMap": true,
11
+ "outDir": "./dist",
12
+ "rootDir": "./",
13
+ "strict": true,
14
+ "noImplicitAny": true,
15
+ "strictNullChecks": true,
16
+ "strictFunctionTypes": true,
17
+ "strictBindCallApply": true,
18
+ "strictPropertyInitialization": true,
19
+ "noImplicitThis": true,
20
+ "alwaysStrict": true,
21
+ "moduleResolution": "node",
22
+ "esModuleInterop": true,
23
+ "skipLibCheck": true,
24
+ "forceConsistentCasingInFileNames": true
25
+ },
26
+ "include": [
27
+ "./index.ts",
28
+ "./utilities/**/*"
29
+ ],
30
+ "exclude": [
31
+ "node_modules",
32
+ "dist"
33
+ ]
34
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Simple in-memory cache service implementation
3
+ */
4
+ export class CacheService {
5
+ private cache: Map<string, any>;
6
+
7
+ constructor() {
8
+ this.cache = new Map();
9
+ }
10
+
11
+ /**
12
+ * Get a value from the cache
13
+ */
14
+ get<T>(key: string): T | undefined {
15
+ return this.cache.get(key);
16
+ }
17
+
18
+ /**
19
+ * Set a value in the cache
20
+ */
21
+ set(key: string, value: any): void {
22
+ this.cache.set(key, value);
23
+ }
24
+
25
+ /**
26
+ * Check if a key exists in the cache
27
+ */
28
+ has(key: string): boolean {
29
+ return this.cache.has(key);
30
+ }
31
+
32
+ /**
33
+ * Delete a value from the cache
34
+ */
35
+ delete(key: string): void {
36
+ this.cache.delete(key);
37
+ }
38
+
39
+ /**
40
+ * Clear all values from the cache
41
+ */
42
+ clear(): void {
43
+ this.cache.clear();
44
+ }
45
+
46
+ /**
47
+ * Delete all keys starting with a prefix
48
+ */
49
+ deleteByPrefix(prefix: string): void {
50
+ for (const key of this.cache.keys()) {
51
+ if (key.startsWith(prefix)) {
52
+ this.cache.delete(key);
53
+ }
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,79 @@
1
+ import { AxiosError } from 'axios';
2
+
3
+ export interface VoltEnvOptions {
4
+ apiKey: string;
5
+ baseURL?: string;
6
+ timeout?: number;
7
+ cache?: boolean;
8
+ onError?: (error: VoltEnvError) => void;
9
+ }
10
+
11
+ export type VoltEnvErrorCode =
12
+ | 'INVALID_API_KEY'
13
+ | 'PERMISSION_DENIED'
14
+ | 'NOT_FOUND'
15
+ | 'RATE_LIMIT_EXCEEDED'
16
+ | 'SERVER_ERROR'
17
+ | 'NETWORK_ERROR'
18
+ | 'REQUEST_ERROR'
19
+ | 'UNKNOWN_ERROR';
20
+
21
+ export class VoltEnvError extends Error {
22
+ constructor(
23
+ message: string,
24
+ public code: VoltEnvErrorCode,
25
+ public statusCode?: number,
26
+ public originalError?: any
27
+ ) {
28
+ super(message);
29
+ this.name = 'VoltEnvError';
30
+ Object.setPrototypeOf(this, VoltEnvError.prototype);
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Helper function to transform Axios errors into VoltEnvErrors
36
+ */
37
+ export function handleAxiosError(error: AxiosError): VoltEnvError {
38
+ if (error.response) {
39
+ const { status, data } = error.response;
40
+ const message = (data as any)?.message || error.message;
41
+ let code: VoltEnvErrorCode = 'UNKNOWN_ERROR';
42
+
43
+ switch (status) {
44
+ case 401:
45
+ code = 'INVALID_API_KEY';
46
+ break;
47
+ case 403:
48
+ code = 'PERMISSION_DENIED';
49
+ break;
50
+ case 404:
51
+ code = 'NOT_FOUND';
52
+ break;
53
+ case 429:
54
+ code = 'RATE_LIMIT_EXCEEDED';
55
+ break;
56
+ case 500:
57
+ case 502:
58
+ case 503:
59
+ code = 'SERVER_ERROR';
60
+ break;
61
+ }
62
+
63
+ return new VoltEnvError(message, code, status, error);
64
+ } else if (error.request) {
65
+ return new VoltEnvError(
66
+ 'No response from server',
67
+ 'NETWORK_ERROR',
68
+ undefined,
69
+ error
70
+ );
71
+ } else {
72
+ return new VoltEnvError(
73
+ error.message,
74
+ 'REQUEST_ERROR',
75
+ undefined,
76
+ error
77
+ );
78
+ }
79
+ }
@@ -0,0 +1,74 @@
1
+ import axios, { AxiosInstance, AxiosError, InternalAxiosRequestConfig } from 'axios';
2
+ import { VoltEnvError, handleAxiosError } from './errorHandler';
3
+
4
+ export interface HttpClientOptions {
5
+ baseURL: string;
6
+ apiKey: string;
7
+ timeout?: number;
8
+ onError?: (error: VoltEnvError) => void;
9
+ }
10
+
11
+ export class HttpClient {
12
+ private client: AxiosInstance;
13
+ private options: HttpClientOptions;
14
+
15
+ constructor(options: HttpClientOptions) {
16
+ this.options = options;
17
+
18
+ this.client = axios.create({
19
+ baseURL: options.baseURL,
20
+ timeout: options.timeout,
21
+ headers: {
22
+ 'Content-Type': 'application/json',
23
+ },
24
+ });
25
+
26
+ this.setupInterceptors();
27
+ }
28
+
29
+ private setupInterceptors(): void {
30
+ // Request interceptor - Add API key to all requests
31
+ this.client.interceptors.request.use(
32
+ (config: InternalAxiosRequestConfig) => {
33
+ if (!config.headers) {
34
+ config.headers = {} as any;
35
+ }
36
+ config.headers['Authorization'] = `Bearer ${this.options.apiKey}`;
37
+ return config;
38
+ },
39
+ (error) => Promise.reject(error)
40
+ );
41
+
42
+ // Response interceptor - Handle errors
43
+ this.client.interceptors.response.use(
44
+ (response) => response,
45
+ (error: AxiosError) => {
46
+ const voltError = handleAxiosError(error);
47
+ if (this.options.onError) {
48
+ this.options.onError(voltError);
49
+ }
50
+ return Promise.reject(voltError);
51
+ }
52
+ );
53
+ }
54
+
55
+ async get<T>(url: string): Promise<T> {
56
+ const response = await this.client.get<T>(url);
57
+ return response.data;
58
+ }
59
+
60
+ async post<T>(url: string, data?: any): Promise<T> {
61
+ const response = await this.client.post<T>(url, data);
62
+ return response.data;
63
+ }
64
+
65
+ async put<T>(url: string, data?: any): Promise<T> {
66
+ const response = await this.client.put<T>(url, data);
67
+ return response.data;
68
+ }
69
+
70
+ async delete<T>(url: string): Promise<T> {
71
+ const response = await this.client.delete<T>(url);
72
+ return response.data;
73
+ }
74
+ }
package/verify.ts ADDED
@@ -0,0 +1,18 @@
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();