soma-core 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/SomaPageClient.tsx +364 -0
- package/app/api/[...path]/route.ts +9 -85
- package/app/api/page/route.ts +57 -35
- package/app/api/soma-manifest/route.ts +39 -0
- package/app/components/SomaHydrationShell.tsx +23 -0
- package/app/favicon.ico/route.ts +13 -0
- package/app/layout.tsx +2 -2
- package/app/loading.tsx +5 -0
- package/app/page.tsx +19 -128
- package/bin/soma-lsp.cjs +195 -0
- package/bin/soma-sync.cjs +3 -0
- package/bin/soma.cjs +13 -1
- package/core/api/IService.ts +6 -10
- package/core/api/load-host-service.ts +75 -0
- package/core/api/service-path.ts +39 -0
- package/core/base/SomaEntity.ts +3 -10
- package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
- package/core/base/SomaPageRootBase.ts +20 -3
- package/core/base/decorators/Event.ts +23 -0
- package/core/base/decorators/Locale.ts +35 -0
- package/core/base/decorators/Page.ts +16 -0
- package/core/base/decorators/Show.ts +20 -0
- package/core/base/decorators/ShowList.ts +34 -0
- package/core/base/decorators/index.ts +5 -0
- package/core/base/index.ts +2 -0
- package/core/base/runAsync.ts +32 -0
- package/core/compiler/full-manifest-builder.ts +33 -7
- package/core/compiler/host-config-sync.cjs +38 -0
- package/core/compiler/manifest-compiler.ts +3 -2
- package/core/compiler/page-base-resolver.ts +26 -0
- package/core/compiler/precompiled-manifest-loader.ts +34 -0
- package/core/compiler/rollup-plugin-soma.ts +45 -0
- package/core/compiler/rspack-plugin-soma.js +33 -0
- package/core/compiler/soma-manifest-webpack-plugin.js +188 -0
- package/core/compiler/soma-precompile.js +3513 -0
- package/core/compiler/soma-precompile.ts +52 -6
- package/core/compiler/soma-rspack-yaml-loader.js +22 -0
- package/core/compiler/soma-webpack-loader.js +7 -1
- package/core/compiler/vite-plugin-soma.ts +11 -1
- package/core/container/IContainerAdapter.ts +36 -0
- package/core/container/MiniProgramContainerAdapter.ts +190 -0
- package/core/container/ReactNativeContainerAdapter.ts +189 -0
- package/core/container/SomaContainerAdapter.ts +17 -6
- package/core/diagnostics/SomaDiagnostics.ts +1 -1
- package/core/diagnostics/SomaTelemetry.ts +100 -0
- package/core/diagnostics/telemetry-context.ts +15 -0
- package/core/engine/asset-resolver.ts +39 -4
- package/core/engine/dom-renderer.ts +169 -35
- package/core/engine/event-engine.ts +110 -41
- package/core/engine/on-change-engine.ts +71 -0
- package/core/engine/property-watcher.ts +117 -0
- package/core/engine/reactive-proxy.ts +11 -5
- package/core/engine/render-engine.ts +97 -11
- package/core/engine/runtime-builder.ts +88 -11
- package/core/engine/scope-resolver.ts +65 -9
- package/core/engine/soma-bootstrap.ts +448 -244
- package/core/engine/style-binder.ts +423 -0
- package/core/engine/theme-variables.ts +37 -0
- package/core/engine/timer-engine.ts +92 -0
- package/core/engine/trigger-engine.ts +55 -0
- package/core/host/app-config.stub.ts +2 -0
- package/core/host/locale.stub.ts +2 -0
- package/core/host/resolve-host-bootstrap.ts +28 -0
- package/core/host/soma-bootstrap-host.stub.ts +4 -0
- package/core/host/source-packages.ts +112 -0
- package/core/index.ts +33 -2
- package/core/infra/auth/TokenAuthService.ts +17 -3
- package/core/infra/http/IHttpClient.ts +11 -1
- package/core/infra/http/SomaHttpService.ts +89 -31
- package/core/infra/i18n/DefaultI18n.ts +6 -18
- package/core/infra/i18n/II18n.ts +2 -3
- package/core/infra/notify/DomNotifyService.ts +98 -0
- package/core/infra/notify/INotifyTheme.ts +22 -0
- package/core/infra/realtime/IRealtimeService.ts +23 -0
- package/core/infra/realtime/IRealtimeTransport.ts +9 -0
- package/core/infra/realtime/SomaRealtimeService.ts +95 -0
- package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
- package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
- package/core/infra/router/router-path.ts +28 -0
- package/core/infra/storage/SessionStorageService.ts +59 -0
- package/core/lsp/cross-file-diagnostics.cjs +160 -0
- package/core/lsp/event-yaml-completion.cjs +106 -0
- package/core/lsp/extra-completion.cjs +64 -0
- package/core/lsp/lsp-navigation.cjs +191 -0
- package/core/lsp/ui-yaml-completion.cjs +170 -0
- package/core/manifest/manifest-contract.ts +12 -0
- package/core/middleware/createPermissionMiddleware.ts +37 -0
- package/core/parser/annotation-extractor.ts +113 -0
- package/core/parser/pipeline-parser.ts +344 -276
- package/core/parser/style-config-parser.ts +340 -0
- package/core/parser/timer-parser.ts +70 -0
- package/core/parser/trigger-parser.ts +90 -0
- package/core/parser/ui-config-parser.ts +73 -1
- package/core/quality/SomaQualityRegistry.ts +68 -0
- package/core/runtime/SomaRuntimeServices.ts +100 -0
- package/core/runtime/SomaSnapshotStore.ts +5 -3
- package/core/types/domain.types.ts +5 -2
- package/core/types/event.types.ts +82 -57
- package/core/types/manifest.types.ts +2 -0
- package/core/types/style-config.types.ts +76 -0
- package/core/types/timer.types.ts +23 -0
- package/core/types/trigger.types.ts +14 -0
- package/core/types/ui-config.types.ts +9 -0
- package/core/types/watcher.types.ts +29 -0
- package/dist/cli/soma.js +23 -23
- package/dist/core/index.cjs +9065 -0
- package/dist/diagnostics/telemetry.cjs +105 -0
- package/dist/preview-export.cjs +3401 -0
- package/dist/tsconfig.types.tsbuildinfo +1 -0
- package/dist/types/api/IService.d.ts +28 -0
- package/dist/types/api/load-host-service.d.ts +5 -0
- package/dist/types/api/service-path.d.ts +13 -0
- package/dist/types/base/SomaApi.d.ts +5 -0
- package/dist/types/base/SomaDict.d.ts +89 -0
- package/dist/types/base/SomaEntity.d.ts +15 -0
- package/dist/types/base/SomaHttpClient.d.ts +66 -0
- package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
- package/dist/types/base/SomaPageRootBase.d.ts +49 -0
- package/dist/types/base/SomaValidator.d.ts +55 -0
- package/dist/types/base/decorators/Event.d.ts +10 -0
- package/dist/types/base/decorators/Locale.d.ts +9 -0
- package/dist/types/base/decorators/Page.d.ts +4 -0
- package/dist/types/base/decorators/Show.d.ts +7 -0
- package/dist/types/base/decorators/ShowList.d.ts +12 -0
- package/dist/types/base/decorators/index.d.ts +5 -0
- package/dist/types/base/index.d.ts +9 -0
- package/dist/types/base/runAsync.d.ts +12 -0
- package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
- package/dist/types/compiler/manifest-compiler.d.ts +29 -0
- package/dist/types/compiler/page-base-resolver.d.ts +6 -0
- package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
- package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
- package/dist/types/compiler/route-builder.d.ts +12 -0
- package/dist/types/compiler/soma-precompile.d.ts +40 -0
- package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
- package/dist/types/container/IContainerAdapter.d.ts +30 -0
- package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
- package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
- package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
- package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
- package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
- package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
- package/dist/types/engine/asset-resolver.d.ts +17 -0
- package/dist/types/engine/dom-renderer.d.ts +41 -0
- package/dist/types/engine/event-engine.d.ts +64 -0
- package/dist/types/engine/lis.d.ts +10 -0
- package/dist/types/engine/on-change-engine.d.ts +16 -0
- package/dist/types/engine/property-watcher.d.ts +18 -0
- package/dist/types/engine/reactive-proxy.d.ts +58 -0
- package/dist/types/engine/render-engine.d.ts +73 -0
- package/dist/types/engine/runtime-builder.d.ts +58 -0
- package/dist/types/engine/scope-resolver.d.ts +90 -0
- package/dist/types/engine/soma-bootstrap.d.ts +112 -0
- package/dist/types/engine/style-binder.d.ts +81 -0
- package/dist/types/engine/theme-variables.d.ts +3 -0
- package/dist/types/engine/timer-engine.d.ts +19 -0
- package/dist/types/engine/trigger-engine.d.ts +16 -0
- package/dist/types/host/app-config.stub.d.ts +3 -0
- package/dist/types/host/locale.stub.d.ts +3 -0
- package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
- package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
- package/dist/types/host/source-packages.d.ts +8 -0
- package/dist/types/index.d.ts +35 -0
- package/dist/types/infra/auth/IAuthService.d.ts +12 -0
- package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
- package/dist/types/infra/cache/ICache.d.ts +12 -0
- package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
- package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
- package/dist/types/infra/config/IConfig.d.ts +9 -0
- package/dist/types/infra/config/SomaConfig.d.ts +21 -0
- package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
- package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
- package/dist/types/infra/http/IHttpClient.d.ts +28 -0
- package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
- package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
- package/dist/types/infra/i18n/II18n.d.ts +6 -0
- package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
- package/dist/types/infra/logger/ILogger.d.ts +17 -0
- package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
- package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
- package/dist/types/infra/notify/INotifyService.d.ts +12 -0
- package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
- package/dist/types/infra/permission/IPermission.d.ts +10 -0
- package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
- package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
- package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
- package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
- package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
- package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
- package/dist/types/infra/router/IRouter.d.ts +16 -0
- package/dist/types/infra/router/SomaRouter.d.ts +18 -0
- package/dist/types/infra/router/router-path.d.ts +6 -0
- package/dist/types/infra/storage/IStorageService.d.ts +7 -0
- package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
- package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
- package/dist/types/infra/timer/ITimer.d.ts +13 -0
- package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
- package/dist/types/manifest/manifest-contract.d.ts +4 -0
- package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
- package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
- package/dist/types/parser/annotation-extractor.d.ts +12 -0
- package/dist/types/parser/pipeline-parser.d.ts +62 -0
- package/dist/types/parser/style-config-parser.d.ts +20 -0
- package/dist/types/parser/timer-parser.d.ts +6 -0
- package/dist/types/parser/trigger-parser.d.ts +13 -0
- package/dist/types/parser/ui-config-parser.d.ts +34 -0
- package/dist/types/parser/yaml-loader.d.ts +21 -0
- package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
- package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
- package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
- package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
- package/dist/types/types/domain.types.d.ts +82 -0
- package/dist/types/types/engine.types.d.ts +22 -0
- package/dist/types/types/event.types.d.ts +61 -0
- package/dist/types/types/keyboard.types.d.ts +4 -0
- package/dist/types/types/manifest.types.d.ts +29 -0
- package/dist/types/types/scheduler.types.d.ts +8 -0
- package/dist/types/types/style-config.types.d.ts +62 -0
- package/dist/types/types/timer.types.d.ts +19 -0
- package/dist/types/types/trigger.types.d.ts +12 -0
- package/dist/types/types/ui-config.types.d.ts +85 -0
- package/dist/types/types/watcher.types.d.ts +24 -0
- package/dist/types/validation/contract-validator.d.ts +40 -0
- package/editor/vscode/extension.js +39 -0
- package/editor/vscode/language-configuration.json +17 -0
- package/editor/vscode/package.json +98 -0
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
- package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
- package/global.d.ts +22 -3
- package/next.config.js +73 -4
- package/package.json +58 -17
- package/tailwind.config.ts +36 -13
- package/tsconfig.json +6 -2
- package/core/router/RouteHandler.ts +0 -93
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export interface SomaAssetSource {
|
|
5
|
+
type: 'host' | 'package';
|
|
6
|
+
name: string;
|
|
7
|
+
assetRoot: string;
|
|
8
|
+
codeRoot: string;
|
|
9
|
+
packageName?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface PackageJson {
|
|
13
|
+
dependencies?: Record<string, string>;
|
|
14
|
+
devDependencies?: Record<string, string>;
|
|
15
|
+
soma?: {
|
|
16
|
+
source?: boolean;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readJson<T>(filePath: string): T | null {
|
|
21
|
+
try {
|
|
22
|
+
if (!fs.existsSync(filePath)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as T;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function packagePathInNodeModules(hostDir: string, packageName: string): string {
|
|
32
|
+
return path.join(hostDir, 'node_modules', ...packageName.split('/'));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function resolvePackageRoot(hostDir: string, packageName: string, version?: string): string | null {
|
|
36
|
+
const nodeModulesPath = packagePathInNodeModules(hostDir, packageName);
|
|
37
|
+
if (fs.existsSync(nodeModulesPath)) {
|
|
38
|
+
return nodeModulesPath;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (version?.startsWith('file:')) {
|
|
42
|
+
const fileRoot = path.resolve(hostDir, version.slice('file:'.length));
|
|
43
|
+
if (fs.existsSync(fileRoot)) {
|
|
44
|
+
return fileRoot;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function readExplicitSourcePackages(): string[] {
|
|
52
|
+
return String(process.env.SOMA_SOURCE_PACKAGES ?? '')
|
|
53
|
+
.split(',')
|
|
54
|
+
.map((item) => item.trim())
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readHostDependencies(hostDir: string): Record<string, string> {
|
|
59
|
+
const packageJson = readJson<PackageJson>(path.join(hostDir, 'package.json'));
|
|
60
|
+
return {
|
|
61
|
+
...(packageJson?.dependencies ?? {}),
|
|
62
|
+
...(packageJson?.devDependencies ?? {}),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isSomaSourcePackage(packageRoot: string, explicit: boolean): boolean {
|
|
67
|
+
if (explicit) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
const packageJson = readJson<PackageJson>(path.join(packageRoot, 'package.json'));
|
|
71
|
+
return packageJson?.soma?.source === true || fs.existsSync(path.join(packageRoot, 'shell', 'html'));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function toPackageSource(packageName: string, packageRoot: string): SomaAssetSource {
|
|
75
|
+
const shellRoot = path.join(packageRoot, 'shell');
|
|
76
|
+
return {
|
|
77
|
+
type: 'package',
|
|
78
|
+
name: packageName,
|
|
79
|
+
packageName,
|
|
80
|
+
assetRoot: fs.existsSync(path.join(shellRoot, 'html')) ? shellRoot : packageRoot,
|
|
81
|
+
codeRoot: packageRoot,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function getSomaAssetSources(hostDir: string): SomaAssetSource[] {
|
|
86
|
+
const normalizedHostDir = path.resolve(hostDir);
|
|
87
|
+
const dependencies = readHostDependencies(normalizedHostDir);
|
|
88
|
+
const explicitPackages = new Set(readExplicitSourcePackages());
|
|
89
|
+
const packageNames = new Set([
|
|
90
|
+
...Object.keys(dependencies),
|
|
91
|
+
...explicitPackages,
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
const sources: SomaAssetSource[] = [
|
|
95
|
+
{
|
|
96
|
+
type: 'host',
|
|
97
|
+
name: 'host',
|
|
98
|
+
assetRoot: normalizedHostDir,
|
|
99
|
+
codeRoot: normalizedHostDir,
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
for (const packageName of packageNames) {
|
|
104
|
+
const packageRoot = resolvePackageRoot(normalizedHostDir, packageName, dependencies[packageName]);
|
|
105
|
+
if (!packageRoot || !isSomaSourcePackage(packageRoot, explicitPackages.has(packageName))) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
sources.push(toPackageSource(packageName, packageRoot));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return sources;
|
|
112
|
+
}
|
package/core/index.ts
CHANGED
|
@@ -1,9 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* soma-core —
|
|
2
|
+
* soma-core — host apps should import subpaths via package exports.
|
|
3
3
|
*/
|
|
4
4
|
export * from './base/index';
|
|
5
5
|
|
|
6
6
|
export type { SomaAppConfig, SomaPageRoot } from './types/domain.types';
|
|
7
7
|
|
|
8
|
-
//
|
|
8
|
+
// Host API service contract
|
|
9
9
|
export type { IService } from './api/IService';
|
|
10
|
+
|
|
11
|
+
// Bootstrap & runtime
|
|
12
|
+
export { SomaBootstrap } from './engine/soma-bootstrap';
|
|
13
|
+
export type { SomaPageAssets, SomaBootstrapOptions } from './engine/soma-bootstrap';
|
|
14
|
+
export { createDefaultRuntimeServices } from './runtime/SomaRuntimeServices';
|
|
15
|
+
export type { SomaRuntimeServices } from './runtime/SomaRuntimeServices';
|
|
16
|
+
export { SomaDiagnostics } from './diagnostics/SomaDiagnostics';
|
|
17
|
+
export type { SomaDiagnosticsSnapshot } from './diagnostics/SomaDiagnostics';
|
|
18
|
+
export { matchGlobPath } from './engine/property-watcher';
|
|
19
|
+
export { extractConfigFromDomainClass } from './parser/annotation-extractor';
|
|
20
|
+
export { somaRollupPlugin } from './compiler/rollup-plugin-soma';
|
|
21
|
+
export { Page, Show, ShowList, Event } from './base/decorators';
|
|
22
|
+
export { runAsync } from './base/runAsync';
|
|
23
|
+
export type { AsyncStateHost } from './base/runAsync';
|
|
24
|
+
export { SomaQualityRegistry } from './quality/SomaQualityRegistry';
|
|
25
|
+
export { SomaTelemetry } from './diagnostics/SomaTelemetry';
|
|
26
|
+
export { setActiveTelemetry, recordMetric } from './diagnostics/telemetry-context';
|
|
27
|
+
export type { IContainerAdapter, SomaAnchorStats } from './container/IContainerAdapter';
|
|
28
|
+
export { SomaContainerAdapter } from './container/SomaContainerAdapter';
|
|
29
|
+
export { ReactNativeContainerAdapter } from './container/ReactNativeContainerAdapter';
|
|
30
|
+
export { MiniProgramContainerAdapter } from './container/MiniProgramContainerAdapter';
|
|
31
|
+
export type { IRealtimeTransport, RealtimeMessageHandler } from './infra/realtime/IRealtimeTransport';
|
|
32
|
+
export type { IRealtimeService, RealtimeSubscribeOptions } from './infra/realtime/IRealtimeService';
|
|
33
|
+
export { SseRealtimeTransport } from './infra/realtime/SseRealtimeTransport';
|
|
34
|
+
export { WebSocketRealtimeTransport } from './infra/realtime/WebSocketRealtimeTransport';
|
|
35
|
+
export { SomaRealtimeService } from './infra/realtime/SomaRealtimeService';
|
|
36
|
+
export { SomaMiddlewareChain } from './middleware/SomaMiddlewareChain';
|
|
37
|
+
export { createPermissionMiddleware } from './middleware/createPermissionMiddleware';
|
|
38
|
+
export type { SomaEventMiddlewareContext } from './engine/event-engine';
|
|
39
|
+
export type { SomaBootstrapConfigurator } from './host/resolve-host-bootstrap';
|
|
40
|
+
export { resolveHostBootstrapOptions } from './host/resolve-host-bootstrap';
|
|
@@ -4,6 +4,7 @@ import { IStorageService } from '../storage/IStorageService';
|
|
|
4
4
|
|
|
5
5
|
const TOKEN_KEY = 'soma_auth_token';
|
|
6
6
|
const TOKEN_EXPIRY_KEY = 'soma_auth_token_expiry';
|
|
7
|
+
const USER_NAME_KEY = 'soma_user_name';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* TokenAuthService — JWT-style token authentication.
|
|
@@ -17,17 +18,25 @@ export class TokenAuthService implements IAuthService {
|
|
|
17
18
|
constructor(
|
|
18
19
|
private http: IHttpClient,
|
|
19
20
|
private storage: IStorageService,
|
|
20
|
-
private loginUrl: string
|
|
21
|
-
private refreshUrl: string
|
|
21
|
+
private loginUrl: string,
|
|
22
|
+
private refreshUrl: string,
|
|
22
23
|
) {}
|
|
23
24
|
|
|
24
25
|
async login(credentials: LoginCredentials): Promise<boolean> {
|
|
25
26
|
try {
|
|
26
|
-
const res = await this.http.post<{
|
|
27
|
+
const res = await this.http.post<{
|
|
28
|
+
token: string;
|
|
29
|
+
expiresIn: number;
|
|
30
|
+
user?: { name?: string; email?: string };
|
|
31
|
+
}>(
|
|
27
32
|
this.loginUrl,
|
|
28
33
|
credentials,
|
|
29
34
|
);
|
|
30
35
|
this.storeToken(res.token, res.expiresIn);
|
|
36
|
+
const displayName = res.user?.name || res.user?.email;
|
|
37
|
+
if (displayName) {
|
|
38
|
+
this.storage.set(USER_NAME_KEY, displayName);
|
|
39
|
+
}
|
|
31
40
|
this.notify(true);
|
|
32
41
|
return true;
|
|
33
42
|
} catch {
|
|
@@ -38,9 +47,14 @@ export class TokenAuthService implements IAuthService {
|
|
|
38
47
|
async logout(): Promise<void> {
|
|
39
48
|
this.storage.remove(TOKEN_KEY);
|
|
40
49
|
this.storage.remove(TOKEN_EXPIRY_KEY);
|
|
50
|
+
this.storage.remove(USER_NAME_KEY);
|
|
41
51
|
this.notify(false);
|
|
42
52
|
}
|
|
43
53
|
|
|
54
|
+
getDisplayName(): string | null {
|
|
55
|
+
return this.storage.get<string>(USER_NAME_KEY);
|
|
56
|
+
}
|
|
57
|
+
|
|
44
58
|
getToken(): string | null {
|
|
45
59
|
if (!this.isAuthenticated()) return null;
|
|
46
60
|
return this.storage.get<string>(TOKEN_KEY);
|
|
@@ -5,6 +5,15 @@ export interface HttpRequestConfig {
|
|
|
5
5
|
signal?: AbortSignal;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
export interface UploadOptions {
|
|
9
|
+
/** multipart 字段名,默认 file */
|
|
10
|
+
fieldName?: string;
|
|
11
|
+
/** 上传文件名 */
|
|
12
|
+
fileName?: string;
|
|
13
|
+
/** 额外表单字段 */
|
|
14
|
+
fields?: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
export interface HttpResponse<T = any> {
|
|
9
18
|
data: T;
|
|
10
19
|
status: number;
|
|
@@ -15,7 +24,8 @@ export interface IHttpClient {
|
|
|
15
24
|
get<T>(url: string, config?: HttpRequestConfig): Promise<T>;
|
|
16
25
|
post<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T>;
|
|
17
26
|
put<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T>;
|
|
27
|
+
patch<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T>;
|
|
18
28
|
delete<T>(url: string, config?: HttpRequestConfig): Promise<T>;
|
|
19
|
-
upload<T>(url: string, file: Blob, onProgress?: (percent: number) => void): Promise<T>;
|
|
29
|
+
upload<T>(url: string, file: Blob, onProgress?: (percent: number) => void, options?: UploadOptions): Promise<T>;
|
|
20
30
|
abortAll(): void;
|
|
21
31
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SomaHttpClient, SomaHttpClientConfig } from '@/core/base/SomaHttpClient';
|
|
2
|
-
import { IHttpClient, HttpRequestConfig } from './IHttpClient';
|
|
2
|
+
import { IHttpClient, HttpRequestConfig, UploadOptions } from './IHttpClient';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* SomaHttpService — wraps the core SomaHttpClient and implements IHttpClient.
|
|
@@ -12,72 +12,126 @@ export class SomaHttpService implements IHttpClient {
|
|
|
12
12
|
private client: SomaHttpClient;
|
|
13
13
|
private baseURL: string;
|
|
14
14
|
private abortControllers: Set<AbortController> = new Set();
|
|
15
|
+
private onHttpError: ((status: number, message: string) => void) | null = null;
|
|
15
16
|
|
|
16
17
|
constructor(config: SomaHttpClientConfig) {
|
|
17
18
|
this.baseURL = config.baseURL;
|
|
18
19
|
this.client = new SomaHttpClient(config);
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
/** 注册 HTTP 错误回调(401/403/422 等统一处理) */
|
|
23
|
+
setErrorHandler(handler: (status: number, message: string) => void): void {
|
|
24
|
+
this.onHttpError = handler;
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
async get<T>(url: string, config?: HttpRequestConfig): Promise<T> {
|
|
22
|
-
|
|
23
|
-
return this.requestWithConfig<T>('GET', url, undefined, config);
|
|
24
|
-
}
|
|
25
|
-
return this.client.get<T>(url);
|
|
28
|
+
return this.requestWithConfig<T>('GET', url, undefined, config);
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
async post<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T> {
|
|
29
|
-
|
|
30
|
-
return this.requestWithConfig<T>('POST', url, data, config);
|
|
31
|
-
}
|
|
32
|
-
return this.client.post<T>(url, data);
|
|
32
|
+
return this.requestWithConfig<T>('POST', url, data, config);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
async put<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T> {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
return this.requestWithConfig<T>('PUT', url, data, config);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async patch<T>(url: string, data?: any, config?: HttpRequestConfig): Promise<T> {
|
|
40
|
+
return this.requestWithConfig<T>('PATCH', url, data, config);
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
async delete<T>(url: string, config?: HttpRequestConfig): Promise<T> {
|
|
43
|
-
|
|
44
|
-
return this.requestWithConfig<T>('DELETE', url, undefined, config);
|
|
45
|
-
}
|
|
46
|
-
return this.client.delete<T>(url);
|
|
44
|
+
return this.requestWithConfig<T>('DELETE', url, undefined, config);
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
async upload<T>(
|
|
50
48
|
url: string,
|
|
51
49
|
file: Blob,
|
|
52
50
|
onProgress?: (percent: number) => void,
|
|
51
|
+
options?: UploadOptions,
|
|
53
52
|
): Promise<T> {
|
|
53
|
+
const fullUrl = `${this.baseURL}${url}`;
|
|
54
|
+
const fieldName = options?.fieldName ?? 'file';
|
|
55
|
+
const fileName = options?.fileName ?? 'upload.bin';
|
|
56
|
+
|
|
57
|
+
if (onProgress && typeof XMLHttpRequest !== 'undefined') {
|
|
58
|
+
return this.uploadWithProgress<T>(fullUrl, file, onProgress, fieldName, fileName, options?.fields);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const form = new FormData();
|
|
62
|
+
form.append(fieldName, file, fileName);
|
|
63
|
+
if (options?.fields) {
|
|
64
|
+
for (const [k, v] of Object.entries(options.fields)) {
|
|
65
|
+
form.append(k, v);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
54
69
|
const controller = new AbortController();
|
|
55
70
|
this.abortControllers.add(controller);
|
|
56
71
|
|
|
57
72
|
try {
|
|
58
|
-
const
|
|
59
|
-
const res = await fetch(fullUrl, {
|
|
60
|
-
method: 'POST',
|
|
61
|
-
body: file,
|
|
62
|
-
signal: controller.signal,
|
|
63
|
-
});
|
|
64
|
-
|
|
73
|
+
const res = await fetch(fullUrl, { method: 'POST', body: form, signal: controller.signal });
|
|
65
74
|
if (!res.ok) {
|
|
75
|
+
this.onHttpError?.(res.status, res.statusText);
|
|
66
76
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
67
77
|
}
|
|
68
|
-
|
|
69
|
-
// Progress callback — since fetch does not natively support upload progress,
|
|
70
|
-
// we signal 100% once the response is received.
|
|
71
|
-
if (onProgress) {
|
|
72
|
-
onProgress(100);
|
|
73
|
-
}
|
|
74
|
-
|
|
78
|
+
onProgress?.(100);
|
|
75
79
|
return (await res.json()) as T;
|
|
76
80
|
} finally {
|
|
77
81
|
this.abortControllers.delete(controller);
|
|
78
82
|
}
|
|
79
83
|
}
|
|
80
84
|
|
|
85
|
+
private uploadWithProgress<T>(
|
|
86
|
+
fullUrl: string,
|
|
87
|
+
file: Blob,
|
|
88
|
+
onProgress: (percent: number) => void,
|
|
89
|
+
fieldName: string,
|
|
90
|
+
fileName: string,
|
|
91
|
+
fields?: Record<string, string>,
|
|
92
|
+
): Promise<T> {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const xhr = new XMLHttpRequest();
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
this.abortControllers.add(controller);
|
|
97
|
+
controller.signal.addEventListener('abort', () => xhr.abort(), { once: true });
|
|
98
|
+
|
|
99
|
+
xhr.open('POST', fullUrl);
|
|
100
|
+
xhr.upload.addEventListener('progress', (ev) => {
|
|
101
|
+
if (ev.lengthComputable && ev.total > 0) {
|
|
102
|
+
onProgress(Math.round((ev.loaded / ev.total) * 100));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
xhr.addEventListener('load', () => {
|
|
106
|
+
this.abortControllers.delete(controller);
|
|
107
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
108
|
+
onProgress(100);
|
|
109
|
+
try {
|
|
110
|
+
resolve(JSON.parse(xhr.responseText) as T);
|
|
111
|
+
} catch {
|
|
112
|
+
reject(new Error('Invalid JSON response'));
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
this.onHttpError?.(xhr.status, xhr.statusText);
|
|
116
|
+
reject(new Error(`HTTP ${xhr.status}: ${xhr.statusText}`));
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
xhr.addEventListener('error', () => {
|
|
120
|
+
this.abortControllers.delete(controller);
|
|
121
|
+
reject(new Error('Upload failed'));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const form = new FormData();
|
|
125
|
+
form.append(fieldName, file, fileName);
|
|
126
|
+
if (fields) {
|
|
127
|
+
for (const [k, v] of Object.entries(fields)) {
|
|
128
|
+
form.append(k, v);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
xhr.send(form);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
81
135
|
abortAll(): void {
|
|
82
136
|
this.client.abortAll();
|
|
83
137
|
this.abortControllers.forEach((c) => c.abort());
|
|
@@ -129,6 +183,10 @@ export class SomaHttpService implements IHttpClient {
|
|
|
129
183
|
const res = await fetch(fullUrl, options);
|
|
130
184
|
|
|
131
185
|
if (!res.ok) {
|
|
186
|
+
const body = typeof res.text === 'function'
|
|
187
|
+
? await res.text().catch(() => '')
|
|
188
|
+
: '';
|
|
189
|
+
this.onHttpError?.(res.status, body || res.statusText);
|
|
132
190
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
133
191
|
}
|
|
134
192
|
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type { II18n } from './II18n';
|
|
2
2
|
|
|
3
3
|
export class DefaultI18n implements II18n {
|
|
4
|
-
private
|
|
5
|
-
private messages: Map<string, Record<string, string>> = new Map();
|
|
4
|
+
private messages: Record<string, string> = {};
|
|
6
5
|
|
|
7
6
|
t(key: string, params?: Record<string, string | number>): string {
|
|
8
|
-
|
|
9
|
-
if (!localeMessages || !(key in localeMessages)) {
|
|
7
|
+
if (!(key in this.messages)) {
|
|
10
8
|
return key;
|
|
11
9
|
}
|
|
12
10
|
|
|
13
|
-
let result =
|
|
11
|
+
let result = this.messages[key];
|
|
14
12
|
|
|
15
13
|
if (params) {
|
|
16
14
|
for (const [param, value] of Object.entries(params)) {
|
|
@@ -21,21 +19,11 @@ export class DefaultI18n implements II18n {
|
|
|
21
19
|
return result;
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
getLocale(): string {
|
|
29
|
-
return this.locale;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
loadMessages(locale: string, messages: Record<string, string>): void {
|
|
33
|
-
const existing = this.messages.get(locale);
|
|
34
|
-
this.messages.set(locale, { ...existing, ...messages });
|
|
22
|
+
loadContent(messages: Record<string, string>): void {
|
|
23
|
+
this.messages = { ...this.messages, ...messages };
|
|
35
24
|
}
|
|
36
25
|
|
|
37
26
|
hasKey(key: string): boolean {
|
|
38
|
-
|
|
39
|
-
return localeMessages !== undefined && key in localeMessages;
|
|
27
|
+
return key in this.messages;
|
|
40
28
|
}
|
|
41
29
|
}
|
package/core/infra/i18n/II18n.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
/** Lookup for content strings loaded from locale.yaml (single dictionary, no locale switching) */
|
|
1
2
|
export interface II18n {
|
|
2
3
|
t(key: string, params?: Record<string, string | number>): string;
|
|
3
|
-
|
|
4
|
-
getLocale(): string;
|
|
5
|
-
loadMessages(locale: string, messages: Record<string, string>): void;
|
|
4
|
+
loadContent(messages: Record<string, string>): void;
|
|
6
5
|
hasKey(key: string): boolean;
|
|
7
6
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { DefaultNotifyService } from './DefaultNotifyService';
|
|
2
|
+
import type { NotifyLevel, NotifyOptions } from './INotifyService';
|
|
3
|
+
import type { ToastTheme } from './INotifyTheme';
|
|
4
|
+
|
|
5
|
+
const ROOT_ID = 'soma-toast-root';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 浏览器端通知:样式类名由宿主 app.config.yaml → notify.toast 注入(Tailwind)。
|
|
9
|
+
*/
|
|
10
|
+
export class DomNotifyService extends DefaultNotifyService {
|
|
11
|
+
private root: HTMLElement | null = null;
|
|
12
|
+
|
|
13
|
+
constructor(private readonly theme: ToastTheme) {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toast(options: NotifyOptions): void {
|
|
18
|
+
super.toast(options);
|
|
19
|
+
if (typeof document === 'undefined') {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this.renderToast(options);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async confirm(message: string, title?: string): Promise<boolean> {
|
|
26
|
+
if (typeof window !== 'undefined' && typeof window.confirm === 'function') {
|
|
27
|
+
const text = title ? `${title}\n\n${message}` : message;
|
|
28
|
+
return window.confirm(text);
|
|
29
|
+
}
|
|
30
|
+
return super.confirm(message, title);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async alert(message: string, title?: string): Promise<void> {
|
|
34
|
+
if (typeof window !== 'undefined' && typeof window.alert === 'function') {
|
|
35
|
+
const text = title ? `${title}\n\n${message}` : message;
|
|
36
|
+
window.alert(text);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
await super.alert(message, title);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private ensureRoot(): HTMLElement {
|
|
43
|
+
if (this.root && document.body.contains(this.root)) {
|
|
44
|
+
return this.root;
|
|
45
|
+
}
|
|
46
|
+
let root = document.getElementById(ROOT_ID);
|
|
47
|
+
if (!root) {
|
|
48
|
+
root = document.createElement('div');
|
|
49
|
+
root.id = ROOT_ID;
|
|
50
|
+
root.className = this.theme.root;
|
|
51
|
+
root.setAttribute('aria-live', 'polite');
|
|
52
|
+
root.setAttribute('aria-relevant', 'additions');
|
|
53
|
+
document.body.appendChild(root);
|
|
54
|
+
}
|
|
55
|
+
this.root = root;
|
|
56
|
+
return root;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private renderToast(options: NotifyOptions): void {
|
|
60
|
+
const level = options.level ?? 'info';
|
|
61
|
+
const duration = options.duration ?? 3000;
|
|
62
|
+
const root = this.ensureRoot();
|
|
63
|
+
const levelClass = this.theme.levels[level] ?? this.theme.levels.info;
|
|
64
|
+
|
|
65
|
+
const el = document.createElement('div');
|
|
66
|
+
el.className = [this.theme.item, levelClass].filter(Boolean).join(' ');
|
|
67
|
+
el.setAttribute('role', 'alert');
|
|
68
|
+
|
|
69
|
+
if (options.title) {
|
|
70
|
+
const titleEl = document.createElement('div');
|
|
71
|
+
titleEl.className = this.theme.title;
|
|
72
|
+
titleEl.textContent = options.title;
|
|
73
|
+
el.appendChild(titleEl);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const body = document.createElement('div');
|
|
77
|
+
body.className = this.theme.message;
|
|
78
|
+
body.setAttribute('data-soma-role', 'toast-message');
|
|
79
|
+
body.textContent = options.message;
|
|
80
|
+
el.appendChild(body);
|
|
81
|
+
|
|
82
|
+
root.appendChild(el);
|
|
83
|
+
|
|
84
|
+
requestAnimationFrame(() => {
|
|
85
|
+
el.classList.add(...this.theme.visible.split(/\s+/).filter(Boolean));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const dismiss = (): void => {
|
|
89
|
+
el.classList.remove(...this.theme.visible.split(/\s+/).filter(Boolean));
|
|
90
|
+
el.classList.add(...this.theme.leaving.split(/\s+/).filter(Boolean));
|
|
91
|
+
window.setTimeout(() => el.remove(), 280);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (duration > 0) {
|
|
95
|
+
window.setTimeout(dismiss, duration);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { NotifyLevel } from './INotifyService';
|
|
2
|
+
|
|
3
|
+
/** 宿主在 app.config.yaml 中声明的 Toast Tailwind 类名 */
|
|
4
|
+
export interface ToastTheme {
|
|
5
|
+
root: string;
|
|
6
|
+
item: string;
|
|
7
|
+
visible: string;
|
|
8
|
+
leaving: string;
|
|
9
|
+
title: string;
|
|
10
|
+
message: string;
|
|
11
|
+
levels: Record<NotifyLevel, string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isToastTheme(value: unknown): value is ToastTheme {
|
|
15
|
+
if (!value || typeof value !== 'object') return false;
|
|
16
|
+
const t = value as ToastTheme;
|
|
17
|
+
return typeof t.root === 'string'
|
|
18
|
+
&& typeof t.item === 'string'
|
|
19
|
+
&& typeof t.visible === 'string'
|
|
20
|
+
&& typeof t.leaving === 'string'
|
|
21
|
+
&& typeof t.levels?.info === 'string';
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RealtimeMessageHandler } from './IRealtimeTransport';
|
|
2
|
+
|
|
3
|
+
export interface RealtimeSubscribeOptions {
|
|
4
|
+
/** Auth token passed as query param */
|
|
5
|
+
token?: string | null;
|
|
6
|
+
/** Extra query params forwarded to the transport URL */
|
|
7
|
+
query?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Realtime push facade for domain code (SSE / WebSocket) */
|
|
11
|
+
export interface IRealtimeService {
|
|
12
|
+
subscribeSse(
|
|
13
|
+
channel: string,
|
|
14
|
+
handler: RealtimeMessageHandler,
|
|
15
|
+
options?: RealtimeSubscribeOptions,
|
|
16
|
+
): () => void;
|
|
17
|
+
subscribeWebSocket(
|
|
18
|
+
channel: string,
|
|
19
|
+
handler: RealtimeMessageHandler,
|
|
20
|
+
options?: RealtimeSubscribeOptions,
|
|
21
|
+
): () => void;
|
|
22
|
+
disconnectAll(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type RealtimeMessageHandler = (data: unknown) => void;
|
|
2
|
+
|
|
3
|
+
export interface IRealtimeTransport {
|
|
4
|
+
readonly kind: 'sse' | 'websocket';
|
|
5
|
+
connect(url: string): void;
|
|
6
|
+
disconnect(): void;
|
|
7
|
+
onMessage(handler: RealtimeMessageHandler): () => void;
|
|
8
|
+
isConnected(): boolean;
|
|
9
|
+
}
|