@env-hopper/backend-core 2.0.1-alpha.1 → 2.0.1-alpha.3
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 +232 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +289 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/db/client.ts +8 -0
- package/src/db/index.ts +8 -4
- package/src/index.ts +67 -52
- package/src/middleware/backendResolver.ts +49 -0
- package/src/middleware/createEhMiddleware.ts +177 -0
- package/src/middleware/database.ts +62 -0
- package/src/middleware/featureRegistry.ts +149 -0
- package/src/middleware/index.ts +18 -0
- package/src/middleware/types.ts +167 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { Router } from 'express'
|
|
2
|
+
import type { LanguageModel, Tool } from 'ai'
|
|
3
|
+
import type { BetterAuthOptions, BetterAuthPlugin } from 'better-auth'
|
|
4
|
+
import type { EhBackendCompanySpecificBackend } from '../types/backend/companySpecificBackend'
|
|
5
|
+
import type { BetterAuth } from '../modules/auth/auth'
|
|
6
|
+
import type { TRPCRouter } from '../server/controller'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Database connection configuration.
|
|
10
|
+
* Supports both connection URL and structured config.
|
|
11
|
+
*/
|
|
12
|
+
export type EhDatabaseConfig =
|
|
13
|
+
| { url: string }
|
|
14
|
+
| {
|
|
15
|
+
host: string
|
|
16
|
+
port: number
|
|
17
|
+
database: string
|
|
18
|
+
username: string
|
|
19
|
+
password: string
|
|
20
|
+
schema?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Auth configuration for Better Auth integration.
|
|
25
|
+
*/
|
|
26
|
+
export interface EhAuthConfig {
|
|
27
|
+
/** Base URL for auth callbacks (e.g., 'http://localhost:4000') */
|
|
28
|
+
baseURL: string
|
|
29
|
+
/** Secret for signing sessions (min 32 chars in production) */
|
|
30
|
+
secret: string
|
|
31
|
+
/** OAuth providers configuration */
|
|
32
|
+
providers?: BetterAuthOptions['socialProviders']
|
|
33
|
+
/** Additional Better Auth plugins (e.g., Okta) */
|
|
34
|
+
plugins?: Array<BetterAuthPlugin>
|
|
35
|
+
/** Session expiration in seconds (default: 30 days) */
|
|
36
|
+
sessionExpiresIn?: number
|
|
37
|
+
/** Session refresh threshold in seconds (default: 1 day) */
|
|
38
|
+
sessionUpdateAge?: number
|
|
39
|
+
/** Application name shown in auth UI */
|
|
40
|
+
appName?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Admin chat (AI) configuration.
|
|
45
|
+
* When provided, enables the admin/chat endpoint.
|
|
46
|
+
*/
|
|
47
|
+
export interface EhAdminChatConfig {
|
|
48
|
+
/** AI model instance from @ai-sdk/* packages */
|
|
49
|
+
model: LanguageModel
|
|
50
|
+
/** System prompt for the AI assistant */
|
|
51
|
+
systemPrompt?: string
|
|
52
|
+
/** Custom tools available to the AI */
|
|
53
|
+
tools?: Record<string, Tool>
|
|
54
|
+
/** Validation function called before each request */
|
|
55
|
+
validateConfig?: () => void
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Feature toggles for enabling/disabling specific functionality.
|
|
60
|
+
* All features are enabled by default.
|
|
61
|
+
*/
|
|
62
|
+
export interface EhFeatureToggles {
|
|
63
|
+
/** Enable tRPC endpoints (default: true) */
|
|
64
|
+
trpc?: boolean
|
|
65
|
+
/** Enable auth endpoints (default: true) */
|
|
66
|
+
auth?: boolean
|
|
67
|
+
/** Enable icon REST endpoints (default: true) */
|
|
68
|
+
icons?: boolean
|
|
69
|
+
/** Enable asset REST endpoints (default: true) */
|
|
70
|
+
assets?: boolean
|
|
71
|
+
/** Enable screenshot REST endpoints (default: true) */
|
|
72
|
+
screenshots?: boolean
|
|
73
|
+
/** Enable admin chat endpoint (default: true if adminChat config provided) */
|
|
74
|
+
adminChat?: boolean
|
|
75
|
+
/** Enable legacy icon endpoint at /static/icon/:icon (default: false) */
|
|
76
|
+
legacyIconEndpoint?: boolean
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Company-specific backend can be provided as:
|
|
81
|
+
* 1. Direct object implementing the interface
|
|
82
|
+
* 2. Factory function called per-request (for DI integration)
|
|
83
|
+
* 3. Async factory function
|
|
84
|
+
*/
|
|
85
|
+
export type EhBackendProvider =
|
|
86
|
+
| EhBackendCompanySpecificBackend
|
|
87
|
+
| (() => EhBackendCompanySpecificBackend)
|
|
88
|
+
| (() => Promise<EhBackendCompanySpecificBackend>)
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Lifecycle hooks for database and middleware events.
|
|
92
|
+
*/
|
|
93
|
+
export interface EhLifecycleHooks {
|
|
94
|
+
/** Called after database connection is established */
|
|
95
|
+
onDatabaseConnected?: () => void | Promise<void>
|
|
96
|
+
/** Called before database disconnection (for cleanup) */
|
|
97
|
+
onDatabaseDisconnecting?: () => void | Promise<void>
|
|
98
|
+
/** Called after all routes are registered - use to add custom routes */
|
|
99
|
+
onRoutesRegistered?: (router: Router) => void | Promise<void>
|
|
100
|
+
/** Custom error handler for middleware errors */
|
|
101
|
+
onError?: (error: Error, context: { path: string }) => void
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Main configuration options for the env-hopper middleware.
|
|
106
|
+
*/
|
|
107
|
+
export interface EhMiddlewareOptions {
|
|
108
|
+
/**
|
|
109
|
+
* Base path prefix for all routes (default: '/api')
|
|
110
|
+
* - tRPC: {basePath}/trpc
|
|
111
|
+
* - Auth: {basePath}/auth (note: auth basePath is hardcoded, this affects where router mounts)
|
|
112
|
+
* - Icons: {basePath}/icons
|
|
113
|
+
* - Assets: {basePath}/assets
|
|
114
|
+
* - Screenshots: {basePath}/screenshots
|
|
115
|
+
* - Admin Chat: {basePath}/admin/chat
|
|
116
|
+
*/
|
|
117
|
+
basePath?: string
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Database connection configuration.
|
|
121
|
+
* Required when icons, assets, or screenshots features are enabled.
|
|
122
|
+
* Can be omitted when these features are disabled and you manage your own database.
|
|
123
|
+
*/
|
|
124
|
+
database?: EhDatabaseConfig
|
|
125
|
+
|
|
126
|
+
/** Auth configuration (required) */
|
|
127
|
+
auth: EhAuthConfig
|
|
128
|
+
|
|
129
|
+
/** Company-specific backend implementation (required) */
|
|
130
|
+
backend: EhBackendProvider
|
|
131
|
+
|
|
132
|
+
/** AI admin chat configuration (optional) */
|
|
133
|
+
adminChat?: EhAdminChatConfig
|
|
134
|
+
|
|
135
|
+
/** Feature toggles (all enabled by default) */
|
|
136
|
+
features?: EhFeatureToggles
|
|
137
|
+
|
|
138
|
+
/** Lifecycle hooks */
|
|
139
|
+
hooks?: EhLifecycleHooks
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Result of middleware initialization.
|
|
144
|
+
*/
|
|
145
|
+
export interface EhMiddlewareResult {
|
|
146
|
+
/** Express router with all env-hopper routes */
|
|
147
|
+
router: Router
|
|
148
|
+
/** Better Auth instance (for extending auth functionality) */
|
|
149
|
+
auth: BetterAuth
|
|
150
|
+
/** tRPC router (for extending with custom procedures) */
|
|
151
|
+
trpcRouter: TRPCRouter
|
|
152
|
+
/** Connect to database (call during app startup) */
|
|
153
|
+
connect: () => Promise<void>
|
|
154
|
+
/** Disconnect from database (call during app shutdown) */
|
|
155
|
+
disconnect: () => Promise<void>
|
|
156
|
+
/** Add custom routes to the middleware router */
|
|
157
|
+
addRoutes: (callback: (router: Router) => void) => void
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Internal context passed to feature registration functions.
|
|
162
|
+
*/
|
|
163
|
+
export interface MiddlewareContext {
|
|
164
|
+
auth: BetterAuth
|
|
165
|
+
trpcRouter: TRPCRouter
|
|
166
|
+
createContext: () => Promise<{ companySpecificBackend: EhBackendCompanySpecificBackend }>
|
|
167
|
+
}
|