@qwickapps/server 1.3.0 → 1.4.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.
Files changed (241) hide show
  1. package/README.md +311 -0
  2. package/dist/core/control-panel.d.ts.map +1 -1
  3. package/dist/core/control-panel.js +144 -2
  4. package/dist/core/control-panel.js.map +1 -1
  5. package/dist/core/plugin-registry.d.ts +36 -0
  6. package/dist/core/plugin-registry.d.ts.map +1 -1
  7. package/dist/core/plugin-registry.js +26 -0
  8. package/dist/core/plugin-registry.js.map +1 -1
  9. package/dist/core/types.d.ts +19 -0
  10. package/dist/core/types.d.ts.map +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/plugins/auth/adapter-wrapper.d.ts +47 -0
  16. package/dist/plugins/auth/adapter-wrapper.d.ts.map +1 -0
  17. package/dist/plugins/auth/adapter-wrapper.js +166 -0
  18. package/dist/plugins/auth/adapter-wrapper.js.map +1 -0
  19. package/dist/plugins/auth/adapter-wrapper.test.d.ts +7 -0
  20. package/dist/plugins/auth/adapter-wrapper.test.d.ts.map +1 -0
  21. package/dist/plugins/auth/adapter-wrapper.test.js +303 -0
  22. package/dist/plugins/auth/adapter-wrapper.test.js.map +1 -0
  23. package/dist/plugins/auth/adapters/index.d.ts +1 -0
  24. package/dist/plugins/auth/adapters/index.d.ts.map +1 -1
  25. package/dist/plugins/auth/adapters/index.js +1 -0
  26. package/dist/plugins/auth/adapters/index.js.map +1 -1
  27. package/dist/plugins/auth/adapters/supabase-adapter.d.ts.map +1 -1
  28. package/dist/plugins/auth/adapters/supabase-adapter.js.map +1 -1
  29. package/dist/plugins/auth/adapters/supertokens-adapter.d.ts +18 -0
  30. package/dist/plugins/auth/adapters/supertokens-adapter.d.ts.map +1 -0
  31. package/dist/plugins/auth/adapters/supertokens-adapter.js +267 -0
  32. package/dist/plugins/auth/adapters/supertokens-adapter.js.map +1 -0
  33. package/dist/plugins/auth/config-store.d.ts +11 -0
  34. package/dist/plugins/auth/config-store.d.ts.map +1 -0
  35. package/dist/plugins/auth/config-store.js +232 -0
  36. package/dist/plugins/auth/config-store.js.map +1 -0
  37. package/dist/plugins/auth/config-store.test.d.ts +7 -0
  38. package/dist/plugins/auth/config-store.test.d.ts.map +1 -0
  39. package/dist/plugins/auth/config-store.test.js +299 -0
  40. package/dist/plugins/auth/config-store.test.js.map +1 -0
  41. package/dist/plugins/auth/env-config.d.ts +138 -0
  42. package/dist/plugins/auth/env-config.d.ts.map +1 -0
  43. package/dist/plugins/auth/env-config.js +1122 -0
  44. package/dist/plugins/auth/env-config.js.map +1 -0
  45. package/dist/plugins/auth/index.d.ts +7 -1
  46. package/dist/plugins/auth/index.d.ts.map +1 -1
  47. package/dist/plugins/auth/index.js +7 -0
  48. package/dist/plugins/auth/index.js.map +1 -1
  49. package/dist/plugins/auth/supertokens-adapter.test.d.ts +10 -0
  50. package/dist/plugins/auth/supertokens-adapter.test.d.ts.map +1 -0
  51. package/dist/plugins/auth/supertokens-adapter.test.js +486 -0
  52. package/dist/plugins/auth/supertokens-adapter.test.js.map +1 -0
  53. package/dist/plugins/auth/types.d.ts +176 -0
  54. package/dist/plugins/auth/types.d.ts.map +1 -1
  55. package/dist/plugins/auth/types.js.map +1 -1
  56. package/dist/plugins/cache-plugin.test.js +3 -0
  57. package/dist/plugins/cache-plugin.test.js.map +1 -1
  58. package/dist/plugins/index.d.ts +6 -2
  59. package/dist/plugins/index.d.ts.map +1 -1
  60. package/dist/plugins/index.js +5 -1
  61. package/dist/plugins/index.js.map +1 -1
  62. package/dist/plugins/postgres-plugin.test.js +3 -0
  63. package/dist/plugins/postgres-plugin.test.js.map +1 -1
  64. package/dist/plugins/preferences/__tests__/deep-merge.test.d.ts +7 -0
  65. package/dist/plugins/preferences/__tests__/deep-merge.test.d.ts.map +1 -0
  66. package/dist/plugins/preferences/__tests__/deep-merge.test.js +215 -0
  67. package/dist/plugins/preferences/__tests__/deep-merge.test.js.map +1 -0
  68. package/dist/plugins/preferences/__tests__/preferences-plugin.test.d.ts +7 -0
  69. package/dist/plugins/preferences/__tests__/preferences-plugin.test.d.ts.map +1 -0
  70. package/dist/plugins/preferences/__tests__/preferences-plugin.test.js +265 -0
  71. package/dist/plugins/preferences/__tests__/preferences-plugin.test.js.map +1 -0
  72. package/dist/plugins/preferences/index.d.ts +12 -0
  73. package/dist/plugins/preferences/index.d.ts.map +1 -0
  74. package/dist/plugins/preferences/index.js +13 -0
  75. package/dist/plugins/preferences/index.js.map +1 -0
  76. package/dist/plugins/preferences/preferences-plugin.d.ts +39 -0
  77. package/dist/plugins/preferences/preferences-plugin.d.ts.map +1 -0
  78. package/dist/plugins/preferences/preferences-plugin.js +226 -0
  79. package/dist/plugins/preferences/preferences-plugin.js.map +1 -0
  80. package/dist/plugins/preferences/stores/index.d.ts +9 -0
  81. package/dist/plugins/preferences/stores/index.d.ts.map +1 -0
  82. package/dist/plugins/preferences/stores/index.js +9 -0
  83. package/dist/plugins/preferences/stores/index.js.map +1 -0
  84. package/dist/plugins/preferences/stores/postgres-store.d.ts +41 -0
  85. package/dist/plugins/preferences/stores/postgres-store.d.ts.map +1 -0
  86. package/dist/plugins/preferences/stores/postgres-store.js +181 -0
  87. package/dist/plugins/preferences/stores/postgres-store.js.map +1 -0
  88. package/dist/plugins/preferences/types.d.ts +91 -0
  89. package/dist/plugins/preferences/types.d.ts.map +1 -0
  90. package/dist/plugins/preferences/types.js +10 -0
  91. package/dist/plugins/preferences/types.js.map +1 -0
  92. package/dist/plugins/rate-limit/__tests__/rate-limit-plugin.test.d.ts +7 -0
  93. package/dist/plugins/rate-limit/__tests__/rate-limit-plugin.test.d.ts.map +1 -0
  94. package/dist/plugins/rate-limit/__tests__/rate-limit-plugin.test.js +220 -0
  95. package/dist/plugins/rate-limit/__tests__/rate-limit-plugin.test.js.map +1 -0
  96. package/dist/plugins/rate-limit/cleanup.d.ts +40 -0
  97. package/dist/plugins/rate-limit/cleanup.d.ts.map +1 -0
  98. package/dist/plugins/rate-limit/cleanup.js +72 -0
  99. package/dist/plugins/rate-limit/cleanup.js.map +1 -0
  100. package/dist/plugins/rate-limit/env-config.d.ts +91 -0
  101. package/dist/plugins/rate-limit/env-config.d.ts.map +1 -0
  102. package/dist/plugins/rate-limit/env-config.js +318 -0
  103. package/dist/plugins/rate-limit/env-config.js.map +1 -0
  104. package/dist/plugins/rate-limit/index.d.ts +76 -0
  105. package/dist/plugins/rate-limit/index.d.ts.map +1 -0
  106. package/dist/plugins/rate-limit/index.js +79 -0
  107. package/dist/plugins/rate-limit/index.js.map +1 -0
  108. package/dist/plugins/rate-limit/middleware.d.ts +40 -0
  109. package/dist/plugins/rate-limit/middleware.d.ts.map +1 -0
  110. package/dist/plugins/rate-limit/middleware.js +169 -0
  111. package/dist/plugins/rate-limit/middleware.js.map +1 -0
  112. package/dist/plugins/rate-limit/rate-limit-plugin.d.ts +44 -0
  113. package/dist/plugins/rate-limit/rate-limit-plugin.d.ts.map +1 -0
  114. package/dist/plugins/rate-limit/rate-limit-plugin.js +354 -0
  115. package/dist/plugins/rate-limit/rate-limit-plugin.js.map +1 -0
  116. package/dist/plugins/rate-limit/rate-limit-service.d.ts +110 -0
  117. package/dist/plugins/rate-limit/rate-limit-service.d.ts.map +1 -0
  118. package/dist/plugins/rate-limit/rate-limit-service.js +172 -0
  119. package/dist/plugins/rate-limit/rate-limit-service.js.map +1 -0
  120. package/dist/plugins/rate-limit/stores/cache-store.d.ts +33 -0
  121. package/dist/plugins/rate-limit/stores/cache-store.d.ts.map +1 -0
  122. package/dist/plugins/rate-limit/stores/cache-store.js +225 -0
  123. package/dist/plugins/rate-limit/stores/cache-store.js.map +1 -0
  124. package/dist/plugins/rate-limit/stores/index.d.ts +8 -0
  125. package/dist/plugins/rate-limit/stores/index.d.ts.map +1 -0
  126. package/dist/plugins/rate-limit/stores/index.js +8 -0
  127. package/dist/plugins/rate-limit/stores/index.js.map +1 -0
  128. package/dist/plugins/rate-limit/stores/postgres-store.d.ts +34 -0
  129. package/dist/plugins/rate-limit/stores/postgres-store.d.ts.map +1 -0
  130. package/dist/plugins/rate-limit/stores/postgres-store.js +320 -0
  131. package/dist/plugins/rate-limit/stores/postgres-store.js.map +1 -0
  132. package/dist/plugins/rate-limit/strategies/fixed-window.d.ts +21 -0
  133. package/dist/plugins/rate-limit/strategies/fixed-window.d.ts.map +1 -0
  134. package/dist/plugins/rate-limit/strategies/fixed-window.js +97 -0
  135. package/dist/plugins/rate-limit/strategies/fixed-window.js.map +1 -0
  136. package/dist/plugins/rate-limit/strategies/index.d.ts +14 -0
  137. package/dist/plugins/rate-limit/strategies/index.d.ts.map +1 -0
  138. package/dist/plugins/rate-limit/strategies/index.js +27 -0
  139. package/dist/plugins/rate-limit/strategies/index.js.map +1 -0
  140. package/dist/plugins/rate-limit/strategies/sliding-window.d.ts +22 -0
  141. package/dist/plugins/rate-limit/strategies/sliding-window.d.ts.map +1 -0
  142. package/dist/plugins/rate-limit/strategies/sliding-window.js +122 -0
  143. package/dist/plugins/rate-limit/strategies/sliding-window.js.map +1 -0
  144. package/dist/plugins/rate-limit/strategies/token-bucket.d.ts +28 -0
  145. package/dist/plugins/rate-limit/strategies/token-bucket.d.ts.map +1 -0
  146. package/dist/plugins/rate-limit/strategies/token-bucket.js +121 -0
  147. package/dist/plugins/rate-limit/strategies/token-bucket.js.map +1 -0
  148. package/dist/plugins/rate-limit/types.d.ts +265 -0
  149. package/dist/plugins/rate-limit/types.d.ts.map +1 -0
  150. package/dist/plugins/rate-limit/types.js +9 -0
  151. package/dist/plugins/rate-limit/types.js.map +1 -0
  152. package/dist/plugins/users/__tests__/users-plugin.test.d.ts +9 -0
  153. package/dist/plugins/users/__tests__/users-plugin.test.d.ts.map +1 -0
  154. package/dist/plugins/users/__tests__/users-plugin.test.js +546 -0
  155. package/dist/plugins/users/__tests__/users-plugin.test.js.map +1 -0
  156. package/dist/plugins/users/index.d.ts +2 -2
  157. package/dist/plugins/users/index.d.ts.map +1 -1
  158. package/dist/plugins/users/index.js +1 -1
  159. package/dist/plugins/users/index.js.map +1 -1
  160. package/dist/plugins/users/types.d.ts +36 -0
  161. package/dist/plugins/users/types.d.ts.map +1 -1
  162. package/dist/plugins/users/users-plugin.d.ts +8 -2
  163. package/dist/plugins/users/users-plugin.d.ts.map +1 -1
  164. package/dist/plugins/users/users-plugin.js +122 -5
  165. package/dist/plugins/users/users-plugin.js.map +1 -1
  166. package/dist-ui/assets/index-D7DoZ9rL.js +478 -0
  167. package/dist-ui/assets/index-D7DoZ9rL.js.map +1 -0
  168. package/dist-ui/index.html +1 -1
  169. package/dist-ui-lib/api/controlPanelApi.d.ts +194 -7
  170. package/dist-ui-lib/dashboard/WidgetComponentRegistry.d.ts +9 -5
  171. package/dist-ui-lib/dashboard/builtInWidgets.d.ts +7 -1
  172. package/dist-ui-lib/dashboard/widgets/AuthStatusWidget.d.ts +9 -0
  173. package/dist-ui-lib/dashboard/widgets/IntegrationStatusWidget.d.ts +9 -0
  174. package/dist-ui-lib/dashboard/widgets/index.d.ts +2 -0
  175. package/dist-ui-lib/index.js +3665 -3945
  176. package/dist-ui-lib/index.js.map +1 -1
  177. package/dist-ui-lib/pages/AuthPage.d.ts +1 -0
  178. package/dist-ui-lib/pages/IntegrationsPage.d.ts +1 -0
  179. package/dist-ui-lib/pages/PluginsPage.d.ts +1 -0
  180. package/dist-ui-lib/pages/RateLimitPage.d.ts +1 -0
  181. package/package.json +7 -2
  182. package/src/core/control-panel.ts +161 -2
  183. package/src/core/plugin-registry.ts +63 -0
  184. package/src/core/types.ts +17 -0
  185. package/src/index.ts +45 -0
  186. package/src/plugins/auth/adapter-wrapper.test.ts +395 -0
  187. package/src/plugins/auth/adapter-wrapper.ts +205 -0
  188. package/src/plugins/auth/adapters/index.ts +1 -0
  189. package/src/plugins/auth/adapters/supabase-adapter.ts +22 -14
  190. package/src/plugins/auth/adapters/supertokens-adapter.ts +326 -0
  191. package/src/plugins/auth/config-store.test.ts +417 -0
  192. package/src/plugins/auth/config-store.ts +305 -0
  193. package/src/plugins/auth/env-config.ts +1279 -0
  194. package/src/plugins/auth/index.ts +30 -0
  195. package/src/plugins/auth/supertokens-adapter.test.ts +621 -0
  196. package/src/plugins/auth/types.ts +218 -0
  197. package/src/plugins/cache-plugin.test.ts +3 -0
  198. package/src/plugins/index.ts +75 -0
  199. package/src/plugins/postgres-plugin.test.ts +3 -0
  200. package/src/plugins/preferences/__tests__/deep-merge.test.ts +242 -0
  201. package/src/plugins/preferences/__tests__/preferences-plugin.test.ts +350 -0
  202. package/src/plugins/preferences/index.ts +30 -0
  203. package/src/plugins/preferences/preferences-plugin.ts +270 -0
  204. package/src/plugins/preferences/stores/index.ts +9 -0
  205. package/src/plugins/preferences/stores/postgres-store.ts +252 -0
  206. package/src/plugins/preferences/types.ts +100 -0
  207. package/src/plugins/rate-limit/__tests__/rate-limit-plugin.test.ts +259 -0
  208. package/src/plugins/rate-limit/cleanup.ts +117 -0
  209. package/src/plugins/rate-limit/env-config.ts +400 -0
  210. package/src/plugins/rate-limit/index.ts +128 -0
  211. package/src/plugins/rate-limit/middleware.ts +212 -0
  212. package/src/plugins/rate-limit/rate-limit-plugin.ts +400 -0
  213. package/src/plugins/rate-limit/rate-limit-service.ts +228 -0
  214. package/src/plugins/rate-limit/stores/cache-store.ts +261 -0
  215. package/src/plugins/rate-limit/stores/index.ts +8 -0
  216. package/src/plugins/rate-limit/stores/postgres-store.ts +402 -0
  217. package/src/plugins/rate-limit/strategies/fixed-window.ts +116 -0
  218. package/src/plugins/rate-limit/strategies/index.ts +30 -0
  219. package/src/plugins/rate-limit/strategies/sliding-window.ts +157 -0
  220. package/src/plugins/rate-limit/strategies/token-bucket.ts +154 -0
  221. package/src/plugins/rate-limit/types.ts +338 -0
  222. package/src/plugins/users/__tests__/users-plugin.test.ts +690 -0
  223. package/src/plugins/users/index.ts +3 -0
  224. package/src/plugins/users/types.ts +38 -0
  225. package/src/plugins/users/users-plugin.ts +142 -5
  226. package/ui/src/App.tsx +35 -14
  227. package/ui/src/api/controlPanelApi.ts +326 -1
  228. package/ui/src/components/ControlPanelApp.tsx +3 -0
  229. package/ui/src/dashboard/PluginWidgetRenderer.tsx +13 -10
  230. package/ui/src/dashboard/WidgetComponentRegistry.tsx +13 -9
  231. package/ui/src/dashboard/builtInWidgets.tsx +13 -3
  232. package/ui/src/dashboard/widgets/AuthStatusWidget.tsx +143 -0
  233. package/ui/src/dashboard/widgets/IntegrationStatusWidget.tsx +135 -0
  234. package/ui/src/dashboard/widgets/index.ts +2 -0
  235. package/ui/src/pages/AuthPage.tsx +1103 -0
  236. package/ui/src/pages/IntegrationsPage.tsx +288 -0
  237. package/ui/src/pages/PluginsPage.tsx +394 -0
  238. package/ui/src/pages/RateLimitPage.tsx +292 -0
  239. package/ui/vite.lib.config.ts +5 -0
  240. package/dist-ui/assets/index-Bsp2ntcw.js +0 -465
  241. package/dist-ui/assets/index-Bsp2ntcw.js.map +0 -1
