soma-kit 0.1.11 → 0.1.12
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/README.md +103 -110
- package/bin/soma-kit.mjs +1 -1
- package/bin/soma.mjs +22 -0
- package/package.json +13 -7
- package/packs/finance/reconcile-board.ts +5 -2
- package/packs/finance/reconcile-desk/compile.ts +3 -2
- package/spec/generated/kit-app.schema.json +776 -38
- package/spec/generated/kit-page.schema.json +1613 -224
- package/spec/kit-grammar.json +770 -529
- package/spec/kit-grammar.schema.json +1962 -392
- package/src/app/app-config.ts +371 -347
- package/src/app/component-gate.ts +12 -15
- package/src/app/compose-apps.ts +79 -8
- package/src/app/define-kit-actions.ts +245 -41
- package/src/app/define-kit-app.ts +174 -31
- package/src/app/define-kit-view.ts +100 -0
- package/src/app/envelope.ts +1 -161
- package/src/app/index.ts +13 -2
- package/src/app/kit-view-context.ts +290 -0
- package/src/app/notify-renderer.ts +1 -115
- package/src/app/paged-list-adapter.ts +1 -240
- package/src/app/server-pages.ts +8 -2
- package/src/app/server.ts +491 -101
- package/src/app/submit.types.ts +1 -1
- package/src/app/theme-endpoint.ts +63 -0
- package/src/app/theme-runtime-shared.ts +1 -0
- package/src/app/theme-runtime.ts +663 -0
- package/src/base/action-busy.ts +2 -2
- package/src/base/action-gate.ts +4 -255
- package/src/base/action-slots.ts +47 -0
- package/src/base/alert.ts +5 -3
- package/src/base/avatar.ts +2 -2
- package/src/base/button.ts +46 -29
- package/src/base/cascader.ts +11 -12
- package/src/base/check-core.ts +1 -1
- package/src/base/choice.ts +28 -26
- package/src/base/class-candidates.ts +17 -0
- package/src/base/color-picker.ts +6 -6
- package/src/base/combobox.ts +5 -3
- package/src/base/command-palette.ts +9 -5
- package/src/base/countdown.ts +1 -1
- package/src/base/date-range.ts +25 -11
- package/src/base/date.ts +85 -17
- package/src/base/divider.ts +2 -2
- package/src/base/fab.ts +11 -4
- package/src/base/image.ts +1 -1
- package/src/base/index.ts +9 -3
- package/src/base/input.ts +42 -13
- package/src/base/kbd.ts +3 -1
- package/src/base/link.ts +4 -4
- package/src/base/listbox-core.ts +50 -30
- package/src/base/loading-gate.ts +133 -0
- package/src/base/mentions.ts +5 -5
- package/src/base/menu-button.ts +6 -1
- package/src/base/menu-core.ts +4 -0
- package/src/base/motion/aura.ts +2 -2
- package/src/base/motion/hover-3d.ts +2 -2
- package/src/base/motion/hover-gallery.ts +2 -2
- package/src/base/motion/marquee.ts +2 -2
- package/src/base/motion/text-rotate.ts +2 -2
- package/src/base/number.ts +93 -80
- package/src/base/otp.ts +4 -1
- package/src/base/permission-gate.ts +6 -0
- package/src/base/popover.ts +40 -8
- package/src/base/popup.ts +79 -14
- package/src/base/price-text.ts +2 -2
- package/src/base/progress.ts +2 -2
- package/src/base/qr-code.ts +4 -4
- package/src/base/qty-stepper.ts +9 -3
- package/src/base/range.ts +2 -2
- package/src/base/rating.ts +1 -1
- package/src/base/segmented.ts +4 -0
- package/src/base/select.ts +4 -3
- package/src/base/shared.ts +221 -29
- package/src/base/swap.ts +4 -1
- package/src/base/switch.ts +1 -1
- package/src/base/tag-input.ts +14 -10
- package/src/base/tag.ts +5 -1
- package/src/base/text.ts +39 -6
- package/src/base/textarea.ts +37 -11
- package/src/base/time-picker.ts +12 -9
- package/src/base/tree-select.ts +15 -8
- package/src/base/upload.ts +34 -5
- package/src/base/virtual-list.ts +107 -1
- package/src/biz/approval-desk/compile.ts +23 -41
- package/src/biz/auth-panel/compile.ts +37 -52
- package/src/biz/auth-panel/index.ts +1 -1
- package/src/biz/category-crud/compile.ts +22 -36
- package/src/biz/checkout-page/compile.ts +21 -5
- package/src/biz/compose.ts +13 -27
- package/src/biz/define.ts +5 -4
- package/src/biz/import-wizard/compile.ts +22 -26
- package/src/biz/import-wizard/hub.ts +126 -61
- package/src/biz/import-wizard/shell.ts +3 -3
- package/src/biz/index.ts +3 -3
- package/src/biz/landing-page/compile.ts +19 -3
- package/src/biz/link/hub.ts +49 -20
- package/src/biz/link/shared.ts +115 -118
- package/src/biz/master-sub/compile.ts +31 -44
- package/src/biz/multi-board/compile.ts +34 -49
- package/src/biz/multi-board/index.ts +18 -1
- package/src/biz/multi-board/shell.ts +24 -15
- package/src/biz/notification-center/compile.ts +21 -33
- package/src/biz/page-templates/domain-templates.ts +80 -13
- package/src/biz/page-templates/result-page/compile.ts +11 -2
- package/src/biz/page-templates/shared.ts +34 -36
- package/src/biz/record-detail/compile.ts +47 -42
- package/src/biz/record-detail/index.ts +11 -1
- package/src/biz/settings-center/compile.ts +6 -5
- package/src/biz/shared/action-host.ts +91 -211
- package/src/biz/shared/compile-helpers.ts +80 -0
- package/src/biz/shared/coordinator.ts +486 -83
- package/src/biz/shared/index.ts +8 -2
- package/src/biz/shared/overlay-shell.ts +80 -0
- package/src/biz/shared/pipes.ts +43 -30
- package/src/biz/shared/remove-key.ts +6 -0
- package/src/biz/shared/remove-wire.ts +29 -0
- package/src/biz/shared/runtime-hub.ts +216 -24
- package/src/biz/single-form/compile.ts +15 -37
- package/src/biz/standard-crud/compile.ts +62 -107
- package/src/biz/standard-crud/shell.ts +16 -66
- package/src/biz/step-form/compile.ts +26 -97
- package/src/biz/step-form/hub.ts +34 -41
- package/src/biz/types.ts +4 -3
- package/src/cli/core-imports.ts +54 -0
- package/src/cli/kit-cli.ts +130 -26
- package/src/cli/kit-init.ts +222 -0
- package/src/cli/service-endpoint-check.ts +425 -0
- package/src/cli/source-path-check.d.mts +17 -0
- package/src/cli/source-path-check.mjs +334 -0
- package/src/cli/view-writes.ts +752 -0
- package/src/compile/action-host-attach.ts +21 -29
- package/src/compile/closed-set.ts +509 -0
- package/src/compile/compile-page.ts +219 -72
- package/src/compile/component-candidates.ts +1 -1
- package/src/compile/hints.ts +30 -1
- package/src/compile/index.ts +1 -0
- package/src/compile/kit-error.ts +1 -0
- package/src/compile/locate.ts +21 -1
- package/src/compile/parse.ts +370 -41
- package/src/compile/redirect.ts +6 -2
- package/src/compile/styles.ts +35 -2
- package/src/compile/types.ts +18 -2
- package/src/compile/view-check.ts +151 -0
- package/src/data/amount-breakdown.ts +2 -2
- package/src/data/anchor-nav.ts +2 -2
- package/src/data/approval-flow.ts +2 -2
- package/src/data/assignment-picker.ts +40 -29
- package/src/data/async-result-panel.ts +62 -6
- package/src/data/breadcrumbs.ts +2 -2
- package/src/data/calendar.ts +22 -25
- package/src/data/carousel.ts +5 -2
- package/src/data/chart.ts +42 -34
- package/src/data/checklist.ts +2 -2
- package/src/data/code-view.ts +4 -2
- package/src/data/comment-thread.ts +5 -2
- package/src/data/confirm-dialog.ts +10 -0
- package/src/data/context-menu.ts +29 -37
- package/src/data/d9-util.ts +2 -18
- package/src/data/diff.ts +4 -2
- package/src/data/dock.ts +2 -2
- package/src/data/embed.ts +2 -1
- package/src/data/field-mapper.ts +31 -26
- package/src/data/flow-editor-contract.ts +1 -1
- package/src/data/flow-editor.ts +89 -100
- package/src/data/flow-node-options.ts +90 -0
- package/src/data/flow-remote-options.ts +0 -103
- package/src/data/footer.ts +2 -2
- package/src/data/form-draft-registry.ts +2 -4
- package/src/data/gantt.ts +14 -17
- package/src/data/hero.ts +2 -2
- package/src/data/image-viewer.ts +20 -26
- package/src/data/index.ts +0 -2
- package/src/data/item-list.ts +31 -22
- package/src/data/kanban.ts +43 -42
- package/src/data/media-grid.ts +14 -19
- package/src/data/menu.ts +2 -2
- package/src/data/mockup.ts +2 -2
- package/src/data/modal-dialog.ts +22 -18
- package/src/data/navbar.ts +7 -2
- package/src/data/notify-renderer.ts +235 -0
- package/src/data/object-form.ts +1036 -648
- package/src/data/pager.ts +65 -29
- package/src/data/permission-matrix.ts +20 -21
- package/src/data/permission-tree.ts +28 -26
- package/src/data/pricing-table.ts +9 -3
- package/src/data/reaction-bar.ts +77 -88
- package/src/data/read-rollback.ts +73 -0
- package/src/data/record-card.ts +3 -3
- package/src/data/record-picker.ts +93 -50
- package/src/data/redirect.ts +32 -32
- package/src/data/remote-options.ts +13 -6
- package/src/data/request-console.ts +5 -3
- package/src/data/resident.ts +2 -1
- package/src/data/result-panel.ts +4 -2
- package/src/data/rich-text.ts +45 -55
- package/src/data/row-editor.ts +285 -52
- package/src/data/rule-builder.ts +18 -12
- package/src/data/shape-util.ts +4 -0
- package/src/data/stat-group.ts +243 -101
- package/src/data/static-registry.ts +22 -11
- package/src/data/steps.ts +2 -2
- package/src/data/survey.ts +24 -29
- package/src/data/system-switcher.ts +2 -2
- package/src/data/timeline.ts +2 -2
- package/src/data/tour-guide.ts +22 -29
- package/src/data/transfer.ts +27 -23
- package/src/data/tree-table.ts +96 -48
- package/src/data/tree.ts +27 -34
- package/src/data/trend-line.ts +6 -24
- package/src/data/virtual-rows.ts +118 -76
- package/src/data/watermark.ts +2 -2
- package/src/data/wrap.ts +63 -38
- package/src/frames/blank-frame.ts +1 -0
- package/src/frames/index.ts +2 -2
- package/src/frames/page-bar.ts +12 -11
- package/src/frames/page-header.ts +14 -5
- package/src/frames/page-status.ts +2 -2
- package/src/frames/shared.ts +23 -17
- package/src/frames/single-frame.ts +1 -0
- package/src/frames/split-frame/shell.ts +5 -5
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +2 -13
- package/src/frames/tabs-frame/wiring.ts +4 -8
- package/src/frames/top-main-frame.ts +1 -0
- package/src/frames/types.ts +13 -2
- package/src/frames/workspace-frame/extras.ts +20 -4
- package/src/frames/workspace-frame/index.ts +1 -1
- package/src/frames/workspace-frame/shell.ts +1 -0
- package/src/frames/workspace-frame/state.ts +13 -6
- package/src/frames/workspace-frame/view.ts +119 -25
- package/src/generated/kit-grammar-runtime.ts +430 -366
- package/src/generated/page-origin.ts +0 -11
- package/src/i18n/kit-text.ts +23 -27
- package/src/index.ts +2 -1
- package/src/object-renderer/action-gate.ts +11 -147
- package/src/object-renderer/arrange.ts +71 -77
- package/src/object-renderer/collapse-wiring.ts +2 -2
- package/src/object-renderer/collection-expand.ts +2 -2
- package/src/object-renderer/collection-parts.ts +216 -100
- package/src/object-renderer/collection-table.ts +180 -42
- package/src/object-renderer/collection.ts +95 -79
- package/src/object-renderer/compile-data.ts +85 -100
- package/src/object-renderer/crud-bridge.ts +32 -0
- package/src/object-renderer/defaults.ts +8 -14
- package/src/object-renderer/display-checks.ts +3 -24
- package/src/object-renderer/emit-events.ts +186 -172
- package/src/object-renderer/emit-object.ts +15 -2
- package/src/object-renderer/emit-static.ts +174 -132
- package/src/object-renderer/field-value.ts +593 -396
- package/src/object-renderer/filter-reload.ts +70 -0
- package/src/object-renderer/form-detail.ts +112 -2
- package/src/object-renderer/index.ts +26 -0
- package/src/object-renderer/key-field.ts +12 -0
- package/src/object-renderer/linkage.ts +7 -270
- package/src/object-renderer/merge-class.ts +18 -1
- package/src/object-renderer/path-pick.ts +27 -0
- package/src/object-renderer/render-object.ts +405 -86
- package/src/object-renderer/request-actions.ts +230 -249
- package/src/object-renderer/table-actions.ts +195 -95
- package/src/object-renderer/table-summary.ts +2 -2
- package/src/object-renderer/table-tools.ts +418 -84
- package/src/object-renderer/types.ts +128 -122
- package/src/object-renderer/view-binding.ts +196 -0
- package/src/page-target/ai-surface.ts +37 -0
- package/src/page-target/compile.ts +48 -8
- package/src/page-target/index.ts +8 -1
- package/src/page-target/intent-schema.ts +154 -28
- package/src/page-target/target.ts +257 -50
- package/src/page-target/writer-policy.ts +14 -3
- package/src/recipes/container.ts +165 -0
- package/src/recipes/control.ts +219 -0
- package/src/recipes/index.ts +263 -0
- package/src/recipes/overlay.ts +182 -0
- package/src/recipes/state.ts +70 -0
- package/src/recipes/table-row.ts +51 -0
- package/src/recipes/typography.ts +87 -0
- package/src/shared/action-field-guard.ts +41 -0
- package/src/shared/envelope.ts +207 -0
- package/src/shared/escape.ts +57 -0
- package/src/shared/kit-error.ts +21 -0
- package/src/shared/paged-list-adapter.ts +240 -0
- package/src/shared/path-guard.ts +127 -0
- package/src/shared/theme-runtime-shared.ts +175 -0
- package/src/shared/view-info.ts +10 -0
- package/src/theme/index.ts +12 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -1
- package/src/theme/kit-theme.tpl +38 -1
- package/src/theme/nine-dim-structure.ts +77 -0
- package/src/theme/surface-layers.ts +97 -0
- package/src/theme/theme-tokens.ts +85 -79
- package/src/theme/theme-types.ts +71 -0
- package/src/theme/typography.ts +17 -17
- package/src/biz/shared/step-values.ts +0 -25
- package/src/biz/step-form/steps-ref.ts +0 -112
- package/src/data/options-bus.ts +0 -24
- package/src/data/prompt-dialog.ts +0 -235
- package/src/object-renderer/adopt-gate.ts +0 -333
- package/src/object-renderer/field-policy.ts +0 -89
- package/src/object-renderer/outcome-draft.ts +0 -61
- package/src/theme/nine-dim-palettes.ts +0 -1473
|
@@ -16,6 +16,8 @@ import { KIT_STATIC_BUILTIN_LAZY_NAMES } from '../data/static-lazy';
|
|
|
16
16
|
import { KIT_RESIDENT_COMPONENT_NAMES, wrappedCandidateSource } from '../data/wrapped-candidates';
|
|
17
17
|
import { checkClasses } from '../object-renderer/merge-class';
|
|
18
18
|
import type { KitDiagnostic } from '../object-renderer/types';
|
|
19
|
+
import { kitFail } from '../compile/kit-error';
|
|
20
|
+
import { getKitControllerSource, isKitControllerFactory } from '../base/shared';
|
|
19
21
|
|
|
20
22
|
/** declareKitUses 允许引用的内置组件全集 */
|
|
21
23
|
export const VALID_KIT_USES_NAMES: ReadonlySet<string> = new Set<string>([
|
|
@@ -118,38 +120,33 @@ export function findForbiddenNativeControls(text: string): Array<{ line: number;
|
|
|
118
120
|
export function assertSinglePathComponent(name: string, def: SomaComponentDefinition): void {
|
|
119
121
|
const meta = wrappedCandidateSource(def);
|
|
120
122
|
if (meta === undefined) {
|
|
121
|
-
|
|
122
|
-
`[soma-kit] [KIT-APP-004] 应用组件 ${name} 未经唯一接入路径声明:必须通过 wrapKitDataComponent / composeKitDataComponents 包装组合,或经 declareKitUses(${name}, uses, candidates) 显式登记依赖组件与候选类`,
|
|
123
|
-
);
|
|
123
|
+
kitFail('KIT-APP-004', `components.${name}`, `应用组件 ${name} 未经唯一接入路径声明:必须通过 wrapKitDataComponent / composeKitDataComponents 包装组合,或经 declareKitUses(${name}, uses, candidates) 显式登记依赖组件与候选类`, `用 wrapKitDataComponent 包装,或在定义处调用 declareKitUses(${name}, uses, candidates)`);
|
|
124
124
|
}
|
|
125
125
|
for (const used of meta.uses ?? []) {
|
|
126
126
|
if (!VALID_KIT_USES_NAMES.has(used)) {
|
|
127
|
-
|
|
128
|
-
`[soma-kit] [KIT-APP-004] 应用组件 ${name} 在 declareKitUses 中声明了未知的内置组件 "${used}"`,
|
|
129
|
-
);
|
|
127
|
+
kitFail('KIT-APP-004', `components.${name}.uses`, `应用组件 ${name} 在 declareKitUses 中声明了未知的内置组件 "${used}"`, '依赖组件名必须是内置组件闭集里的名字');
|
|
130
128
|
}
|
|
131
129
|
}
|
|
132
130
|
for (const tok of meta.own) {
|
|
133
131
|
const bad = checkClasses(tok, `${name}.candidates`);
|
|
134
132
|
if (bad.length > 0) {
|
|
135
|
-
|
|
136
|
-
`[soma-kit] [KIT-APP-004] 应用组件 ${name} 的候选类 "${tok}" 非法:${bad[0]!.message}`,
|
|
137
|
-
);
|
|
133
|
+
kitFail('KIT-APP-004', `components.${name}.candidates`, `应用组件 ${name} 的候选类 "${tok}" 非法:${bad[0]!.message}`, '候选类只能用刻度内的 Tailwind 类,不写方括号任意值');
|
|
138
134
|
}
|
|
139
135
|
}
|
|
136
|
+
if (typeof def.controllerFactory !== 'function' || !isKitControllerFactory(def.controllerFactory)) {
|
|
137
|
+
kitFail('KIT-APP-014', `components.${name}.controllerFactory`, `应用组件 ${name} 的 controllerFactory 必须由 createKitController((api) => ({ ... })) 生成(不接受纯声明式无控制器组件或手写工厂)`, '使用 createKitController((api) => ({ render(root, p, api) { ... } })) 定义控制器');
|
|
138
|
+
}
|
|
140
139
|
if (typeof def.templateHtml === 'string' && /\sstyle\s*=/i.test(def.templateHtml)) {
|
|
141
|
-
|
|
142
|
-
`[soma-kit] [KIT-APP-005] 应用组件 ${name} 的 templateHtml 含内联 style 属性;样式必须统一使用 Tailwind 候选类`,
|
|
143
|
-
);
|
|
140
|
+
kitFail('KIT-APP-005', `components.${name}.templateHtml`, `应用组件 ${name} 的 templateHtml 含内联 style 属性;样式必须统一使用 Tailwind 候选类`, '删掉 style 属性,改用 class 里的 Tailwind 候选类');
|
|
144
141
|
}
|
|
145
142
|
const tplHits = typeof def.templateHtml === 'string' ? findForbiddenNativeControls(def.templateHtml) : [];
|
|
146
143
|
if (tplHits.length > 0) {
|
|
147
|
-
|
|
144
|
+
kitFail('KIT-APP-005', `components.${name}.templateHtml`, `应用组件 ${name} 的 templateHtml 违规:${tplHits[0]!.message}`, '改用 soma-kit 标准组件(Select / Slider / DatePicker 等),不手写原生控件');
|
|
148
145
|
}
|
|
149
146
|
if (typeof def.controllerFactory === 'function') {
|
|
150
|
-
const fnHits = findForbiddenNativeControls(
|
|
147
|
+
const fnHits = findForbiddenNativeControls(getKitControllerSource(def.controllerFactory));
|
|
151
148
|
if (fnHits.length > 0) {
|
|
152
|
-
|
|
149
|
+
kitFail('KIT-APP-005', `components.${name}.controller`, `应用组件 ${name} 的控制器违规:${fnHits[0]!.message}`, '控制器里不要创建原生控件,改用 soma-kit 标准组件');
|
|
153
150
|
}
|
|
154
151
|
}
|
|
155
152
|
}
|
package/src/app/compose-apps.ts
CHANGED
|
@@ -15,9 +15,22 @@ import { kitText } from '../i18n/kit-text';
|
|
|
15
15
|
import { kitActionExtras } from '../biz/shared/runtime-hub';
|
|
16
16
|
import { kitPermissionAllows } from '../base/permission-gate';
|
|
17
17
|
import type { DefineKitAppOptions } from './define-kit-app';
|
|
18
|
+
import type { KitViewClass } from './define-kit-view';
|
|
19
|
+
import { KitDiagnosticError, kitFail } from '../compile/kit-error';
|
|
18
20
|
|
|
19
21
|
type Grant = ReturnType<NonNullable<DefineKitAppOptions['pageGrants']>>[number];
|
|
20
22
|
type ExclusiveKey = 'auth' | 'eventMiddleware' | 'login';
|
|
23
|
+
type FileLayoutKey = 'root' | 'app' | 'pagesDir' | 'extraPagesDirs' | 'services' | 'defaultTheme';
|
|
24
|
+
type ComposedOptionKey =
|
|
25
|
+
| 'appYaml' | 'devFallback' | 'dicts' | 'entities' | 'packs'
|
|
26
|
+
| 'baseComponents' | 'staticObjectComponents' | 'actions' | 'requestTimeout' | 'views'
|
|
27
|
+
| 'bizComponents' | 'frames' | 'overrides' | 'appCandidates'
|
|
28
|
+
| 'appName' | 'title' | 'locales' | 'defaultLocale' | 'auth' | 'eventMiddleware'
|
|
29
|
+
| 'renderPorts' | 'identity' | 'defaultAccess' | 'pageGrants' | 'requestHook'
|
|
30
|
+
| 'startup' | 'responseHeaders' | 'reservedRoutes' | 'login';
|
|
31
|
+
type UnhandledOptionKey = Exclude<keyof DefineKitAppOptions, FileLayoutKey | ComposedOptionKey>;
|
|
32
|
+
type AssertNever<T extends never> = T;
|
|
33
|
+
type EveryOptionHasACompositionPolicy = AssertNever<UnhandledOptionKey>;
|
|
21
34
|
|
|
22
35
|
export interface ComposeKitAppsOptions {
|
|
23
36
|
/** 基座独占、叠加不得再写的选项键;缺省 auth / eventMiddleware / login */
|
|
@@ -34,14 +47,56 @@ export function mergeKitTables<T>(label: string, ...tables: ReadonlyArray<Readon
|
|
|
34
47
|
const dup: string[] = [];
|
|
35
48
|
for (const table of tables) {
|
|
36
49
|
for (const [name, value] of Object.entries(table ?? {})) {
|
|
37
|
-
if (name
|
|
38
|
-
else out
|
|
50
|
+
if (Object.hasOwn(out, name)) dup.push(name);
|
|
51
|
+
else Object.defineProperty(out, name, { value, enumerable: true, configurable: true, writable: true });
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
|
-
if (dup.length > 0)
|
|
54
|
+
if (dup.length > 0) kitFail('KIT-APP-012', label, `${label}重名:${[...new Set(dup)].join('、')}(不提供覆盖)`, '给叠加应用里的同名项改名');
|
|
42
55
|
return out;
|
|
43
56
|
}
|
|
44
57
|
|
|
58
|
+
function normalizeViewTable(views: DefineKitAppOptions['views'], side: string): Record<string, KitViewClass> {
|
|
59
|
+
const out: Record<string, KitViewClass> = {};
|
|
60
|
+
const entries: Array<[string | undefined, KitViewClass]> = Array.isArray(views)
|
|
61
|
+
? (views as readonly KitViewClass[]).map((cls) => [undefined, cls])
|
|
62
|
+
: Object.entries(views ?? {}) as Array<[string, KitViewClass]>;
|
|
63
|
+
for (const [declaredName, cls] of entries) {
|
|
64
|
+
const name = (cls as { viewName?: unknown } | null)?.viewName;
|
|
65
|
+
if (typeof name !== 'string' || name === '') {
|
|
66
|
+
kitFail('KIT-VIEW-005', `views.${String(declaredName ?? '')}`, `${side}的展示对象缺少有效 viewName`, '用 defineKitView 定义后再登记');
|
|
67
|
+
}
|
|
68
|
+
if (declaredName !== undefined && declaredName !== name) {
|
|
69
|
+
kitFail('KIT-VIEW-005', `views.${declaredName}`, `${side}展示对象登记名 ${declaredName} 与 viewName ${name} 不一致`, '登记键必须等于 defineKitView 的 viewName');
|
|
70
|
+
}
|
|
71
|
+
if (Object.hasOwn(out, name)) {
|
|
72
|
+
kitFail('KIT-VIEW-005', `views.${name}`, `${side}重复登记展示对象 ${name}`, '同一个展示对象只登记一次');
|
|
73
|
+
}
|
|
74
|
+
Object.defineProperty(out, name, { value: cls, enumerable: true, configurable: true, writable: true });
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function mergeKitPacks(base: DefineKitAppOptions['packs'], overlay: DefineKitAppOptions['packs']): NonNullable<DefineKitAppOptions['packs']> | undefined {
|
|
80
|
+
const out: NonNullable<DefineKitAppOptions['packs']>[number][] = [];
|
|
81
|
+
const byName = new Map<string, NonNullable<DefineKitAppOptions['packs']>[number]>();
|
|
82
|
+
for (const pack of [...(base ?? []), ...(overlay ?? [])]) {
|
|
83
|
+
if (pack === null || typeof pack !== 'object' || typeof pack.name !== 'string') {
|
|
84
|
+
out.push(pack);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const previous = byName.get(pack.name);
|
|
88
|
+
if (previous !== undefined) {
|
|
89
|
+
if (previous !== pack) {
|
|
90
|
+
kitFail('KIT-PACK-003', `packs.${pack.name}`, `领域包名 ${pack.name} 对应不同的包实例`, '基座与叠加应用的领域包名称不得冲突');
|
|
91
|
+
}
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
byName.set(pack.name, pack);
|
|
95
|
+
out.push(pack);
|
|
96
|
+
}
|
|
97
|
+
return out.length === 0 ? undefined : out;
|
|
98
|
+
}
|
|
99
|
+
|
|
45
100
|
/**
|
|
46
101
|
* 合并选项(不含 root / app / pagesDir / extraPagesDirs / services / defaultTheme:那几项取决于文件布局,由应用自己的装配补)。
|
|
47
102
|
* `base` 取自基座 `soma.app.ts` 默认导出的 `.options`,原样不改。
|
|
@@ -51,13 +106,14 @@ export function composeKitApps(base: DefineKitAppOptions, overlay: DefineKitAppO
|
|
|
51
106
|
const names = opts.names ?? { base: '基座', overlay: '叠加应用' };
|
|
52
107
|
const exclusive = opts.exclusive ?? ['auth', 'eventMiddleware', 'login'];
|
|
53
108
|
const bad = exclusive.filter((k) => overlay[k] !== undefined);
|
|
54
|
-
if (bad.length > 0) throw new
|
|
109
|
+
if (bad.length > 0) throw new KitDiagnosticError({ code: 'KIT-APP-012', path: bad.join(' / '), message: `${names.overlay}不得设置 ${bad.join(' / ')}:由${names.base}持有`, hint: `从${names.overlay}的 defineKitApp 选项里删掉 ${bad.join(' / ')}` }, tag);
|
|
55
110
|
|
|
56
111
|
const table = <T>(label: string, a: Readonly<Record<string, T>> | undefined, b: Readonly<Record<string, T>> | undefined): Record<string, T> => {
|
|
57
112
|
try {
|
|
58
113
|
return mergeKitTables(label, a, b);
|
|
59
114
|
} catch (e) {
|
|
60
|
-
|
|
115
|
+
const d = (e as KitDiagnosticError).diagnostics[0]!;
|
|
116
|
+
throw new KitDiagnosticError({ ...d, message: `${d.message}(${names.base}已登记同名项)` }, tag);
|
|
61
117
|
}
|
|
62
118
|
};
|
|
63
119
|
const merged: DefineKitAppOptions = {
|
|
@@ -66,13 +122,28 @@ export function composeKitApps(base: DefineKitAppOptions, overlay: DefineKitAppO
|
|
|
66
122
|
actions: table('具名动作', base.actions, overlay.actions),
|
|
67
123
|
bizComponents: table('业务组件', base.bizComponents, overlay.bizComponents),
|
|
68
124
|
frames: table('页面框', base.frames, overlay.frames),
|
|
125
|
+
dicts: table('共享字典', base.dicts, overlay.dicts),
|
|
126
|
+
entities: table('共享实体', base.entities, overlay.entities),
|
|
69
127
|
appCandidates: [...new Set([...(base.appCandidates ?? []), ...(overlay.appCandidates ?? [])])].sort(),
|
|
70
128
|
};
|
|
71
129
|
|
|
72
|
-
|
|
73
|
-
|
|
130
|
+
const views = table('展示对象', normalizeViewTable(base.views, names.base), normalizeViewTable(overlay.views, names.overlay));
|
|
131
|
+
if (Object.keys(views).length > 0) merged.views = views;
|
|
132
|
+
const packs = mergeKitPacks(base.packs, overlay.packs);
|
|
133
|
+
if (packs !== undefined) merged.packs = packs;
|
|
134
|
+
|
|
135
|
+
// 覆盖白名单只授权覆盖 kit 内置名;基座与叠加的声明并集去重,绝不授权覆盖对方登记项。
|
|
136
|
+
const overrides = [...new Set([...(base.overrides ?? []), ...(overlay.overrides ?? [])])];
|
|
74
137
|
if (overrides.length > 0) merged.overrides = overrides;
|
|
75
138
|
|
|
139
|
+
// 页面编译错误降级开关归叠加应用优先,显式 false 可关闭基座默认值。
|
|
140
|
+
const devFallback = overlay.devFallback !== undefined ? overlay.devFallback : base.devFallback;
|
|
141
|
+
if (devFallback !== undefined) merged.devFallback = devFallback;
|
|
142
|
+
|
|
143
|
+
// HTTP 默认超时:显式 0 有意义,只有 undefined 才继承基座。
|
|
144
|
+
const requestTimeout = overlay.requestTimeout !== undefined ? overlay.requestTimeout : base.requestTimeout;
|
|
145
|
+
if (requestTimeout !== undefined) merged.requestTimeout = requestTimeout;
|
|
146
|
+
|
|
76
147
|
// 基座独占面:原样取基座的
|
|
77
148
|
for (const k of exclusive) {
|
|
78
149
|
if (base[k] !== undefined) (merged as Record<string, unknown>)[k] = base[k];
|
|
@@ -146,7 +217,7 @@ export function createKitPermissionMiddleware(): SomaMiddlewareChain<SomaEventMi
|
|
|
146
217
|
if (typeof code === 'string' && code !== '' && !kitPermissionAllows(code)) {
|
|
147
218
|
context.status = 'blocked';
|
|
148
219
|
context.message = kitText('soma.kit.perm_denied', { code });
|
|
149
|
-
kitActionExtras().notify(context.message, 'danger');
|
|
220
|
+
kitActionExtras(context.element).notify(context.message, 'danger');
|
|
150
221
|
return;
|
|
151
222
|
}
|
|
152
223
|
await next();
|
|
@@ -5,18 +5,34 @@
|
|
|
5
5
|
// - `http` 是 fetch 的薄封装,非 2xx 或命中应用声明的信封失败判定(envelope.failWhen)抛错;
|
|
6
6
|
// - `selected`(勾选列已选行数组)与 `focusRow()`(协调组件 focus 的当前行;浮层里的动作靠它取行,因为浮层不在行作用域、`row` 是 {});
|
|
7
7
|
// - 处理函数返回 `false` 或抛错都会中止后续步骤;
|
|
8
|
-
// - `reload(object?)` / `notify(message, tone?)` / `filter(scope?)` / `page(scope?)` 来自页面上已登记的协作组件
|
|
9
|
-
// (biz/shared/runtime-hub 的 kitActionExtras
|
|
8
|
+
// - `reload(object?)` / `notify(message, tone?)` / `close()` / `filter(scope?)` / `page(scope?)` 来自页面上已登记的协作组件
|
|
9
|
+
// (biz/shared/runtime-hub 的 kitActionExtras):动作做完可主动刷新、提示、关弹窗,并读到当前筛选与分页——
|
|
10
|
+
// 动作之后的顺序编排(请求 → 提示 → 重取 → 关窗)就写在处理函数里,页面不声明;
|
|
11
|
+
// - 固定协作:页面上有协调组件(StandardCrud 等业务组件)时,处理函数成功返回后自动重取一次(返回 false 或抛错不重取);
|
|
10
12
|
// - 每个动作进行中都登记到 base/action-busy(忙闲通道):带保存钮的组件声明 busy_action 即可禁用触发钮、防重复提交。
|
|
11
13
|
|
|
12
|
-
import { kitActionExtras, type KitActionExtras } from '../biz/shared';
|
|
14
|
+
import { currentKitActionTarget, getKitHubByScope, hasKitHub, kitActionExtras, type KitActionExtras } from '../biz/shared/runtime-hub';
|
|
13
15
|
import { beginKitAction } from '../base/action-busy';
|
|
14
|
-
import { envelopeMessage,
|
|
16
|
+
import { envelopeMessage, isEnvelopeFailure, kitHostEnvelopeAt, type KitEnvelope } from './envelope';
|
|
17
|
+
import { kitFail } from '../compile/kit-error';
|
|
18
|
+
import { SOMA_STD_KEY_LIMITS } from 'soma-core';
|
|
19
|
+
import { SOMA_PIPELINE_HOST_HTTP_REQUEST_TIMEOUT_MS } from 'soma-foundation/values';
|
|
20
|
+
|
|
21
|
+
const TIMEOUT_LIMIT = SOMA_STD_KEY_LIMITS['write.timeoutMs'];
|
|
22
|
+
const DEFAULT_TIMEOUT_MS = SOMA_PIPELINE_HOST_HTTP_REQUEST_TIMEOUT_MS;
|
|
23
|
+
|
|
24
|
+
export interface KitActionRequestConfig {
|
|
25
|
+
params?: Record<string, string>;
|
|
26
|
+
timeoutMs?: number;
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}
|
|
15
29
|
|
|
16
30
|
export interface KitActionHttp {
|
|
17
|
-
get(url: string): Promise<unknown>;
|
|
18
|
-
post(url: string, body?: unknown): Promise<unknown>;
|
|
19
|
-
put(url: string, body?: unknown): Promise<unknown>;
|
|
31
|
+
get(url: string, config?: KitActionRequestConfig): Promise<unknown>;
|
|
32
|
+
post(url: string, body?: unknown, config?: KitActionRequestConfig): Promise<unknown>;
|
|
33
|
+
put(url: string, body?: unknown, config?: KitActionRequestConfig): Promise<unknown>;
|
|
34
|
+
patch(url: string, body?: unknown, config?: KitActionRequestConfig): Promise<unknown>;
|
|
35
|
+
delete(url: string, config?: KitActionRequestConfig): Promise<unknown>;
|
|
20
36
|
}
|
|
21
37
|
|
|
22
38
|
export interface KitActionArgs extends KitActionExtras {
|
|
@@ -28,62 +44,250 @@ export interface KitActionArgs extends KitActionExtras {
|
|
|
28
44
|
}
|
|
29
45
|
|
|
30
46
|
export type KitActionHandler = (args: KitActionArgs) => unknown;
|
|
47
|
+
/** 带动作级缺省超时的登记条目:`{ handler, timeoutMs }`;timeoutMs 取 0(禁用)或 write.timeoutMs 区间内的整数毫秒 */
|
|
48
|
+
export interface KitActionEntry {
|
|
49
|
+
handler: KitActionHandler;
|
|
50
|
+
timeoutMs?: number;
|
|
51
|
+
}
|
|
31
52
|
export type KitNamedAction = (payload: Record<string, unknown>) => Promise<unknown>;
|
|
32
53
|
|
|
33
54
|
const ACTION_NAME = /^[a-z][a-zA-Z0-9_]*$/;
|
|
34
55
|
|
|
35
|
-
|
|
56
|
+
function kitActionConfigError(message: string): Error {
|
|
57
|
+
const error = new Error(message);
|
|
58
|
+
error.name = 'KitActionConfigError';
|
|
59
|
+
(error as unknown as { code: string }).code = 'KIT_ACTION_CONFIG_ERROR';
|
|
60
|
+
return error;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 应用级缺省走 write.timeoutMs 标准区间;0 明确禁用自动超时。逐次 timeoutMs 保留旧的非负有限数值范围。 */
|
|
64
|
+
export function validateKitAppRequestTimeout(timeoutMs: unknown): void {
|
|
65
|
+
validateTimeoutValue(timeoutMs, '应用 requestTimeout');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function validateTimeoutValue(timeoutMs: unknown, label: string): void {
|
|
69
|
+
if (timeoutMs === undefined || timeoutMs === 0) return;
|
|
70
|
+
if (
|
|
71
|
+
typeof timeoutMs !== 'number'
|
|
72
|
+
|| !Number.isInteger(timeoutMs)
|
|
73
|
+
|| timeoutMs < TIMEOUT_LIMIT.min
|
|
74
|
+
|| timeoutMs > TIMEOUT_LIMIT.max
|
|
75
|
+
) {
|
|
76
|
+
throw kitActionConfigError(
|
|
77
|
+
`${label} 配置错误:必须为 0(禁用)或 ${TIMEOUT_LIMIT.min}..${TIMEOUT_LIMIT.max} 的整数毫秒(收到 ${String(timeoutMs)})`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function appendQueryParams(url: string, params?: Record<string, string>): string {
|
|
83
|
+
if (!params || Object.keys(params).length === 0) return url;
|
|
84
|
+
const search = new URLSearchParams(params).toString();
|
|
85
|
+
if (search === '') return url;
|
|
86
|
+
return url.includes('?') ? `${url}&${search}` : `${url}?${search}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function request(
|
|
90
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
91
|
+
url: string,
|
|
92
|
+
body: unknown,
|
|
93
|
+
env: KitEnvelope | undefined,
|
|
94
|
+
config?: KitActionRequestConfig,
|
|
95
|
+
defaultTimeoutMs: number = DEFAULT_TIMEOUT_MS,
|
|
96
|
+
): Promise<unknown> {
|
|
97
|
+
const fullUrl = appendQueryParams(url, config?.params);
|
|
36
98
|
const init: RequestInit = { method, headers: { 'content-type': 'application/json' } };
|
|
37
99
|
if (body !== undefined) init.body = JSON.stringify(body);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
100
|
+
|
|
101
|
+
const timeoutMs = config?.timeoutMs ?? defaultTimeoutMs;
|
|
102
|
+
if (typeof timeoutMs !== 'number' || Number.isNaN(timeoutMs) || !Number.isFinite(timeoutMs) || timeoutMs < 0) {
|
|
103
|
+
throw kitActionConfigError(`请求超时配置错误:timeoutMs 必须为非负有限数字(收到 ${String(timeoutMs)})`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const controller = new AbortController();
|
|
107
|
+
let timedOut = false;
|
|
108
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
109
|
+
if (timeoutMs > 0) {
|
|
110
|
+
timer = setTimeout(() => {
|
|
111
|
+
timedOut = true;
|
|
112
|
+
controller.abort();
|
|
113
|
+
}, timeoutMs);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let externalAborted = false;
|
|
117
|
+
const onExternalAbort = (): void => {
|
|
118
|
+
externalAborted = true;
|
|
119
|
+
controller.abort();
|
|
120
|
+
};
|
|
121
|
+
if (config?.signal) {
|
|
122
|
+
if (config.signal.aborted) {
|
|
123
|
+
externalAborted = true;
|
|
124
|
+
controller.abort();
|
|
125
|
+
} else {
|
|
126
|
+
config.signal.addEventListener('abort', onExternalAbort, { once: true });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
const res = await fetch(fullUrl, { ...init, signal: controller.signal });
|
|
132
|
+
if (!res.ok) throw new Error(`请求失败 ${method} ${fullUrl}:HTTP ${res.status}`);
|
|
133
|
+
const text = await res.text();
|
|
134
|
+
let json: unknown = null;
|
|
135
|
+
if (text !== '') {
|
|
136
|
+
try {
|
|
137
|
+
json = JSON.parse(text);
|
|
138
|
+
} catch {
|
|
139
|
+
throw new Error(`请求失败 ${method} ${fullUrl}:接口返回了非 JSON 内容(可能登录态已失效或网关拦截)`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// 业务信封失败:判定与文案位置来自应用声明的信封约定(kit.app.yaml#envelope),这里不写任何后端字段
|
|
143
|
+
if (isEnvelopeFailure(json, env)) {
|
|
144
|
+
const msg = envelopeMessage(json, env);
|
|
145
|
+
throw new Error(msg !== '' ? msg : `请求失败 ${method} ${fullUrl}`);
|
|
146
|
+
}
|
|
147
|
+
return json;
|
|
148
|
+
} catch (err: unknown) {
|
|
149
|
+
if (timedOut) {
|
|
150
|
+
const timeoutErr = new Error(`请求超时:等待超过 ${timeoutMs} ms 仍无响应。服务端可能仍在处理该请求(超时不代表未执行),客户端不会自动重试,请先确认结果再决定是否重新提交`);
|
|
151
|
+
timeoutErr.name = 'KitActionTimeoutError';
|
|
152
|
+
(timeoutErr as unknown as { code: string }).code = 'KIT_ACTION_TIMEOUT';
|
|
153
|
+
throw timeoutErr;
|
|
154
|
+
}
|
|
155
|
+
if (externalAborted || config?.signal?.aborted) {
|
|
156
|
+
const abortErr = new Error('请求已取消');
|
|
157
|
+
abortErr.name = 'KitActionAbortError';
|
|
158
|
+
(abortErr as unknown as { code: string }).code = 'KIT_ACTION_ABORTED';
|
|
159
|
+
throw abortErr;
|
|
160
|
+
}
|
|
161
|
+
throw err;
|
|
162
|
+
} finally {
|
|
163
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
164
|
+
if (config?.signal) {
|
|
165
|
+
config.signal.removeEventListener('abort', onExternalAbort);
|
|
166
|
+
}
|
|
46
167
|
}
|
|
47
|
-
return json;
|
|
48
168
|
}
|
|
49
169
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
170
|
+
type KitActionTarget = ReturnType<typeof currentKitActionTarget>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 给定动作目标所属页面宿主登记的信封:目标是元素按元素找,是作用域 id 按协调组件的宿主元素找;
|
|
174
|
+
* 依次取第一个能定位到元素的目标,都没有时由 kitHostEnvelopeAt 判唯一性。
|
|
175
|
+
*/
|
|
176
|
+
function targetEnvelope(...targets: readonly KitActionTarget[]): KitEnvelope | undefined {
|
|
177
|
+
for (const target of targets) {
|
|
178
|
+
const from = typeof target === 'string' ? getKitHubByScope(target)?.host : target;
|
|
179
|
+
if (from !== undefined && from !== null) return kitHostEnvelopeAt(from);
|
|
180
|
+
}
|
|
181
|
+
return kitHostEnvelopeAt(undefined);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 按给定信封约定造一个 http。信封优先级:显式传入 > 页面宿主信封(`hostEnvelope` 取值函数,缺省取调用那一刻的动作目标栈顶)
|
|
186
|
+
* > 所属应用实例持有的应用级信封(appEnvelope 取值函数:服务端装配 / 浏览器引导写入该应用自己的持有者,不是全局)。
|
|
187
|
+
* 具名动作每次调用传入「本次已解析好的宿主信封」,handler 里先 await 再发请求也不会串到别的动作的目标。
|
|
188
|
+
*/
|
|
189
|
+
function createKitActionHttpWithDefaultTimeout(
|
|
190
|
+
envelope: KitEnvelope | undefined,
|
|
191
|
+
defaultTimeoutMs: number,
|
|
192
|
+
appEnvelope?: () => KitEnvelope | undefined,
|
|
193
|
+
hostEnvelope: () => KitEnvelope | undefined = () => targetEnvelope(currentKitActionTarget()),
|
|
194
|
+
): KitActionHttp {
|
|
195
|
+
const env = (): KitEnvelope | undefined => envelope ?? hostEnvelope() ?? appEnvelope?.();
|
|
53
196
|
return {
|
|
54
|
-
get: (url) => request('GET', url, undefined, env()),
|
|
55
|
-
post: (url, body) => request('POST', url, body, env()),
|
|
56
|
-
put: (url, body) => request('PUT', url, body, env()),
|
|
197
|
+
get: (url, config) => request('GET', url, undefined, env(), config, defaultTimeoutMs),
|
|
198
|
+
post: (url, body, config) => request('POST', url, body, env(), config, defaultTimeoutMs),
|
|
199
|
+
put: (url, body, config) => request('PUT', url, body, env(), config, defaultTimeoutMs),
|
|
200
|
+
patch: (url, body, config) => request('PATCH', url, body, env(), config, defaultTimeoutMs),
|
|
201
|
+
delete: (url, config) => request('DELETE', url, undefined, env(), config, defaultTimeoutMs),
|
|
57
202
|
};
|
|
58
203
|
}
|
|
59
204
|
|
|
205
|
+
/** 原签名保持兼容;普通调用使用 design hostDefaults 的共用默认值。 */
|
|
206
|
+
export function createKitActionHttp(envelope?: KitEnvelope): KitActionHttp {
|
|
207
|
+
return createKitActionHttpWithDefaultTimeout(envelope, DEFAULT_TIMEOUT_MS);
|
|
208
|
+
}
|
|
209
|
+
|
|
60
210
|
export const kitActionHttp: KitActionHttp = createKitActionHttp();
|
|
61
211
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
212
|
+
/** 按「本次调用解析好的宿主信封」造本次专用的 http */
|
|
213
|
+
type KitActionHttpFor = (hostEnvelope: KitEnvelope | undefined) => KitActionHttp;
|
|
214
|
+
type KitNamedActionFactory = (httpFor: KitActionHttpFor) => KitNamedAction;
|
|
215
|
+
interface DefinedActionMeta { factory: KitNamedActionFactory; timeoutMs?: number }
|
|
216
|
+
// 不可枚举的内部标记:由 defineKitActions 产出的动作才在表里(应用装配据此识别手写动作)
|
|
217
|
+
const DEFINED_ACTION_FACTORIES = new WeakMap<KitNamedAction, DefinedActionMeta>();
|
|
218
|
+
|
|
219
|
+
/** 该动作是否由 defineKitActions 产出(只有这类动作吃应用级 requestTimeout) */
|
|
220
|
+
export function isKitDefinedAction(action: KitNamedAction): boolean {
|
|
221
|
+
return DEFINED_ACTION_FACTORIES.has(action);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function createNamedAction(name: string, handler: KitActionHandler, httpFor: KitActionHttpFor): KitNamedAction {
|
|
225
|
+
return async (payload) => {
|
|
226
|
+
const p = payload ?? {};
|
|
227
|
+
// 动作目标在调用同步段里取一次(此后 handler 可能 await,栈顶会换成别的动作的目标)
|
|
228
|
+
const ambientTarget = currentKitActionTarget();
|
|
229
|
+
const boundTarget =
|
|
230
|
+
(typeof p.scope === 'string' && p.scope !== '' ? p.scope : undefined)
|
|
231
|
+
?? (typeof p.object === 'string' && p.object !== '' ? p.object : undefined)
|
|
232
|
+
?? ambientTarget;
|
|
233
|
+
// 本次调用的信封:按 boundTarget 所属页面宿主解析一次(定位不到元素时退回调用时的动作目标),固定进本次专用的 http
|
|
234
|
+
const http = httpFor(targetEnvelope(boundTarget, ambientTarget));
|
|
235
|
+
const extras = kitActionExtras(boundTarget);
|
|
236
|
+
const row = p.row !== null && typeof p.row === 'object' ? (p.row as Record<string, unknown>) : {};
|
|
237
|
+
const end = beginKitAction(name);
|
|
238
|
+
// 行组件在行动作载荷里带来本行的在途通道名:执行期间一并登记,行组件读同名通道把该行置灰(不区分动作是不是删除);
|
|
239
|
+
// 覆盖到动作后的协调重取结束为止:删除类动作的行不会先闪回可点、再随重取消失
|
|
240
|
+
const endRow = typeof p.kit_row_busy === 'string' && p.kit_row_busy !== '' ? beginKitAction(p.kit_row_busy) : undefined;
|
|
241
|
+
try {
|
|
77
242
|
let result: unknown;
|
|
78
243
|
try {
|
|
79
|
-
result = await handler({ ...
|
|
244
|
+
result = await handler({ ...extras, row, value: p.value, payload: p, http });
|
|
80
245
|
} finally {
|
|
81
246
|
end();
|
|
82
247
|
}
|
|
83
|
-
// 返回 false =
|
|
248
|
+
// 返回 false = 动作主动中止:与抛错同一口径
|
|
84
249
|
if (result === false) throw new Error(`[soma-kit] 具名动作 ${name} 返回 false,终止后续步骤`);
|
|
250
|
+
// 业务组件的固定协作:有协调组件的页面里,动作成功后重取主对象与附带对象(重取失败只记日志,不让动作失败)
|
|
251
|
+
if (hasKitHub(boundTarget)) {
|
|
252
|
+
try { await extras.reload(); } catch (error) { console.error('[soma-kit] 具名动作后的重取失败', error); }
|
|
253
|
+
}
|
|
85
254
|
return result;
|
|
86
|
-
}
|
|
255
|
+
} finally {
|
|
256
|
+
endRow?.();
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 仅重绑 defineKitActions 产物;手写 KitNamedAction 原样返回,HTTP 默认保存在每个 action 的闭包中,不读写跨应用状态。 */
|
|
262
|
+
export function bindKitActionRequestTimeout(action: KitNamedAction, requestTimeout?: number, appEnvelope?: () => KitEnvelope | undefined): KitNamedAction {
|
|
263
|
+
const meta = DEFINED_ACTION_FACTORIES.get(action);
|
|
264
|
+
if (meta === undefined) return action;
|
|
265
|
+
// 优先级:单次 config.timeoutMs(request 内)> 动作级 > 应用级 > 真源默认;应用级信封取值函数由所属应用实例交入
|
|
266
|
+
const timeoutMs = meta.timeoutMs ?? requestTimeout ?? DEFAULT_TIMEOUT_MS;
|
|
267
|
+
const bound = meta.factory((hostEnvelope) => createKitActionHttpWithDefaultTimeout(undefined, timeoutMs, appEnvelope, () => hostEnvelope));
|
|
268
|
+
DEFINED_ACTION_FACTORIES.set(bound, meta);
|
|
269
|
+
return bound;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function defineKitActions<T extends Record<string, KitActionHandler | KitActionEntry>>(
|
|
273
|
+
handlers: T,
|
|
274
|
+
): { readonly [K in keyof T]: KitNamedAction } {
|
|
275
|
+
const out: Record<string, KitNamedAction> = {};
|
|
276
|
+
for (const [name, entry] of Object.entries(handlers)) {
|
|
277
|
+
const isEntry = typeof entry === 'object' && entry !== null;
|
|
278
|
+
const handler = isEntry ? (entry as KitActionEntry).handler : entry;
|
|
279
|
+
const timeoutMs = isEntry ? (entry as KitActionEntry).timeoutMs : undefined;
|
|
280
|
+
if (!ACTION_NAME.test(name)) {
|
|
281
|
+
kitFail('KIT-APP-008', `defineKitActions.${name}`, `具名动作名应是小写字母开头的标识符(与 ACTION | 名字 同一文法)`, '动作名改为小写字母开头,只含字母数字下划线');
|
|
282
|
+
}
|
|
283
|
+
if (typeof handler !== 'function') {
|
|
284
|
+
kitFail('KIT-APP-008', `defineKitActions.${name}`, '具名动作不是函数', '值写成 async (ctx) => {...}');
|
|
285
|
+
}
|
|
286
|
+
validateTimeoutValue(timeoutMs, `动作 ${name} 的 timeoutMs`);
|
|
287
|
+
const factory: KitNamedActionFactory = (httpFor) => createNamedAction(name, handler as KitActionHandler, httpFor);
|
|
288
|
+
const action = factory((hostEnvelope) => createKitActionHttpWithDefaultTimeout(undefined, timeoutMs ?? DEFAULT_TIMEOUT_MS, undefined, () => hostEnvelope));
|
|
289
|
+
DEFINED_ACTION_FACTORIES.set(action, { factory, timeoutMs });
|
|
290
|
+
out[name] = action;
|
|
87
291
|
}
|
|
88
292
|
return out as { readonly [K in keyof T]: KitNamedAction };
|
|
89
293
|
}
|