keyenv 0.1.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 CHANGED
@@ -103,6 +103,19 @@ for (const env of project.environments) {
103
103
  }
104
104
  ```
105
105
 
106
+ ### Service Token Info
107
+
108
+ ```typescript
109
+ // Get current user or service token info
110
+ const user = await client.getCurrentUser();
111
+
112
+ if (user.auth_type === 'service_token') {
113
+ // Service tokens can access multiple projects
114
+ console.log('Projects:', user.project_ids);
115
+ console.log('Scopes:', user.scopes);
116
+ }
117
+ ```
118
+
106
119
  ## Error Handling
107
120
 
108
121
  ```typescript
@@ -156,6 +169,12 @@ Create a new KeyEnv client.
156
169
  | `bulkImport(projectId, env, secrets)` | Bulk import secrets |
157
170
  | `loadEnv(projectId, env)` | Load secrets into process.env |
158
171
  | `generateEnvFile(projectId, env)` | Generate .env file content |
172
+ | `listPermissions(projectId, env)` | List permissions for an environment |
173
+ | `setPermission(projectId, env, userId, role)` | Set user's permission |
174
+ | `deletePermission(projectId, env, userId)` | Delete user's permission |
175
+ | `getMyPermissions(projectId)` | Get current user's permissions |
176
+ | `getProjectDefaults(projectId)` | Get default permissions |
177
+ | `setProjectDefaults(projectId, defaults)` | Set default permissions |
159
178
 
160
179
  ## License
161
180
 
package/dist/index.d.mts CHANGED
@@ -4,6 +4,10 @@ interface KeyEnvOptions {
4
4
  token: string;
5
5
  /** Request timeout in milliseconds (default: 30000) */
6
6
  timeout?: number;
7
+ /** Cache TTL in seconds for exportSecrets/loadEnv (default: 0 = disabled). Also configurable via KEYENV_CACHE_TTL env var. */
8
+ cacheTtl?: number;
9
+ /** Custom API base URL (default: https://api.keyenv.dev). Also configurable via KEYENV_API_URL env var. */
10
+ baseUrl?: string;
7
11
  }
8
12
  /** User or service token info */
