learn-secrets-sdk 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,385 @@
1
- export { SecretsSDK } from './client.js';
2
- export { SecretsManagement } from './management.js';
3
- export { resolveEnvString, resolveEnvInSecret, resolveEnvInSecrets, loadSecretsConfig } from './env-resolver.js';
4
- export { SecretsSDKError, OriginMismatchError, RateLimitError, InvalidTokenError } from './types.js';
5
- export type { SecretsSDKOptions, ProxyRequest, ProxyResponse, RateLimitInfo, SecretsManagementOptions, SecretConfig, MaskedSecret, SyncOptions, SyncResult, CLICredentials } from './types.js';
1
+ interface SecretsSDKOptions {
2
+ /**
3
+ * App ID from dashboard (optional in zero-config mode)
4
+ * If omitted, SDK uses origin-based authentication
5
+ */
6
+ appId?: string;
7
+ /**
8
+ * SDK token (optional in zero-config mode)
9
+ * If omitted, SDK uses origin-based authentication
10
+ */
11
+ token?: string;
12
+ sessionToken?: string;
13
+ /**
14
+ * Base URL of the proxy server
15
+ * Default: https://ctklearn.carsontkempf.workers.dev
16
+ */
17
+ baseUrl?: string;
18
+ timeout?: number;
19
+ retryOn429?: boolean;
20
+ }
21
+ interface ProxyRequest {
22
+ keyName: string;
23
+ endpoint: string;
24
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
25
+ body?: any;
26
+ headers?: Record<string, string>;
27
+ }
28
+ interface ProxyResponse<T = any> {
29
+ success: boolean;
30
+ status: number;
31
+ data: T;
32
+ }
33
+ interface RateLimitInfo {
34
+ limit: number;
35
+ remaining: number;
36
+ reset: number;
37
+ }
38
+ declare class SecretsSDKError extends Error {
39
+ status: number;
40
+ response?: any | undefined;
41
+ constructor(message: string, status: number, response?: any | undefined);
42
+ }
43
+ declare class OriginMismatchError extends SecretsSDKError {
44
+ constructor(message?: string);
45
+ }
46
+ declare class RateLimitError extends SecretsSDKError {
47
+ retryAfter: number;
48
+ remaining: number;
49
+ limit: number;
50
+ constructor(message?: string, retryAfter?: number, remaining?: number, limit?: number);
51
+ }
52
+ declare class InvalidTokenError extends SecretsSDKError {
53
+ constructor(message?: string);
54
+ }
55
+ interface SecretsManagementOptions {
56
+ appId: string;
57
+ baseUrl?: string;
58
+ timeout?: number;
59
+ }
60
+ interface SecretConfig {
61
+ name: string;
62
+ provider: string;
63
+ api_key: string;
64
+ base_url?: string;
65
+ auth_header?: string;
66
+ auth_prefix?: string;
67
+ }
68
+ interface MaskedSecret {
69
+ id: string;
70
+ name: string;
71
+ provider: string;
72
+ api_key: string;
73
+ base_url?: string;
74
+ auth_header?: string;
75
+ auth_prefix?: string;
76
+ created?: string;
77
+ updated?: string;
78
+ }
79
+ interface SyncOptions {
80
+ deleteMissing?: boolean;
81
+ dryRun?: boolean;
82
+ }
83
+ interface SyncDiffItem {
84
+ name: string;
85
+ provider?: string;
86
+ changes?: string[];
87
+ }
88
+ interface SyncResult {
89
+ success: boolean;
90
+ diff: {
91
+ created: SyncDiffItem[];
92
+ updated: SyncDiffItem[];
93
+ deleted: SyncDiffItem[];
94
+ unchanged: SyncDiffItem[];
95
+ };
96
+ summary: {
97
+ created: number;
98
+ updated: number;
99
+ deleted: number;
100
+ unchanged: number;
101
+ };
102
+ }
103
+ interface CLICredentials {
104
+ access_token: string;
105
+ refresh_token: string;
106
+ expires_at: string;
107
+ user_id: string;
108
+ }
109
+
110
+ declare class SecretsSDK {
111
+ private appId;
112
+ private token;
113
+ private baseUrl;
114
+ private timeout;
115
+ private retryOn429;
116
+ private rateLimitInfo;
117
+ private zeroConfigMode;
118
+ /**
119
+ * Create a new SecretsSDK instance
120
+ *
121
+ * Zero-config mode (recommended for static sites):
122
+ * const sdk = new SecretsSDK();
123
+ * // Origin header is used for authentication
124
+ *
125
+ * Token mode (for backward compatibility):
126
+ * const sdk = new SecretsSDK({ appId: '...', token: 'sk_live_...' });
127
+ */
128
+ constructor(options?: SecretsSDKOptions);
129
+ /**
130
+ * Get current rate limit information
131
+ */
132
+ getUsage(): RateLimitInfo | null;
133
+ /**
134
+ * Parse rate limit headers from response
135
+ */
136
+ private parseRateLimitHeaders;
137
+ /**
138
+ * Sleep utility for retry backoff
139
+ */
140
+ private sleep;
141
+ /**
142
+ * Make fetch request with timeout
143
+ */
144
+ private fetchWithTimeout;
145
+ /**
146
+ * Call an external API securely through the proxy
147
+ * @param keyName - Name of the API key to use
148
+ * @param endpoint - API endpoint path (e.g., '/v1/chat/completions')
149
+ * @param options - Request options (method, body, headers)
150
+ * @returns Promise with the API response
151
+ */
152
+ call<T = any>(keyName: string, endpoint: string, options?: Omit<ProxyRequest, 'keyName' | 'endpoint'>): Promise<T>;
153
+ /**
154
+ * Make a GET request
155
+ */
156
+ get<T = any>(keyName: string, endpoint: string, headers?: Record<string, string>): Promise<T>;
157
+ /**
158
+ * Make a POST request
159
+ */
160
+ post<T = any>(keyName: string, endpoint: string, body: any, headers?: Record<string, string>): Promise<T>;
161
+ /**
162
+ * Make a PUT request
163
+ */
164
+ put<T = any>(keyName: string, endpoint: string, body: any, headers?: Record<string, string>): Promise<T>;
165
+ /**
166
+ * Make a DELETE request
167
+ */
168
+ delete<T = any>(keyName: string, endpoint: string, headers?: Record<string, string>): Promise<T>;
169
+ /**
170
+ * Make a PATCH request
171
+ */
172
+ patch<T = any>(keyName: string, endpoint: string, body: any, headers?: Record<string, string>): Promise<T>;
173
+ /**
174
+ * Secrets Management API (zero-config mode)
175
+ * These methods allow programmatic CRUD operations on project secrets
176
+ * Requires appId to be set (can be set in constructor or via setAppId)
177
+ */
178
+ /**
179
+ * Set App ID for secrets management
180
+ * Required before calling secrets management methods
181
+ */
182
+ setAppId(appId: string): void;
183
+ /**
184
+ * Make authenticated request to secrets management API
185
+ */
186
+ private secretsRequest;
187
+ /**
188
+ * List all secrets for the project
189
+ * Returns secret metadata (no API keys)
190
+ */
191
+ listSecrets(): Promise<{
192
+ success: boolean;
193
+ secrets: Array<{
194
+ name: string;
195
+ provider: string;
196
+ base_url: string;
197
+ auth_header: string;
198
+ auth_prefix: string;
199
+ created: string;
200
+ updated: string;
201
+ }>;
202
+ count: number;
203
+ }>;
204
+ /**
205
+ * Create a new secret
206
+ */
207
+ createSecret(secret: {
208
+ name: string;
209
+ provider: string;
210
+ api_key: string;
211
+ base_url?: string;
212
+ auth_header?: string;
213
+ auth_prefix?: string;
214
+ }): Promise<{
215
+ success: boolean;
216
+ message: string;
217
+ secret: any;
218
+ }>;
219
+ /**
220
+ * Update an existing secret
221
+ */
222
+ updateSecret(update: {
223
+ name: string;
224
+ api_key?: string;
225
+ provider?: string;
226
+ base_url?: string;
227
+ auth_header?: string;
228
+ auth_prefix?: string;
229
+ }): Promise<{
230
+ success: boolean;
231
+ message: string;
232
+ secret: any;
233
+ }>;
234
+ /**
235
+ * Delete a secret
236
+ */
237
+ deleteSecret(name: string): Promise<{
238
+ success: boolean;
239
+ message: string;
240
+ }>;
241
+ /**
242
+ * Sync secrets from .env file format
243
+ * Bulk upload/update secrets
244
+ */
245
+ syncEnv(secrets: Record<string, string>, provider?: string): Promise<{
246
+ success: boolean;
247
+ message: string;
248
+ results: {
249
+ created: number;
250
+ updated: number;
251
+ failed: number;
252
+ details: {
253
+ created: string[];
254
+ updated: string[];
255
+ failed: Array<{
256
+ name: string;
257
+ error: string;
258
+ }>;
259
+ };
260
+ };
261
+ }>;
262
+ }
263
+
264
+ /**
265
+ * SecretsManagement class for CLI authentication and secrets management
266
+ */
267
+ declare class SecretsManagement {
268
+ private appId;
269
+ private baseUrl;
270
+ private timeout;
271
+ constructor(options: SecretsManagementOptions);
272
+ /**
273
+ * Open a URL in the default browser
274
+ */
275
+ private openBrowser;
276
+ /**
277
+ * Sleep for specified milliseconds
278
+ */
279
+ private sleep;
280
+ /**
281
+ * Browser OAuth login flow
282
+ * Opens browser for user to authorize, then stores credentials locally
283
+ */
284
+ login(): Promise<{
285
+ success: boolean;
286
+ user: string;
287
+ }>;
288
+ /**
289
+ * Check if user is authenticated
290
+ */
291
+ isAuthenticated(): Promise<boolean>;
292
+ /**
293
+ * Logout - clear stored credentials
294
+ */
295
+ logout(): void;
296
+ /**
297
+ * Get access token from stored credentials
298
+ * Throws error if not authenticated
299
+ */
300
+ private getAccessToken;
301
+ /**
302
+ * Make authenticated request
303
+ */
304
+ private request;
305
+ /**
306
+ * List all secrets (with masked API keys)
307
+ */
308
+ list(): Promise<MaskedSecret[]>;
309
+ /**
310
+ * Sync secrets from config
311
+ */
312
+ sync(secrets: SecretConfig[], options?: SyncOptions): Promise<SyncResult>;
313
+ /**
314
+ * Import secrets from a config object (bulk import)
315
+ * Also creates SDK token if origins are provided
316
+ *
317
+ * @param config - The secrets configuration
318
+ * @returns Import results including created/updated counts and optional SDK token
319
+ */
320
+ importSecrets(config: {
321
+ version?: string;
322
+ origins?: string[];
323
+ secrets: SecretConfig[];
324
+ }): Promise<{
325
+ success: boolean;
326
+ results: {
327
+ created: number;
328
+ updated: number;
329
+ failed: number;
330
+ details: {
331
+ created: string[];
332
+ updated: string[];
333
+ failed: {
334
+ name: string;
335
+ error: string;
336
+ }[];
337
+ };
338
+ };
339
+ sdkToken?: {
340
+ token: string;
341
+ message: string;
342
+ };
343
+ }>;
344
+ /**
345
+ * Import secrets from a JSON file path
346
+ * Resolves environment variables in the config
347
+ */
348
+ importFromFile(configPath: string): Promise<{
349
+ success: boolean;
350
+ results: {
351
+ created: number;
352
+ updated: number;
353
+ failed: number;
354
+ };
355
+ sdkToken?: {
356
+ token: string;
357
+ message: string;
358
+ };
359
+ }>;
360
+ }
361
+
362
+ /**
363
+ * Resolve environment variable references in a string
364
+ * Supports ${VAR} and $VAR syntax
365
+ */
366
+ declare function resolveEnvString(value: string): string;
367
+ /**
368
+ * Resolve environment variables in a single secret config
369
+ */
370
+ declare function resolveEnvInSecret(secret: SecretConfig): SecretConfig;
371
+ /**
372
+ * Resolve environment variables in an array of secrets
373
+ */
374
+ declare function resolveEnvInSecrets(secrets: SecretConfig[]): SecretConfig[];
375
+ /**
376
+ * Load and parse a secrets config file
377
+ * Resolves environment variables in the config
378
+ */
379
+ declare function loadSecretsConfig(configPath: string): {
380
+ version: string;
381
+ project: string;
382
+ secrets: SecretConfig[];
383
+ };
384
+
385
+ export { type CLICredentials, InvalidTokenError, type MaskedSecret, OriginMismatchError, type ProxyRequest, type ProxyResponse, RateLimitError, type RateLimitInfo, type SecretConfig, SecretsManagement, type SecretsManagementOptions, SecretsSDK, SecretsSDKError, type SecretsSDKOptions, type SyncOptions, type SyncResult, loadSecretsConfig, resolveEnvInSecret, resolveEnvInSecrets, resolveEnvString };
@@ -0,0 +1,7 @@
1
+ "use strict";var SecretsSDK=(()=>{var U=Object.create;var b=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,J=Object.prototype.hasOwnProperty;var l=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var K=(r,e)=>()=>(r&&(e=r(r=0)),e);var O=(r,e)=>{for(var t in e)b(r,t,{get:e[t],enumerable:!0})},D=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of F(e))!J.call(r,n)&&n!==t&&b(r,n,{get:()=>e[n],enumerable:!(s=N(e,n))||s.enumerable});return r};var w=(r,e,t)=>(t=r!=null?U(z(r)):{},D(e||!r||!r.__esModule?b(t,"default",{value:r,enumerable:!0}):t,r)),H=r=>D(b({},"__esModule",{value:!0}),r);var j={};O(j,{loadSecretsConfig:()=>I,resolveEnvInSecret:()=>x,resolveEnvInSecrets:()=>k,resolveEnvString:()=>d});function d(r){return r.replace(/\$\{([A-Z0-9_]+)\}|\$([A-Z0-9_]+)/g,(e,t,s)=>{let n=t||s,a=process.env[n];if(a===void 0)throw new Error(`Environment variable ${n} is not defined`);return a})}function x(r){let e={name:d(r.name),provider:d(r.provider),api_key:d(r.api_key)};return r.base_url&&(e.base_url=d(r.base_url)),r.auth_header&&(e.auth_header=d(r.auth_header)),r.auth_prefix&&(e.auth_prefix=d(r.auth_prefix)),e}function k(r){return r.map(x)}function I(r){let e=l("fs"),s=l("path").resolve(r);if(!e.existsSync(s))throw new Error(`Config file not found: ${s}`);let n=e.readFileSync(s,"utf-8"),a=JSON.parse(n);if(!a.version)throw new Error('Config file missing "version" field');if(!a.project)throw new Error('Config file missing "project" field');if(!Array.isArray(a.secrets))throw new Error('Config file missing "secrets" array');let i=k(a.secrets);return{version:a.version,project:a.project,secrets:i}}var P=K(()=>{"use strict"});var G={};O(G,{InvalidTokenError:()=>h,OriginMismatchError:()=>m,RateLimitError:()=>f,SecretsManagement:()=>_,SecretsSDK:()=>S,SecretsSDKError:()=>o,loadSecretsConfig:()=>I,resolveEnvInSecret:()=>x,resolveEnvInSecrets:()=>k,resolveEnvString:()=>d});var o=class extends Error{constructor(t,s,n){super(t);this.status=s;this.response=n;this.name="SecretsSDKError"}},m=class extends o{constructor(e="Origin not allowed for this token"){super(e,403),this.name="OriginMismatchError"}},f=class extends o{constructor(e="Rate limit exceeded",t=60,s=0,n=100){super(e,429),this.name="RateLimitError",this.retryAfter=t,this.remaining=s,this.limit=n}},h=class extends o{constructor(e="Invalid or expired token"){super(e,401),this.name="InvalidTokenError"}};var S=class{constructor(e={}){this.rateLimitInfo=null;this.appId=e.appId||null,this.token=e.token||e.sessionToken||null,this.baseUrl=e.baseUrl||"https://ctklearn.carsontkempf.workers.dev",this.timeout=e.timeout||3e4,this.retryOn429=e.retryOn429!==void 0?e.retryOn429:!0,this.zeroConfigMode=!this.appId&&!this.token}getUsage(){return this.rateLimitInfo}parseRateLimitHeaders(e){let t=e.get("X-RateLimit-Limit"),s=e.get("X-RateLimit-Remaining"),n=e.get("X-RateLimit-Reset");t&&s&&n&&(this.rateLimitInfo={limit:parseInt(t),remaining:parseInt(s),reset:parseInt(n)})}sleep(e){return new Promise(t=>setTimeout(t,e))}async fetchWithTimeout(e,t,s){let n=new AbortController,a=setTimeout(()=>n.abort(),s);try{return await fetch(e,{...t,signal:n.signal})}finally{clearTimeout(a)}}async call(e,t,s={}){let n=0,a=this.retryOn429?3:0;for(;;)try{let i=this.zeroConfigMode?`${this.baseUrl}/api/sdk/proxy`:`${this.baseUrl}/api/sdk/${this.appId}/proxy`,p={"Content-Type":"application/json"};!this.zeroConfigMode&&this.token&&(p.Authorization=`Bearer ${this.token}`);let u=await this.fetchWithTimeout(i,{method:"POST",headers:p,body:JSON.stringify({keyName:e,endpoint:t,method:s.method||"GET",body:s.body,headers:s.headers})},this.timeout);this.parseRateLimitHeaders(u.headers);let g=await u.json();if(!u.ok){let y=g.data?.message||g?.message||`Request failed with status ${u.status}`;if(u.status===403&&y.includes("Origin"))throw new m(y);if(u.status===401)throw new h(y);if(u.status===429){let $=this.rateLimitInfo?this.rateLimitInfo.reset-Math.floor(Date.now()/1e3):60,q=new f(y,$,this.rateLimitInfo?.remaining||0,this.rateLimitInfo?.limit||100);if(this.retryOn429&&n<a){n++;let M=Math.min(1e3*Math.pow(2,n-1),8e3);await this.sleep(M);continue}throw q}throw new o(y,u.status,g)}return g.data}catch(i){throw i instanceof o?i:i instanceof Error&&i.name==="AbortError"?new o("Request timeout",408):new o(i instanceof Error?i.message:"Unknown error occurred",500)}}async get(e,t,s){return this.call(e,t,{method:"GET",headers:s})}async post(e,t,s,n){return this.call(e,t,{method:"POST",body:s,headers:n})}async put(e,t,s,n){return this.call(e,t,{method:"PUT",body:s,headers:n})}async delete(e,t,s){return this.call(e,t,{method:"DELETE",headers:s})}async patch(e,t,s,n){return this.call(e,t,{method:"PATCH",body:s,headers:n})}setAppId(e){this.appId=e}async secretsRequest(e,t){if(!this.appId)throw new o("App ID required for secrets management. Call setAppId() first.",400);let s=await this.fetchWithTimeout(`${this.baseUrl}/api/projects/${this.appId}/secrets/manage?action=${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:t?JSON.stringify(t):void 0},this.timeout);if(!s.ok){let n=await s.json().catch(()=>({}));throw new o(n.message||`Secrets management failed: ${s.statusText}`,s.status,n)}return s.json()}async listSecrets(){return this.secretsRequest("list")}async createSecret(e){return this.secretsRequest("create",e)}async updateSecret(e){return this.secretsRequest("update",e)}async deleteSecret(e){return this.secretsRequest("delete",{name:e})}async syncEnv(e,t){return this.secretsRequest("sync",{secrets:e,provider:t})}};var c=w(l("fs")),v=w(l("path")),T=w(l("os"));function R(){let r=T.homedir(),e=v.join(r,".learn");return v.join(e,"credentials.json")}function B(){let r=T.homedir(),e=v.join(r,".learn");c.existsSync(e)||c.mkdirSync(e,{recursive:!0,mode:448})}function C(){try{let r=R();if(!c.existsSync(r))return null;let e=c.readFileSync(r,"utf-8"),t=JSON.parse(e);return!t.access_token||!t.refresh_token||!t.expires_at||!t.user_id?null:t}catch{return null}}function E(r){B();let e=R(),t=JSON.stringify(r,null,2);c.writeFileSync(e,t,{mode:384})}function A(){try{let r=R();c.existsSync(r)&&c.unlinkSync(r)}catch{}}function L(){let r=C();return r?new Date(r.expires_at)>new Date:!1}var _=class{constructor(e){this.appId=e.appId,this.baseUrl=e.baseUrl||"https://ctklearn.carsontkempf.workers.dev",this.timeout=e.timeout||3e4}async openBrowser(e){let{exec:t}=await import("child_process"),{promisify:s}=await import("util"),n=s(t),a=process.platform;try{a==="darwin"?await n(`open "${e}"`):a==="win32"?await n(`start "${e}"`):await n(`xdg-open "${e}"`)}catch(i){throw console.error("Failed to open browser:",i),new o("Failed to open browser",500)}}sleep(e){return new Promise(t=>setTimeout(t,e))}async login(){try{let e=await fetch(`${this.baseUrl}/api/auth/cli/device`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!e.ok)throw new o("Failed to generate device code",e.status);let t=await e.json();console.log(`
2
+ To authorize this application, visit:`),console.log(` ${t.verification_uri}`),console.log(`
3
+ And enter the code:`),console.log(` ${t.user_code}`),console.log(`
4
+ Opening browser...
5
+ `),await this.openBrowser(t.verification_uri_complete);let s=t.interval*1e3,n=Math.ceil(t.expires_in/t.interval),a=0;for(;a<n;){await this.sleep(s),a++;let i=await fetch(`${this.baseUrl}/api/auth/cli/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:t.device_code})});if(i.status===202){process.stdout.write(".");continue}if(!i.ok){let g=await i.json().catch(()=>({}));throw new o(g.message||"Failed to exchange device code",i.status)}let p=await i.json(),u=new Date(Date.now()+p.expires_in*1e3);return E({access_token:p.access_token,refresh_token:p.refresh_token,expires_at:u.toISOString(),user_id:p.user_id}),console.log(`
6
+
7
+ Authentication successful!`),{success:!0,user:p.user_id}}throw new o("Authorization timeout",408)}catch(e){throw e instanceof o?e:new o(e.message||"Login failed",e.status||500)}}async isAuthenticated(){return L()}logout(){A(),console.log("Logged out successfully")}getAccessToken(){let e=C();if(!e)throw new o("Not authenticated. Run login() first.",401);if(new Date(e.expires_at)<=new Date)throw new o("Token expired. Run login() again.",401);return e.access_token}async request(e,t,s){let n=this.getAccessToken(),a={method:e,headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`}};s&&(a.body=JSON.stringify(s));let i=await fetch(`${this.baseUrl}${t}`,a);if(!i.ok){let p=await i.json().catch(()=>({}));throw new o(p.message||`Request failed: ${i.statusText}`,i.status,p)}return i.json()}async list(){return(await this.request("GET",`/api/projects/${this.appId}/secrets`)).secrets}async sync(e,t){return await this.request("POST",`/api/projects/${this.appId}/secrets/sync`,{secrets:e,options:{deleteMissing:t?.deleteMissing??!1,dryRun:t?.dryRun??!1}})}async importSecrets(e){return this.request("POST",`/api/projects/${this.appId}/import-secrets`,{version:e.version||"1.0",origins:e.origins,secrets:e.secrets})}async importFromFile(e){let{loadSecretsConfig:t}=await Promise.resolve().then(()=>(P(),j)),s=t(e);return this.importSecrets(s)}};P();return H(G);})();
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import{a as P,b as C,c as O,d as D}from"./chunk-AS6G7JYX.mjs";var i=class extends Error{constructor(t,r,s){super(t);this.status=r;this.response=s;this.name="SecretsSDKError"}},m=class extends i{constructor(e="Origin not allowed for this token"){super(e,403),this.name="OriginMismatchError"}},g=class extends i{constructor(e="Rate limit exceeded",t=60,r=0,s=100){super(e,429),this.name="RateLimitError",this.retryAfter=t,this.remaining=r,this.limit=s}},h=class extends i{constructor(e="Invalid or expired token"){super(e,401),this.name="InvalidTokenError"}};var y=class{constructor(e={}){this.rateLimitInfo=null;this.appId=e.appId||null,this.token=e.token||e.sessionToken||null,this.baseUrl=e.baseUrl||"https://ctklearn.carsontkempf.workers.dev",this.timeout=e.timeout||3e4,this.retryOn429=e.retryOn429!==void 0?e.retryOn429:!0,this.zeroConfigMode=!this.appId&&!this.token}getUsage(){return this.rateLimitInfo}parseRateLimitHeaders(e){let t=e.get("X-RateLimit-Limit"),r=e.get("X-RateLimit-Remaining"),s=e.get("X-RateLimit-Reset");t&&r&&s&&(this.rateLimitInfo={limit:parseInt(t),remaining:parseInt(r),reset:parseInt(s)})}sleep(e){return new Promise(t=>setTimeout(t,e))}async fetchWithTimeout(e,t,r){let s=new AbortController,a=setTimeout(()=>s.abort(),r);try{return await fetch(e,{...t,signal:s.signal})}finally{clearTimeout(a)}}async call(e,t,r={}){let s=0,a=this.retryOn429?3:0;for(;;)try{let n=this.zeroConfigMode?`${this.baseUrl}/api/sdk/proxy`:`${this.baseUrl}/api/sdk/${this.appId}/proxy`,p={"Content-Type":"application/json"};!this.zeroConfigMode&&this.token&&(p.Authorization=`Bearer ${this.token}`);let u=await this.fetchWithTimeout(n,{method:"POST",headers:p,body:JSON.stringify({keyName:e,endpoint:t,method:r.method||"GET",body:r.body,headers:r.headers})},this.timeout);this.parseRateLimitHeaders(u.headers);let d=await u.json();if(!u.ok){let l=d.data?.message||d?.message||`Request failed with status ${u.status}`;if(u.status===403&&l.includes("Origin"))throw new m(l);if(u.status===401)throw new h(l);if(u.status===429){let R=this.rateLimitInfo?this.rateLimitInfo.reset-Math.floor(Date.now()/1e3):60,_=new g(l,R,this.rateLimitInfo?.remaining||0,this.rateLimitInfo?.limit||100);if(this.retryOn429&&s<a){s++;let I=Math.min(1e3*Math.pow(2,s-1),8e3);await this.sleep(I);continue}throw _}throw new i(l,u.status,d)}return d.data}catch(n){throw n instanceof i?n:n instanceof Error&&n.name==="AbortError"?new i("Request timeout",408):new i(n instanceof Error?n.message:"Unknown error occurred",500)}}async get(e,t,r){return this.call(e,t,{method:"GET",headers:r})}async post(e,t,r,s){return this.call(e,t,{method:"POST",body:r,headers:s})}async put(e,t,r,s){return this.call(e,t,{method:"PUT",body:r,headers:s})}async delete(e,t,r){return this.call(e,t,{method:"DELETE",headers:r})}async patch(e,t,r,s){return this.call(e,t,{method:"PATCH",body:r,headers:s})}setAppId(e){this.appId=e}async secretsRequest(e,t){if(!this.appId)throw new i("App ID required for secrets management. Call setAppId() first.",400);let r=await this.fetchWithTimeout(`${this.baseUrl}/api/projects/${this.appId}/secrets/manage?action=${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:t?JSON.stringify(t):void 0},this.timeout);if(!r.ok){let s=await r.json().catch(()=>({}));throw new i(s.message||`Secrets management failed: ${r.statusText}`,r.status,s)}return r.json()}async listSecrets(){return this.secretsRequest("list")}async createSecret(e){return this.secretsRequest("create",e)}async updateSecret(e){return this.secretsRequest("update",e)}async deleteSecret(e){return this.secretsRequest("delete",{name:e})}async syncEnv(e,t){return this.secretsRequest("sync",{secrets:e,provider:t})}};import*as c from"fs";import*as f from"path";import*as w from"os";function b(){let o=w.homedir(),e=f.join(o,".learn");return f.join(e,"credentials.json")}function L(){let o=w.homedir(),e=f.join(o,".learn");c.existsSync(e)||c.mkdirSync(e,{recursive:!0,mode:448})}function x(){try{let o=b();if(!c.existsSync(o))return null;let e=c.readFileSync(o,"utf-8"),t=JSON.parse(e);return!t.access_token||!t.refresh_token||!t.expires_at||!t.user_id?null:t}catch{return null}}function k(o){L();let e=b(),t=JSON.stringify(o,null,2);c.writeFileSync(e,t,{mode:384})}function v(){try{let o=b();c.existsSync(o)&&c.unlinkSync(o)}catch{}}function T(){let o=x();return o?new Date(o.expires_at)>new Date:!1}var S=class{constructor(e){this.appId=e.appId,this.baseUrl=e.baseUrl||"https://ctklearn.carsontkempf.workers.dev",this.timeout=e.timeout||3e4}async openBrowser(e){let{exec:t}=await import("child_process"),{promisify:r}=await import("util"),s=r(t),a=process.platform;try{a==="darwin"?await s(`open "${e}"`):a==="win32"?await s(`start "${e}"`):await s(`xdg-open "${e}"`)}catch(n){throw console.error("Failed to open browser:",n),new i("Failed to open browser",500)}}sleep(e){return new Promise(t=>setTimeout(t,e))}async login(){try{let e=await fetch(`${this.baseUrl}/api/auth/cli/device`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!e.ok)throw new i("Failed to generate device code",e.status);let t=await e.json();console.log(`
2
+ To authorize this application, visit:`),console.log(` ${t.verification_uri}`),console.log(`
3
+ And enter the code:`),console.log(` ${t.user_code}`),console.log(`
4
+ Opening browser...
5
+ `),await this.openBrowser(t.verification_uri_complete);let r=t.interval*1e3,s=Math.ceil(t.expires_in/t.interval),a=0;for(;a<s;){await this.sleep(r),a++;let n=await fetch(`${this.baseUrl}/api/auth/cli/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:t.device_code})});if(n.status===202){process.stdout.write(".");continue}if(!n.ok){let d=await n.json().catch(()=>({}));throw new i(d.message||"Failed to exchange device code",n.status)}let p=await n.json(),u=new Date(Date.now()+p.expires_in*1e3);return k({access_token:p.access_token,refresh_token:p.refresh_token,expires_at:u.toISOString(),user_id:p.user_id}),console.log(`
6
+
7
+ Authentication successful!`),{success:!0,user:p.user_id}}throw new i("Authorization timeout",408)}catch(e){throw e instanceof i?e:new i(e.message||"Login failed",e.status||500)}}async isAuthenticated(){return T()}logout(){v(),console.log("Logged out successfully")}getAccessToken(){let e=x();if(!e)throw new i("Not authenticated. Run login() first.",401);if(new Date(e.expires_at)<=new Date)throw new i("Token expired. Run login() again.",401);return e.access_token}async request(e,t,r){let s=this.getAccessToken(),a={method:e,headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`}};r&&(a.body=JSON.stringify(r));let n=await fetch(`${this.baseUrl}${t}`,a);if(!n.ok){let p=await n.json().catch(()=>({}));throw new i(p.message||`Request failed: ${n.statusText}`,n.status,p)}return n.json()}async list(){return(await this.request("GET",`/api/projects/${this.appId}/secrets`)).secrets}async sync(e,t){return await this.request("POST",`/api/projects/${this.appId}/secrets/sync`,{secrets:e,options:{deleteMissing:t?.deleteMissing??!1,dryRun:t?.dryRun??!1}})}async importSecrets(e){return this.request("POST",`/api/projects/${this.appId}/import-secrets`,{version:e.version||"1.0",origins:e.origins,secrets:e.secrets})}async importFromFile(e){let{loadSecretsConfig:t}=await import("./env-resolver-Y4SFGOKB.mjs"),r=t(e);return this.importSecrets(r)}};export{h as InvalidTokenError,m as OriginMismatchError,g as RateLimitError,S as SecretsManagement,y as SecretsSDK,i as SecretsSDKError,D as loadSecretsConfig,C as resolveEnvInSecret,O as resolveEnvInSecrets,P as resolveEnvString};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "learn-secrets-sdk",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Secure API proxy SDK for static sites with domain-scoped tokens",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -13,10 +13,13 @@
13
13
  "bin",
14
14
  "secrets.template.json"
15
15
  ],
