@spfn/core 0.1.0-alpha.7 → 0.1.0-alpha.74

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 (59) hide show
  1. package/README.md +168 -195
  2. package/dist/auto-loader-JFaZ9gON.d.ts +80 -0
  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 +92 -92
  7. package/dist/client/index.js +80 -85
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/codegen/generators/index.d.ts +19 -0
  10. package/dist/codegen/generators/index.js +1500 -0
  11. package/dist/codegen/generators/index.js.map +1 -0
  12. package/dist/codegen/index.d.ts +76 -60
  13. package/dist/codegen/index.js +1486 -736
  14. package/dist/codegen/index.js.map +1 -1
  15. package/dist/database-errors-BNNmLTJE.d.ts +86 -0
  16. package/dist/db/index.d.ts +844 -44
  17. package/dist/db/index.js +1262 -1309
  18. package/dist/db/index.js.map +1 -1
  19. package/dist/env/index.d.ts +508 -0
  20. package/dist/env/index.js +1106 -0
  21. package/dist/env/index.js.map +1 -0
  22. package/dist/error-handler-wjLL3v-a.d.ts +44 -0
  23. package/dist/errors/index.d.ts +136 -0
  24. package/dist/errors/index.js +172 -0
  25. package/dist/errors/index.js.map +1 -0
  26. package/dist/index-DHiAqhKv.d.ts +101 -0
  27. package/dist/index.d.ts +3 -374
  28. package/dist/index.js +2394 -2176
  29. package/dist/index.js.map +1 -1
  30. package/dist/logger/index.d.ts +94 -0
  31. package/dist/logger/index.js +774 -0
  32. package/dist/logger/index.js.map +1 -0
  33. package/dist/middleware/index.d.ts +33 -0
  34. package/dist/middleware/index.js +890 -0
  35. package/dist/middleware/index.js.map +1 -0
  36. package/dist/route/index.d.ts +21 -53
  37. package/dist/route/index.js +1234 -219
  38. package/dist/route/index.js.map +1 -1
  39. package/dist/server/index.d.ts +18 -0
  40. package/dist/server/index.js +2390 -2058
  41. package/dist/server/index.js.map +1 -1
  42. package/dist/types-Dzggq1Yb.d.ts +170 -0
  43. package/package.json +59 -15
  44. package/dist/auto-loader-C44TcLmM.d.ts +0 -125
  45. package/dist/bind-pssq1NRT.d.ts +0 -34
  46. package/dist/postgres-errors-CY_Es8EJ.d.ts +0 -1703
  47. package/dist/scripts/index.d.ts +0 -24
  48. package/dist/scripts/index.js +0 -1201
  49. package/dist/scripts/index.js.map +0 -1
  50. package/dist/scripts/templates/api-index.template.txt +0 -10
  51. package/dist/scripts/templates/api-tag.template.txt +0 -11
  52. package/dist/scripts/templates/contract.template.txt +0 -87
  53. package/dist/scripts/templates/entity-type.template.txt +0 -31
  54. package/dist/scripts/templates/entity.template.txt +0 -19
  55. package/dist/scripts/templates/index.template.txt +0 -10
  56. package/dist/scripts/templates/repository.template.txt +0 -37
  57. package/dist/scripts/templates/routes-id.template.txt +0 -59
  58. package/dist/scripts/templates/routes-index.template.txt +0 -44
  59. package/dist/types-SlzTr8ZO.d.ts +0 -143
@@ -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 };