@spfn/core 0.1.0-alpha.64 → 0.1.0-alpha.68

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 (37) hide show
  1. package/README.md +5 -5
  2. package/dist/{auto-loader-CdsxOceW.d.ts → auto-loader-JFaZ9gON.d.ts} +3 -2
  3. package/dist/cache/index.d.ts +211 -0
  4. package/dist/cache/index.js +992 -0
  5. package/dist/cache/index.js.map +1 -0
  6. package/dist/client/index.d.ts +2 -2
  7. package/dist/codegen/generators/index.d.ts +7 -6
  8. package/dist/codegen/generators/index.js +208 -27
  9. package/dist/codegen/generators/index.js.map +1 -1
  10. package/dist/codegen/index.d.ts +67 -118
  11. package/dist/codegen/index.js +1419 -1295
  12. package/dist/codegen/index.js.map +1 -1
  13. package/dist/database-errors-CoPrcOpq.d.ts +86 -0
  14. package/dist/db/index.d.ts +316 -9
  15. package/dist/db/index.js +6 -6
  16. package/dist/db/index.js.map +1 -1
  17. package/dist/error-handler-wjLL3v-a.d.ts +44 -0
  18. package/dist/errors/index.d.ts +119 -0
  19. package/dist/errors/index.js +160 -0
  20. package/dist/errors/index.js.map +1 -0
  21. package/dist/index-DHiAqhKv.d.ts +101 -0
  22. package/dist/index.d.ts +2 -228
  23. package/dist/index.js +274 -292
  24. package/dist/index.js.map +1 -1
  25. package/dist/middleware/index.d.ts +33 -0
  26. package/dist/middleware/index.js +890 -0
  27. package/dist/middleware/index.js.map +1 -0
  28. package/dist/route/index.d.ts +172 -7
  29. package/dist/route/index.js +209 -70
  30. package/dist/route/index.js.map +1 -1
  31. package/dist/server/index.js +267 -176
  32. package/dist/server/index.js.map +1 -1
  33. package/dist/{types-Bd8YsFSU.d.ts → types-CAON3Mmg.d.ts} +1 -1
  34. package/package.json +19 -2
  35. package/dist/bind-CSzshBtm.d.ts +0 -17
  36. package/dist/contract-generator-CqKsfsNE.d.ts +0 -52
  37. package/dist/postgres-errors-lw1aRUFe.d.ts +0 -397
package/README.md CHANGED
@@ -85,7 +85,7 @@ npm run spfn:dev
85
85
 
86
86
  ## Architecture Pattern
87
87
 
88
- SPFN follows a **layered architecture** that separates concerns and keeps code maintainable:
88
+ Superfunction follows a **layered architecture** that separates concerns and keeps code maintainable:
89
89
 
90
90
  ```
91
91
  ┌─────────────────────────────────────────┐
@@ -323,7 +323,7 @@ Drizzle ORM integration with type-safe helper functions and automatic transactio
323
323
  - **Function schema auto-discovery** (see below)
324
324
 
325
325
  ### 📦 Function Schema Discovery
326
- Automatic discovery of database schemas from SPFN ecosystem functions.
326
+ Automatic discovery of database schemas from Superfunction ecosystem functions.
327
327
 
328
328
  **[→ Read Database Manager Documentation](./src/db/manager/README.md)**
329
329
 
@@ -347,7 +347,7 @@ Functions declare their schemas in `package.json`:
347
347
  }
348
348
  ```
349
349
 
350
- SPFN automatically discovers and merges these schemas during migration generation:
350
+ Superfunction automatically discovers and merges these schemas during migration generation:
351
351
  ```typescript
352
352
  import { getDrizzleConfig } from '@spfn/core'
353
353
 
@@ -364,7 +364,7 @@ pnpm spfn add @spfn/cms
364
364
  # ✅ Shows setup guide
365
365
  ```
366
366
 