@@ -0,0 +1,252 @@
1
+ /**
2
+ * PostgreSQL Preferences Store
3
+ *
4
+ * Preferences storage implementation using PostgreSQL with Row-Level Security (RLS).
5
+ * Requires the 'pg' package and the Users plugin to be installed.
6
+ *
7
+ * RLS Context Pattern:
8
+ * Each operation uses an explicit transaction and sets `app.current_user_id`
9
+ * as a transaction-local configuration variable. The RLS policy checks this
10
+ * variable to enforce that users can only access their own preferences.
11
+ *
12
+ * Copyright (c) 2025 QwickApps.com. All rights reserved.
13
+ */
14
+
15
+ import type {
16
+ PreferencesStore,
17
+ PostgresPreferencesStoreConfig,
18
+ } from '../types.js';
19
+
20
+ // Pool interface (from pg package)
21
+ interface PgPool {
22
+ query(text: string, values?: unknown[]): Promise<{ rows: unknown[]; rowCount: number | null }>;
23
+ connect(): Promise<PgPoolClient>;
24
+ }
25
+
26
+ interface PgPoolClient {
27
+ query(text: string, values?: unknown[]): Promise<{ rows: unknown[]; rowCount: number | null }>;
28
+ release(): void;
29
+ }
30
+
31
+ /**
32
+ * Deep merge two objects
33
+ * - Objects are recursively merged
34
+ * - Arrays are replaced (not merged)
35
+ * - Source values override target values
36
+ */
37
+ export function deepMerge(
38
+ target: Record<string, unknown>,
39
+ source: Record<string, unknown>
40
+ ): Record<string, unknown> {
41
+ const output = { ...target };
42
+
43
+ for (const key of Object.keys(source)) {
44
+ const sourceValue = source[key];
45
+ const targetValue = target[key];
46
+
47
+ // Skip undefined values in source
48
+ if (sourceValue === undefined) {
49
+ continue;
50
+ }
51
+
52
+ // If both are plain objects, merge recursively
53
+ if (
54
+ sourceValue !== null &&
55
+ typeof sourceValue === 'object' &&
56
+ !Array.isArray(sourceValue) &&
57
+ targetValue !== null &&
58
+ typeof targetValue === 'object' &&
59
+ !Array.isArray(targetValue)
60
+ ) {
61
+ output[key] = deepMerge(
62
+ targetValue as Record<string, unknown>,
63
+ sourceValue as Record<string, unknown>
64
+ );
65
+ } else {
66
+ // Otherwise, source overwrites target
67
+ output[key] = sourceValue;
68
+ }
69
+ }
70
+
71
+ return output;
72
+ }
73
+
74
+ /**
75
+ * Execute a function within an RLS-protected transaction
76
+ *
77
+ * This helper ensures that:
78
+ * 1. All queries run within the same transaction
79
+ * 2. The RLS context is set before any data access
80
+ * 3. The transaction is properly committed or rolled back
81
+ *
82
+ * @param pool PostgreSQL pool
83
+ * @param userId User ID to set as the RLS context
84
+ * @param callback Function to execute within the transaction
85
+ */
86
+ async function withRLSContext<T>(
87
+ pool: PgPool,
88
+ userId: string,
89
+ callback: (client: PgPoolClient) => Promise<T>
90
+ ): Promise<T> {
91
+ const client = await pool.connect();
92
+ try {
93
+ await client.query('BEGIN');
94
+ // Set transaction-local user context for RLS
95
+ await client.query(
96
+ "SELECT set_config('app.current_user_id', $1, true)",
97
+ [userId]
98
+ );
99
+ const result = await callback(client);
100
+ await client.query('COMMIT');
101
+ return result;
102
+ } catch (error) {
103
+ await client.query('ROLLBACK');
104
+ throw error;
105
+ } finally {
106
+ client.release();
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Create a PostgreSQL preferences store with RLS
112
+ *
113
+ * @param config Configuration including a pg Pool instance
114
+ * @returns PreferencesStore implementation
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * import { Pool } from 'pg';
119
+ * import { postgresPreferencesStore } from '@qwickapps/server';
120
+ *
121
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
122
+ * const store = postgresPreferencesStore({ pool });
123
+ *
124
+ * // Or with lazy initialization:
125
+ * const store = postgresPreferencesStore({ pool: () => getPostgres().getPool() });
126
+ * ```
127
+ */
128
+ export function postgresPreferencesStore(config: PostgresPreferencesStoreConfig): PreferencesStore {
129
+ const {
130
+ pool: poolOrFn,
131
+ tableName = 'user_preferences',
132
+ schema = 'public',
133
+ autoCreateTables = true,
134
+ enableRLS = true,
135
+ } = config;
136
+
137
+ // Helper to get pool (supports lazy initialization via function)
138
+ const getPool = (): PgPool => {
139
+ const pool = typeof poolOrFn === 'function' ? poolOrFn() : poolOrFn;
140
+ if (!pool || typeof (pool as PgPool).query !== 'function') {
141
+ throw new Error('Invalid pool: must have query method');
142
+ }
143
+ return pool as PgPool;
144
+ };
145
+
146
+ const tableFullName = `"${schema}"."${tableName}"`;
147
+
148
+ return {
149
+ name: 'postgres',
150
+
151
+ async initialize(): Promise<void> {
152
+ if (!autoCreateTables) return;
153
+
154
+ const pool = getPool();
155
+
156
+ // Create table with foreign key to users
157
+ await pool.query(`
158
+ CREATE TABLE IF NOT EXISTS ${tableFullName} (
159
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
160
+ user_id UUID NOT NULL REFERENCES "public"."users"(id) ON DELETE CASCADE,
161
+ preferences JSONB NOT NULL DEFAULT '{}',
162
+ created_at TIMESTAMPTZ DEFAULT NOW(),
163
+ updated_at TIMESTAMPTZ DEFAULT NOW(),
164
+ UNIQUE(user_id)
165
+ );
166
+
167
+ CREATE INDEX IF NOT EXISTS idx_${tableName}_user_id ON ${tableFullName}(user_id);
168
+ `);
169
+
170
+ // Enable RLS if configured
171
+ if (enableRLS) {
172
+ await pool.query(`
173
+ ALTER TABLE ${tableFullName} ENABLE ROW LEVEL SECURITY;
174
+ ALTER TABLE ${tableFullName} FORCE ROW LEVEL SECURITY;
175
+ `);
176
+
177
+ // Create or replace the RLS policy
178
+ // Drop existing policy first to avoid errors on re-initialization
179
+ await pool.query(`
180
+ DROP POLICY IF EXISTS "${tableName}_owner" ON ${tableFullName};
181
+ `);
182
+
183
+ // RLS policy with both USING (for SELECT/UPDATE/DELETE reads)
184
+ // and WITH CHECK (for INSERT/UPDATE writes) clauses
185
+ await pool.query(`
186
+ CREATE POLICY "${tableName}_owner" ON ${tableFullName}
187
+ FOR ALL
188
+ USING (user_id::text = current_setting('app.current_user_id', true))
189
+ WITH CHECK (user_id::text = current_setting('app.current_user_id', true));
190
+ `);
191
+ }
192
+ },
193
+
194
+ async get(userId: string): Promise<Record<string, unknown> | null> {
195
+ return withRLSContext(getPool(), userId, async (client) => {
196
+ const result = await client.query(
197
+ `SELECT preferences FROM ${tableFullName} WHERE user_id = $1`,
198
+ [userId]
199
+ );
200
+
201
+ if (result.rows.length === 0) {
202
+ return null;
203
+ }
204
+
205
+ return (result.rows[0] as { preferences: Record<string, unknown> }).preferences;
206
+ });
207
+ },
208
+
209
+ async update(userId: string, preferences: Record<string, unknown>): Promise<Record<string, unknown>> {
210
+ return withRLSContext(getPool(), userId, async (client) => {
211
+ // Get existing preferences within the same transaction
212
+ const existingResult = await client.query(
213
+ `SELECT preferences FROM ${tableFullName} WHERE user_id = $1`,
214
+ [userId]
215
+ );
216
+
217
+ const existing = existingResult.rows.length > 0
218
+ ? (existingResult.rows[0] as { preferences: Record<string, unknown> }).preferences
219
+ : null;
220
+
221
+ const merged = existing ? deepMerge(existing, preferences) : preferences;
222
+
223
+ // Upsert the merged preferences
224
+ await client.query(
225
+ `INSERT INTO ${tableFullName} (user_id, preferences, updated_at)
226
+ VALUES ($1, $2, NOW())
227
+ ON CONFLICT (user_id) DO UPDATE SET
228
+ preferences = $2,
229
+ updated_at = NOW()`,
230
+ [userId, JSON.stringify(merged)]
231
+ );
232
+
233
+ return merged;
234
+ });
235
+ },
236
+
237
+ async delete(userId: string): Promise<boolean> {
238
+ return withRLSContext(getPool(), userId, async (client) => {
239
+ const result = await client.query(
240
+ `DELETE FROM ${tableFullName} WHERE user_id = $1`,
241
+ [userId]
242
+ );
243
+
244
+ return (result.rowCount ?? 0) > 0;
245
+ });
246
+ },
247
+
248
+ async shutdown(): Promise<void> {
249
+ // Pool is managed externally, nothing to do here
250
+ },
251
+ };
252
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Preferences Plugin Types
3
+ *
4
+ * Type definitions for user preferences management.
5
+ * Supports PostgreSQL with Row-Level Security (RLS) for data isolation.
6
+ *
7
+ * Copyright (c) 2025 QwickApps.com. All rights reserved.
8
+ */
9
+
10
+ /**
11
+ * User preferences record in the database
12
+ */
13
+ export interface UserPreferences {
14
+ /** Primary key - UUID */
15
+ id: string;
16
+ /** User ID (foreign key to users table) */
17
+ user_id: string;
18
+ /** Preferences as JSON object */
19
+ preferences: Record<string, unknown>;
20
+ /** When the preferences were created */
21
+ created_at: Date;
22
+ /** When the preferences were last updated */
23
+ updated_at: Date;
24
+ }
25
+
26
+ /**
27
+ * Preferences store interface - all storage backends must implement this
28
+ */
29
+ export interface PreferencesStore {
30
+ /** Store name (e.g., 'postgres', 'memory') */
31
+ name: string;
32
+
33
+ /**
34
+ * Initialize the store (create tables, RLS policies, etc.)
35
+ */
36
+ initialize(): Promise<void>;
37
+
38
+ /**
39
+ * Get preferences for a user
40
+ * Returns null if no preferences exist for the user
41
+ */
42
+ get(userId: string): Promise<Record<string, unknown> | null>;
43
+
44
+ /**
45
+ * Update preferences for a user (upsert with deep merge)
46
+ * Returns the merged preferences
47
+ */
48
+ update(userId: string, preferences: Record<string, unknown>): Promise<Record<string, unknown>>;
49
+
50
+ /**
51
+ * Delete preferences for a user
52
+ * Returns true if preferences were deleted, false if none existed
53
+ */
54
+ delete(userId: string): Promise<boolean>;
55
+
56
+ /**
57
+ * Shutdown the store
58
+ */
59
+ shutdown(): Promise<void>;
60
+ }
61
+
62
+ /**
63
+ * PostgreSQL preferences store configuration
64
+ */
65
+ export interface PostgresPreferencesStoreConfig {
66
+ /** PostgreSQL pool instance or a function that returns one (for lazy initialization) */
67
+ pool: unknown | (() => unknown);
68
+ /** Table name (default: 'user_preferences') */
69
+ tableName?: string;
70
+ /** Schema name (default: 'public') */
71
+ schema?: string;
72
+ /** Auto-create tables on init (default: true) */
73
+ autoCreateTables?: boolean;
74
+ /** Enable RLS (default: true) */
75
+ enableRLS?: boolean;
76
+ }
77
+
78
+ /**
79
+ * Preferences API configuration
80
+ */
81
+ export interface PreferencesApiConfig {
82
+ /** API route prefix (default: '/preferences') */
83
+ prefix?: string;
84
+ /** Enable API endpoints (default: true) */
85
+ enabled?: boolean;
86
+ }
87
+
88
+ /**
89
+ * Preferences plugin configuration
90
+ */
91
+ export interface PreferencesPluginConfig {
92
+ /** Preferences storage backend */
93
+ store: PreferencesStore;
94
+ /** Default preferences to merge with stored preferences on read */
95
+ defaults?: Record<string, unknown>;
96
+ /** API configuration */
97
+ api?: PreferencesApiConfig;
98
+ /** Enable debug logging */
99
+ debug?: boolean;
100
+ }
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Rate Limit Plugin Tests
3
+ *
4
+ * Copyright (c) 2025 QwickApps.com. All rights reserved.
5
+ */
6
+
7
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
8
+ import type { RateLimitStore, RateLimitCache, CachedLimit, StoredLimit, IncrementOptions } from '../types.js';
9
+ import { RateLimitService } from '../rate-limit-service.js';
10
+ import { createSlidingWindowStrategy } from '../strategies/sliding-window.js';
11
+ import { createFixedWindowStrategy } from '../strategies/fixed-window.js';
12
+ import { createTokenBucketStrategy } from '../strategies/token-bucket.js';
13
+
14
+ // Mock store implementation
15
+ function createMockStore(): RateLimitStore {
16
+ const records = new Map<string, StoredLimit>();
17
+
18
+ return {
19
+ name: 'mock',
20
+
21
+ async initialize(): Promise<void> {
22
+ // No-op
23
+ },
24
+
25
+ async get(key: string): Promise<StoredLimit | null> {
26
+ return records.get(key) || null;
27
+ },
28
+
29
+ async increment(key: string, options: IncrementOptions): Promise<StoredLimit> {
30
+ const now = new Date();
31
+ const windowMs = options.windowMs;
32
+ const windowStart = new Date(now.getTime() - (now.getTime() % windowMs));
33
+ const windowEnd = new Date(windowStart.getTime() + windowMs);
34
+
35
+ const existing = records.get(key);
36
+ if (existing && existing.windowStart.getTime() === windowStart.getTime()) {
37
+ existing.count += options.amount || 1;
38
+ existing.updatedAt = now;
39
+ return existing;
40
+ }
41
+
42
+ const newRecord: StoredLimit = {
43
+ id: `mock-${Date.now()}`,
44
+ key,
45
+ count: options.amount || 1,
46
+ maxRequests: options.maxRequests,
47
+ windowMs: options.windowMs,
48
+ windowStart,
49
+ windowEnd,
50
+ strategy: options.strategy,
51
+ userId: options.userId,
52
+ tenantId: options.tenantId,
53
+ ipAddress: options.ipAddress,
54
+ createdAt: now,
55
+ updatedAt: now,
56
+ };
57
+ records.set(key, newRecord);
58
+ return newRecord;
59
+ },
60
+
61
+ async clear(key: string): Promise<boolean> {
62
+ return records.delete(key);
63
+ },
64
+
65
+ async cleanup(): Promise<number> {
66
+ const now = Date.now();
67
+ let deleted = 0;
68
+ for (const [key, record] of records) {
69
+ if (record.windowEnd.getTime() < now) {
70
+ records.delete(key);
71
+ deleted++;
72
+ }
73
+ }
74
+ return deleted;
75
+ },
76
+
77
+ async shutdown(): Promise<void> {
78
+ records.clear();
79
+ },
80
+ };
81
+ }
82
+
83
+ // Mock cache implementation
84
+ function createMockCache(): RateLimitCache {
85
+ const cache = new Map<string, { value: CachedLimit; expiresAt: number }>();
86
+
87
+ return {
88
+ name: 'mock',
89
+
90
+ async get(key: string): Promise<CachedLimit | null> {
91
+ const entry = cache.get(key);
92
+ if (!entry) return null;
93
+ if (entry.expiresAt <= Date.now()) {
94
+ cache.delete(key);
95
+ return null;
96
+ }
97
+ return entry.value;
98
+ },
99
+
100
+ async set(key: string, value: CachedLimit, ttlMs: number): Promise<void> {
101
+ cache.set(key, { value, expiresAt: Date.now() + ttlMs });
102
+ },
103
+
104
+ async increment(key: string, amount = 1): Promise<number | null> {
105
+ const entry = cache.get(key);
106
+ if (!entry || entry.expiresAt <= Date.now()) return null;
107
+ entry.value.count += amount;
108
+ return entry.value.count;
109
+ },
110
+
111
+ async delete(key: string): Promise<boolean> {
112
+ return cache.delete(key);
113
+ },
114
+
115
+ isAvailable(): boolean {
116
+ return true;
117
+ },
118
+
119
+ async shutdown(): Promise<void> {
120
+ cache.clear();
121
+ },
122
+ };
123
+ }
124
+
125
+ describe('RateLimitService', () => {
126
+ let store: RateLimitStore;
127
+ let cache: RateLimitCache;
128
+ let service: RateLimitService;
129
+
130
+ beforeEach(() => {
131
+ store = createMockStore();
132
+ cache = createMockCache();
133
+ service = new RateLimitService({
134
+ store,
135
+ cache,
136
+ defaults: {
137
+ windowMs: 60000,
138
+ maxRequests: 100,
139
+ strategy: 'sliding-window',
140
+ },
141
+ });
142
+ });
143
+
144
+ afterEach(async () => {
145
+ await store.shutdown();
146
+ await cache.shutdown();
147
+ });
148
+
149
+ describe('checkLimit', () => {
150
+ it('should return not limited for first request', async () => {
151
+ const status = await service.checkLimit('test:key', { increment: false });
152
+
153
+ expect(status.limited).toBe(false);
154
+ expect(status.current).toBe(0);
155
+ expect(status.limit).toBe(100);
156
+ expect(status.remaining).toBe(100);
157
+ });
158
+
159
+ it('should use provided options over defaults', async () => {
160
+ const status = await service.checkLimit('test:key', {
161
+ maxRequests: 50,
162
+ windowMs: 30000,
163
+ increment: false,
164
+ });
165
+
166
+ expect(status.limit).toBe(50);
167
+ });
168
+ });
169
+
170
+ describe('incrementLimit', () => {
171
+ it('should increment the counter', async () => {
172
+ const status1 = await service.incrementLimit('test:key');
173
+ expect(status1.current).toBe(1);
174
+ expect(status1.remaining).toBe(99);
175
+
176
+ const status2 = await service.incrementLimit('test:key');
177
+ expect(status2.current).toBe(2);
178
+ expect(status2.remaining).toBe(98);
179
+ });
180
+
181
+ it('should return limited when max reached', async () => {
182
+ // Set low limit for testing
183
+ for (let i = 0; i < 5; i++) {
184
+ await service.incrementLimit('test:key', { maxRequests: 5 });
185
+ }
186
+
187
+ const status = await service.incrementLimit('test:key', { maxRequests: 5 });
188
+ expect(status.limited).toBe(true);
189
+ expect(status.remaining).toBe(0);
190
+ });
191
+ });
192
+
193
+ describe('isLimited', () => {
194
+ it('should return false when not limited', async () => {
195
+ const limited = await service.isLimited('test:key');
196
+ expect(limited).toBe(false);
197
+ });
198
+
199
+ it('should return true when limited', async () => {
200
+ // Exhaust the limit
201
+ for (let i = 0; i < 5; i++) {
202
+ await service.incrementLimit('test:key', { maxRequests: 5 });
203
+ }
204
+
205
+ const limited = await service.isLimited('test:key', { maxRequests: 5 });
206
+ expect(limited).toBe(true);
207
+ });
208
+ });
209
+
210
+ describe('clearLimit', () => {
211
+ it('should clear the limit', async () => {
212
+ // Create some limits
213
+ await service.incrementLimit('test:key');
214
+ await service.incrementLimit('test:key');
215
+
216
+ // Verify exists
217
+ const beforeStatus = await service.checkLimit('test:key', { increment: false });
218
+ expect(beforeStatus.current).toBeGreaterThan(0);
219
+
220
+ // Clear
221
+ await service.clearLimit('test:key');
222
+
223
+ // Verify cleared
224
+ const afterStatus = await service.checkLimit('test:key', { increment: false });
225
+ expect(afterStatus.current).toBe(0);
226
+ });
227
+ });
228
+ });
229
+
230
+ describe('Strategies', () => {
231
+ describe('Sliding Window', () => {
232
+ it('should create strategy with correct name', () => {
233
+ const strategy = createSlidingWindowStrategy();
234
+ expect(strategy.name).toBe('sliding-window');
235
+ });
236
+ });
237
+
238
+ describe('Fixed Window', () => {
239
+ it('should create strategy with correct name', () => {
240
+ const strategy = createFixedWindowStrategy();
241
+ expect(strategy.name).toBe('fixed-window');
242
+ });
243
+ });
244
+
245
+ describe('Token Bucket', () => {
246
+ it('should create strategy with correct name', () => {
247
+ const strategy = createTokenBucketStrategy();
248
+ expect(strategy.name).toBe('token-bucket');
249
+ });
250
+ });
251
+ });
252
+
253
+ describe('Types', () => {
254
+ it('should export all required types', async () => {
255
+ // This test verifies that the types module compiles correctly
256
+ const types = await import('../types.js');
257
+ expect(types).toBeDefined();
258
+ });
259
+ });