@pylonts/dsl 1.1.5 → 1.1.11
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 +4 -0
- package/dist/action.d.ts +32 -0
- package/dist/action.js +14 -0
- package/dist/aggregate.d.ts +38 -0
- package/dist/aggregate.js +46 -0
- package/dist/business-flow.d.ts +9 -0
- package/dist/business-flow.js +72 -0
- package/dist/controller.d.ts +35 -0
- package/dist/controller.js +17 -0
- package/dist/convert.d.ts +37 -7
- package/dist/convert.js +23 -2
- package/dist/curd.d.ts +7 -12
- package/dist/curd.js +10 -1
- package/dist/dao.d.ts +140 -3
- package/dist/dao.js +307 -2
- package/dist/db.d.ts +6 -0
- package/dist/db.js +13 -0
- package/dist/domain-event.d.ts +48 -0
- package/dist/domain-event.js +24 -0
- package/dist/dsl.d.ts +32 -2
- package/dist/dsl.js +13 -0
- package/dist/dto.d.ts +8 -2
- package/dist/dto.js +21 -9
- package/dist/endpoint.d.ts +15 -0
- package/dist/endpoint.js +3 -0
- package/dist/entity.d.ts +29 -0
- package/dist/entity.js +13 -0
- package/dist/exception.d.ts +20 -0
- package/dist/exception.js +33 -0
- package/dist/expr.d.ts +45 -0
- package/dist/expr.js +32 -0
- package/dist/field-rule.d.ts +20 -0
- package/dist/field-rule.js +19 -0
- package/dist/filter.d.ts +45 -0
- package/dist/filter.js +21 -0
- package/dist/flow-script.d.ts +108 -0
- package/dist/flow-script.js +505 -0
- package/dist/flow.d.ts +309 -10
- package/dist/flow.js +819 -22
- package/dist/index.d.ts +12 -1
- package/dist/index.js +14 -1
- package/dist/mermaid-driver.js +278 -9
- package/dist/method.d.ts +11 -0
- package/dist/method.js +3 -0
- package/dist/mysql-driver.js +7 -0
- package/dist/project.d.ts +5 -4
- package/dist/project.js +14 -2
- package/dist/provider.d.ts +6 -11
- package/dist/provider.js +2 -2
- package/dist/repository.d.ts +26 -0
- package/dist/repository.js +8 -0
- package/dist/service.d.ts +30 -8
- package/dist/service.js +62 -2
- package/dist/third-service.d.ts +80 -0
- package/dist/third-service.js +97 -0
- package/dist/typebox-driver.d.ts +6 -0
- package/dist/typebox-driver.js +77 -12
- package/dist/utils.d.ts +32 -10
- package/dist/utils.js +32 -11
- package/docs/aggregate.md +110 -0
- package/docs/dao-generation.md +478 -0
- package/docs/ddd-principles.md +75 -0
- package/docs/domain-event.md +137 -0
- package/docs/dto.md +73 -66
- package/docs/keyword-matcher.md +182 -0
- package/docs/third-service.md +122 -0
- package/docs/token.md +327 -0
- package/docs/trans-reentrant.md +85 -0
- package/package.json +27 -5
- package/src/action.ts +51 -10
- package/src/aggregate.ts +104 -0
- package/src/business-flow.ts +80 -0
- package/src/controller.ts +54 -0
- package/src/convert.ts +78 -15
- package/src/curd.ts +104 -93
- package/src/dao.ts +486 -13
- package/src/db.ts +199 -181
- package/src/domain-event.ts +74 -0
- package/src/dsl.ts +48 -2
- package/src/dto.ts +266 -247
- package/src/entity.ts +43 -0
- package/src/exception.ts +53 -0
- package/src/expr.ts +65 -0
- package/src/field-rule.ts +47 -0
- package/src/filter.ts +70 -0
- package/src/flow-script.ts +696 -0
- package/src/flow.ts +1226 -103
- package/src/index.ts +47 -33
- package/src/mermaid-driver.ts +339 -84
- package/src/method.ts +20 -0
- package/src/mysql-driver.ts +7 -0
- package/src/project.ts +114 -97
- package/src/repository.ts +35 -0
- package/src/service.ts +107 -20
- package/src/third-service.ts +192 -0
- package/src/typebox-driver.ts +86 -11
- package/src/utils.ts +74 -26
- package/dist/check-inheritance.d.ts +0 -9
- package/dist/check-inheritance.js +0 -58
- package/src/provider.ts +0 -73
package/src/project.ts
CHANGED
|
@@ -1,98 +1,115 @@
|
|
|
1
|
-
import { SchemaBase } from './dsl.js';
|
|
2
|
-
import type { TableSchema } from './db.js';
|
|
3
|
-
|
|
4
|
-
// Project topology definitions: describe the applications (frontends) and
|
|
5
|
-
// backend APIs of a repository, and which frontends each API serves.
|
|
6
|
-
|
|
7
|
-
/** Frontend form factor. Closed enum, extend when new form factors appear. */
|
|
8
|
-
export type FrontType = 'admin' | 'wxmini';
|
|
9
|
-
|
|
10
|
-
/** A frontend application (e.g. admin console, wechat mini program). */
|
|
11
|
-
export interface FrontAppSchema extends SchemaBase {
|
|
12
|
-
type: FrontType;
|
|
13
|
-
/** Source directory relative to project root, e.g. 'web-admin/'. */
|
|
14
|
-
dir: string;
|
|
15
|
-
/**
|
|
16
|
-
* Tenant table for this app.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
1
|
+
import { SchemaBase } from './dsl.js';
|
|
2
|
+
import type { TableSchema } from './db.js';
|
|
3
|
+
|
|
4
|
+
// Project topology definitions: describe the applications (frontends) and
|
|
5
|
+
// backend APIs of a repository, and which frontends each API serves.
|
|
6
|
+
|
|
7
|
+
/** Frontend form factor. Closed enum, extend when new form factors appear. */
|
|
8
|
+
export type FrontType = 'admin' | 'wxmini' | 'mobile';
|
|
9
|
+
|
|
10
|
+
/** A frontend application (e.g. admin console, wechat mini program). */
|
|
11
|
+
export interface FrontAppSchema extends SchemaBase {
|
|
12
|
+
type: FrontType;
|
|
13
|
+
/** Source directory relative to project root, e.g. 'web-admin/'. */
|
|
14
|
+
dir: string;
|
|
15
|
+
/**
|
|
16
|
+
* Tenant table for this app. The tenant column of a business table is
|
|
17
|
+
* deterministic: `{tenant.phrase}_{tenant.pk}` (e.g. shop with pk id →
|
|
18
|
+
* `shop_id`). Tables carrying that column get automatic tenant scoping;
|
|
19
|
+
* tables without it are global tables (e.g. system config) — both valid.
|
|
20
|
+
*/
|
|
21
|
+
tenant?: TableSchema;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A backend API service. apps references shared FrontAppSchema instances. */
|
|
25
|
+
export interface ProjectApiSchema extends SchemaBase {
|
|
26
|
+
/** Source directory relative to project root, e.g. 'api/'. */
|
|
27
|
+
dir: string;
|
|
28
|
+
/** Frontends this API serves. Direct instance references (see defineProject). */
|
|
29
|
+
apps: FrontAppSchema[];
|
|
30
|
+
/** API base URL prefix shared by all apps it serves, e.g. '/mall'. '' = no prefix. */
|
|
31
|
+
contextPath?: string;
|
|
32
|
+
/** API service base URL for node clients, e.g. 'http://127.0.0.1:3000'. */
|
|
33
|
+
baseUrl?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A third-party system (e.g. wechat pay, unionpay). Owns its own
|
|
37
|
+
* implementation dir and contract (controller_types), just like an API,
|
|
38
|
+
* but is not part of this repo's served surface. */
|
|
39
|
+
export interface ThirdApiSchema extends SchemaBase {
|
|
40
|
+
/** Source directory relative to project root, e.g. 'wechat/'. */
|
|
41
|
+
dir: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ProjectSchema extends SchemaBase {
|
|
45
|
+
apps: FrontAppSchema[];
|
|
46
|
+
apis: ProjectApiSchema[];
|
|
47
|
+
thirdApis: ThirdApiSchema[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Instance naming: lowercase letters/digits/dashes only. Underscores belong
|
|
51
|
+
// to table names; the instance export symbol in project.config.ts must equal
|
|
52
|
+
// the kebab-camel of the name, so 'admin_api' cannot map to a valid symbol.
|
|
53
|
+
const INSTANCE_NAME_RE = /^[a-z][a-z0-9-]*$/;
|
|
54
|
+
|
|
55
|
+
function checkInstanceName(project: string, kind: string, name: string): void {
|
|
56
|
+
if (!INSTANCE_NAME_RE.test(name)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`project ${project}: ${kind} name '${name}' must match ${INSTANCE_NAME_RE} (lowercase letters/digits/dashes; underscores are table-only)`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Defines the project topology. FrontAppSchema instances are shared value objects:
|
|
65
|
+
* api.apps references the same instances from project.apps, so an app served
|
|
66
|
+
* by multiple APIs is defined once and referenced many times.
|
|
67
|
+
*
|
|
68
|
+
* Runtime-validates app type whitelist, unique names and api.apps reference
|
|
69
|
+
* integrity (same style as defineTable/defineCurd).
|
|
70
|
+
*/
|
|
71
|
+
export function defineProject(
|
|
72
|
+
name: string,
|
|
73
|
+
schema: {
|
|
74
|
+
description?: string;
|
|
75
|
+
apps: FrontAppSchema[];
|
|
76
|
+
apis: ProjectApiSchema[];
|
|
77
|
+
thirdApis?: ThirdApiSchema[];
|
|
78
|
+
},
|
|
79
|
+
): ProjectSchema {
|
|
80
|
+
const project: ProjectSchema = { name, ...schema, thirdApis: schema.thirdApis ?? [] };
|
|
81
|
+
|
|
82
|
+
const appNames = new Set<string>();
|
|
83
|
+
for (const app of project.apps) {
|
|
84
|
+
if (!app.name) throw new Error(`project ${name}: app name is required`);
|
|
85
|
+
checkInstanceName(name, 'app', app.name);
|
|
86
|
+
if (appNames.has(app.name)) throw new Error(`project ${name}: duplicate app name '${app.name}'`);
|
|
87
|
+
appNames.add(app.name);
|
|
88
|
+
if (app.type !== 'admin' && app.type !== 'wxmini' && app.type !== 'mobile') {
|
|
89
|
+
throw new Error(`project ${name}: app '${app.name}' must be type 'admin', 'wxmini' or 'mobile' (got '${app.type}')`);
|
|
90
|
+
}
|
|
91
|
+
if (!app.dir) throw new Error(`project ${name}: app '${app.name}' dir is required`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const apiNames = new Set<string>();
|
|
95
|
+
for (const api of project.apis) {
|
|
96
|
+
if (!api.name) throw new Error(`project ${name}: api name is required`);
|
|
97
|
+
checkInstanceName(name, 'api', api.name);
|
|
98
|
+
if (apiNames.has(api.name)) throw new Error(`project ${name}: duplicate api name '${api.name}'`);
|
|
99
|
+
apiNames.add(api.name);
|
|
100
|
+
if (!api.dir) throw new Error(`project ${name}: api '${api.name}' dir is required`);
|
|
101
|
+
for (const ref of api.apps) {
|
|
102
|
+
if (!project.apps.includes(ref)) {
|
|
103
|
+
throw new Error(`project ${name}: api '${api.name}' references app '${ref.name}' that is not a shared instance in project.apps (define once and reference it)`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
for (const third of project.thirdApis) {
|
|
109
|
+
if (!third.name) throw new Error(`project ${name}: thirdApi name is required`);
|
|
110
|
+
checkInstanceName(name, 'thirdApi', third.name);
|
|
111
|
+
if (!third.dir) throw new Error(`project ${name}: thirdApi '${third.name}' dir is required`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return project;
|
|
98
115
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SchemaBase } from './dsl.js';
|
|
2
|
+
import type { DomainAggregate } from './aggregate.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Aggregate-grained storage entry. The repository binds one aggregate and
|
|
6
|
+
* exposes the three fixed operation skeletons (load / save / delete) that the
|
|
7
|
+
* generator expands from the aggregate structure:
|
|
8
|
+
*
|
|
9
|
+
* save(order) = tx { rootDao.upsert + memberDao cascade by via FK }
|
|
10
|
+
* load(id) = rootDao.get + memberDao by via FK
|
|
11
|
+
* delete(id) = tx { memberDao delete + rootDao delete }
|
|
12
|
+
*
|
|
13
|
+
* Callers face the domain concept (Order), never the tables. DAO stays
|
|
14
|
+
* single-table; Repository is the multi-table (aggregate) unit; Service is the
|
|
15
|
+
* use-case unit. Inter-aggregate joins are prohibited — repositories only
|
|
16
|
+
* reference each other by root ID.
|
|
17
|
+
*/
|
|
18
|
+
export interface RepositorySchema extends SchemaBase {
|
|
19
|
+
type: 'repository';
|
|
20
|
+
/** The aggregate this repository persists (shared instance). */
|
|
21
|
+
aggregate: DomainAggregate;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function defineRepository(options: {
|
|
25
|
+
name: string;
|
|
26
|
+
aggregate: DomainAggregate;
|
|
27
|
+
description?: string;
|
|
28
|
+
}): RepositorySchema {
|
|
29
|
+
return {
|
|
30
|
+
type: 'repository',
|
|
31
|
+
name: options.name,
|
|
32
|
+
description: options.description,
|
|
33
|
+
aggregate: options.aggregate,
|
|
34
|
+
};
|
|
35
|
+
}
|
package/src/service.ts
CHANGED
|
@@ -1,21 +1,108 @@
|
|
|
1
|
-
import { SchemaBase } from './dsl.js';
|
|
2
|
-
import { FrontAppSchema } from './project.js';
|
|
3
|
-
import type { DtoArrayField, DtoField, DtoMessage, DtoObjectField } from './dto.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import type { SchemaBase } from './dsl.js';
|
|
2
|
+
import type { FrontAppSchema, ProjectApiSchema } from './project.js';
|
|
3
|
+
import type { DtoArrayField, DtoField, DtoMessage, DtoObjectField } from './dto.js';
|
|
4
|
+
import type { ExceptionSchema } from './exception.js';
|
|
5
|
+
import type { FlowSchema } from './flow.js';
|
|
6
|
+
import { exceptionEndNames } from './flow.js';
|
|
7
|
+
|
|
8
|
+
/** A backend service serving exactly one frontend app (1:1 module). */
|
|
9
|
+
export interface ServiceSchema extends SchemaBase {
|
|
10
|
+
type: 'service';
|
|
11
|
+
/** The backend api module this service belongs to (shared instance from
|
|
12
|
+
* project.config.ts apis). Services are always backend-side, so storage is
|
|
13
|
+
* service_schema/{api.name}/{app.name}/service/. */
|
|
14
|
+
api: ProjectApiSchema;
|
|
15
|
+
/** The frontend app this service serves (shared instance from project.config). */
|
|
16
|
+
app: FrontAppSchema;
|
|
17
|
+
/** Methods keyed by name — the map key is written back as the method name. */
|
|
18
|
+
methods: Record<string, ServiceMethodSchema>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Method input for defineService: type/schema/name are set by the builder. */
|
|
22
|
+
export type ServiceMethodDef = Omit<ServiceMethodSchema, 'type' | 'schema' | 'name'>;
|
|
23
|
+
|
|
24
|
+
export function defineService(options: {
|
|
25
|
+
name: string;
|
|
26
|
+
api: ProjectApiSchema;
|
|
27
|
+
app: FrontAppSchema;
|
|
28
|
+
methods: Record<string, ServiceMethodDef>;
|
|
29
|
+
description?: string;
|
|
30
|
+
}): ServiceSchema {
|
|
31
|
+
if (!options.api.apps.includes(options.app)) {
|
|
32
|
+
throw new Error(`service ${options.name}: api '${options.api.name}' does not serve app '${options.app.name}'`);
|
|
33
|
+
}
|
|
34
|
+
const schema: ServiceSchema = {
|
|
35
|
+
type: 'service',
|
|
36
|
+
name: options.name,
|
|
37
|
+
description: options.description,
|
|
38
|
+
api: options.api,
|
|
39
|
+
app: options.app,
|
|
40
|
+
methods: {},
|
|
41
|
+
};
|
|
42
|
+
for (const key of Object.keys(options.methods)) {
|
|
43
|
+
const method = options.methods[key];
|
|
44
|
+
schema.methods[key] = { type: 'method', schema, ...method, name: key };
|
|
45
|
+
}
|
|
46
|
+
validateFlowBindings(schema);
|
|
47
|
+
return schema;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A method bound to a flow: the flow's escape set (its exception ends) must
|
|
51
|
+
// equal the method's declared throws, the flow's own args/results (when
|
|
52
|
+
// present) must be the same objects as the method's, and one flow may serve
|
|
53
|
+
// only one method.
|
|
54
|
+
//
|
|
55
|
+
// Notes: (1) the unique-binding check is per defineService call — a flow
|
|
56
|
+
// shared across two service schemas is not detected; (2) escape-set equality
|
|
57
|
+
// means exceptions swallowed by an internal catch (or routed through a
|
|
58
|
+
// catch-all end) disappear from the contract, so the method must not declare
|
|
59
|
+
// them.
|
|
60
|
+
function validateFlowBindings(schema: ServiceSchema): void {
|
|
61
|
+
const seenFlows = new Set<FlowSchema>();
|
|
62
|
+
for (const key of Object.keys(schema.methods)) {
|
|
63
|
+
const m = schema.methods[key];
|
|
64
|
+
if (!m.flow) continue;
|
|
65
|
+
if (seenFlows.has(m.flow)) {
|
|
66
|
+
throw new Error(`service ${schema.name}: flow "${m.flow.name}" is bound to more than one method`);
|
|
67
|
+
}
|
|
68
|
+
seenFlows.add(m.flow);
|
|
69
|
+
if (m.flow.args !== undefined && m.flow.args !== m.args) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`service ${schema.name}: method "${key}" args and its flow "${m.flow.name}" args must be the same object`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (m.flow.results !== undefined && m.flow.results !== m.results) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`service ${schema.name}: method "${key}" results and its flow "${m.flow.name}" results must be the same object`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const escaped = exceptionEndNames(m.flow);
|
|
80
|
+
const declared = new Set((m.throws ?? []).map((t) => t.name));
|
|
81
|
+
for (const e of escaped) {
|
|
82
|
+
if (!declared.has(e)) {
|
|
83
|
+
throw new Error(`service ${schema.name}: method "${key}" flow escapes ${e} but the method does not declare it`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const d of declared) {
|
|
87
|
+
if (!escaped.has(d)) {
|
|
88
|
+
throw new Error(`service ${schema.name}: method "${key}" declares throw ${d} but its flow never escapes it`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (m.flow.name !== key) {
|
|
92
|
+
console.warn(`service ${schema.name}: method "${key}" flow name "${m.flow.name}" differs from the method key`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A method exposed by a service. */
|
|
98
|
+
export interface ServiceMethodSchema extends SchemaBase {
|
|
99
|
+
type: 'method';
|
|
100
|
+
schema : ServiceSchema;
|
|
101
|
+
args: DtoMessage,
|
|
102
|
+
results : DtoMessage
|
|
103
|
+
/** Exceptions this method may throw (e.g. IOException, CodeException). */
|
|
104
|
+
throws?: ExceptionSchema[];
|
|
105
|
+
/** Implementation flow — the method's logic graph. The method carries the
|
|
106
|
+
* contract (args/results/throws), the flow carries the structure. */
|
|
107
|
+
flow?: FlowSchema;
|
|
21
108
|
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { CollectionSchemaBase, Field, SchemaBase } from './dsl.js';
|
|
2
|
+
import type { ExceptionSchema } from './exception.js';
|
|
3
|
+
import type { FieldRuleEnd, FieldRuleSchema } from './field-rule.js';
|
|
4
|
+
import type { ThirdApiSchema } from './project.js';
|
|
5
|
+
|
|
6
|
+
// Third-party integration services.
|
|
7
|
+
// A ThirdMethodSchema is the Field-collection counterpart of a DtoMessage —
|
|
8
|
+
// like TableSchema.columns but for wire-format fields instead of DB columns.
|
|
9
|
+
// DTOs project from it via from(thirdMethod), and its fields may either share
|
|
10
|
+
// a local entity column instance (same fact, same type) or declare their own
|
|
11
|
+
// wire type plus a refs link (same fact, different type/format).
|
|
12
|
+
|
|
13
|
+
/** A third-party integration service (e.g. tenpay wechat pay).
|
|
14
|
+
* Distinct from ServiceSchema (backend service bound to an app) and
|
|
15
|
+
* ThirdApiSchema (project topology: the dir the integration lives in).
|
|
16
|
+
* Describes the adapter class contract: constructor config + methods. */
|
|
17
|
+
export interface ThirdServiceSchema extends SchemaBase {
|
|
18
|
+
type: 'thirdService';
|
|
19
|
+
/** The third-party system this service integrates (topology reference). */
|
|
20
|
+
schema: ThirdApiSchema;
|
|
21
|
+
/** Methods keyed by name — the map key is written back as the method name. */
|
|
22
|
+
methods: Record<string, ThirdServiceMethodSchema>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A method exposed by a third-party service. */
|
|
26
|
+
export interface ThirdServiceMethodSchema extends SchemaBase {
|
|
27
|
+
type: 'method';
|
|
28
|
+
schema: ThirdServiceSchema;
|
|
29
|
+
/** Input message — the Field-collection counterpart of a DtoMessage. */
|
|
30
|
+
args: ThirdMethodSchema;
|
|
31
|
+
/** Output message. */
|
|
32
|
+
results: ThirdMethodSchema;
|
|
33
|
+
/** Exceptions this method may throw (e.g. IOException, CodeException). */
|
|
34
|
+
throws?: ExceptionSchema[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Field binding to a rule: which end the local wire field stands on.
|
|
38
|
+
* The ref always stands on the other end — from/to carry no extra information. */
|
|
39
|
+
export interface ConvertFieldSchema {
|
|
40
|
+
/** The rule binding field and ref (rule = name + two ends). */
|
|
41
|
+
rule: FieldRuleSchema;
|
|
42
|
+
/** The end instance the local wire field stands on. */
|
|
43
|
+
end: FieldRuleEnd;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Same-fact variant link: a wire field carrying the same fact as a local
|
|
47
|
+
* entity column under a different type/format (e.g. total_fee in fen vs amount in yuan). */
|
|
48
|
+
export interface ThirdFieldRef {
|
|
49
|
+
/** Wire field defined in this message (local definition). */
|
|
50
|
+
field: Field;
|
|
51
|
+
/** Field in another schema (table column or another message). */
|
|
52
|
+
ref: Field;
|
|
53
|
+
/** Optional rule binding — omitted when the fact is merely linked, not converted. */
|
|
54
|
+
convert?: ConvertFieldSchema;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A directional message of a third-party method: a Field collection mirroring
|
|
58
|
+
* TableSchema.columns, but fields hold wire-format names/types. A field may be
|
|
59
|
+
* a shared instance of a local entity column (same fact, same type) — its
|
|
60
|
+
* name/schema keep pointing at the table and the DTO projection inherits
|
|
61
|
+
* type/semantics from the entity, exactly like from(table). */
|
|
62
|
+
export interface ThirdMethodSchema extends CollectionSchemaBase {
|
|
63
|
+
type: 'thirdMethod';
|
|
64
|
+
/** The method this message belongs to (direction implied by args/results slot). */
|
|
65
|
+
schema: ThirdServiceMethodSchema;
|
|
66
|
+
/** Wire-format fields. */
|
|
67
|
+
fields: Record<string, Field>;
|
|
68
|
+
/** Same-fact variant links: wire field -> local entity column. */
|
|
69
|
+
refs?: ThirdFieldRef[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Message input for defineThirdMethod: type/schema are set by the builder. */
|
|
73
|
+
export type ThirdMethodDef = Omit<ThirdMethodSchema, 'type' | 'schema'>;
|
|
74
|
+
|
|
75
|
+
/** Build a third-party method message. Writes back name/schema on own fields
|
|
76
|
+
* (top-level and nested); shared entity columns keep their table identity and
|
|
77
|
+
* must be keyed by their column name. */
|
|
78
|
+
export function defineThirdMethod(def: ThirdMethodDef): ThirdMethodSchema {
|
|
79
|
+
const message: ThirdMethodSchema = {
|
|
80
|
+
type: 'thirdMethod',
|
|
81
|
+
name: def.name,
|
|
82
|
+
description: def.description,
|
|
83
|
+
// Filled by defineThirdService.
|
|
84
|
+
schema: undefined as unknown as ThirdServiceMethodSchema,
|
|
85
|
+
fields: def.fields,
|
|
86
|
+
refs: def.refs,
|
|
87
|
+
};
|
|
88
|
+
for (const key of Object.keys(message.fields)) {
|
|
89
|
+
const field = message.fields[key] as Field;
|
|
90
|
+
if (field.schema === undefined) {
|
|
91
|
+
field.name = key;
|
|
92
|
+
field.schema = message;
|
|
93
|
+
} else if (field.schema.type === 'table') {
|
|
94
|
+
// Shared entity column: from() names the projection after field.name, so
|
|
95
|
+
// a mismatched key would silently rename the wire field. Same-fact fields
|
|
96
|
+
// with different names go through refs instead.
|
|
97
|
+
if (field.name !== key) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`thirdMethod '${message.name}': shared column key '${key}' must match the column name '${field.name}' — ` +
|
|
100
|
+
`same-fact fields with different names go through refs instead`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
} else if (field.schema !== message) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`thirdMethod '${message.name}': field '${key}' already belongs to ${field.schema.type} '${field.schema.name}', cannot reuse`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
writeBackNested(message, field);
|
|
109
|
+
}
|
|
110
|
+
if (message.refs !== undefined) {
|
|
111
|
+
const ownFields = Object.values(message.fields);
|
|
112
|
+
for (const link of message.refs) {
|
|
113
|
+
if (!ownFields.includes(link.field)) {
|
|
114
|
+
throw new Error(`thirdMethod '${message.name}': ref field must be one of its fields`);
|
|
115
|
+
}
|
|
116
|
+
if (link.ref.schema === undefined || link.ref.schema === message) {
|
|
117
|
+
throw new Error(`thirdMethod '${message.name}': ref target '${link.ref.name}' must be defined in another schema`);
|
|
118
|
+
}
|
|
119
|
+
if (link.convert !== undefined) {
|
|
120
|
+
const ends = Object.values(link.convert.rule.ends);
|
|
121
|
+
if (!ends.includes(link.convert.end)) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`thirdMethod '${message.name}': convert end '${link.convert.end.name}' must be one of rule '${link.convert.rule.name}' ends`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return message;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Write back name/schema on nested wire fields (array items, object properties);
|
|
133
|
+
* shared entity columns keep their table identity. */
|
|
134
|
+
function writeBackNested(message: ThirdMethodSchema, field: Field): void {
|
|
135
|
+
if (field.type === 'array') {
|
|
136
|
+
writeBackNested(message, field.items);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (field.type !== 'object') return;
|
|
140
|
+
for (const key of Object.keys(field.properties)) {
|
|
141
|
+
const child = field.properties[key] as Field;
|
|
142
|
+
if (child.schema === undefined) {
|
|
143
|
+
child.name = key;
|
|
144
|
+
child.schema = message;
|
|
145
|
+
}
|
|
146
|
+
writeBackNested(message, child);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Method input for defineThirdService: name is written back from the methods map key. */
|
|
151
|
+
export interface ThirdServiceMethodDef {
|
|
152
|
+
/** Input message. */
|
|
153
|
+
args: ThirdMethodDef;
|
|
154
|
+
/** Output message. */
|
|
155
|
+
results: ThirdMethodDef;
|
|
156
|
+
/** Exceptions this method may throw (e.g. IOException, CodeException). */
|
|
157
|
+
throws?: ExceptionSchema[];
|
|
158
|
+
description?: string;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function defineThirdService(options: {
|
|
162
|
+
schema: ThirdApiSchema;
|
|
163
|
+
name: string;
|
|
164
|
+
methods: Record<string, ThirdServiceMethodDef>;
|
|
165
|
+
description?: string;
|
|
166
|
+
}): ThirdServiceSchema {
|
|
167
|
+
const schema: ThirdServiceSchema = {
|
|
168
|
+
type: 'thirdService',
|
|
169
|
+
name: options.name,
|
|
170
|
+
description: options.description,
|
|
171
|
+
schema: options.schema,
|
|
172
|
+
methods: {},
|
|
173
|
+
};
|
|
174
|
+
for (const key of Object.keys(options.methods)) {
|
|
175
|
+
const method = options.methods[key] as ThirdServiceMethodDef;
|
|
176
|
+
const methodSchema: ThirdServiceMethodSchema = {
|
|
177
|
+
type: 'method',
|
|
178
|
+
name: key,
|
|
179
|
+
description: method.description,
|
|
180
|
+
schema,
|
|
181
|
+
args: undefined as unknown as ThirdMethodSchema,
|
|
182
|
+
results: undefined as unknown as ThirdMethodSchema,
|
|
183
|
+
throws: method.throws,
|
|
184
|
+
};
|
|
185
|
+
methodSchema.args = defineThirdMethod(method.args);
|
|
186
|
+
methodSchema.results = defineThirdMethod(method.results);
|
|
187
|
+
methodSchema.args.schema = methodSchema;
|
|
188
|
+
methodSchema.results.schema = methodSchema;
|
|
189
|
+
schema.methods[key] = methodSchema;
|
|
190
|
+
}
|
|
191
|
+
return schema;
|
|
192
|
+
}
|