@artzok/zp_lib 0.0.0-stage → 0.1.0
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/package.json +39 -4
- package/src/client-available.js +79 -0
- package/src/client-available.tsp +18 -0
- package/src/main.tsp +2 -0
- package/src/named-enum.js +22 -0
- package/src/named-enum.tsp +13 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@artzok/zp_lib",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"tspMain": "src/main.tsp",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"typespec": "./src/main.tsp"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@typespec/compiler": "^1.17.0",
|
|
13
|
+
"@typespec/openapi": "^1.17.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "latest",
|
|
17
|
+
"typescript-eslint": "^8.49.0",
|
|
18
|
+
"@typespec/compiler": "^1.17.0",
|
|
19
|
+
"@typespec/openapi": "^1.17.0",
|
|
20
|
+
"@typespec/library-linter": "latest",
|
|
21
|
+
"eslint": "^9.15.0",
|
|
22
|
+
"prettier": "^3.3.3",
|
|
23
|
+
"typescript": "^5.3.3"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "npm run build:tsp",
|
|
27
|
+
"watch": "tsc --watch",
|
|
28
|
+
"build:tsp": "tsp compile . --warn-as-error --import @typespec/library-linter --no-emit",
|
|
29
|
+
"lint": "eslint src/ test/ --report-unused-disable-directives --max-warnings=0",
|
|
30
|
+
"lint:fix": "eslint . --report-unused-disable-directives --fix",
|
|
31
|
+
"format": "prettier . --write",
|
|
32
|
+
"format:check": "prettier --check ."
|
|
33
|
+
},
|
|
34
|
+
"packageManager": "npm@12.2.3+sha512.a2f98d4517bbe89bdca7513a88e78f43a1fd66be1730eaea81f69e18d86d46f3f75d29762e8fdaf42d4de7f8d194b2ebb2a09e9bfa268067591e96796baca7d5",
|
|
35
|
+
"files": [
|
|
36
|
+
"src"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"registry": "https://registry.npmjs.org/"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createTypeSpecLibrary, paramMessage } from "@typespec/compiler";
|
|
2
|
+
import { getExtensions, setExtension } from "@typespec/openapi";
|
|
3
|
+
|
|
4
|
+
export const namespace = "ZpLib";
|
|
5
|
+
|
|
6
|
+
export const $lib = createTypeSpecLibrary({
|
|
7
|
+
name: "sawa-client-available",
|
|
8
|
+
diagnostics: {
|
|
9
|
+
"invalid-version": {
|
|
10
|
+
severity: "error",
|
|
11
|
+
messages: { default: "客户端版本必须是非空且不包含空白的字符串。" },
|
|
12
|
+
},
|
|
13
|
+
"duplicate-application": {
|
|
14
|
+
severity: "error",
|
|
15
|
+
messages: {
|
|
16
|
+
default: paramMessage`同一目标不能重复声明应用 ${"application"} 的可用版本。`,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
state: {
|
|
21
|
+
availability: { description: "各目标的客户端最低可用版本。" },
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {import('@typespec/compiler').Program} program
|
|
27
|
+
* @param {import('@typespec/compiler').Type} target
|
|
28
|
+
* @returns {Readonly<Partial<Record<string, string>>> | undefined}
|
|
29
|
+
*/
|
|
30
|
+
export function getClientAvailability(program, target) {
|
|
31
|
+
return program.stateMap($lib.stateKeys.availability).get(target);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Add visible Redoc badges after all decorators (including @extension) run.
|
|
35
|
+
* @param {import('@typespec/compiler').Program} program
|
|
36
|
+
*/
|
|
37
|
+
// 用来在文档中生成标记
|
|
38
|
+
// export function $onValidate(program) {
|
|
39
|
+
// for (const [target, availability] of program.stateMap($lib.stateKeys.availability)) {
|
|
40
|
+
// const existingBadges = getExtensions(program, target).get("x-badges") ?? [];
|
|
41
|
+
// const badges = Object.entries(availability).map(([application, version]) => ({
|
|
42
|
+
// name: `${application} ≥ ${version}`,
|
|
43
|
+
// color: application.startsWith("iOS") ? "blue" : "green",
|
|
44
|
+
// position: "after",
|
|
45
|
+
// }));
|
|
46
|
+
// setExtension(program, target, "x-badges", [...existingBadges, ...badges]);
|
|
47
|
+
// }
|
|
48
|
+
// }
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {import('@typespec/compiler').DecoratorContext} context
|
|
52
|
+
* @param {import('@typespec/compiler').Operation | import('@typespec/compiler').Model | import('@typespec/compiler').ModelProperty} target
|
|
53
|
+
* @param {string} application
|
|
54
|
+
* @param {string} version
|
|
55
|
+
*/
|
|
56
|
+
export function $clientAvailable(context, target, application, version) {
|
|
57
|
+
if (version.length === 0 || /\s/u.test(version)) {
|
|
58
|
+
$lib.reportDiagnostic(context.program, {
|
|
59
|
+
code: "invalid-version", target: context.getArgumentTarget(1),
|
|
60
|
+
});
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const current = getClientAvailability(context.program, target) ?? {};
|
|
65
|
+
if (Object.hasOwn(current, application)) {
|
|
66
|
+
$lib.reportDiagnostic(context.program, {
|
|
67
|
+
code: "duplicate-application", target: context.decoratorTarget,
|
|
68
|
+
format: { application },
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Decorators execute in reverse source order. Sort keys for stable output.
|
|
74
|
+
const availability = Object.freeze(Object.fromEntries(
|
|
75
|
+
Object.entries({ ...current, [application]: version }).sort(([a], [b]) => a.localeCompare(b)),
|
|
76
|
+
));
|
|
77
|
+
context.program.stateMap($lib.stateKeys.availability).set(target, availability);
|
|
78
|
+
setExtension(context.program, target, "x-client-available", availability);
|
|
79
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "@typespec/openapi";
|
|
2
|
+
import "./client-available.js";
|
|
3
|
+
|
|
4
|
+
using TypeSpec.Reflection;
|
|
5
|
+
|
|
6
|
+
namespace ZpLib {
|
|
7
|
+
/**
|
|
8
|
+
* 记录 operation、模型或字段从哪个客户端版本开始可用。
|
|
9
|
+
* 可重复标注不同应用,同一目标上的应用不能重复;生成 x-client-available 和文档可见的版本徽章。
|
|
10
|
+
* @param application 客户端应用标识,可以是任意字符串。
|
|
11
|
+
* @param version 该目标首次可用的客户端版本。
|
|
12
|
+
*/
|
|
13
|
+
extern dec clientAvailable(
|
|
14
|
+
target: Operation | Model | ModelProperty,
|
|
15
|
+
application: valueof string,
|
|
16
|
+
version: valueof string
|
|
17
|
+
);
|
|
18
|
+
}
|
package/src/main.tsp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { $summary } from "@typespec/compiler";
|
|
2
|
+
import { setExtension } from "@typespec/openapi";
|
|
3
|
+
|
|
4
|
+
export const namespace = "ZpLib";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('@typespec/compiler').DecoratorContext} context
|
|
8
|
+
* @param {import('@typespec/compiler').Enum} target
|
|
9
|
+
*/
|
|
10
|
+
export function $namedEnum(context, target) {
|
|
11
|
+
// The OpenAPI emitter removes duplicate values while preserving source order.
|
|
12
|
+
const namesByValue = new Map();
|
|
13
|
+
for (const member of target.members.values()) {
|
|
14
|
+
const value = member.value ?? member.name;
|
|
15
|
+
// Redoc uses the title as the variant label and may omit the const value.
|
|
16
|
+
context.call($summary, member, `${member.name}`);
|
|
17
|
+
if (!namesByValue.has(value)) {
|
|
18
|
+
namesByValue.set(value, member.name);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
setExtension(context.program, target, "x-enum-varnames", [Object.fromEntries(namesByValue)]);
|
|
22
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "@typespec/openapi";
|
|
2
|
+
import "./named-enum.js";
|
|
3
|
+
|
|
4
|
+
using TypeSpec.Reflection;
|
|
5
|
+
|
|
6
|
+
namespace ZpLib {
|
|
7
|
+
/**
|
|
8
|
+
* 将枚举成员名作为标题;成员值与 `@doc` 描述分别保留,供文档按“值|变量名|描述”展示。
|
|
9
|
+
* 配合 enum-strategy: annotated,生成包含 const(枚举值)、title 和 description(`@doc`)的 oneOf 成员。
|
|
10
|
+
* x-enum-varnames 使用 [{ "枚举值": "成员名", ... }] 格式,重复值保留第一个成员名。
|
|
11
|
+
*/
|
|
12
|
+
extern dec namedEnum(target: Enum);
|
|
13
|
+
}
|
package/README.md
DELETED