@artzok/zp_lib 0.1.1 → 0.1.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/README.md CHANGED
@@ -54,8 +54,8 @@ VS Code 通过 `tsconfig.json` 的 `allowJs: true` 将 `src/` 中的 JS 纳入
54
54
  ## 打包并验证下游导入
55
55
 
56
56
  1. 在库根目录运行 `npm run build` 和 `npm pack --dry-run`。检查清单里包含 `src/main.tsp`、两个装饰器的 `.tsp` 和配套的 `.js` 文件。
57
- 2. 运行 `npm pack` 生成本地压缩包。文件名以命令输出为准,例如版本 `0.1.1` 对应 `artzok-zp_lib-0.1.1.tgz`。
58
- 3. 在独立的 TypeSpec 项目中执行 `npm install /实际路径/artzok-zp_lib-0.1.1.tgz`,使用上面的 `main.tsp` 示例运行 `npx tsp compile . --no-emit`。这一步验证的是下游实际安装的文件和包入口。
57
+ 2. 运行 `npm pack` 生成本地压缩包。文件名以命令输出为准,例如版本 `0.1.1` 对应 `sawa-zp_lib-0.1.1.tgz`。
58
+ 3. 在独立的 TypeSpec 项目中执行 `npm install /实际路径/sawa-zp_lib-0.1.1.tgz`,使用上面的 `main.tsp` 示例运行 `npx tsp compile . --no-emit`。这一步验证的是下游实际安装的文件和包入口。
59
59
 
60
60
  ## 发布到官方 npm
61
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artzok/zp_lib",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "tspMain": "src/main.tsp",
6
6
  "exports": {
package/src/rpc.js ADDED
@@ -0,0 +1,112 @@
1
+ import {
2
+ createTypeSpecLibrary,
3
+ isTemplateDeclaration,
4
+ paramMessage,
5
+ validateDecoratorUniqueOnNode,
6
+ } from "@typespec/compiler";
7
+ import {
8
+ $delete,
9
+ $get,
10
+ $head,
11
+ $patch,
12
+ $post,
13
+ $put,
14
+ getOperationVerb,
15
+ getRoutePath,
16
+ setRoute,
17
+ } from "@typespec/http";
18
+ import { setExtension } from "@typespec/openapi";
19
+
20
+ export const namespace = "ZpLib";
21
+
22
+ export const $lib = createTypeSpecLibrary({
23
+ name: "sawa-rpc",
24
+ diagnostics: {
25
+ "http-verb-not-allowed": {
26
+ severity: "error",
27
+ messages: {
28
+ default: paramMessage`@rpc 命名空间中的操作 ${"name"} 不能显式指定 HTTP 方法;请移除 HTTP 方法装饰器(包括从模板或基操作继承的方法)。`,
29
+ },
30
+ },
31
+ "operation-route-not-allowed": {
32
+ severity: "error",
33
+ messages: {
34
+ default: paramMessage`@rpc 自动使用操作 ${"name"} 的名称作为路由;请移除操作上的 @route,将公共前缀放到命名空间或接口上。`,
35
+ },
36
+ },
37
+ },
38
+ state: {
39
+ processed: { description: "已处理的 RPC 操作,避免嵌套 @rpc 重复处理。" },
40
+ },
41
+ });
42
+
43
+ const verbDecorators = new Set([$get, $post, $put, $patch, $delete, $head]);
44
+
45
+ /**
46
+ * Visit declared operations only; do not follow operation references into other namespaces.
47
+ * @param {import('@typespec/compiler').Namespace} target
48
+ * @returns {Generator<import('@typespec/compiler').Operation>}
49
+ */
50
+ function* operationsIn(target) {
51
+ yield* target.operations.values();
52
+ for (const iface of target.interfaces.values()) {
53
+ yield* iface.operations.values();
54
+ }
55
+ for (const child of target.namespaces.values()) {
56
+ yield* operationsIn(child);
57
+ }
58
+ }
59
+
60
+ /**
61
+ * @param {import('@typespec/compiler').DecoratorContext} context
62
+ * @param {import('@typespec/compiler').Namespace} target
63
+ * @returns {import('@typespec/compiler').DecoratorValidatorCallbacks}
64
+ */
65
+ export function $rpc(context, target, name) {
66
+ validateDecoratorUniqueOnNode(context, target, $rpc);
67
+ setExtension(context.program, target, "x-namespace", name.value)
68
+
69
+ return {
70
+ onGraphFinish() {
71
+ const diagnostics = [];
72
+ const processed = context.program.stateSet($lib.stateKeys.processed);
73
+ for (const operation of operationsIn(target)) {
74
+ if (processed.has(operation)) continue;
75
+ processed.add(operation);
76
+
77
+ // Inspect both resolved HTTP state and decorator identities: generic
78
+ // declarations may not have executed their decorators yet.
79
+ const hasVerb =
80
+ getOperationVerb(context.program, operation) !== undefined ||
81
+ operation.decorators.some((d) => verbDecorators.has(d.decorator));
82
+ if (hasVerb) {
83
+ diagnostics.push(
84
+ $lib.createDiagnostic({
85
+ code: "http-verb-not-allowed",
86
+ target: operation,
87
+ format: { name: operation.name },
88
+ }),
89
+ );
90
+ }
91
+
92
+ if (getRoutePath(context.program, operation) !== undefined) {
93
+ diagnostics.push(
94
+ $lib.createDiagnostic({
95
+ code: "operation-route-not-allowed",
96
+ target: operation,
97
+ format: { name: operation.name },
98
+ }),
99
+ );
100
+ continue;
101
+ }
102
+ if (isTemplateDeclaration(operation)) continue;
103
+
104
+ // Namespace decorators run before their contents are checked. Wait for
105
+ // the complete graph, then write HTTP route metadata before @typespec/http
106
+ // resolves and caches services in $onValidate. No type graph fields change.
107
+ setRoute(context, operation, { path: operation.name, shared: false });
108
+ }
109
+ return diagnostics;
110
+ },
111
+ };
112
+ }
package/src/rpc.tsp ADDED
@@ -0,0 +1,13 @@
1
+ import "@typespec/http";
2
+ import "./rpc.js";
3
+
4
+ using TypeSpec.Reflection;
5
+
6
+ namespace ZpLib {
7
+ /**
8
+ * 为命名空间及其子命名空间、接口中的操作自动使用方法名作为相对路由。
9
+ * 禁止操作显式指定 HTTP 方法或自定义路由;HTTP 方法沿用 TypeSpec 的默认推导。
10
+ * 命名空间和接口仍可用 `@route` 声明公共前缀。
11
+ */
12
+ extern dec rpc(target: Namespace, name: string);
13
+ }