16
- "scripts": {
17
- "build": "tsc && tsc --project tsconfig.esm.json",
18
- "prepublishOnly": "npm run build"
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/carsontkempf/learn.git",
19
+ "directory": "packages/secrets-sdk"
19
20
  },
21
+ "homepage": "https://github.com/carsontkempf/learn/tree/main/packages/secrets-sdk",
22
+ "author": "Carson Kempf",
20
23
  "keywords": [
21
24
  "api",
22
25
  "proxy",
@@ -26,13 +29,17 @@
26
29
  "cli",
27
30
  "secrets-management"
28
31
  ],
29
- "author": "",
30
32
  "license": "MIT",
31
33
  "dependencies": {
32
34
  "commander": "^12.0.0"
33
35
  },
34
36
  "devDependencies": {
35
37
  "@types/node": "^20.0.0",
38
+ "tsup": "^8.5.1",
36
39
  "typescript": "^5.0.0"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup src/index.ts --format esm,iife --dts --minify --global-name SecretsSDK && tsup src/cli/index.ts --format esm --dts --out-dir dist/cli --external commander",
43
+ "prepublishOnly": "npm run build"
37
44
  }
38
45
  }
@@ -1,11 +0,0 @@
1
- export interface InitOptions {
2
- origins: string;
3
- env?: string;
4
- project?: string;
5
- baseUrl?: string;
6
- yes?: boolean;
7
- }
8
- /**
9
- * Init command - onboard a new site by uploading .env secrets
10
- */
11
- export declare function initCommand(options: InitOptions): Promise<void>;
@@ -1,113 +0,0 @@
1
- import { SecretsManagement } from '../../management.js';
2
- import { parseEnvFile } from '../utils/env-parser.js';
3
- import { detectApiKeys, summarizeDetectedSecrets } from '../utils/key-detector.js';
4
- import { hasValidCredentials } from '../../credentials.js';
5
- /**
6
- * Init command - onboard a new site by uploading .env secrets
7
- */
8
- export async function initCommand(options) {
9
- // Validate authentication
10
- if (!hasValidCredentials()) {
11
- console.error('Error: Not authenticated. Run "learn-secrets login" first.');
12
- process.exit(1);
13
- }
14
- // Validate required options
15
- if (!options.origins) {
16
- console.error('Error: --origins is required');
17
- console.error('Example: learn-secrets init --origins mysite.com,localhost');
18
- process.exit(1);
19
- }
20
- if (!options.project) {
21
- console.error('Error: --project is required');
22
- console.error('Example: learn-secrets init --project my-app-id --origins mysite.com');
23
- process.exit(1);
24
- }
25
- // Parse origins
26
- const origins = options.origins.split(',').map(o => o.trim()).filter(Boolean);
27
- if (origins.length === 0) {
28
- console.error('Error: No valid origins provided');
29
- process.exit(1);
30
- }
31
- console.log(`\nOnboarding site for project: ${options.project}`);
32
- console.log(`Origins: ${origins.join(', ')}\n`);
33
- // Load and detect secrets from .env
34
- const envFile = options.env || '.env';
35
- let secrets;
36
- try {
37
- console.log(`Reading ${envFile}...`);
38
- const envVars = parseEnvFile(envFile);
39
- secrets = detectApiKeys(envVars);
40
- if (secrets.length === 0) {
41
- console.log('\nNo API keys detected in .env file.');
42
- console.log('Make sure your .env contains variables like:');
43
- console.log(' OPENAI_API_KEY=sk-...');
44
- console.log(' ANTHROPIC_API_KEY=sk-ant-...');
45
- process.exit(0);
46
- }
47
- console.log('\n' + summarizeDetectedSecrets(secrets));
48
- console.log('');
49
- }
50
- catch (error) {
51
- console.error(`Error reading ${envFile}:`, error.message);
52
- process.exit(1);
53
- }
54
- // Ask for confirmation unless --yes flag
55
- if (!options.yes) {
56
- const readline = await import('readline');
57
- const rl = readline.createInterface({
58
- input: process.stdin,
59
- output: process.stdout
60
- });
61
- const answer = await new Promise((resolve) => {
62
- rl.question('Upload these secrets? (y/N): ', resolve);
63
- });
64
- rl.close();
65
- if (answer.toLowerCase() !== 'y' && answer.toLowerCase() !== 'yes') {
66
- console.log('Cancelled.');
67
- process.exit(0);
68
- }
69
- }
70
- // Upload secrets
71
- console.log('\nUploading secrets...');
72
- const mgmt = new SecretsManagement({
73
- appId: options.project,
74
- baseUrl: options.baseUrl
75
- });
76
- try {
77
- const result = await mgmt.importSecrets({
78
- version: '1.0',
79
- origins,
80
- secrets
81
- });
82
- console.log('\nSuccess!');
83
- console.log(` Created: ${result.results.created} secrets`);
84
- console.log(` Updated: ${result.results.updated} secrets`);
85
- if (result.results.failed > 0) {
86
- console.log(` Failed: ${result.results.failed} secrets`);
87
- for (const failure of result.results.details.failed) {
88
- console.log(` - ${failure.name}: ${failure.error}`);
89
- }
90
- }
91
- if (result.sdkToken) {
92
- console.log('\nSDK Token created:');
93
- console.log(` ${result.sdkToken.token}`);
94
- console.log('\n IMPORTANT: Save this token - it will not be shown again.');
95
- console.log(' Add it to your static site code:');
96
- console.log('');
97
- console.log(' const sdk = new SecretsSDK();');
98
- console.log(' // No appId or token needed - uses origin-based auth!');
99
- }
100
- console.log('\nYour site is ready!');
101
- console.log(`Visit: https://ctklearn.carsontkempf.workers.dev to manage secrets`);
102
- }
103
- catch (error) {
104
- console.error('\nUpload failed:', error.message);
105
- if (error.status === 401) {
106
- console.error('Your session may have expired. Try running "learn-secrets login" again.');
107
- }
108
- if (error.status === 404) {
109
- console.error(`Project "${options.project}" not found. Check your project ID.`);
110
- }
111
- process.exit(1);
112
- }
113
- }
@@ -1,7 +0,0 @@
1
- export interface LoginOptions {
2
- baseUrl?: string;
3
- }
4
- /**
5
- * Login command - authenticate via browser OAuth flow
6
- */
7
- export declare function loginCommand(options?: LoginOptions): Promise<void>;
@@ -1,22 +0,0 @@
1
- import { SecretsManagement } from '../../management.js';
2
- /**
3
- * Login command - authenticate via browser OAuth flow
4
- */
5
- export async function loginCommand(options = {}) {
6
- console.log('Authenticating with Learn Secrets...\n');
7
- // Create management client (appId not needed for login)
8
- const mgmt = new SecretsManagement({
9
- appId: 'temp', // Will be set after login
10
- baseUrl: options.baseUrl
11
- });
12
- try {
13
- const result = await mgmt.login();
14
- console.log(`\nLogged in as: ${result.user}`);
15
- console.log('Credentials saved to ~/.learn/credentials.json');
16
- console.log('\nYou can now run: learn-secrets init --origins yourdomain.com');
17
- }
18
- catch (error) {
19
- console.error('\nLogin failed:', error.message);
20
- process.exit(1);
21
- }
22
- }
package/dist/cli/index.js DELETED
@@ -1,35 +0,0 @@
1
- #!/usr/bin/env node
2
- import { Command } from 'commander';
3
- import { loginCommand } from './commands/login.js';
4
- import { initCommand } from './commands/init.js';
5
- const program = new Command();
6
- program
7
- .name('learn-secrets')
8
- .description('CLI tool for managing API secrets in static sites')
9
- .version('1.4.0');
10
- // Login command
11
- program
12
- .command('login')
13
- .description('Authenticate with Learn Secrets via browser')
14
- .option('--base-url <url>', 'Custom base URL for API')
15
- .action(async (options) => {
16
- await loginCommand(options);
17
- });
18
- // Init command
19
- program
20
- .command('init')
21
- .description('Initialize secrets for a new site')
22
- .requiredOption('-o, --origins <domains>', 'Comma-separated list of allowed origins (e.g., mysite.com,localhost)')
23
- .requiredOption('-p, --project <appid>', 'Project app ID from dashboard')
24
- .option('-e, --env <file>', 'Path to .env file (default: .env)', '.env')
25
- .option('--base-url <url>', 'Custom base URL for API')
26
- .option('-y, --yes', 'Skip confirmation prompt')
27
- .action(async (options) => {
28
- await initCommand(options);
29
- });
30
- // Parse arguments
31
- program.parse(process.argv);
32
- // Show help if no command provided
33
- if (!process.argv.slice(2).length) {
34
- program.outputHelp();
35
- }