9
13
  interface User {
@@ -16,8 +20,8 @@ interface User {
16
20
  auth_type?: 'service_token' | 'user';
17
21
  /** Team ID (for service tokens) */
18
22
  team_id?: string;
19
- /** Project ID (for project-scoped service tokens) */
20
- project_id?: string;
23
+ /** Project IDs (for project-scoped service tokens) */
24
+ project_ids?: string[];
21
25
  /** Token scopes (for service tokens) */
22
26
  scopes?: string[];
23
27
  created_at: string;
@@ -87,6 +91,42 @@ declare class KeyEnvError extends Error {
87
91
  readonly details?: Record<string, unknown>;
88
92
  constructor(message: string, status: number, code?: string, details?: Record<string, unknown>);
89
93
  }
94
+ /** Environment permission role */
95
+ type EnvironmentRole = 'none' | 'read' | 'write' | 'admin';
96
+ /** Environment permission for a user */
97
+ interface EnvironmentPermission {
98
+ id: string;
99
+ environment_id: string;
100
+ user_id: string;
101
+ role: EnvironmentRole;
102
+ user_email?: string;
103
+ user_name?: string;
104
+ granted_by?: string;
105
+ created_at: string;
106
+ updated_at: string;
107
+ }
108
+ /** User's permission for an environment */
109
+ interface MyPermission {
110
+ environment_id: string;
111
+ environment_name: string;
112
+ role: EnvironmentRole;
113
+ can_read: boolean;
114
+ can_write: boolean;
115
+ can_admin: boolean;
116
+ }
117
+ /** Response for getting user's permissions */
118
+ interface MyPermissionsResponse {
119
+ permissions: MyPermission[];
120
+ is_team_admin: boolean;
121
+ }
122
+ /** Project default permission for an environment */
123
+ interface ProjectDefault {
124
+ id: string;
125
+ project_id: string;
126
+ environment_name: string;
127
+ default_role: EnvironmentRole;
128
+ created_at: string;
129
+ }
90
130
 
91
131
  /**
92
132
  * KeyEnv API client for managing secrets
@@ -103,7 +143,10 @@ declare class KeyEnvError extends Error {
103
143
  */
104
144
  declare class KeyEnv {
105
145
  private readonly token;
146
+ private readonly baseUrl;
106
147
  private readonly timeout;
148
+ private readonly cacheTtl;
149
+ private readonly secretsCache;
107
150
  constructor(options: KeyEnvOptions);
108
151
  private request;
109
152
  /** Get the current user or service token info */
@@ -127,7 +170,8 @@ declare class KeyEnv {
127
170
  /** List secrets in an environment (keys and metadata only) */
128
171
  listSecrets(projectId: string, environment: string): Promise<Secret[]>;
129
172
  /**
130
- * Export all secrets with their decrypted values
173
+ * Export all secrets with their decrypted values.
174
+ * Results are cached when cacheTtl > 0.
131
175
  * @example
132
176
  * ```ts
133
177
  * const secrets = await client.exportSecrets('project-id', 'production');
@@ -182,6 +226,112 @@ declare class KeyEnv {
182
226
  loadEnv(projectId: string, environment: string): Promise<number>;
183
227
  /** Generate .env file content from secrets */
184
228
  generateEnvFile(projectId: string, environment: string): Promise<string>;
229
+ /**
230
+ * Clear the secrets cache.
231
+ * @param projectId - Clear cache for specific project (optional)
232
+ * @param environment - Clear cache for specific environment (requires projectId)
233
+ */
234
+ clearCache(projectId?: string, environment?: string): void;
235
+ /**
236
+ * List all permissions for an environment.
237
+ * @param projectId - The project ID
238
+ * @param environment - The environment name
239
+ * @returns Array of environment permissions
240
+ * @example
241
+ * ```ts
242
+ * const permissions = await client.listPermissions('project-id', 'production');
243
+ * for (const perm of permissions) {
244
+ * console.log(`${perm.user_email}: ${perm.role}`);
245
+ * }
246
+ * ```
247
+ */
248
+ listPermissions(projectId: string, environment: string): Promise<EnvironmentPermission[]>;
249
+ /**
250
+ * Set a user's permission for an environment.
251
+ * @param projectId - The project ID
252
+ * @param environment - The environment name
253
+ * @param userId - The user ID to set permission for
254
+ * @param role - The permission role ('none', 'read', 'write', or 'admin')
255
+ * @returns The created or updated permission
256
+ * @example
257
+ * ```ts
258
+ * const permission = await client.setPermission('project-id', 'production', 'user-id', 'write');
259
+ * console.log(`Set ${permission.user_email} to ${permission.role}`);
260
+ * ```
261
+ */
262
+ setPermission(projectId: string, environment: string, userId: string, role: EnvironmentRole): Promise<EnvironmentPermission>;
263
+ /**
264
+ * Delete a user's permission for an environment.
265
+ * @param projectId - The project ID
266
+ * @param environment - The environment name
267
+ * @param userId - The user ID to delete permission for
268
+ * @example
269
+ * ```ts
270
+ * await client.deletePermission('project-id', 'production', 'user-id');
271
+ * ```
272
+ */
273
+ deletePermission(projectId: string, environment: string, userId: string): Promise<void>;
274
+ /**
275
+ * Bulk set permissions for multiple users in an environment.
276
+ * @param projectId - The project ID
277
+ * @param environment - The environment name
278
+ * @param permissions - Array of user permissions to set
279
+ * @returns Array of created or updated permissions
280
+ * @example
281
+ * ```ts
282
+ * const permissions = await client.bulkSetPermissions('project-id', 'production', [
283
+ * { userId: 'user-1', role: 'write' },
284
+ * { userId: 'user-2', role: 'read' },
285
+ * ]);
286
+ * ```
287
+ */
288
+ bulkSetPermissions(projectId: string, environment: string, permissions: Array<{
289
+ userId: string;
290
+ role: EnvironmentRole;
291
+ }>): Promise<EnvironmentPermission[]>;
292
+ /**
293
+ * Get the current user's permissions for all environments in a project.
294
+ * @param projectId - The project ID
295
+ * @returns The user's permissions and team admin status
296
+ * @example
297
+ * ```ts
298
+ * const { permissions, is_team_admin } = await client.getMyPermissions('project-id');
299
+ * for (const perm of permissions) {
300
+ * console.log(`${perm.environment_name}: ${perm.role} (can_write: ${perm.can_write})`);
301
+ * }
302
+ * ```
303
+ */
304
+ getMyPermissions(projectId: string): Promise<MyPermissionsResponse>;
305
+ /**
306
+ * Get default permission settings for a project's environments.
307
+ * @param projectId - The project ID
308
+ * @returns Array of project default permissions
309
+ * @example
310
+ * ```ts
311
+ * const defaults = await client.getProjectDefaults('project-id');
312
+ * for (const def of defaults) {
313
+ * console.log(`${def.environment_name}: ${def.default_role}`);
314
+ * }
315
+ * ```
316
+ */
317
+ getProjectDefaults(projectId: string): Promise<ProjectDefault[]>;
318
+ /**
319
+ * Set default permission settings for a project's environments.
320
+ * @param projectId - The project ID
321
+ * @param defaults - Array of default permissions to set
322
+ * @returns Array of updated project default permissions
323
+ * @example
324
+ * ```ts
325
+ * const defaults = await client.setProjectDefaults('project-id', [
326
+ * { environmentName: 'development', defaultRole: 'write' },
327
+ * { environmentName: 'production', defaultRole: 'read' },
328
+ * ]);
329
+ * ```
330
+ */
331
+ setProjectDefaults(projectId: string, defaults: Array<{
332
+ environmentName: string;
333
+ defaultRole: EnvironmentRole;
334
+ }>): Promise<ProjectDefault[]>;
185
335
  }
186
336
 
187
- export { type BulkImportResult, type BulkSecretItem, type Environment, KeyEnv, KeyEnvError, type KeyEnvOptions, type Project, type ProjectWithEnvironments, type Secret, type SecretHistory, type SecretWithValue, type User };
337
+ export { type BulkImportResult, type BulkSecretItem, type Environment, type EnvironmentPermission, type EnvironmentRole, KeyEnv, KeyEnvError, type KeyEnvOptions, type MyPermission, type MyPermissionsResponse, type Project, type ProjectDefault, type ProjectWithEnvironments, type Secret, type SecretHistory, type SecretWithValue, type User };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ interface KeyEnvOptions {
4
4
  token: string;
5
5
  /** Request timeout in milliseconds (default: 30000) */
6
6
  timeout?: number;
7
+ /** Cache TTL in seconds for exportSecrets/loadEnv (default: 0 = disabled). Also configurable via KEYENV_CACHE_TTL env var. */
8
+ cacheTtl?: number;
9
+ /** Custom API base URL (default: https://api.keyenv.dev). Also configurable via KEYENV_API_URL env var. */
10
+ baseUrl?: string;
7
11
  }
8
12
  /** User or service token info */
9
13
  interface User {
@@ -16,8 +20,8 @@ interface User {
16
20
  auth_type?: 'service_token' | 'user';
17
21
  /** Team ID (for service tokens) */
18
22
  team_id?: string;
19
- /** Project ID (for project-scoped service tokens) */
20
- project_id?: string;
23
+ /** Project IDs (for project-scoped service tokens) */
24
+ project_ids?: string[];
21
25
  /** Token scopes (for service tokens) */
22
26
  scopes?: string[];
23
27
  created_at: string;
@@ -87,6 +91,42 @@ declare class KeyEnvError extends Error {
87
91
  readonly details?: Record<string, unknown>;
88
92
  constructor(message: string, status: number, code?: string, details?: Record<string, unknown>);
89
93
  }
94
+ /** Environment permission role */
95
+ type EnvironmentRole = 'none' | 'read' | 'write' | 'admin';
96
+ /** Environment permission for a user */
97
+ interface EnvironmentPermission {
98
+ id: string;
99
+ environment_id: string;
100
+ user_id: string;
101
+ role: EnvironmentRole;
102
+ user_email?: string;
103
+ user_name?: string;
104
+ granted_by?: string;
105
+ created_at: string;
106
+ updated_at: string;
107
+ }
108
+ /** User's permission for an environment */
109
+ interface MyPermission {
110
+ environment_id: string;
111
+ environment_name: string;
112
+ role: EnvironmentRole;
113
+ can_read: boolean;
114
+ can_write: boolean;
115
+ can_admin: boolean;
116
+ }
117
+ /** Response for getting user's permissions */
118
+ interface MyPermissionsResponse {
119
+ permissions: MyPermission[];
120
+ is_team_admin: boolean;
121
+ }
122
+ /** Project default permission for an environment */
123
+ interface ProjectDefault {
124
+ id: string;
125
+ project_id: string;
126
+ environment_name: string;
127
+ default_role: EnvironmentRole;
128
+ created_at: string;
129
+ }
90
130
 
91
131
  /**
92
132
  * KeyEnv API client for managing secrets
@@ -103,7 +143,10 @@ declare class KeyEnvError extends Error {
103
143
  */
104
144
  declare class KeyEnv {
105
145
  private readonly token;
146
+ private readonly baseUrl;
106
147
  private readonly timeout;
148
+ private readonly cacheTtl;
149
+ private readonly secretsCache;
107
150
  constructor(options: KeyEnvOptions);
108
151
  private request;
109
152
  /** Get the current user or service token info */
@@ -127,7 +170,8 @@ declare class KeyEnv {
127
170
  /** List secrets in an environment (keys and metadata only) */
128
171
  listSecrets(projectId: string, environment: string): Promise<Secret[]>;
129
172
  /**
130
- * Export all secrets with their decrypted values
173
+ * Export all secrets with their decrypted values.
174
+ * Results are cached when cacheTtl > 0.
131
175
  * @example
132
176
  * ```ts
133
177
  * const secrets = await client.exportSecrets('project-id', 'production');
@@ -182,6 +226,112 @@ declare class KeyEnv {
182
226
  loadEnv(projectId: string, environment: string): Promise<number>;
183
227
  /** Generate .env file content from secrets */
184
228
  generateEnvFile(projectId: string, environment: string): Promise<string>;
229
+ /**
230
+ * Clear the secrets cache.
231
+ * @param projectId - Clear cache for specific project (optional)
232
+ * @param environment - Clear cache for specific environment (requires projectId)
233
+ */
234
+ clearCache(projectId?: string, environment?: string): void;
235
+ /**
236
+ * List all permissions for an environment.
237
+ * @param projectId - The project ID
238
+ * @param environment - The environment name
239
+ * @returns Array of environment permissions
240
+ * @example
241
+ * ```ts
242
+ * const permissions = await client.listPermissions('project-id', 'production');
243
+ * for (const perm of permissions) {
244
+ * console.log(`${perm.user_email}: ${perm.role}`);
245
+ * }
246
+ * ```
247
+ */
248
+ listPermissions(projectId: string, environment: string): Promise<EnvironmentPermission[]>;
249
+ /**
250
+ * Set a user's permission for an environment.
251
+ * @param projectId - The project ID
252
+ * @param environment - The environment name
253
+ * @param userId - The user ID to set permission for
254
+ * @param role - The permission role ('none', 'read', 'write', or 'admin')
255
+ * @returns The created or updated permission
256
+ * @example
257
+ * ```ts
258
+ * const permission = await client.setPermission('project-id', 'production', 'user-id', 'write');
259
+ * console.log(`Set ${permission.user_email} to ${permission.role}`);
260
+ * ```
261
+ */
262
+ setPermission(projectId: string, environment: string, userId: string, role: EnvironmentRole): Promise<EnvironmentPermission>;
263
+ /**
264
+ * Delete a user's permission for an environment.
265
+ * @param projectId - The project ID
266
+ * @param environment - The environment name
267
+ * @param userId - The user ID to delete permission for
268
+ * @example
269
+ * ```ts
270
+ * await client.deletePermission('project-id', 'production', 'user-id');
271
+ * ```
272
+ */
273
+ deletePermission(projectId: string, environment: string, userId: string): Promise<void>;
274
+ /**
275
+ * Bulk set permissions for multiple users in an environment.
276
+ * @param projectId - The project ID
277
+ * @param environment - The environment name
278
+ * @param permissions - Array of user permissions to set
279
+ * @returns Array of created or updated permissions
280
+ * @example
281
+ * ```ts
282
+ * const permissions = await client.bulkSetPermissions('project-id', 'production', [
283
+ * { userId: 'user-1', role: 'write' },
284
+ * { userId: 'user-2', role: 'read' },
285
+ * ]);
286
+ * ```
287
+ */
288
+ bulkSetPermissions(projectId: string, environment: string, permissions: Array<{
289
+ userId: string;
290
+ role: EnvironmentRole;
291
+ }>): Promise<EnvironmentPermission[]>;
292
+ /**
293
+ * Get the current user's permissions for all environments in a project.
294
+ * @param projectId - The project ID
295
+ * @returns The user's permissions and team admin status
296
+ * @example
297
+ * ```ts
298
+ * const { permissions, is_team_admin } = await client.getMyPermissions('project-id');
299
+ * for (const perm of permissions) {
300
+ * console.log(`${perm.environment_name}: ${perm.role} (can_write: ${perm.can_write})`);
301
+ * }
302
+ * ```
303
+ */
304
+ getMyPermissions(projectId: string): Promise<MyPermissionsResponse>;
305
+ /**
306
+ * Get default permission settings for a project's environments.
307
+ * @param projectId - The project ID
308
+ * @returns Array of project default permissions
309
+ * @example
310
+ * ```ts
311
+ * const defaults = await client.getProjectDefaults('project-id');
312
+ * for (const def of defaults) {
313
+ * console.log(`${def.environment_name}: ${def.default_role}`);
314
+ * }
315
+ * ```
316
+ */
317
+ getProjectDefaults(projectId: string): Promise<ProjectDefault[]>;
318
+ /**
319
+ * Set default permission settings for a project's environments.
320
+ * @param projectId - The project ID
321
+ * @param defaults - Array of default permissions to set
322
+ * @returns Array of updated project default permissions
323
+ * @example
324
+ * ```ts
325
+ * const defaults = await client.setProjectDefaults('project-id', [
326
+ * { environmentName: 'development', defaultRole: 'write' },
327
+ * { environmentName: 'production', defaultRole: 'read' },
328
+ * ]);
329
+ * ```
330
+ */
331
+ setProjectDefaults(projectId: string, defaults: Array<{
332
+ environmentName: string;
333
+ defaultRole: EnvironmentRole;
334
+ }>): Promise<ProjectDefault[]>;
185
335
  }
186
336
 
187
- export { type BulkImportResult, type BulkSecretItem, type Environment, KeyEnv, KeyEnvError, type KeyEnvOptions, type Project, type ProjectWithEnvironments, type Secret, type SecretHistory, type SecretWithValue, type User };
337
+ export { type BulkImportResult, type BulkSecretItem, type Environment, type EnvironmentPermission, type EnvironmentRole, KeyEnv, KeyEnvError, type KeyEnvOptions, type MyPermission, type MyPermissionsResponse, type Project, type ProjectDefault, type ProjectWithEnvironments, type Secret, type SecretHistory, type SecretWithValue, type User };