367
- **Create your own SPFN packages:**
367
+ **Create your own Superfunction packages:**
368
368
  ```typescript
369
369
  // 1. Define entities
370
370
  export const myTable = pgTable('my_table', { ... })
@@ -557,4 +557,4 @@ MIT
557
557
 
558
558
  ---
559
559
 
560
- Part of the [SPFN Framework](https://github.com/spfn/spfn)
560
+ Part of the [Superfunction Framework](https://github.com/spfn/spfn)
@@ -47,14 +47,15 @@ declare class AutoRouteLoader {
47
47
  load(app: Hono): Promise<RouteStats>;
48
48
  /**
49
49
  * Load routes from an external directory (e.g., from SPFN function packages)
50
- * Routes use contract-based absolute paths, so no basePath prefix needed
50
+ * Reads package.json spfn.prefix and mounts routes under that prefix
51
51
  *
52
52
  * @param app - Hono app instance
53
53
  * @param routesDir - Directory containing route handlers
54
54
  * @param packageName - Name of the package (for logging)
55
+ * @param prefix - Optional prefix to mount routes under (from package.json spfn.prefix)
55
56
  * @returns Route statistics
56
57
  */
57
- loadExternalRoutes(app: Hono, routesDir: string, packageName: string): Promise<RouteStats>;
58
+ loadExternalRoutes(app: Hono, routesDir: string, packageName: string, prefix?: string): Promise<RouteStats>;
58
59
  getStats(): RouteStats;
59
60
  private scanFiles;
60
61
  private isValidRouteFile;
@@ -0,0 +1,211 @@
1
+ import { Redis, Cluster } from 'ioredis';
2
+
3
+ /**
4
+ * Cache factory with automatic environment variable detection
5
+ * Supports Valkey and Redis with multiple deployment patterns
6
+ *
7
+ * Valkey is a Redis fork (7.2.4 base) with 100% protocol compatibility
8
+ * https://valkey.io
9
+ */
10
+
11
+ interface CacheClients {
12
+ /** Primary cache for writes (or both read/write if no replica) */
13
+ write?: Redis | Cluster;
14
+ /** Replica cache for reads (optional, falls back to write) */
15
+ read?: Redis | Cluster;
16
+ }
17
+ /**
18
+ * Create cache client(s) from environment variables
19
+ *
20
+ * Supported patterns (priority order):
21
+ * 1. Single instance: VALKEY_URL or CACHE_URL or REDIS_URL
22
+ * 2. Master-Replica: VALKEY_WRITE_URL + VALKEY_READ_URL (or CACHE_*, REDIS_*)
23
+ * 3. Sentinel: VALKEY_SENTINEL_HOSTS + VALKEY_MASTER_NAME (or REDIS_*)
24
+ * 4. Cluster: VALKEY_CLUSTER_NODES (or REDIS_*)
25
+ *
26
+ * @returns Cache client(s) or undefined if no configuration found
27
+ *
28
+ * @example
29
+ * ```bash
30
+ * # Single (most common)
31
+ * VALKEY_URL=valkey://localhost:6379
32
+ * CACHE_URL=redis://localhost:6379
33
+ *
34
+ * # Legacy (still supported)
35
+ * REDIS_URL=redis://localhost:6379
36
+ * REDIS_URL=rediss://secure.redis.com:6380 # TLS
37
+ *
38
+ * # Master-Replica
39
+ * VALKEY_WRITE_URL=valkey://master:6379
40
+ * VALKEY_READ_URL=valkey://replica:6379
41
+ *
42
+ * # Sentinel
43
+ * VALKEY_SENTINEL_HOSTS=sentinel1:26379,sentinel2:26379
44
+ * VALKEY_MASTER_NAME=mymaster
45
+ * VALKEY_PASSWORD=secret
46
+ *
47
+ * # Cluster
48
+ * VALKEY_CLUSTER_NODES=node1:6379,node2:6379,node3:6379
49
+ * VALKEY_PASSWORD=secret
50
+ * ```
51
+ */
52
+ declare function createCacheFromEnv(): Promise<CacheClients>;
53
+ /**
54
+ * Create single cache client (backward compatibility)
55
+ * Only returns write instance
56
+ */
57
+ declare function createSingleCacheFromEnv(): Promise<Redis | Cluster | undefined>;
58
+
59
+ /**
60
+ * Global cache instance manager
61
+ * Provides singleton access to cache (Valkey/Redis) across all modules
62
+ * Supports Master-Replica pattern with separate read/write instances
63
+ *
64
+ * When cache is unavailable, falls back to disabled mode gracefully
65
+ */
66
+
67
+ /**
68
+ * Get global cache write instance
69
+ *
70
+ * @returns Cache write instance or undefined if disabled/not initialized
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * import { getCache } from '@spfn/core/cache';
75
+ *
76
+ * const cache = getCache();
77
+ * if (cache) {
78
+ * await cache.set('key', 'value');
79
+ * } else {
80
+ * // Cache disabled - handle gracefully
81
+ * console.log('Cache unavailable, skipping...');
82
+ * }
83
+ * ```
84
+ */
85
+ declare function getCache(): Redis | Cluster | undefined;
86
+ /**
87
+ * Get global cache read instance (falls back to write if no replica)
88
+ *
89
+ * @returns Cache read instance or write instance as fallback, undefined if disabled
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * import { getCacheRead } from '@spfn/core/cache';
94
+ *
95
+ * const cache = getCacheRead();
96
+ * if (cache) {
97
+ * const value = await cache.get('key');
98
+ * }
99
+ * ```
100
+ */
101
+ declare function getCacheRead(): Redis | Cluster | undefined;
102
+ /**
103
+ * Check if cache is disabled (connection failed or not configured)
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * import { isCacheDisabled } from '@spfn/core/cache';
108
+ *
109
+ * if (isCacheDisabled()) {
110
+ * // Use alternative strategy (e.g., in-memory cache, database)
111
+ * return await fetchFromDatabase();
112
+ * }
113
+ * ```
114
+ */
115
+ declare function isCacheDisabled(): boolean;
116
+ /**
117
+ * Set global cache instances (for testing or manual configuration)
118
+ *
119
+ * @param write - Cache write instance
120
+ * @param read - Cache read instance (optional, defaults to write)
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { setCache } from '@spfn/core/cache';
125
+ * import Redis from 'ioredis';
126
+ *
127
+ * const write = new Redis('redis://master:6379');
128
+ * const read = new Redis('redis://replica:6379');
129
+ * setCache(write, read);
130
+ * ```
131
+ */
132
+ declare function setCache(write: Redis | Cluster | undefined, read?: Redis | Cluster | undefined): void;
133
+ /**
134
+ * Initialize cache from environment variables
135
+ * Automatically called by startServer()
136
+ *
137
+ * Supported environment variables (priority order):
138
+ * - VALKEY_URL / CACHE_URL (single instance)
139
+ * - VALKEY_WRITE_URL + VALKEY_READ_URL (master-replica)
140
+ * - VALKEY_SENTINEL_HOSTS + VALKEY_MASTER_NAME (sentinel)
141
+ * - VALKEY_CLUSTER_NODES (cluster)
142
+ * - VALKEY_TLS_REJECT_UNAUTHORIZED (TLS config)
143
+ * - Legacy: REDIS_* (backward compatibility)
144
+ *
145
+ * @returns Object with write and read instances, or undefined if disabled
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * import { initCache } from '@spfn/core/cache';
150
+ *
151
+ * // Manual initialization (not needed if using startServer)
152
+ * const { write, read, disabled } = await initCache();
153
+ * if (!disabled) {
154
+ * console.log('Cache available');
155
+ * }
156
+ * ```
157
+ */
158
+ declare function initCache(): Promise<{
159
+ write?: Redis | Cluster;
160
+ read?: Redis | Cluster;
161
+ disabled: boolean;
162
+ }>;
163
+ /**
164
+ * Close all cache connections and cleanup
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * import { closeCache } from '@spfn/core/cache';
169
+ *
170
+ * // During graceful shutdown
171
+ * await closeCache();
172
+ * ```
173
+ */
174
+ declare function closeCache(): Promise<void>;
175
+ /**
176
+ * Get cache connection info (for debugging)
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * import { getCacheInfo } from '@spfn/core/cache';
181
+ *
182
+ * const info = getCacheInfo();
183
+ * console.log(info);
184
+ * // {
185
+ * // hasWrite: true,
186
+ * // hasRead: true,
187
+ * // isReplica: true,
188
+ * // disabled: false
189
+ * // }
190
+ * ```
191
+ */
192
+ declare function getCacheInfo(): {
193
+ hasWrite: boolean;
194
+ hasRead: boolean;
195
+ isReplica: boolean;
196
+ disabled: boolean;
197
+ };
198
+ /** @deprecated Use getCache() instead */
199
+ declare const getRedis: typeof getCache;
200
+ /** @deprecated Use getCacheRead() instead */
201
+ declare const getRedisRead: typeof getCacheRead;
202
+ /** @deprecated Use setCache() instead */
203
+ declare const setRedis: typeof setCache;
204
+ /** @deprecated Use initCache() instead */
205
+ declare const initRedis: typeof initCache;
206
+ /** @deprecated Use closeCache() instead */
207
+ declare const closeRedis: typeof closeCache;
208
+ /** @deprecated Use getCacheInfo() instead */
209
+ declare const getRedisInfo: typeof getCacheInfo;
210
+
211
+ export { type CacheClients, type CacheClients as RedisClients, closeCache, closeRedis, createCacheFromEnv, createCacheFromEnv as createRedisFromEnv, createSingleCacheFromEnv, createSingleCacheFromEnv as createSingleRedisFromEnv, getCache, getCacheInfo, getCacheRead, getRedis, getRedisInfo, getRedisRead, initCache, initRedis, isCacheDisabled, setCache, setRedis };