soma-core 1.0.1 → 1.0.2
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 +336 -0
- package/app/api/[...path]/route.ts +9 -85
- package/app/api/page/route.ts +18 -16
- package/app/api/soma-manifest/route.ts +39 -0
- package/app/components/SomaHydrationShell.tsx +23 -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 +14 -2
- package/core/api/IService.ts +6 -10
- package/core/api/load-host-service.ts +72 -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 +23 -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 +72 -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 +9 -11
- 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/resolve-host-bootstrap.ts +28 -0
- package/core/host/soma-bootstrap-host.stub.ts +4 -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 +8968 -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 +35 -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/resolve-host-bootstrap.d.ts +11 -0
- package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -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 +19 -0
- package/next.config.js +9 -4
- package/package.json +53 -16
- package/tailwind.config.ts +26 -7
- package/tsconfig.json +6 -2
- package/core/router/RouteHandler.ts +0 -93
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { IRealtimeService, RealtimeSubscribeOptions } from './IRealtimeService';
|
|
2
|
+
import type { IRealtimeTransport, RealtimeMessageHandler } from './IRealtimeTransport';
|
|
3
|
+
import { SseRealtimeTransport } from './SseRealtimeTransport';
|
|
4
|
+
import { WebSocketRealtimeTransport } from './WebSocketRealtimeTransport';
|
|
5
|
+
|
|
6
|
+
export interface SomaRealtimeServiceOptions {
|
|
7
|
+
sseBasePath?: string;
|
|
8
|
+
wsBaseUrl?: string;
|
|
9
|
+
tokenProvider?: () => string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class SomaRealtimeService implements IRealtimeService {
|
|
13
|
+
private readonly sseBasePath: string;
|
|
14
|
+
private readonly wsBaseUrl: string;
|
|
15
|
+
private readonly tokenProvider?: () => string | null;
|
|
16
|
+
private readonly active = new Set<IRealtimeTransport>();
|
|
17
|
+
private readonly unsubscribers: Array<() => void> = [];
|
|
18
|
+
|
|
19
|
+
constructor(options?: SomaRealtimeServiceOptions) {
|
|
20
|
+
this.sseBasePath = options?.sseBasePath ?? '';
|
|
21
|
+
this.wsBaseUrl = options?.wsBaseUrl ?? '';
|
|
22
|
+
this.tokenProvider = options?.tokenProvider;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
subscribeSse(
|
|
26
|
+
channel: string,
|
|
27
|
+
handler: RealtimeMessageHandler,
|
|
28
|
+
options?: RealtimeSubscribeOptions,
|
|
29
|
+
): () => void {
|
|
30
|
+
if (!this.sseBasePath) {
|
|
31
|
+
return () => {};
|
|
32
|
+
}
|
|
33
|
+
const transport = new SseRealtimeTransport();
|
|
34
|
+
this.track(transport);
|
|
35
|
+
const url = this.buildSseUrl(channel, options);
|
|
36
|
+
transport.connect(url);
|
|
37
|
+
const off = transport.onMessage(handler);
|
|
38
|
+
return () => {
|
|
39
|
+
off();
|
|
40
|
+
transport.disconnect();
|
|
41
|
+
this.active.delete(transport);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private buildSseUrl(channel: string, options?: RealtimeSubscribeOptions): string {
|
|
46
|
+
const params = new URLSearchParams({ channel });
|
|
47
|
+
const token = options?.token ?? this.tokenProvider?.() ?? null;
|
|
48
|
+
if (token) params.set('token', token);
|
|
49
|
+
if (options?.query) {
|
|
50
|
+
for (const [k, v] of Object.entries(options.query)) {
|
|
51
|
+
params.set(k, v);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return `${this.sseBasePath}?${params.toString()}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
subscribeWebSocket(
|
|
58
|
+
channel: string,
|
|
59
|
+
handler: RealtimeMessageHandler,
|
|
60
|
+
options?: RealtimeSubscribeOptions,
|
|
61
|
+
): () => void {
|
|
62
|
+
if (!this.wsBaseUrl) {
|
|
63
|
+
return () => {};
|
|
64
|
+
}
|
|
65
|
+
const transport = new WebSocketRealtimeTransport({ reconnect: true, heartbeatMs: 25000 });
|
|
66
|
+
this.track(transport);
|
|
67
|
+
const token = options?.token ?? this.tokenProvider?.() ?? null;
|
|
68
|
+
const url = new URL(this.wsBaseUrl);
|
|
69
|
+
url.searchParams.set('channel', channel);
|
|
70
|
+
if (token) url.searchParams.set('token', token);
|
|
71
|
+
if (options?.query) {
|
|
72
|
+
for (const [k, v] of Object.entries(options.query)) {
|
|
73
|
+
url.searchParams.set(k, v);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
transport.connect(url.toString());
|
|
77
|
+
const off = transport.onMessage(handler);
|
|
78
|
+
return () => {
|
|
79
|
+
off();
|
|
80
|
+
transport.disconnect();
|
|
81
|
+
this.active.delete(transport);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
disconnectAll(): void {
|
|
86
|
+
for (const off of this.unsubscribers) off();
|
|
87
|
+
this.unsubscribers.length = 0;
|
|
88
|
+
for (const t of this.active) t.disconnect();
|
|
89
|
+
this.active.clear();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private track(transport: IRealtimeTransport): void {
|
|
93
|
+
this.active.add(transport);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IRealtimeTransport, RealtimeMessageHandler } from './IRealtimeTransport';
|
|
2
|
+
|
|
3
|
+
/** 浏览器 SSE 实时传输 */
|
|
4
|
+
export class SseRealtimeTransport implements IRealtimeTransport {
|
|
5
|
+
readonly kind = 'sse' as const;
|
|
6
|
+
private source: EventSource | null = null;
|
|
7
|
+
private handlers = new Set<RealtimeMessageHandler>();
|
|
8
|
+
|
|
9
|
+
connect(url: string): void {
|
|
10
|
+
this.disconnect();
|
|
11
|
+
if (typeof EventSource === 'undefined') return;
|
|
12
|
+
this.source = new EventSource(url);
|
|
13
|
+
this.source.onmessage = (ev) => {
|
|
14
|
+
try {
|
|
15
|
+
const data = JSON.parse(ev.data);
|
|
16
|
+
for (const h of this.handlers) h(data);
|
|
17
|
+
} catch {
|
|
18
|
+
for (const h of this.handlers) h(ev.data);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
disconnect(): void {
|
|
24
|
+
this.source?.close();
|
|
25
|
+
this.source = null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
onMessage(handler: RealtimeMessageHandler): () => void {
|
|
29
|
+
this.handlers.add(handler);
|
|
30
|
+
return () => this.handlers.delete(handler);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isConnected(): boolean {
|
|
34
|
+
return this.source !== null && this.source.readyState !== EventSource.CLOSED;
|
|
35
|
+
}
|
|
36
|
+
}
|