wexts 4.0.0 → 4.1.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 (41) hide show
  1. package/dist/chunk-342VRT25.mjs +504 -0
  2. package/dist/chunk-342VRT25.mjs.map +1 -0
  3. package/dist/chunk-7HNQWJWV.js +504 -0
  4. package/dist/chunk-7HNQWJWV.js.map +1 -0
  5. package/dist/chunk-7SSCNCTW.mjs +137 -0
  6. package/dist/chunk-7SSCNCTW.mjs.map +1 -0
  7. package/dist/chunk-7TLSPR65.mjs +95 -0
  8. package/dist/chunk-7TLSPR65.mjs.map +1 -0
  9. package/dist/chunk-AVMQJWYD.js +95 -0
  10. package/dist/chunk-AVMQJWYD.js.map +1 -0
  11. package/dist/chunk-O4II6N34.js +137 -0
  12. package/dist/chunk-O4II6N34.js.map +1 -0
  13. package/dist/cli/index.d.mts +13 -1
  14. package/dist/cli/index.d.ts +13 -1
  15. package/dist/cli/index.js +438 -54
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/cli/index.mjs +438 -54
  18. package/dist/cli/index.mjs.map +1 -1
  19. package/dist/client/index.js +3 -2
  20. package/dist/client/index.js.map +1 -1
  21. package/dist/client/index.mjs +2 -1
  22. package/dist/codegen/index.js +3 -2
  23. package/dist/codegen/index.js.map +1 -1
  24. package/dist/codegen/index.mjs +2 -1
  25. package/dist/index.d.mts +37 -1
  26. package/dist/index.d.ts +37 -1
  27. package/dist/index.js +22 -8
  28. package/dist/index.js.map +1 -1
  29. package/dist/index.mjs +18 -4
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/next/index.js +68 -5
  32. package/dist/next/index.js.map +1 -1
  33. package/dist/next/index.mjs +68 -5
  34. package/dist/next/index.mjs.map +1 -1
  35. package/dist/runtime/index.js +9 -1
  36. package/dist/runtime/index.js.map +1 -1
  37. package/dist/runtime/index.mjs +9 -1
  38. package/dist/runtime/index.mjs.map +1 -1
  39. package/package.json +1 -1
  40. package/templates/nestjs-api/package-lock.json +0 -5623
  41. package/templates/nextjs-web/package-lock.json +0 -3254
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/index.ts","../src/core/config.ts","../src/core/process-manager.ts","../src/core/errors.ts","../src/config/index.ts","../src/config/loader.ts","../src/insight/index.ts","../src/insight/server.ts","../src/create-fusion/index.ts","../src/create-fusion/scaffold.ts","../src/next/index.ts","../src/next/provider.tsx","../src/next/useAuth.ts"],"sourcesContent":["// Core exports placeholder\nexport * from './config';\nexport * from './logger';\nexport * from './filesystem';\nexport * from './process-manager';\nexport * from './errors';\n","import * as fs from 'fs';\nimport * as path from 'path';\n\nexport interface FusionConfig {\n [key: string]: any;\n}\n\nexport class ConfigLoader {\n private config: Map<string, any> = new Map();\n private configDir: string;\n\n constructor(configDir: string = process.cwd()) {\n this.configDir = configDir;\n }\n\n /**\n * Load configuration from file or environment\n */\n load(key: string, defaultValue?: any): any {\n if (this.config.has(key)) {\n return this.config.get(key);\n }\n\n // Try to load from environment variable\n const envKey = `FUSION_${key.toUpperCase()}`;\n if (process.env[envKey]) {\n const value = this.parseEnvValue(process.env[envKey]!);\n this.config.set(key, value);\n return value;\n }\n\n // Try to load from config file\n const configPath = path.join(this.configDir, 'fusion.config.json');\n if (fs.existsSync(configPath)) {\n try {\n const fileConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));\n if (fileConfig[key] !== undefined) {\n this.config.set(key, fileConfig[key]);\n return fileConfig[key];\n }\n } catch (error) {\n // Silently fail and use default\n }\n }\n\n return defaultValue;\n }\n\n /**\n * Set configuration value\n */\n set(key: string, value: any): void {\n this.config.set(key, value);\n }\n\n /**\n * Get all configuration\n */\n getAll(): FusionConfig {\n return Object.fromEntries(this.config);\n }\n\n /**\n * Parse environment value (handles JSON strings)\n */\n private parseEnvValue(value: string): any {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n }\n}\n\n// Export singleton instance\nexport const config = new ConfigLoader();\n\n// Helper function for backward compatibility\nexport function load(key: string, defaultValue?: any): any {\n return config.load(key, defaultValue);\n}\n","export class ProcessManager {\n /**\n * Setup process error handlers\n */\n static initialize(): void {\n process.on('uncaughtException', (error) => {\n console.error('[Fusion] Uncaught Exception:', error);\n process.exit(1);\n });\n\n process.on('unhandledRejection', (reason, promise) => {\n console.error('[Fusion] Unhandled Rejection at:', promise, 'reason:', reason);\n });\n\n process.on('SIGTERM', () => {\n console.log('[Fusion] SIGTERM received, shutting down gracefully');\n process.exit(0);\n });\n\n process.on('SIGINT', () => {\n console.log('[Fusion] SIGINT received, shutting down gracefully');\n process.exit(0);\n });\n }\n\n /**\n * Graceful shutdown helper\n */\n static async shutdown(cleanup: () => Promise<void>): Promise<void> {\n try {\n await cleanup();\n process.exit(0);\n } catch (error) {\n console.error('[Fusion] Error during shutdown:', error);\n process.exit(1);\n }\n }\n}\n","export class FusionError extends Error {\n constructor(message: string, public code?: string) {\n super(message);\n this.name = 'FusionError';\n }\n}\n\nexport class ConfigError extends FusionError {\n constructor(message: string) {\n super(message, 'CONFIG_ERROR');\n this.name = 'ConfigError';\n }\n}\n\nexport class ValidationError extends FusionError {\n constructor(message: string) {\n super(message, 'VALIDATION_ERROR');\n this.name = 'ValidationError';\n }\n}\n\nexport class APIError extends FusionError {\n constructor(message: string, public statusCode?: number) {\n super(message, 'API_ERROR');\n this.name = 'APIError';\n }\n}\n","// Config exports placeholder\nexport * from './loader';\n","export const loader = {\n load: () => ({}),\n};\n","// Insight exports placeholder\nexport * from './server';\n","export const server = {\n start: () => console.log('Insight server started'),\n};\n","export * from './scaffold';\n","import { mkdir, writeFile } from 'fs/promises';\nimport { join } from 'path';\nimport { logger } from '../core/logger';\n\nexport interface ScaffoldOptions {\n projectName: string;\n template: 'monorepo' | 'api' | 'web';\n packageManager?: 'npm' | 'pnpm' | 'yarn';\n}\n\n/**\n * @deprecated Use the `wexts create` CLI and `examples/hello-rpc` canonical scaffold instead.\n */\nexport class ProjectScaffolder {\n async scaffold(options: ScaffoldOptions): Promise<void> {\n const { projectName, template } = options;\n const projectPath = join(process.cwd(), projectName);\n\n logger.info(`Creating project at: ${projectPath}`);\n\n // Create base directory\n await mkdir(projectPath, { recursive: true });\n\n if (template === 'monorepo') {\n await this.createMonorepo(projectPath, projectName);\n } else if (template === 'api') {\n await this.createNestJSApp(projectPath, projectName);\n } else if (template === 'web') {\n await this.createNextJSApp(projectPath, projectName);\n }\n\n logger.success(`Project ${projectName} created successfully!`);\n logger.info(`\\nNext steps:`);\n logger.info(` cd ${projectName}`);\n logger.info(` wexts dev`);\n }\n\n private async createMonorepo(projectPath: string, projectName: string): Promise<void> {\n // Create directory structure\n await mkdir(join(projectPath, 'apps', 'api'), { recursive: true });\n await mkdir(join(projectPath, 'apps', 'web'), { recursive: true });\n await mkdir(join(projectPath, 'packages', 'types'), { recursive: true });\n await mkdir(join(projectPath, 'packages', 'api-client'), { recursive: true });\n\n // Create root package.json\n const packageJson = {\n name: projectName,\n version: '0.0.1',\n private: true,\n workspaces: ['apps/*', 'packages/*'],\n scripts: {\n dev: 'wexts dev',\n build: 'wexts build',\n },\n devDependencies: {\n 'wexts': '^3.0.3',\n },\n };\n\n await writeFile(\n join(projectPath, 'package.json'),\n JSON.stringify(packageJson, null, 2)\n );\n\n // Create README\n const readme = `# ${projectName}\\n\\nCreated with the deprecated ProjectScaffolder compatibility API.\\n\\nUse examples/hello-rpc as the canonical Wexts reference.\\n\\n## Getting Started\\n\\n\\`\\`\\`bash\\nwexts dev\\n\\`\\`\\`\\n`;\n await writeFile(join(projectPath, 'README.md'), readme);\n }\n\n private async createNestJSApp(projectPath: string, projectName: string): Promise<void> {\n await writeFile(\n join(projectPath, 'README.md'),\n `# ${projectName}\\n\\nStandalone API scaffolding through ProjectScaffolder is deprecated. Use examples/hello-rpc or wexts create for the canonical path.\\n`\n );\n }\n\n private async createNextJSApp(projectPath: string, projectName: string): Promise<void> {\n await writeFile(\n join(projectPath, 'README.md'),\n `# ${projectName}\\n\\nStandalone web scaffolding through ProjectScaffolder is deprecated. Use examples/hello-rpc or wexts create for the canonical path.\\n`\n );\n }\n}\n","export * from './provider';\nexport * from './useAuth';\n","'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;ACAA,YAAYA,QAAQ;AACpB,YAAYC,UAAU;AAMf,IAAMC,eAAN,MAAMA;EAPb,OAOaA;;;EACDC,SAA2B,oBAAIC,IAAAA;EAC/BC;EAER,YAAYA,YAAoBC,QAAQC,IAAG,GAAI;AAC3C,SAAKF,YAAYA;EACrB;;;;EAKAG,KAAKC,KAAaC,cAAyB;AACvC,QAAI,KAAKP,OAAOQ,IAAIF,GAAAA,GAAM;AACtB,aAAO,KAAKN,OAAOS,IAAIH,GAAAA;IAC3B;AAGA,UAAMI,SAAS,UAAUJ,IAAIK,YAAW,CAAA;AACxC,QAAIR,QAAQS,IAAIF,MAAAA,GAAS;AACrB,YAAMG,QAAQ,KAAKC,cAAcX,QAAQS,IAAIF,MAAAA,CAAO;AACpD,WAAKV,OAAOe,IAAIT,KAAKO,KAAAA;AACrB,aAAOA;IACX;AAGA,UAAMG,aAAkBC,UAAK,KAAKf,WAAW,oBAAA;AAC7C,QAAOgB,cAAWF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AAW,cAAA;IAC5B,UAAA;AACIT,iBAAW,KAAA;IACf;EACJ,GATiB;AAWjB,QAAMU,QAAQ,8BAAOC,OAAeC,aAAAA;AAChC,UAAMC,WAAW,MAAMnB,OAAOoB,KAAwC,eAAe;MACjFH;MACAC;IACJ,CAAA;AACAT,iBAAaY,QAAQ,gBAAgBF,SAASX,KAAK;AACnDL,YAAQgB,SAASjB,IAAI;EACzB,GAPc;AASd,QAAMoB,SAAS,mCAAA;AACXb,iBAAaM,WAAW,cAAA;AACxBZ,YAAQ,IAAA;EACZ,GAHe;AAKf,SAAO;IACHD;IACAG;IACAW;IACAM;IACAC,iBAAiB,CAAC,CAACrB;EACvB;AACJ;AAhDgBH;","names":["fs","path","ConfigLoader","config","Map","configDir","process","cwd","load","key","defaultValue","has","get","envKey","toUpperCase","env","value","parseEnvValue","set","configPath","join","existsSync","fileConfig","JSON","parse","readFileSync","undefined","error","getAll","Object","fromEntries","ProcessManager","initialize","process","on","error","console","exit","reason","promise","log","shutdown","cleanup","FusionError","Error","message","code","name","ConfigError","ValidationError","APIError","statusCode","loader","load","server","start","console","log","mkdir","writeFile","join","ProjectScaffolder","scaffold","options","projectName","template","projectPath","join","process","cwd","logger","info","mkdir","recursive","createMonorepo","createNestJSApp","createNextJSApp","success","packageJson","name","version","private","workspaces","scripts","dev","build","devDependencies","writeFile","JSON","stringify","readme","React","createContext","useContext","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","useState","useEffect","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
1
+ {"version":3,"sources":["../src/core/index.ts","../src/core/config.ts","../src/core/process-manager.ts","../src/core/errors.ts","../src/config/index.ts","../src/config/loader.ts","../src/insight/index.ts","../src/insight/server.ts","../src/create-fusion/index.ts","../src/create-fusion/scaffold.ts","../src/next/index.ts","../src/next/provider.tsx","../src/next/useAuth.ts"],"sourcesContent":["// Core exports placeholder\nexport * from './config';\nexport * from './logger';\nexport * from './filesystem';\nexport * from './process-manager';\nexport * from './errors';\n","import * as fs from 'fs';\nimport * as path from 'path';\n\nexport interface FusionConfig {\n [key: string]: any;\n}\n\nexport class ConfigLoader {\n private config: Map<string, any> = new Map();\n private configDir: string;\n\n constructor(configDir: string = process.cwd()) {\n this.configDir = configDir;\n }\n\n /**\n * Load configuration from file or environment\n */\n load(key: string, defaultValue?: any): any {\n if (this.config.has(key)) {\n return this.config.get(key);\n }\n\n // Try to load from environment variable\n const envKey = `FUSION_${key.toUpperCase()}`;\n if (process.env[envKey]) {\n const value = this.parseEnvValue(process.env[envKey]!);\n this.config.set(key, value);\n return value;\n }\n\n // Try to load from config file\n const configPath = path.join(this.configDir, 'fusion.config.json');\n if (fs.existsSync(configPath)) {\n try {\n const fileConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));\n if (fileConfig[key] !== undefined) {\n this.config.set(key, fileConfig[key]);\n return fileConfig[key];\n }\n } catch (error) {\n // Silently fail and use default\n }\n }\n\n return defaultValue;\n }\n\n /**\n * Set configuration value\n */\n set(key: string, value: any): void {\n this.config.set(key, value);\n }\n\n /**\n * Get all configuration\n */\n getAll(): FusionConfig {\n return Object.fromEntries(this.config);\n }\n\n /**\n * Parse environment value (handles JSON strings)\n */\n private parseEnvValue(value: string): any {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n }\n}\n\n// Export singleton instance\nexport const config = new ConfigLoader();\n\n// Helper function for backward compatibility\nexport function load(key: string, defaultValue?: any): any {\n return config.load(key, defaultValue);\n}\n","export class ProcessManager {\n /**\n * Setup process error handlers\n */\n static initialize(): void {\n process.on('uncaughtException', (error) => {\n console.error('[Fusion] Uncaught Exception:', error);\n process.exit(1);\n });\n\n process.on('unhandledRejection', (reason, promise) => {\n console.error('[Fusion] Unhandled Rejection at:', promise, 'reason:', reason);\n });\n\n process.on('SIGTERM', () => {\n console.log('[Fusion] SIGTERM received, shutting down gracefully');\n process.exit(0);\n });\n\n process.on('SIGINT', () => {\n console.log('[Fusion] SIGINT received, shutting down gracefully');\n process.exit(0);\n });\n }\n\n /**\n * Graceful shutdown helper\n */\n static async shutdown(cleanup: () => Promise<void>): Promise<void> {\n try {\n await cleanup();\n process.exit(0);\n } catch (error) {\n console.error('[Fusion] Error during shutdown:', error);\n process.exit(1);\n }\n }\n}\n","export class FusionError extends Error {\n constructor(message: string, public code?: string) {\n super(message);\n this.name = 'FusionError';\n }\n}\n\nexport class ConfigError extends FusionError {\n constructor(message: string) {\n super(message, 'CONFIG_ERROR');\n this.name = 'ConfigError';\n }\n}\n\nexport class ValidationError extends FusionError {\n constructor(message: string) {\n super(message, 'VALIDATION_ERROR');\n this.name = 'ValidationError';\n }\n}\n\nexport class APIError extends FusionError {\n constructor(message: string, public statusCode?: number) {\n super(message, 'API_ERROR');\n this.name = 'APIError';\n }\n}\n","// Config exports placeholder\nexport * from './loader';\n","export const loader = {\n load: () => ({}),\n};\n","// Insight exports placeholder\nexport * from './server';\n","export const server = {\n start: () => console.log('Insight server started'),\n};\n","export * from './scaffold';\n","import { mkdir, writeFile } from 'fs/promises';\nimport { join } from 'path';\nimport { logger } from '../core/logger';\n\nexport interface ScaffoldOptions {\n projectName: string;\n template: 'monorepo' | 'api' | 'web';\n packageManager?: 'npm' | 'pnpm' | 'yarn';\n}\n\n/**\n * @deprecated Use the `wexts create` CLI and `examples/hello-rpc` canonical scaffold instead.\n */\nexport class ProjectScaffolder {\n async scaffold(options: ScaffoldOptions): Promise<void> {\n const { projectName, template } = options;\n const projectPath = join(process.cwd(), projectName);\n\n logger.info(`Creating project at: ${projectPath}`);\n\n // Create base directory\n await mkdir(projectPath, { recursive: true });\n\n if (template === 'monorepo') {\n await this.createMonorepo(projectPath, projectName);\n } else if (template === 'api') {\n await this.createNestJSApp(projectPath, projectName);\n } else if (template === 'web') {\n await this.createNextJSApp(projectPath, projectName);\n }\n\n logger.success(`Project ${projectName} created successfully!`);\n logger.info(`\\nNext steps:`);\n logger.info(` cd ${projectName}`);\n logger.info(` wexts dev`);\n }\n\n private async createMonorepo(projectPath: string, projectName: string): Promise<void> {\n // Create directory structure\n await mkdir(join(projectPath, 'apps', 'api'), { recursive: true });\n await mkdir(join(projectPath, 'apps', 'web'), { recursive: true });\n await mkdir(join(projectPath, 'packages', 'types'), { recursive: true });\n await mkdir(join(projectPath, 'packages', 'api-client'), { recursive: true });\n\n // Create root package.json\n const packageJson = {\n name: projectName,\n version: '0.0.1',\n private: true,\n workspaces: ['apps/*', 'packages/*'],\n scripts: {\n dev: 'wexts dev',\n build: 'wexts build',\n },\n devDependencies: {\n 'wexts': '^4.0.0',\n },\n };\n\n await writeFile(\n join(projectPath, 'package.json'),\n JSON.stringify(packageJson, null, 2)\n );\n\n // Create README\n const readme = `# ${projectName}\\n\\nCreated with the deprecated ProjectScaffolder compatibility API.\\n\\nUse examples/hello-rpc as the canonical Wexts reference.\\n\\n## Getting Started\\n\\n\\`\\`\\`bash\\nwexts dev\\n\\`\\`\\`\\n`;\n await writeFile(join(projectPath, 'README.md'), readme);\n }\n\n private async createNestJSApp(projectPath: string, projectName: string): Promise<void> {\n await writeFile(\n join(projectPath, 'README.md'),\n `# ${projectName}\\n\\nStandalone API scaffolding through ProjectScaffolder is deprecated. Use examples/hello-rpc or wexts create for the canonical path.\\n`\n );\n }\n\n private async createNextJSApp(projectPath: string, projectName: string): Promise<void> {\n await writeFile(\n join(projectPath, 'README.md'),\n `# ${projectName}\\n\\nStandalone web scaffolding through ProjectScaffolder is deprecated. Use examples/hello-rpc or wexts create for the canonical path.\\n`\n );\n }\n}\n","export * from './provider';\nexport * from './useAuth';\n","'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;ACAA,YAAYA,QAAQ;AACpB,YAAYC,UAAU;AAMf,IAAMC,eAAN,MAAMA;EAPb,OAOaA;;;EACDC,SAA2B,oBAAIC,IAAAA;EAC/BC;EAER,YAAYA,YAAoBC,QAAQC,IAAG,GAAI;AAC3C,SAAKF,YAAYA;EACrB;;;;EAKAG,KAAKC,KAAaC,cAAyB;AACvC,QAAI,KAAKP,OAAOQ,IAAIF,GAAAA,GAAM;AACtB,aAAO,KAAKN,OAAOS,IAAIH,GAAAA;IAC3B;AAGA,UAAMI,SAAS,UAAUJ,IAAIK,YAAW,CAAA;AACxC,QAAIR,QAAQS,IAAIF,MAAAA,GAAS;AACrB,YAAMG,QAAQ,KAAKC,cAAcX,QAAQS,IAAIF,MAAAA,CAAO;AACpD,WAAKV,OAAOe,IAAIT,KAAKO,KAAAA;AACrB,aAAOA;IACX;AAGA,UAAMG,aAAkBC,UAAK,KAAKf,WAAW,oBAAA;AAC7C,QAAOgB,cAAWF,UAAAA,GAAa;AAC3B,UAAI;AACA,cAAMG,aAAaC,KAAKC,MAASC,gBAAaN,YAAY,OAAA,CAAA;AAC1D,YAAIG,WAAWb,GAAAA,MAASiB,QAAW;AAC/B,eAAKvB,OAAOe,IAAIT,KAAKa,WAAWb,GAAAA,CAAI;AACpC,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,QAAQ,gBAAgBF,SAASX,KAAK;AACnDL,YAAQgB,SAASjB,IAAI;EACzB,GAPc;AASd,QAAMoB,SAAS,mCAAA;AACXb,iBAAaM,WAAW,cAAA;AACxBZ,YAAQ,IAAA;EACZ,GAHe;AAKf,SAAO;IACHD;IACAG;IACAW;IACAM;IACAC,iBAAiB,CAAC,CAACrB;EACvB;AACJ;AAhDgBH;","names":["fs","path","ConfigLoader","config","Map","configDir","process","cwd","load","key","defaultValue","has","get","envKey","toUpperCase","env","value","parseEnvValue","set","configPath","join","existsSync","fileConfig","JSON","parse","readFileSync","undefined","error","getAll","Object","fromEntries","ProcessManager","initialize","process","on","error","console","exit","reason","promise","log","shutdown","cleanup","FusionError","Error","message","code","name","ConfigError","ValidationError","APIError","statusCode","loader","load","server","start","console","log","mkdir","writeFile","join","ProjectScaffolder","scaffold","options","projectName","template","projectPath","join","process","cwd","logger","info","mkdir","recursive","createMonorepo","createNestJSApp","createNextJSApp","success","packageJson","name","version","private","workspaces","scripts","dev","build","devDependencies","writeFile","JSON","stringify","readme","React","createContext","useContext","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","useState","useEffect","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
@@ -42,6 +42,40 @@ module.exports = __toCommonJS(next_exports);
42
42
  // src/next/provider.tsx
43
43
  var import_react = __toESM(require("react"));
44
44
 
45
+ // src/errors.ts
46
+ var WextsError = class extends Error {
47
+ static {
48
+ __name(this, "WextsError");
49
+ }
50
+ code;
51
+ suggestedFix;
52
+ docsSlug;
53
+ constructor(options) {
54
+ super(options.message, options.cause === void 0 ? void 0 : {
55
+ cause: options.cause
56
+ });
57
+ this.name = "WextsError";
58
+ this.code = options.code;
59
+ this.suggestedFix = options.suggestedFix;
60
+ this.docsSlug = options.docsSlug;
61
+ }
62
+ get docsUrl() {
63
+ return this.docsSlug ? `https://github.com/ziadmustafa1/wexts/blob/main/docs/${this.docsSlug}.md` : void 0;
64
+ }
65
+ };
66
+ var WextsRpcError = class extends WextsError {
67
+ static {
68
+ __name(this, "WextsRpcError");
69
+ }
70
+ constructor(options) {
71
+ super({
72
+ code: options.code ?? "WEXTS_RPC_ERROR",
73
+ ...options
74
+ });
75
+ this.name = "WextsRpcError";
76
+ }
77
+ };
78
+
45
79
  // src/client/fetcher.ts
46
80
  var FusionFetcher = class {
47
81
  static {
@@ -65,7 +99,12 @@ var FusionFetcher = class {
65
99
  body: body ? JSON.stringify(body) : void 0
66
100
  });
67
101
  if (!response.ok) {
68
- throw new Error(`Fusion API Error: ${response.status} ${response.statusText}`);
102
+ throw new WextsRpcError({
103
+ code: "WEXTS_API_REQUEST_FAILED",
104
+ message: `Fusion API Error: ${response.status} ${response.statusText}`,
105
+ suggestedFix: "Check the API route, server logs, and authentication headers.",
106
+ docsSlug: "troubleshooting"
107
+ });
69
108
  }
70
109
  if (response.status === 204) {
71
110
  return void 0;
@@ -87,6 +126,7 @@ var FusionFetcher = class {
87
126
  };
88
127
  var apiFetcher = new FusionFetcher();
89
128
  function createWextsRpcClient(manifest, options = {}) {
129
+ const hasManifest = Boolean(manifest);
90
130
  const services = new Set((manifest?.services ?? []).map((service) => service.name));
91
131
  const methodMap = /* @__PURE__ */ new Map();
92
132
  for (const service of manifest?.services ?? []) {
@@ -98,7 +138,12 @@ function createWextsRpcClient(manifest, options = {}) {
98
138
  if (methodName === "then") return void 0;
99
139
  const knownMethods = methodMap.get(serviceName);
100
140
  if (knownMethods && !knownMethods.has(methodName)) {
101
- throw new Error(`Wexts RPC method not found: ${serviceName}.${methodName}`);
141
+ throw new WextsRpcError({
142
+ code: "WEXTS_RPC_METHOD_NOT_FOUND",
143
+ message: `Wexts RPC method not found: ${serviceName}.${methodName}`,
144
+ suggestedFix: "Run `wexts generate` and verify the method is decorated with @RpcMethod().",
145
+ docsSlug: "rpc"
146
+ });
102
147
  }
103
148
  return (...args) => invokeRpc(serviceName, methodName, args, options);
104
149
  }
@@ -107,8 +152,21 @@ function createWextsRpcClient(manifest, options = {}) {
107
152
  get(_target, serviceName) {
108
153
  if (typeof serviceName !== "string") return void 0;
109
154
  if (serviceName === "then") return void 0;
110
- if (services.size > 0 && !services.has(serviceName)) {
111
- throw new Error(`Wexts RPC service not found: ${serviceName}`);
155
+ if (!hasManifest) {
156
+ throw new WextsRpcError({
157
+ code: "WEXTS_RPC_MANIFEST_MISSING",
158
+ message: "Wexts RPC manifest is missing.",
159
+ suggestedFix: "Run `wexts generate` and import the generated client/provider instead of creating an empty client.",
160
+ docsSlug: "codegen"
161
+ });
162
+ }
163
+ if (!services.has(serviceName)) {
164
+ throw new WextsRpcError({
165
+ code: "WEXTS_RPC_SERVICE_NOT_FOUND",
166
+ message: `Wexts RPC service not found: ${serviceName}`,
167
+ suggestedFix: "Run `wexts generate` and verify the service is decorated with @RpcService().",
168
+ docsSlug: "rpc"
169
+ });
112
170
  }
113
171
  return createServiceProxy(serviceName);
114
172
  }
@@ -130,7 +188,12 @@ async function invokeRpc(serviceName, methodName, args, options) {
130
188
  })
131
189
  });
132
190
  if (!response.ok) {
133
- throw new Error(`Wexts RPC Error: ${response.status} ${response.statusText}`);
191
+ throw new WextsRpcError({
192
+ code: "WEXTS_RPC_REQUEST_FAILED",
193
+ message: `Wexts RPC Error: ${response.status} ${response.statusText}`,
194
+ suggestedFix: "Check the RPC route, service policy, and server logs.",
195
+ docsSlug: "troubleshooting"
196
+ });
134
197
  }
135
198
  const payload = await response.json();
136
199
  return payload.data;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/next/index.ts","../../src/next/provider.tsx","../../src/client/fetcher.ts","../../src/next/useAuth.ts"],"sourcesContent":["export * from './provider';\nexport * from './useAuth';\n","'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","import type { RpcManifest, RpcInvocationResponse } from '../rpc/types';\n\nexport class FusionFetcher {\n private baseUrl: string;\n\n constructor(baseUrl: string = '/api') {\n this.baseUrl = baseUrl;\n }\n\n private async request<T>(method: string, path: string, body?: any): Promise<T> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Automatically attach Fusion Token if present\n if (typeof window !== 'undefined') {\n const token = localStorage.getItem('fusion_token');\n if (token) headers['Authorization'] = `Bearer ${token}`;\n }\n\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n throw new Error(`Fusion API Error: ${response.status} ${response.statusText}`);\n }\n\n if (response.status === 204) {\n return undefined as T;\n }\n\n return response.json();\n }\n\n get<T>(path: string) { return this.request<T>('GET', path); }\n post<T>(path: string, body: any) { return this.request<T>('POST', path, body); }\n put<T>(path: string, body: any) { return this.request<T>('PUT', path, body); }\n delete<T>(path: string) { return this.request<T>('DELETE', path); }\n}\n\nexport const apiFetcher = new FusionFetcher();\n\nexport interface WextsRpcClientOptions {\n baseUrl?: string;\n fetch?: typeof fetch;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n}\n\nexport type WextsRpcClient = Record<string, Record<string, (...args: unknown[]) => Promise<unknown>>>;\n\nexport function createWextsRpcClient(\n manifest: Pick<RpcManifest, 'services'> | undefined,\n options: WextsRpcClientOptions = {}\n): WextsRpcClient {\n const services = new Set((manifest?.services ?? []).map((service) => service.name));\n const methodMap = new Map<string, Set<string>>();\n\n for (const service of manifest?.services ?? []) {\n methodMap.set(service.name, new Set(service.methods.map((method) => method.name)));\n }\n\n const createServiceProxy = (serviceName: string) => new Proxy({}, {\n get(_target, methodName) {\n if (typeof methodName !== 'string') return undefined;\n if (methodName === 'then') return undefined;\n\n const knownMethods = methodMap.get(serviceName);\n if (knownMethods && !knownMethods.has(methodName)) {\n throw new Error(`Wexts RPC method not found: ${serviceName}.${methodName}`);\n }\n\n return (...args: unknown[]) => invokeRpc(serviceName, methodName, args, options);\n },\n }) as Record<string, (...args: unknown[]) => Promise<unknown>>;\n\n return new Proxy({}, {\n get(_target, serviceName) {\n if (typeof serviceName !== 'string') return undefined;\n if (serviceName === 'then') return undefined;\n if (services.size > 0 && !services.has(serviceName)) {\n throw new Error(`Wexts RPC service not found: ${serviceName}`);\n }\n\n return createServiceProxy(serviceName);\n },\n }) as WextsRpcClient;\n}\n\nasync function invokeRpc(\n serviceName: string,\n methodName: string,\n args: unknown[],\n options: WextsRpcClientOptions\n): Promise<unknown> {\n const fetchImpl = options.fetch ?? fetch;\n const baseUrl = options.baseUrl ?? '/rpc';\n const headers = {\n 'Content-Type': 'application/json',\n ...(await options.getHeaders?.() ?? {}),\n };\n const response = await fetchImpl(`${baseUrl}/${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`, {\n method: 'POST',\n headers,\n body: JSON.stringify({ args }),\n });\n\n if (!response.ok) {\n throw new Error(`Wexts RPC Error: ${response.status} ${response.statusText}`);\n }\n\n const payload = await response.json() as RpcInvocationResponse;\n return payload.data;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACEA,mBAA4D;;;ACArD,IAAMA,gBAAN,MAAMA;EAA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AG,CAAA,CAAE;AAEL,QAAMK,WAAW,mCAAA;AACb,QAAI;AACA,YAAMC,WAAW,MAAMZ,OAAOa,IAAc,UAAA;AAC5CV,cAAQS,QAAAA;IACZ,SAASE,OAAO;AACZL,mBAAaM,WAAW,cAAA;IAC5B,UAAA;AACIT,iBAAW,KAAA;IACf;EACJ,GATiB;AAWjB,QAAMU,QAAQ,8BAAOC,OAAeC,aAAAA;AAChC,UAAMC,WAAW,MAAMnB,OAAOoB,KAAwC,eAAe;MACjFH;MACAC;IACJ,CAAA;AACAT,iBAAaY,QAAQ,gBAAgBF,SAASX,KAAK;AACnDL,YAAQgB,SAASjB,IAAI;EACzB,GAPc;AASd,QAAMoB,SAAS,mCAAA;AACXb,iBAAaM,WAAW,cAAA;AACxBZ,YAAQ,IAAA;EACZ,GAHe;AAKf,SAAO;IACHD;IACAG;IACAW;IACAM;IACAC,iBAAiB,CAAC,CAACrB;EACvB;AACJ;AAhDgBH;","names":["FusionFetcher","baseUrl","request","method","path","body","headers","window","token","localStorage","getItem","response","fetch","JSON","stringify","undefined","ok","Error","status","statusText","json","get","post","put","delete","apiFetcher","createWextsRpcClient","manifest","options","services","Set","map","service","name","methodMap","Map","set","methods","createServiceProxy","serviceName","Proxy","_target","methodName","knownMethods","has","args","invokeRpc","size","fetchImpl","getHeaders","encodeURIComponent","payload","data","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","import_react","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
1
+ {"version":3,"sources":["../../src/next/index.ts","../../src/next/provider.tsx","../../src/errors.ts","../../src/client/fetcher.ts","../../src/next/useAuth.ts"],"sourcesContent":["export * from './provider';\nexport * from './useAuth';\n","'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","export interface WextsErrorOptions {\n code: string;\n message: string;\n cause?: unknown;\n suggestedFix?: string;\n docsSlug?: string;\n}\n\nexport class WextsError extends Error {\n readonly code: string;\n readonly suggestedFix?: string;\n readonly docsSlug?: string;\n\n constructor(options: WextsErrorOptions) {\n super(options.message, options.cause === undefined ? undefined : { cause: options.cause });\n this.name = 'WextsError';\n this.code = options.code;\n this.suggestedFix = options.suggestedFix;\n this.docsSlug = options.docsSlug;\n }\n\n get docsUrl(): string | undefined {\n return this.docsSlug ? `https://github.com/ziadmustafa1/wexts/blob/main/docs/${this.docsSlug}.md` : undefined;\n }\n}\n\nexport class WextsRpcError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_RPC_ERROR', ...options });\n this.name = 'WextsRpcError';\n }\n}\n\nexport class WextsCodegenError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_CODEGEN_ERROR', ...options });\n this.name = 'WextsCodegenError';\n }\n}\n\nexport class WextsRuntimeError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_RUNTIME_ERROR', ...options });\n this.name = 'WextsRuntimeError';\n }\n}\n\nexport class WextsSecurityError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_SECURITY_ERROR', ...options });\n this.name = 'WextsSecurityError';\n }\n}\n\nexport function formatWextsError(error: unknown): string {\n if (!(error instanceof WextsError)) {\n return error instanceof Error ? error.message : String(error);\n }\n\n const lines = [`${error.code}: ${error.message}`];\n if (error.suggestedFix) lines.push(`Suggested fix: ${error.suggestedFix}`);\n if (error.docsUrl) lines.push(`Docs: ${error.docsUrl}`);\n return lines.join('\\n');\n}\n","import type { RpcManifest, RpcInvocationResponse } from '../rpc/types';\nimport { WextsRpcError } from '../errors';\n\nexport class FusionFetcher {\n private baseUrl: string;\n\n constructor(baseUrl: string = '/api') {\n this.baseUrl = baseUrl;\n }\n\n private async request<T>(method: string, path: string, body?: any): Promise<T> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Automatically attach Fusion Token if present\n if (typeof window !== 'undefined') {\n const token = localStorage.getItem('fusion_token');\n if (token) headers['Authorization'] = `Bearer ${token}`;\n }\n\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n throw new WextsRpcError({\n code: 'WEXTS_API_REQUEST_FAILED',\n message: `Fusion API Error: ${response.status} ${response.statusText}`,\n suggestedFix: 'Check the API route, server logs, and authentication headers.',\n docsSlug: 'troubleshooting',\n });\n }\n\n if (response.status === 204) {\n return undefined as T;\n }\n\n return response.json();\n }\n\n get<T>(path: string) { return this.request<T>('GET', path); }\n post<T>(path: string, body: any) { return this.request<T>('POST', path, body); }\n put<T>(path: string, body: any) { return this.request<T>('PUT', path, body); }\n delete<T>(path: string) { return this.request<T>('DELETE', path); }\n}\n\nexport const apiFetcher = new FusionFetcher();\n\nexport interface WextsRpcClientOptions {\n baseUrl?: string;\n fetch?: typeof fetch;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n}\n\nexport type WextsRpcClient = Record<string, Record<string, (...args: unknown[]) => Promise<unknown>>>;\n\nexport function createWextsRpcClient(\n manifest: Pick<RpcManifest, 'services'> | undefined,\n options: WextsRpcClientOptions = {}\n): WextsRpcClient {\n const hasManifest = Boolean(manifest);\n const services = new Set((manifest?.services ?? []).map((service) => service.name));\n const methodMap = new Map<string, Set<string>>();\n\n for (const service of manifest?.services ?? []) {\n methodMap.set(service.name, new Set(service.methods.map((method) => method.name)));\n }\n\n const createServiceProxy = (serviceName: string) => new Proxy({}, {\n get(_target, methodName) {\n if (typeof methodName !== 'string') return undefined;\n if (methodName === 'then') return undefined;\n\n const knownMethods = methodMap.get(serviceName);\n if (knownMethods && !knownMethods.has(methodName)) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_METHOD_NOT_FOUND',\n message: `Wexts RPC method not found: ${serviceName}.${methodName}`,\n suggestedFix: 'Run `wexts generate` and verify the method is decorated with @RpcMethod().',\n docsSlug: 'rpc',\n });\n }\n\n return (...args: unknown[]) => invokeRpc(serviceName, methodName, args, options);\n },\n }) as Record<string, (...args: unknown[]) => Promise<unknown>>;\n\n return new Proxy({}, {\n get(_target, serviceName) {\n if (typeof serviceName !== 'string') return undefined;\n if (serviceName === 'then') return undefined;\n if (!hasManifest) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_MANIFEST_MISSING',\n message: 'Wexts RPC manifest is missing.',\n suggestedFix: 'Run `wexts generate` and import the generated client/provider instead of creating an empty client.',\n docsSlug: 'codegen',\n });\n }\n if (!services.has(serviceName)) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_SERVICE_NOT_FOUND',\n message: `Wexts RPC service not found: ${serviceName}`,\n suggestedFix: 'Run `wexts generate` and verify the service is decorated with @RpcService().',\n docsSlug: 'rpc',\n });\n }\n\n return createServiceProxy(serviceName);\n },\n }) as WextsRpcClient;\n}\n\nasync function invokeRpc(\n serviceName: string,\n methodName: string,\n args: unknown[],\n options: WextsRpcClientOptions\n): Promise<unknown> {\n const fetchImpl = options.fetch ?? fetch;\n const baseUrl = options.baseUrl ?? '/rpc';\n const headers = {\n 'Content-Type': 'application/json',\n ...(await options.getHeaders?.() ?? {}),\n };\n const response = await fetchImpl(`${baseUrl}/${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`, {\n method: 'POST',\n headers,\n body: JSON.stringify({ args }),\n });\n\n if (!response.ok) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_REQUEST_FAILED',\n message: `Wexts RPC Error: ${response.status} ${response.statusText}`,\n suggestedFix: 'Check the RPC route, service policy, and server logs.',\n docsSlug: 'troubleshooting',\n });\n }\n\n const payload = await response.json() as RpcInvocationResponse;\n return payload.data;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACEA,mBAA4D;;;ACMrD,IAAMA,aAAN,cAAyBC,MAAAA;EAAhC,OAAgCA;;;EACnBC;EACAC;EACAC;EAET,YAAYC,SAA4B;AACpC,UAAMA,QAAQC,SAASD,QAAQE,UAAUC,SAAYA,SAAY;MAAED,OAAOF,QAAQE;IAAM,CAAA;AACxF,SAAKE,OAAO;AACZ,SAAKP,OAAOG,QAAQH;AACpB,SAAKC,eAAeE,QAAQF;AAC5B,SAAKC,WAAWC,QAAQD;EAC5B;EAEA,IAAIM,UAA8B;AAC9B,WAAO,KAAKN,WAAW,wDAAwD,KAAKA,QAAQ,QAAQI;EACxG;AACJ;AAEO,IAAMG,gBAAN,cAA4BX,WAAAA;EAlBnC,OAkBmCA;;;EAC/B,YAAYK,SAA8D;AACtE,UAAM;MAAEH,MAAMG,QAAQH,QAAQ;MAAmB,GAAGG;IAAQ,CAAA;AAC5D,SAAKI,OAAO;EAChB;AACJ;;;AC5BO,IAAMG,gBAAN,MAAMA;EAFb,OAEaA;;;EACDC;EAER,YAAYA,UAAkB,QAAQ;AAClC,SAAKA,UAAUA;EACnB;EAEA,MAAcC,QAAWC,QAAgBC,MAA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C,UAAAA;AACnB,QAAM,CAACC,MAAMC,OAAAA,QAAWC,wBAA0B,IAAA;AAClD,QAAM,CAACC,SAASC,UAAAA,QAAcF,wBAAS,IAAA;AAEvCG,+BAAU,MAAA;AAEN,UAAMC,QAAQC,aAAaC,QAAQ,cAAA;AACnC,QAAIF,OAAO;AAEPG,eAAAA;IACJ,OAAO;AACHL,iBAAW,KAAA;IACf;EACJ,GAAG,CAAA,CAAE;AAEL,QAAMK,WAAW,mCAAA;AACb,QAAI;AACA,YAAMC,WAAW,MAAMZ,OAAOa,IAAc,UAAA;AAC5CV,cAAQS,QAAAA;IACZ,SAASE,OAAO;AACZL,mBAAaM,WAAW,cAAA;IAC5B,UAAA;AACIT,iBAAW,KAAA;IACf;EACJ,GATiB;AAWjB,QAAMU,QAAQ,8BAAOC,OAAeC,aAAAA;AAChC,UAAMC,WAAW,MAAMnB,OAAOoB,KAAwC,eAAe;MACjFH;MACAC;IACJ,CAAA;AACAT,iBAAaY,QAAQ,gBAAgBF,SAASX,KAAK;AACnDL,YAAQgB,SAASjB,IAAI;EACzB,GAPc;AASd,QAAMoB,SAAS,mCAAA;AACXb,iBAAaM,WAAW,cAAA;AACxBZ,YAAQ,IAAA;EACZ,GAHe;AAKf,SAAO;IACHD;IACAG;IACAW;IACAM;IACAC,iBAAiB,CAAC,CAACrB;EACvB;AACJ;AAhDgBH;","names":["WextsError","Error","code","suggestedFix","docsSlug","options","message","cause","undefined","name","docsUrl","WextsRpcError","FusionFetcher","baseUrl","request","method","path","body","headers","window","token","localStorage","getItem","response","fetch","JSON","stringify","undefined","ok","WextsRpcError","code","message","status","statusText","suggestedFix","docsSlug","json","get","post","put","delete","apiFetcher","createWextsRpcClient","manifest","options","hasManifest","Boolean","services","Set","map","service","name","methodMap","Map","set","methods","createServiceProxy","serviceName","Proxy","_target","methodName","knownMethods","has","args","invokeRpc","fetchImpl","getHeaders","encodeURIComponent","payload","data","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","import_react","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
@@ -5,6 +5,40 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
5
5
  // src/next/provider.tsx
6
6
  import React, { createContext, useContext } from "react";
7
7
 
8
+ // src/errors.ts
9
+ var WextsError = class extends Error {
10
+ static {
11
+ __name(this, "WextsError");
12
+ }
13
+ code;
14
+ suggestedFix;
15
+ docsSlug;
16
+ constructor(options) {
17
+ super(options.message, options.cause === void 0 ? void 0 : {
18
+ cause: options.cause
19
+ });
20
+ this.name = "WextsError";
21
+ this.code = options.code;
22
+ this.suggestedFix = options.suggestedFix;
23
+ this.docsSlug = options.docsSlug;
24
+ }
25
+ get docsUrl() {
26
+ return this.docsSlug ? `https://github.com/ziadmustafa1/wexts/blob/main/docs/${this.docsSlug}.md` : void 0;
27
+ }
28
+ };
29
+ var WextsRpcError = class extends WextsError {
30
+ static {
31
+ __name(this, "WextsRpcError");
32
+ }
33
+ constructor(options) {
34
+ super({
35
+ code: options.code ?? "WEXTS_RPC_ERROR",
36
+ ...options
37
+ });
38
+ this.name = "WextsRpcError";
39
+ }
40
+ };
41
+
8
42
  // src/client/fetcher.ts
9
43
  var FusionFetcher = class {
10
44
  static {
@@ -28,7 +62,12 @@ var FusionFetcher = class {
28
62
  body: body ? JSON.stringify(body) : void 0
29
63
  });
30
64
  if (!response.ok) {
31
- throw new Error(`Fusion API Error: ${response.status} ${response.statusText}`);
65
+ throw new WextsRpcError({
66
+ code: "WEXTS_API_REQUEST_FAILED",
67
+ message: `Fusion API Error: ${response.status} ${response.statusText}`,
68
+ suggestedFix: "Check the API route, server logs, and authentication headers.",
69
+ docsSlug: "troubleshooting"
70
+ });
32
71
  }
33
72
  if (response.status === 204) {
34
73
  return void 0;
@@ -50,6 +89,7 @@ var FusionFetcher = class {
50
89
  };
51
90
  var apiFetcher = new FusionFetcher();
52
91
  function createWextsRpcClient(manifest, options = {}) {
92
+ const hasManifest = Boolean(manifest);
53
93
  const services = new Set((manifest?.services ?? []).map((service) => service.name));
54
94
  const methodMap = /* @__PURE__ */ new Map();
55
95
  for (const service of manifest?.services ?? []) {
@@ -61,7 +101,12 @@ function createWextsRpcClient(manifest, options = {}) {
61
101
  if (methodName === "then") return void 0;
62
102
  const knownMethods = methodMap.get(serviceName);
63
103
  if (knownMethods && !knownMethods.has(methodName)) {
64
- throw new Error(`Wexts RPC method not found: ${serviceName}.${methodName}`);
104
+ throw new WextsRpcError({
105
+ code: "WEXTS_RPC_METHOD_NOT_FOUND",
106
+ message: `Wexts RPC method not found: ${serviceName}.${methodName}`,
107
+ suggestedFix: "Run `wexts generate` and verify the method is decorated with @RpcMethod().",
108
+ docsSlug: "rpc"
109
+ });
65
110
  }
66
111
  return (...args) => invokeRpc(serviceName, methodName, args, options);
67
112
  }
@@ -70,8 +115,21 @@ function createWextsRpcClient(manifest, options = {}) {
70
115
  get(_target, serviceName) {
71
116
  if (typeof serviceName !== "string") return void 0;
72
117
  if (serviceName === "then") return void 0;
73
- if (services.size > 0 && !services.has(serviceName)) {
74
- throw new Error(`Wexts RPC service not found: ${serviceName}`);
118
+ if (!hasManifest) {
119
+ throw new WextsRpcError({
120
+ code: "WEXTS_RPC_MANIFEST_MISSING",
121
+ message: "Wexts RPC manifest is missing.",
122
+ suggestedFix: "Run `wexts generate` and import the generated client/provider instead of creating an empty client.",
123
+ docsSlug: "codegen"
124
+ });
125
+ }
126
+ if (!services.has(serviceName)) {
127
+ throw new WextsRpcError({
128
+ code: "WEXTS_RPC_SERVICE_NOT_FOUND",
129
+ message: `Wexts RPC service not found: ${serviceName}`,
130
+ suggestedFix: "Run `wexts generate` and verify the service is decorated with @RpcService().",
131
+ docsSlug: "rpc"
132
+ });
75
133
  }
76
134
  return createServiceProxy(serviceName);
77
135
  }
@@ -93,7 +151,12 @@ async function invokeRpc(serviceName, methodName, args, options) {
93
151
  })
94
152
  });
95
153
  if (!response.ok) {
96
- throw new Error(`Wexts RPC Error: ${response.status} ${response.statusText}`);
154
+ throw new WextsRpcError({
155
+ code: "WEXTS_RPC_REQUEST_FAILED",
156
+ message: `Wexts RPC Error: ${response.status} ${response.statusText}`,
157
+ suggestedFix: "Check the RPC route, service policy, and server logs.",
158
+ docsSlug: "troubleshooting"
159
+ });
97
160
  }
98
161
  const payload = await response.json();
99
162
  return payload.data;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/next/provider.tsx","../../src/client/fetcher.ts","../../src/next/useAuth.ts"],"sourcesContent":["'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","import type { RpcManifest, RpcInvocationResponse } from '../rpc/types';\n\nexport class FusionFetcher {\n private baseUrl: string;\n\n constructor(baseUrl: string = '/api') {\n this.baseUrl = baseUrl;\n }\n\n private async request<T>(method: string, path: string, body?: any): Promise<T> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Automatically attach Fusion Token if present\n if (typeof window !== 'undefined') {\n const token = localStorage.getItem('fusion_token');\n if (token) headers['Authorization'] = `Bearer ${token}`;\n }\n\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n throw new Error(`Fusion API Error: ${response.status} ${response.statusText}`);\n }\n\n if (response.status === 204) {\n return undefined as T;\n }\n\n return response.json();\n }\n\n get<T>(path: string) { return this.request<T>('GET', path); }\n post<T>(path: string, body: any) { return this.request<T>('POST', path, body); }\n put<T>(path: string, body: any) { return this.request<T>('PUT', path, body); }\n delete<T>(path: string) { return this.request<T>('DELETE', path); }\n}\n\nexport const apiFetcher = new FusionFetcher();\n\nexport interface WextsRpcClientOptions {\n baseUrl?: string;\n fetch?: typeof fetch;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n}\n\nexport type WextsRpcClient = Record<string, Record<string, (...args: unknown[]) => Promise<unknown>>>;\n\nexport function createWextsRpcClient(\n manifest: Pick<RpcManifest, 'services'> | undefined,\n options: WextsRpcClientOptions = {}\n): WextsRpcClient {\n const services = new Set((manifest?.services ?? []).map((service) => service.name));\n const methodMap = new Map<string, Set<string>>();\n\n for (const service of manifest?.services ?? []) {\n methodMap.set(service.name, new Set(service.methods.map((method) => method.name)));\n }\n\n const createServiceProxy = (serviceName: string) => new Proxy({}, {\n get(_target, methodName) {\n if (typeof methodName !== 'string') return undefined;\n if (methodName === 'then') return undefined;\n\n const knownMethods = methodMap.get(serviceName);\n if (knownMethods && !knownMethods.has(methodName)) {\n throw new Error(`Wexts RPC method not found: ${serviceName}.${methodName}`);\n }\n\n return (...args: unknown[]) => invokeRpc(serviceName, methodName, args, options);\n },\n }) as Record<string, (...args: unknown[]) => Promise<unknown>>;\n\n return new Proxy({}, {\n get(_target, serviceName) {\n if (typeof serviceName !== 'string') return undefined;\n if (serviceName === 'then') return undefined;\n if (services.size > 0 && !services.has(serviceName)) {\n throw new Error(`Wexts RPC service not found: ${serviceName}`);\n }\n\n return createServiceProxy(serviceName);\n },\n }) as WextsRpcClient;\n}\n\nasync function invokeRpc(\n serviceName: string,\n methodName: string,\n args: unknown[],\n options: WextsRpcClientOptions\n): Promise<unknown> {\n const fetchImpl = options.fetch ?? fetch;\n const baseUrl = options.baseUrl ?? '/rpc';\n const headers = {\n 'Content-Type': 'application/json',\n ...(await options.getHeaders?.() ?? {}),\n };\n const response = await fetchImpl(`${baseUrl}/${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`, {\n method: 'POST',\n headers,\n body: JSON.stringify({ args }),\n });\n\n if (!response.ok) {\n throw new Error(`Wexts RPC Error: ${response.status} ${response.statusText}`);\n }\n\n const payload = await response.json() as RpcInvocationResponse;\n return payload.data;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;AAEA,OAAOA,SAASC,eAAeC,kBAA6B;;;ACArD,IAAMC,gBAAN,MAAMA;EAAb,OAAaA;;;EACDC;EAER,YAAYA,UAAkB,QAAQ;AAClC,SAAKA,UAAUA;EACnB;EAEA,MAAcC,QAAWC,QAAgBC,MAAcC,MAAwB;AAC3E,UAAMC,UAAkC;MACpC,gBAAgB;IACpB;AAGA,QAAI,OAAOC,WAAW,aAAa;AAC/B,YAAMC,QAAQC,aAAaC,QAAQ,cAAA;AACnC,UAAIF,MAAOF,SAAQ,eAAA,IAAmB,UAAUE,KAAAA;IACpD;AAEJ,UAAMG,WAAW,MAAMC,MAAM,GAAG,KAAKX,OAAO,GAAGG,IAAAA,IAAQ;MAC/CD;MACAG;MACAD,MAAMA,OAAOQ,KAAKC,UAAUT,IAAAA,IAAQU;IACxC,CAAA;AAEA,QAAI,CAACJ,SAASK,IAAI;AACd,YAAM,IAAIC,MAAM,qBAAqBN,SAASO,MAAM,IAAIP,SAASQ,UAAU,EAAE;IACjF;AAEA,QAAIR,SAASO,WAAW,KAAK;AACzB,aAAOH;IACX;AAEA,WAAOJ,SAASS,KAAI;EACxB;EAEAC,IAAOjB,MAAc;AAAE,WAAO,KAAKF,QAAW,OAAOE,IAAAA;EAAO;EAC5DkB,KAAQlB,MAAcC,MAAW;AAAE,WAAO,KAAKH,QAAW,QAAQE,MAAMC,I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,SAAOF;AACX;AANgBD;AAQT,SAASI,WAAAA;AACZ,QAAMH,UAAUD,UAAAA;AAChB,SAAOC,QAAQN;AACnB;AAHgBS;;;AExDhB,SAASC,UAAUC,iBAAiB;AAwB7B,SAASC,UAAAA;AACZ,QAAM,EAAEC,OAAM,IAAKC,UAAAA;AACnB,QAAM,CAACC,MAAMC,OAAAA,IAAWC,SAA0B,IAAA;AAClD,QAAM,CAACC,SAASC,UAAAA,IAAcF,SAAS,IAAA;AAEvCG,YAAU,MAAA;AAEN,UAAMC,QAAQC,aAAaC,QAAQ,cAAA;AACnC,QAAIF,OAAO;AAEPG,eAAAA;IACJ,OAAO;AACHL,iBAAW,KAAA;IACf;EACJ,GAAG,CAAA,CAAE;AAEL,QAAMK,WAAW,mCAAA;AACb,QAAI;AACA,YAAMC,WAAW,MAAMZ,OAAOa,IAAc,UAAA;AAC5CV,cAAQS,QAAAA;IACZ,SAASE,OAAO;AACZL,mBAAaM,WAAW,cAAA;IAC5B,UAAA;AACIT,iBAAW,KAAA;IACf;EACJ,GATiB;AAWjB,QAAMU,QAAQ,8BAAOC,OAAeC,aAAAA;AAChC,UAAMC,WAAW,MAAMnB,OAAOoB,KAAwC,eAAe;MACjFH;MACAC;IACJ,CAAA;AACAT,iBAAaY,QAAQ,gBAAgBF,SAASX,KAAK;AACnDL,YAAQgB,SAASjB,IAAI;EACzB,GAPc;AASd,QAAMoB,SAAS,mCAAA;AACXb,iBAAaM,WAAW,cAAA;AACxBZ,YAAQ,IAAA;EACZ,GAHe;AAKf,SAAO;IACHD;IACAG;IACAW;IACAM;IACAC,iBAAiB,CAAC,CAACrB;EACvB;AACJ;AAhDgBH;","names":["React","createContext","useContext","FusionFetcher","baseUrl","request","method","path","body","headers","window","token","localStorage","getItem","response","fetch","JSON","stringify","undefined","ok","Error","status","statusText","json","get","post","put","delete","apiFetcher","createWextsRpcClient","manifest","options","services","Set","map","service","name","methodMap","Map","set","methods","createServiceProxy","serviceName","Proxy","_target","methodName","knownMethods","has","args","invokeRpc","size","fetchImpl","getHeaders","encodeURIComponent","payload","data","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","useState","useEffect","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
1
+ {"version":3,"sources":["../../src/next/provider.tsx","../../src/errors.ts","../../src/client/fetcher.ts","../../src/next/useAuth.ts"],"sourcesContent":["'use client';\n\nimport React, { createContext, useContext, ReactNode } from 'react';\nimport { createWextsRpcClient, FusionFetcher, WextsRpcClient } from '../client/fetcher';\n\ninterface FusionContextType {\n client: FusionFetcher;\n wexts: WextsRpcClient;\n}\n\nconst FusionContext = createContext<FusionContextType | null>(null);\n\nexport interface FusionProviderProps {\n children: ReactNode;\n baseUrl?: string;\n rpcBaseUrl?: string;\n rpcClient?: object;\n}\n\n/**\n * FusionProvider - Provides API client to React components\n * Usage:\n * ```tsx\n * <FusionProvider baseUrl=\"/api\">\n * <App />\n * </FusionProvider>\n * ```\n */\nexport function FusionProvider({ children, baseUrl = '/api', rpcBaseUrl = '/rpc', rpcClient }: FusionProviderProps) {\n const client = React.useMemo(() => new FusionFetcher(baseUrl), [baseUrl]);\n const wexts = React.useMemo(\n () => (rpcClient as WextsRpcClient | undefined) ?? createWextsRpcClient(undefined, { baseUrl: rpcBaseUrl }),\n [rpcBaseUrl, rpcClient]\n );\n\n return (\n <FusionContext.Provider value={{ client, wexts }}>\n {children}\n </FusionContext.Provider>\n );\n}\n\n/**\n * useFusion hook - Access API client in components\n * Usage:\n * ```tsx\n * const { client } = useFusion();\n * const data = await client.get('/users');\n * ```\n */\nexport function useFusion(): FusionContextType {\n const context = useContext(FusionContext);\n if (!context) {\n throw new Error('useFusion must be used within FusionProvider');\n }\n return context;\n}\n\nexport function useWexts<TClient = WextsRpcClient>(): TClient {\n const context = useFusion();\n return context.wexts as TClient;\n}\n","export interface WextsErrorOptions {\n code: string;\n message: string;\n cause?: unknown;\n suggestedFix?: string;\n docsSlug?: string;\n}\n\nexport class WextsError extends Error {\n readonly code: string;\n readonly suggestedFix?: string;\n readonly docsSlug?: string;\n\n constructor(options: WextsErrorOptions) {\n super(options.message, options.cause === undefined ? undefined : { cause: options.cause });\n this.name = 'WextsError';\n this.code = options.code;\n this.suggestedFix = options.suggestedFix;\n this.docsSlug = options.docsSlug;\n }\n\n get docsUrl(): string | undefined {\n return this.docsSlug ? `https://github.com/ziadmustafa1/wexts/blob/main/docs/${this.docsSlug}.md` : undefined;\n }\n}\n\nexport class WextsRpcError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_RPC_ERROR', ...options });\n this.name = 'WextsRpcError';\n }\n}\n\nexport class WextsCodegenError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_CODEGEN_ERROR', ...options });\n this.name = 'WextsCodegenError';\n }\n}\n\nexport class WextsRuntimeError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_RUNTIME_ERROR', ...options });\n this.name = 'WextsRuntimeError';\n }\n}\n\nexport class WextsSecurityError extends WextsError {\n constructor(options: Omit<WextsErrorOptions, 'code'> & { code?: string }) {\n super({ code: options.code ?? 'WEXTS_SECURITY_ERROR', ...options });\n this.name = 'WextsSecurityError';\n }\n}\n\nexport function formatWextsError(error: unknown): string {\n if (!(error instanceof WextsError)) {\n return error instanceof Error ? error.message : String(error);\n }\n\n const lines = [`${error.code}: ${error.message}`];\n if (error.suggestedFix) lines.push(`Suggested fix: ${error.suggestedFix}`);\n if (error.docsUrl) lines.push(`Docs: ${error.docsUrl}`);\n return lines.join('\\n');\n}\n","import type { RpcManifest, RpcInvocationResponse } from '../rpc/types';\nimport { WextsRpcError } from '../errors';\n\nexport class FusionFetcher {\n private baseUrl: string;\n\n constructor(baseUrl: string = '/api') {\n this.baseUrl = baseUrl;\n }\n\n private async request<T>(method: string, path: string, body?: any): Promise<T> {\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Automatically attach Fusion Token if present\n if (typeof window !== 'undefined') {\n const token = localStorage.getItem('fusion_token');\n if (token) headers['Authorization'] = `Bearer ${token}`;\n }\n\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n throw new WextsRpcError({\n code: 'WEXTS_API_REQUEST_FAILED',\n message: `Fusion API Error: ${response.status} ${response.statusText}`,\n suggestedFix: 'Check the API route, server logs, and authentication headers.',\n docsSlug: 'troubleshooting',\n });\n }\n\n if (response.status === 204) {\n return undefined as T;\n }\n\n return response.json();\n }\n\n get<T>(path: string) { return this.request<T>('GET', path); }\n post<T>(path: string, body: any) { return this.request<T>('POST', path, body); }\n put<T>(path: string, body: any) { return this.request<T>('PUT', path, body); }\n delete<T>(path: string) { return this.request<T>('DELETE', path); }\n}\n\nexport const apiFetcher = new FusionFetcher();\n\nexport interface WextsRpcClientOptions {\n baseUrl?: string;\n fetch?: typeof fetch;\n getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;\n}\n\nexport type WextsRpcClient = Record<string, Record<string, (...args: unknown[]) => Promise<unknown>>>;\n\nexport function createWextsRpcClient(\n manifest: Pick<RpcManifest, 'services'> | undefined,\n options: WextsRpcClientOptions = {}\n): WextsRpcClient {\n const hasManifest = Boolean(manifest);\n const services = new Set((manifest?.services ?? []).map((service) => service.name));\n const methodMap = new Map<string, Set<string>>();\n\n for (const service of manifest?.services ?? []) {\n methodMap.set(service.name, new Set(service.methods.map((method) => method.name)));\n }\n\n const createServiceProxy = (serviceName: string) => new Proxy({}, {\n get(_target, methodName) {\n if (typeof methodName !== 'string') return undefined;\n if (methodName === 'then') return undefined;\n\n const knownMethods = methodMap.get(serviceName);\n if (knownMethods && !knownMethods.has(methodName)) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_METHOD_NOT_FOUND',\n message: `Wexts RPC method not found: ${serviceName}.${methodName}`,\n suggestedFix: 'Run `wexts generate` and verify the method is decorated with @RpcMethod().',\n docsSlug: 'rpc',\n });\n }\n\n return (...args: unknown[]) => invokeRpc(serviceName, methodName, args, options);\n },\n }) as Record<string, (...args: unknown[]) => Promise<unknown>>;\n\n return new Proxy({}, {\n get(_target, serviceName) {\n if (typeof serviceName !== 'string') return undefined;\n if (serviceName === 'then') return undefined;\n if (!hasManifest) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_MANIFEST_MISSING',\n message: 'Wexts RPC manifest is missing.',\n suggestedFix: 'Run `wexts generate` and import the generated client/provider instead of creating an empty client.',\n docsSlug: 'codegen',\n });\n }\n if (!services.has(serviceName)) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_SERVICE_NOT_FOUND',\n message: `Wexts RPC service not found: ${serviceName}`,\n suggestedFix: 'Run `wexts generate` and verify the service is decorated with @RpcService().',\n docsSlug: 'rpc',\n });\n }\n\n return createServiceProxy(serviceName);\n },\n }) as WextsRpcClient;\n}\n\nasync function invokeRpc(\n serviceName: string,\n methodName: string,\n args: unknown[],\n options: WextsRpcClientOptions\n): Promise<unknown> {\n const fetchImpl = options.fetch ?? fetch;\n const baseUrl = options.baseUrl ?? '/rpc';\n const headers = {\n 'Content-Type': 'application/json',\n ...(await options.getHeaders?.() ?? {}),\n };\n const response = await fetchImpl(`${baseUrl}/${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`, {\n method: 'POST',\n headers,\n body: JSON.stringify({ args }),\n });\n\n if (!response.ok) {\n throw new WextsRpcError({\n code: 'WEXTS_RPC_REQUEST_FAILED',\n message: `Wexts RPC Error: ${response.status} ${response.statusText}`,\n suggestedFix: 'Check the RPC route, service policy, and server logs.',\n docsSlug: 'troubleshooting',\n });\n }\n\n const payload = await response.json() as RpcInvocationResponse;\n return payload.data;\n}\n","'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useFusion } from './provider';\n\nexport interface AuthUser {\n id: string;\n email: string;\n name?: string;\n}\n\nexport interface UseAuthReturn {\n user: AuthUser | null;\n loading: boolean;\n login: (email: string, password: string) => Promise<void>;\n logout: () => Promise<void>;\n isAuthenticated: boolean;\n}\n\n/**\n * useAuth hook - Authentication state management\n * Usage:\n * ```tsx\n * const { user, login, logout, isAuthenticated } = useAuth();\n * ```\n */\nexport function useAuth(): UseAuthReturn {\n const { client } = useFusion();\n const [user, setUser] = useState<AuthUser | null>(null);\n const [loading, setLoading] = useState(true);\n\n useEffect(() => {\n // Check for existing session\n const token = localStorage.getItem('fusion_token');\n if (token) {\n // Validate token and load user\n loadUser();\n } else {\n setLoading(false);\n }\n }, []);\n\n const loadUser = async () => {\n try {\n const userData = await client.get<AuthUser>('/auth/me');\n setUser(userData);\n } catch (error) {\n localStorage.removeItem('fusion_token');\n } finally {\n setLoading(false);\n }\n };\n\n const login = async (email: string, password: string) => {\n const response = await client.post<{ token: string; user: AuthUser }>('/auth/login', {\n email,\n password,\n });\n localStorage.setItem('fusion_token', response.token);\n setUser(response.user);\n };\n\n const logout = async () => {\n localStorage.removeItem('fusion_token');\n setUser(null);\n };\n\n return {\n user,\n loading,\n login,\n logout,\n isAuthenticated: !!user,\n };\n}\n"],"mappings":";;;;;AAEA,OAAOA,SAASC,eAAeC,kBAA6B;;;ACMrD,IAAMC,aAAN,cAAyBC,MAAAA;EAAhC,OAAgCA;;;EACnBC;EACAC;EACAC;EAET,YAAYC,SAA4B;AACpC,UAAMA,QAAQC,SAASD,QAAQE,UAAUC,SAAYA,SAAY;MAAED,OAAOF,QAAQE;IAAM,CAAA;AACxF,SAAKE,OAAO;AACZ,SAAKP,OAAOG,QAAQH;AACpB,SAAKC,eAAeE,QAAQF;AAC5B,SAAKC,WAAWC,QAAQD;EAC5B;EAEA,IAAIM,UAA8B;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sError","Error","code","suggestedFix","docsSlug","options","message","cause","undefined","name","docsUrl","WextsRpcError","FusionFetcher","baseUrl","request","method","path","body","headers","window","token","localStorage","getItem","response","fetch","JSON","stringify","undefined","ok","WextsRpcError","code","message","status","statusText","suggestedFix","docsSlug","json","get","post","put","delete","apiFetcher","createWextsRpcClient","manifest","options","hasManifest","Boolean","services","Set","map","service","name","methodMap","Map","set","methods","createServiceProxy","serviceName","Proxy","_target","methodName","knownMethods","has","args","invokeRpc","fetchImpl","getHeaders","encodeURIComponent","payload","data","FusionContext","createContext","FusionProvider","children","baseUrl","rpcBaseUrl","rpcClient","client","React","useMemo","FusionFetcher","wexts","createWextsRpcClient","undefined","Provider","value","useFusion","context","useContext","Error","useWexts","useState","useEffect","useAuth","client","useFusion","user","setUser","useState","loading","setLoading","useEffect","token","localStorage","getItem","loadUser","userData","get","error","removeItem","login","email","password","response","post","setItem","logout","isAuthenticated"]}
@@ -1,5 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
+ var _chunkAVMQJWYDjs = require('../chunk-AVMQJWYD.js');
4
+
5
+
3
6
  var _chunkXE4OXN2Wjs = require('../chunk-XE4OXN2W.js');
4
7
 
5
8
  // src/runtime/rpc-router.ts
@@ -158,7 +161,12 @@ async function mountNext(fastify, rootDir, config) {
158
161
  const nextModule = await Promise.resolve().then(() => _interopRequireWildcard(require("next")));
159
162
  const next = nextModule.default;
160
163
  if (!next) {
161
- throw new Error("Next.js could not be loaded. Install next or omit nextDir.");
164
+ throw new (0, _chunkAVMQJWYDjs.WextsRuntimeError)({
165
+ code: "WEXTS_RUNTIME_NEXT_MISSING",
166
+ message: "Next.js could not be loaded. Install next or omit nextDir.",
167
+ suggestedFix: "Install `next` in the application or remove `nextDir` from wexts.runtime.js.",
168
+ docsSlug: "runtime"
169
+ });
162
170
  }
163
171
  const nextApp = next({
164
172
  dev: _nullishCoalesce(config.dev, () => ( process.env.NODE_ENV !== "production")),
@@ -1 +1 @@
1
- {"version":3,"sources":["/Volumes/Projects/wexts/packages/dist/runtime/index.js","../../src/runtime/rpc-router.ts","../../src/runtime/server.ts","../../src/runtime/vercel-handler.ts"],"names":["registerRpcRoutes","fastify","options","serviceMap","Map","manifest","services","map","service","name","post","request","reply","get","params","status","error","method","methods","find","candidate","requireAuth","authorized","authorize","instance","handler","handlerName","args","Array","isArray","body","data","apply","createWextsRuntimeServer","config","rootDir","resolve","process","cwd","Fastify","logger","bodyLimit","security","bodyLimitBytes","requestTimeout","requestTimeoutMs","rpcManifest","loadJson","rpcManifestPath","routePolicies","rpcPoliciesFromManifest","registerWextsShield","ok","runtime","scope","rpcServices","Boolean","headers","authorization","cookie","nestAppModule","nestAppModulePath","mountNest","nextDir","mountNext","start","listen","port","Number","env","PORT","host","close","registerShutdown","startWextsRuntime","server","flatMap","path","undefined","filePath","absolutePath","AppModule","moduleValue","default","NODE_ENV","modulePath","importModule","pathToFileUrl","res","req","createWextsHandler"],"mappings":"AAAA;AACE;AACF,uDAA6B;AAC7B;AACA;ACOA,M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AS,MAAA;AFwBpB,IAAA;AEvB1C,EAAA;AACwB,EAAA;AACN,EAAA;AACtB;AAZepB;AAcqD5B;AAChC,EAAA;AACRiD,EAAAA;AACb,EAAA;AACS,IAAA;AACpB,EAAA;AACqB,EAAA;AACcC,IAAAA;AACSlD,IAAAA;AAC5C,EAAA;AACqB,EAAA;AACoB,EAAA;AAEPtB,EAAAA;AACM,IAAA;AACxB,IAAA;AAChB,EAAA;AACJ;AAjBeoD;AAmB8BqB;AACJA,EAAAA;AACTL,EAAAA;AAChC;AAHeM;AAKwB;AAClBP,EAAAA;AACrB;AAFSQ;AAI2C;AAChC,EAAA;AACNf,IAAAA;AACO,IAAA;AAFD,EAAA;AAKO/C,EAAAA;AACCA,EAAAA;AAC5B;AARSgD;AFgCqC;AACA;AG1KZ;AAETxC,EAAAA;AACdC,IAAAA;AH2KmC;AGzKb,IAAA;AAC7B,EAAA;AAI0B,EAAA;AAEIsD,EAAAA;AACYC,IAAAA;AAC1C,EAAA;AACJ;AAhBsBC;AHwLwB;AACA;AACA;AACA;AACA;AACA","file":"/Volumes/Projects/wexts/packages/dist/runtime/index.js","sourcesContent":[null,"import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';\nimport type { RpcManifest, RpcServiceManifest } from '../rpc/types';\n\nexport type RpcServiceInstances = Record<string, Record<string, (...args: unknown[]) => unknown | Promise<unknown>>>;\n\nexport interface RegisterRpcRoutesOptions {\n manifest: RpcManifest;\n services: RpcServiceInstances;\n authorize?: (request: FastifyRequest, service: RpcServiceManifest, methodName: string) => boolean | Promise<boolean>;\n}\n\nexport async function registerRpcRoutes(fastify: FastifyInstance, options: RegisterRpcRoutesOptions): Promise<void> {\n const serviceMap = new Map(options.manifest.services.map((service) => [service.name, service]));\n\n fastify.post('/rpc/:service/:method', async (request: FastifyRequest<{\n Params: { service: string; method: string };\n Body: { args?: unknown[] };\n }>, reply: FastifyReply) => {\n const service = serviceMap.get(request.params.service);\n if (!service) {\n reply.status(404);\n return { error: 'WEXTS_RPC_SERVICE_NOT_FOUND' };\n }\n\n const method = service.methods.find((candidate) => candidate.name === request.params.method);\n if (!method) {\n reply.status(404);\n return { error: 'WEXTS_RPC_METHOD_NOT_FOUND' };\n }\n\n if (method.requireAuth || service.requireAuth) {\n const authorized = await options.authorize?.(request, service, method.name);\n if (!authorized) {\n reply.status(401);\n return { error: 'WEXTS_RPC_AUTH_REQUIRED' };\n }\n }\n\n const instance = options.services[service.name];\n const handler = instance?.[method.handlerName];\n if (!handler) {\n reply.status(500);\n return { error: 'WEXTS_RPC_HANDLER_NOT_BOUND' };\n }\n\n const args = Array.isArray(request.body?.args) ? request.body.args : [];\n const data = await handler.apply(instance, args);\n return { data };\n });\n}\n","import * as fs from 'fs';\nimport * as path from 'path';\nimport Fastify, { FastifyInstance, FastifyServerOptions } from 'fastify';\nimport { registerWextsShield, type WextsShieldConfig, type WextsShieldRoutePolicy } from '@wexts/security';\nimport type { RpcManifest } from '../rpc/types';\nimport { registerRpcRoutes, type RpcServiceInstances } from './rpc-router';\n\nexport interface WextsRuntimeConfig {\n rootDir?: string;\n port?: number;\n host?: string;\n dev?: boolean;\n nextDir?: string;\n nestAppModule?: unknown;\n nestAppModulePath?: string;\n rpcManifest?: RpcManifest;\n rpcManifestPath?: string;\n rpcServices?: RpcServiceInstances;\n security?: WextsShieldConfig;\n logger?: FastifyServerOptions['logger'];\n}\n\nexport interface WextsRuntimeServer {\n fastify: FastifyInstance;\n start: () => Promise<void>;\n close: () => Promise<void>;\n}\n\nexport async function createWextsRuntimeServer(config: WextsRuntimeConfig = {}): Promise<WextsRuntimeServer> {\n const rootDir = path.resolve(config.rootDir ?? process.cwd());\n const fastify = Fastify({\n logger: config.logger ?? true,\n bodyLimit: config.security?.bodyLimitBytes ?? 1_048_576,\n requestTimeout: config.security?.requestTimeoutMs ?? 30_000,\n });\n\n const manifest = config.rpcManifest ?? loadJson<RpcManifest>(rootDir, config.rpcManifestPath);\n const routePolicies = [\n ...(config.security?.routePolicies ?? []),\n ...rpcPoliciesFromManifest(manifest),\n ];\n\n await registerWextsShield(fastify, {\n ...config.security,\n routePolicies,\n });\n\n fastify.get('/health', async () => ({\n ok: true,\n runtime: 'wexts',\n }));\n\n fastify.get('/api/health', async () => ({\n ok: true,\n runtime: 'wexts',\n scope: 'api',\n }));\n\n if (manifest && config.rpcServices) {\n await registerRpcRoutes(fastify, {\n manifest,\n services: config.rpcServices,\n authorize: (request) => Boolean(request.headers.authorization || request.headers.cookie),\n });\n }\n\n if (config.nestAppModule || config.nestAppModulePath) {\n await mountNest(fastify, rootDir, config);\n }\n\n if (config.nextDir) {\n await mountNext(fastify, rootDir, config);\n }\n\n const start = async () => {\n await fastify.listen({\n port: config.port ?? Number(process.env.PORT ?? 3000),\n host: config.host ?? '0.0.0.0',\n });\n };\n\n const close = async () => {\n await fastify.close();\n };\n\n registerShutdown(close);\n\n return {\n fastify,\n start,\n close,\n };\n}\n\nexport async function startWextsRuntime(config: WextsRuntimeConfig = {}): Promise<WextsRuntimeServer> {\n const server = await createWextsRuntimeServer(config);\n await server.start();\n return server;\n}\n\nfunction rpcPoliciesFromManifest(manifest?: RpcManifest): WextsShieldRoutePolicy[] {\n if (!manifest) return [];\n\n return manifest.services.flatMap((service) => service.methods.map((method) => ({\n path: `/rpc/${service.name}/${method.name}`,\n methods: ['POST'],\n mode: service.requireAuth || method.requireAuth ? 'requireAuth' : 'public',\n })));\n}\n\nfunction loadJson<T>(rootDir: string, filePath?: string): T | undefined {\n if (!filePath) return undefined;\n\n const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(rootDir, filePath);\n if (!fs.existsSync(absolutePath)) return undefined;\n return JSON.parse(fs.readFileSync(absolutePath, 'utf8')) as T;\n}\n\nasync function mountNest(fastify: FastifyInstance, rootDir: string, config: WextsRuntimeConfig): Promise<void> {\n const [{ NestFactory }, { FastifyAdapter }] = await Promise.all([\n import('@nestjs/core'),\n import('@nestjs/platform-fastify'),\n ]);\n const AppModule = config.nestAppModule ?? await importModule(rootDir, config.nestAppModulePath!);\n const moduleValue = (AppModule as { AppModule?: unknown }).AppModule ?? AppModule;\n const nestApp = await NestFactory.create(moduleValue as never, new FastifyAdapter(fastify as never), {\n logger: config.dev ? ['log', 'error', 'warn'] : ['error', 'warn'],\n });\n nestApp.setGlobalPrefix('api');\n await nestApp.init();\n}\n\nasync function mountNext(fastify: FastifyInstance, rootDir: string, config: WextsRuntimeConfig): Promise<void> {\n const nextModule = await import('next') as unknown as { default?: (options: unknown) => { prepare: () => Promise<void>; getRequestHandler: () => (req: unknown, res: unknown) => Promise<void> } };\n const next = nextModule.default;\n if (!next) {\n throw new Error('Next.js could not be loaded. Install next or omit nextDir.');\n }\n const nextApp = next({\n dev: config.dev ?? process.env.NODE_ENV !== 'production',\n dir: path.isAbsolute(config.nextDir!) ? config.nextDir : path.join(rootDir, config.nextDir!),\n });\n await nextApp.prepare();\n const handler = nextApp.getRequestHandler();\n\n fastify.all('/*', async (request, reply) => {\n await handler(request.raw, reply.raw);\n reply.hijack();\n });\n}\n\nasync function importModule(rootDir: string, modulePath: string): Promise<unknown> {\n const absolutePath = path.isAbsolute(modulePath) ? modulePath : path.join(rootDir, modulePath);\n return import(pathToFileUrl(absolutePath));\n}\n\nfunction pathToFileUrl(filePath: string): string {\n return `file://${filePath}`;\n}\n\nfunction registerShutdown(close: () => Promise<void>): void {\n const handler = async () => {\n await close();\n process.exit(0);\n };\n\n process.once('SIGINT', handler);\n process.once('SIGTERM', handler);\n}\n","/**\n * Vercel / serverless handler adapter for the Wexts runtime.\n *\n * Creates a standard Node.js (req, res) handler that delegates to the\n * Fastify-backed Wexts runtime **without** calling fastify.listen().\n *\n * Usage:\n * const handler = await createWextsHandler({ ... });\n * export default handler; // Vercel function entry\n */\n\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport type { WextsRuntimeConfig } from './server';\nimport { createWextsRuntimeServer } from './server';\n\nexport type WextsHandler = (req: IncomingMessage, res: ServerResponse) => void;\n\n/**\n * Build a serverless-compatible handler from the Wexts runtime.\n * The returned function accepts Node http (req, res) and passes them\n * into the Fastify instance without ever calling listen().\n */\nexport async function createWextsHandler(\n config: WextsRuntimeConfig = {},\n): Promise<WextsHandler> {\n const server = await createWextsRuntimeServer({\n ...config,\n // Serverless does not use long-lived logging\n logger: config.logger ?? false,\n });\n\n // Fastify exposes a raw Node handler via server.server (the http.Server)\n // But we need to call .ready() first so all plugins are loaded.\n await server.fastify.ready();\n\n return (req: IncomingMessage, res: ServerResponse) => {\n server.fastify.server.emit('request', req, res);\n };\n}\n"]}
1
+ {"version":3,"sources":["/Volumes/Projects/wexts/packages/dist/runtime/index.js","../../src/runtime/rpc-router.ts","../../src/runtime/server.ts","../../src/runtime/vercel-handler.ts"],"names":["registerRpcRoutes","fastify","options","serviceMap","Map","manifest","services","map","service","name","post","request","reply","get","params","status","error","method","methods","find","candidate","requireAuth","authorized","authorize","instance","handler","handlerName","args","Array","isArray","body","data","apply","createWextsRuntimeServer","config","rootDir","resolve","process","cwd","Fastify","logger","bodyLimit","security","bodyLimitBytes","requestTimeout","requestTimeoutMs","rpcManifest","loadJson","rpcManifestPath","routePolicies","rpcPoliciesFromManifest","registerWextsShield","ok","runtime","scope","rpcServices","Boolean","headers","authorization","cookie","nestAppModule","nestAppModulePath","mountNest","nextDir","mountNext","start","listen","port","Number","env","PORT","host","close","registerShutdown","startWextsRuntime","server","flatMap","path","undefined","filePath","absolutePath","AppModule","moduleValue","default","NODE_ENV","modulePath","importModule","pathToFileUrl","res","req","createWextsHandler"],"mappings":"AAAA;AACE;AACF,uDAA6B;AAC7B;AACE;AACF,uDAA6B;AAC7B;AACA;ACIA,MAAA,SAAsBA,iBAAAA,CAAkBC,OAAAA,EAA0BC,OAAAA,EAAiC;AAC/F,EAAA,MAAMC,WAAAA,EAAa,IAAIC,GAAAA,CAAIF,OAAAA,CAAQG,QAAAA,CAASC,QAAAA,CAASC,GAAAA,CAAI,CAACC,OAAAA,EAAAA,GAAY;ADF1E,ICE2EA,OAAAA,CA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IAAAA;AACSlD,IAAAA;AAC5C,EAAA;AACqB,EAAA;AACoB,EAAA;AAEPtB,EAAAA;AACM,IAAA;AACxB,IAAA;AAChB,EAAA;AACJ;AAtBeoD;AAwB8BqB;AACJA,EAAAA;AACTL,EAAAA;AAChC;AAHeM;AAKwB;AAClBP,EAAAA;AACrB;AAFSQ;AAI2C;AAChC,EAAA;AACNf,IAAAA;AACO,IAAA;AAFD,EAAA;AAKO/C,EAAAA;AACCA,EAAAA;AAC5B;AARSgD;AFkCqC;AACA;AGlLZ;AAETxC,EAAAA;AACdC,IAAAA;AHmLmC;AGjLb,IAAA;AAC7B,EAAA;AAI0B,EAAA;AAEIsD,EAAAA;AACYC,IAAAA;AAC1C,EAAA;AACJ;AAhBsBC;AHgMwB;AACA;AACA;AACA;AACA;AACA","file":"/Volumes/Projects/wexts/packages/dist/runtime/index.js","sourcesContent":[null,"import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';\nimport type { RpcManifest, RpcServiceManifest } from '../rpc/types';\n\nexport type RpcServiceInstances = Record<string, Record<string, (...args: unknown[]) => unknown | Promise<unknown>>>;\n\nexport interface RegisterRpcRoutesOptions {\n manifest: RpcManifest;\n services: RpcServiceInstances;\n authorize?: (request: FastifyRequest, service: RpcServiceManifest, methodName: string) => boolean | Promise<boolean>;\n}\n\nexport async function registerRpcRoutes(fastify: FastifyInstance, options: RegisterRpcRoutesOptions): Promise<void> {\n const serviceMap = new Map(options.manifest.services.map((service) => [service.name, service]));\n\n fastify.post('/rpc/:service/:method', async (request: FastifyRequest<{\n Params: { service: string; method: string };\n Body: { args?: unknown[] };\n }>, reply: FastifyReply) => {\n const service = serviceMap.get(request.params.service);\n if (!service) {\n reply.status(404);\n return { error: 'WEXTS_RPC_SERVICE_NOT_FOUND' };\n }\n\n const method = service.methods.find((candidate) => candidate.name === request.params.method);\n if (!method) {\n reply.status(404);\n return { error: 'WEXTS_RPC_METHOD_NOT_FOUND' };\n }\n\n if (method.requireAuth || service.requireAuth) {\n const authorized = await options.authorize?.(request, service, method.name);\n if (!authorized) {\n reply.status(401);\n return { error: 'WEXTS_RPC_AUTH_REQUIRED' };\n }\n }\n\n const instance = options.services[service.name];\n const handler = instance?.[method.handlerName];\n if (!handler) {\n reply.status(500);\n return { error: 'WEXTS_RPC_HANDLER_NOT_BOUND' };\n }\n\n const args = Array.isArray(request.body?.args) ? request.body.args : [];\n const data = await handler.apply(instance, args);\n return { data };\n });\n}\n","import * as fs from 'fs';\nimport * as path from 'path';\nimport Fastify, { FastifyInstance, FastifyServerOptions } from 'fastify';\nimport { registerWextsShield, type WextsShieldConfig, type WextsShieldRoutePolicy } from '@wexts/security';\nimport type { RpcManifest } from '../rpc/types';\nimport { registerRpcRoutes, type RpcServiceInstances } from './rpc-router';\nimport { WextsRuntimeError } from '../errors';\n\nexport interface WextsRuntimeConfig {\n rootDir?: string;\n port?: number;\n host?: string;\n dev?: boolean;\n nextDir?: string;\n nestAppModule?: unknown;\n nestAppModulePath?: string;\n rpcManifest?: RpcManifest;\n rpcManifestPath?: string;\n rpcServices?: RpcServiceInstances;\n security?: WextsShieldConfig;\n logger?: FastifyServerOptions['logger'];\n}\n\nexport interface WextsRuntimeServer {\n fastify: FastifyInstance;\n start: () => Promise<void>;\n close: () => Promise<void>;\n}\n\nexport async function createWextsRuntimeServer(config: WextsRuntimeConfig = {}): Promise<WextsRuntimeServer> {\n const rootDir = path.resolve(config.rootDir ?? process.cwd());\n const fastify = Fastify({\n logger: config.logger ?? true,\n bodyLimit: config.security?.bodyLimitBytes ?? 1_048_576,\n requestTimeout: config.security?.requestTimeoutMs ?? 30_000,\n });\n\n const manifest = config.rpcManifest ?? loadJson<RpcManifest>(rootDir, config.rpcManifestPath);\n const routePolicies = [\n ...(config.security?.routePolicies ?? []),\n ...rpcPoliciesFromManifest(manifest),\n ];\n\n await registerWextsShield(fastify, {\n ...config.security,\n routePolicies,\n });\n\n fastify.get('/health', async () => ({\n ok: true,\n runtime: 'wexts',\n }));\n\n fastify.get('/api/health', async () => ({\n ok: true,\n runtime: 'wexts',\n scope: 'api',\n }));\n\n if (manifest && config.rpcServices) {\n await registerRpcRoutes(fastify, {\n manifest,\n services: config.rpcServices,\n authorize: (request) => Boolean(request.headers.authorization || request.headers.cookie),\n });\n }\n\n if (config.nestAppModule || config.nestAppModulePath) {\n await mountNest(fastify, rootDir, config);\n }\n\n if (config.nextDir) {\n await mountNext(fastify, rootDir, config);\n }\n\n const start = async () => {\n await fastify.listen({\n port: config.port ?? Number(process.env.PORT ?? 3000),\n host: config.host ?? '0.0.0.0',\n });\n };\n\n const close = async () => {\n await fastify.close();\n };\n\n registerShutdown(close);\n\n return {\n fastify,\n start,\n close,\n };\n}\n\nexport async function startWextsRuntime(config: WextsRuntimeConfig = {}): Promise<WextsRuntimeServer> {\n const server = await createWextsRuntimeServer(config);\n await server.start();\n return server;\n}\n\nfunction rpcPoliciesFromManifest(manifest?: RpcManifest): WextsShieldRoutePolicy[] {\n if (!manifest) return [];\n\n return manifest.services.flatMap((service) => service.methods.map((method) => ({\n path: `/rpc/${service.name}/${method.name}`,\n methods: ['POST'],\n mode: service.requireAuth || method.requireAuth ? 'requireAuth' : 'public',\n })));\n}\n\nfunction loadJson<T>(rootDir: string, filePath?: string): T | undefined {\n if (!filePath) return undefined;\n\n const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(rootDir, filePath);\n if (!fs.existsSync(absolutePath)) return undefined;\n return JSON.parse(fs.readFileSync(absolutePath, 'utf8')) as T;\n}\n\nasync function mountNest(fastify: FastifyInstance, rootDir: string, config: WextsRuntimeConfig): Promise<void> {\n const [{ NestFactory }, { FastifyAdapter }] = await Promise.all([\n import('@nestjs/core'),\n import('@nestjs/platform-fastify'),\n ]);\n const AppModule = config.nestAppModule ?? await importModule(rootDir, config.nestAppModulePath!);\n const moduleValue = (AppModule as { AppModule?: unknown }).AppModule ?? AppModule;\n const nestApp = await NestFactory.create(moduleValue as never, new FastifyAdapter(fastify as never), {\n logger: config.dev ? ['log', 'error', 'warn'] : ['error', 'warn'],\n });\n nestApp.setGlobalPrefix('api');\n await nestApp.init();\n}\n\nasync function mountNext(fastify: FastifyInstance, rootDir: string, config: WextsRuntimeConfig): Promise<void> {\n const nextModule = await import('next') as unknown as { default?: (options: unknown) => { prepare: () => Promise<void>; getRequestHandler: () => (req: unknown, res: unknown) => Promise<void> } };\n const next = nextModule.default;\n if (!next) {\n throw new WextsRuntimeError({\n code: 'WEXTS_RUNTIME_NEXT_MISSING',\n message: 'Next.js could not be loaded. Install next or omit nextDir.',\n suggestedFix: 'Install `next` in the application or remove `nextDir` from wexts.runtime.js.',\n docsSlug: 'runtime',\n });\n }\n const nextApp = next({\n dev: config.dev ?? process.env.NODE_ENV !== 'production',\n dir: path.isAbsolute(config.nextDir!) ? config.nextDir : path.join(rootDir, config.nextDir!),\n });\n await nextApp.prepare();\n const handler = nextApp.getRequestHandler();\n\n fastify.all('/*', async (request, reply) => {\n await handler(request.raw, reply.raw);\n reply.hijack();\n });\n}\n\nasync function importModule(rootDir: string, modulePath: string): Promise<unknown> {\n const absolutePath = path.isAbsolute(modulePath) ? modulePath : path.join(rootDir, modulePath);\n return import(pathToFileUrl(absolutePath));\n}\n\nfunction pathToFileUrl(filePath: string): string {\n return `file://${filePath}`;\n}\n\nfunction registerShutdown(close: () => Promise<void>): void {\n const handler = async () => {\n await close();\n process.exit(0);\n };\n\n process.once('SIGINT', handler);\n process.once('SIGTERM', handler);\n}\n","/**\n * Vercel / serverless handler adapter for the Wexts runtime.\n *\n * Creates a standard Node.js (req, res) handler that delegates to the\n * Fastify-backed Wexts runtime **without** calling fastify.listen().\n *\n * Usage:\n * const handler = await createWextsHandler({ ... });\n * export default handler; // Vercel function entry\n */\n\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport type { WextsRuntimeConfig } from './server';\nimport { createWextsRuntimeServer } from './server';\n\nexport type WextsHandler = (req: IncomingMessage, res: ServerResponse) => void;\n\n/**\n * Build a serverless-compatible handler from the Wexts runtime.\n * The returned function accepts Node http (req, res) and passes them\n * into the Fastify instance without ever calling listen().\n */\nexport async function createWextsHandler(\n config: WextsRuntimeConfig = {},\n): Promise<WextsHandler> {\n const server = await createWextsRuntimeServer({\n ...config,\n // Serverless does not use long-lived logging\n logger: config.logger ?? false,\n });\n\n // Fastify exposes a raw Node handler via server.server (the http.Server)\n // But we need to call .ready() first so all plugins are loaded.\n await server.fastify.ready();\n\n return (req: IncomingMessage, res: ServerResponse) => {\n server.fastify.server.emit('request', req, res);\n };\n}\n"]}