@enkryptify/sdk 0.1.0 → 0.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.
Files changed (2) hide show
  1. package/README.md +160 -19
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -20,42 +20,183 @@ npm install @enkryptify/sdk
20
20
  yarn add @enkryptify/sdk
21
21
  ```
22
22
 
23
- ## Usage
23
+ ## Quick Start
24
24
 
25
25
  ```typescript
26
26
  import Enkryptify from "@enkryptify/sdk";
27
27
 
28
28
  const client = new Enkryptify({
29
- apiKey: "your-api-key",
30
- workspaceId: "your-workspace-id",
31
- projectId: "your-project-id",
32
- environment: "production",
29
+ auth: Enkryptify.fromEnv(),
30
+ workspace: "my-workspace",
31
+ project: "my-project",
32
+ environment: "env-id",
33
33
  });
34
34
 
35
35
  const dbUrl = await client.get("DATABASE_URL");
36
- console.log(dbUrl);
37
36
  ```
38
37
 
38
+ ## Usage
39
+
40
+ ### Preloading Secrets
41
+
42
+ When caching is enabled (the default), you can preload all secrets up front. This makes subsequent `get()` and `getFromCache()` calls instant.
43
+
44
+ ```typescript
45
+ const client = new Enkryptify({
46
+ auth: Enkryptify.fromEnv(),
47
+ workspace: "my-workspace",
48
+ project: "my-project",
49
+ environment: "env-id",
50
+ });
51
+
52
+ await client.preload();
53
+
54
+ // Synchronous — no API call needed
55
+ const dbHost = client.getFromCache("DB_HOST");
56
+ const dbPort = client.getFromCache("DB_PORT");
57
+ ```
58
+
59
+ ### Eager Caching
60
+
61
+ By default `cache.eager` is `true`. This means the first `get()` call fetches _all_ secrets and caches them, so subsequent calls are served from the cache without additional API requests.
62
+
63
+ ```typescript
64
+ // First call fetches all secrets from the API
65
+ const dbHost = await client.get("DB_HOST");
66
+
67
+ // Second call is served from cache — no API call
68
+ const dbPort = await client.get("DB_PORT");
69
+ ```
70
+
71
+ Set `cache.eager` to `false` to fetch secrets individually:
72
+
73
+ ```typescript
74
+ const client = new Enkryptify({
75
+ auth: Enkryptify.fromEnv(),
76
+ workspace: "my-workspace",
77
+ project: "my-project",
78
+ environment: "env-id",
79
+ cache: { eager: false },
80
+ });
81
+
82
+ // Each call fetches only the requested secret
83
+ const dbHost = await client.get("DB_HOST");
84
+ const dbPort = await client.get("DB_PORT");
85
+ ```
86
+
87
+ ### Bypassing the Cache
88
+
89
+ Pass `{ cache: false }` to always fetch a fresh value from the API:
90
+
91
+ ```typescript
92
+ const secret = await client.get("ROTATING_KEY", { cache: false });
93
+ ```
94
+
95
+ ### Strict vs Non-Strict Mode
96
+
97
+ By default, `get()` throws a `SecretNotFoundError` when a key doesn't exist. Disable strict mode to return an empty string instead:
98
+
99
+ ```typescript
100
+ const client = new Enkryptify({
101
+ auth: Enkryptify.fromEnv(),
102
+ workspace: "my-workspace",
103
+ project: "my-project",
104
+ environment: "env-id",
105
+ options: { strict: false },
106
+ });
107
+
108
+ const value = await client.get("MAYBE_MISSING"); // "" if not found
109
+ ```
110
+
111
+ ### Personal Values
112
+
113
+ When `usePersonalValues` is `true` (the default), the SDK prefers your personal override for a secret. If no personal value exists, it falls back to the shared value.
114
+
115
+ ```typescript
116
+ const client = new Enkryptify({
117
+ auth: Enkryptify.fromEnv(),
118
+ workspace: "my-workspace",
119
+ project: "my-project",
120
+ environment: "env-id",
121
+ options: { usePersonalValues: false }, // always use shared values
122
+ });
123
+ ```
124
+
125
+ ### Cleanup
126
+
127
+ Destroy the client when you're done to clear all cached secrets from memory:
128
+
129
+ ```typescript
130
+ client.destroy();
131
+ ```
132
+
133
+ ## Configuration
134
+
135
+ | Option | Type | Default | Description |
136
+ | --------------------------- | ---------------------------------------- | ------------------------------ | ------------------------------------------------ |
137
+ | `auth` | `EnkryptifyAuthProvider` | _required_ | Auth provider created via `Enkryptify.fromEnv()` |
138
+ | `workspace` | `string` | _required_ | Workspace slug or ID |
139
+ | `project` | `string` | _required_ | Project slug or ID |
140
+ | `environment` | `string` | _required_ | Environment ID |
141
+ | `baseUrl` | `string` | `"https://api.enkryptify.com"` | API base URL |
142
+ | `options.strict` | `boolean` | `true` | Throw on missing secrets |
143
+ | `options.usePersonalValues` | `boolean` | `true` | Prefer personal secret values |
144
+ | `cache.enabled` | `boolean` | `true` | Enable in-memory caching |
145
+ | `cache.ttl` | `number` | `-1` | Cache TTL in ms (`-1` = never expire) |
146
+ | `cache.eager` | `boolean` | `true` | Fetch all secrets on first `get()` |
147
+ | `logger.level` | `"debug" \| "info" \| "warn" \| "error"` | `"info"` | Minimum log level |
148
+
39
149
  ## API Reference
40
150
 
41
- ### `new Enkryptify(config)`
151
+ ### `Enkryptify.fromEnv(): EnkryptifyAuthProvider`
152
+
153
+ Creates an auth provider by reading the `ENKRYPTIFY_TOKEN` environment variable.
154
+
155
+ ### `client.get(key, options?): Promise<string>`
156
+
157
+ Fetches a secret by key. Uses the cache when available, otherwise calls the API.
42
158
 
43
- Creates a new Enkryptify client.
159
+ - `key` the secret name
160
+ - `options.cache` — set to `false` to bypass the cache (default: `true`)
44
161
 
45
- | Parameter | Type | Description |
46
- | ------------- | -------- | ------------------------------------- |
47
- | `apiKey` | `string` | Your Enkryptify API key |
48
- | `workspaceId` | `string` | The workspace ID |
49
- | `projectId` | `string` | The project ID |
50
- | `environment` | `string` | The environment (e.g. `"production"`) |
162
+ ### `client.getFromCache(key): string`
51
163
 
52
- ### `client.get(key): Promise<string>`
164
+ Returns a secret from the cache synchronously. Throws if the key is not cached or caching is disabled.
53
165
 
54
- Fetches a secret by key. Throws `EnkryptifyError` if the secret is not found.
166
+ ### `client.preload(): Promise<void>`
55
167
 
56
- ### `EnkryptifyError`
168
+ Fetches all secrets and populates the cache. Throws if caching is disabled.
169
+
170
+ ### `client.destroy(): void`
171
+
172
+ Clears the cache and marks the client as destroyed. All subsequent method calls will throw.
173
+
174
+ ## Error Handling
175
+
176
+ The SDK provides specific error classes so you can handle different failure modes:
177
+
178
+ ```typescript
179
+ import Enkryptify, { SecretNotFoundError, AuthenticationError, ApiError } from "@enkryptify/sdk";
180
+
181
+ try {
182
+ const value = await client.get("MY_SECRET");
183
+ } catch (error) {
184
+ if (error instanceof SecretNotFoundError) {
185
+ // Secret doesn't exist in the project/environment
186
+ } else if (error instanceof AuthenticationError) {
187
+ // Token is invalid or expired (HTTP 401/403)
188
+ } else if (error instanceof ApiError) {
189
+ // Other API error (500, network issues, etc.)
190
+ }
191
+ }
192
+ ```
57
193
 
58
- Custom error class for all SDK errors.
194
+ | Error Class | When |
195
+ | --------------------- | ----------------------------------------------- |
196
+ | `EnkryptifyError` | Base class for all SDK errors |
197
+ | `SecretNotFoundError` | Secret key not found in the project/environment |
198
+ | `AuthenticationError` | HTTP 401 or 403 from the API |
199
+ | `ApiError` | Any other non-OK HTTP response |
59
200
 
60
201
  ## Development
61
202
 
@@ -79,7 +220,7 @@ pnpm lint
79
220
  pnpm format
80
221
 
81
222
  # Typecheck
82
- pnpm check
223
+ pnpm typecheck
83
224
  ```
84
225
 
85
226
  ## Contributing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enkryptify/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Official TypeScript SDK for Enkryptify — fetch and manage secrets from the Enkryptify API",
5
5
  "keywords": [
6
6
  "api",