@azure-tools/typespec-azure-resource-manager 0.75.0-dev.5 → 0.75.0-dev.6
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-standard-response-codes.d.ts","sourceRoot":"","sources":["../../../src/rules/use-standard-response-codes.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-standard-response-codes.d.ts","sourceRoot":"","sources":["../../../src/rules/use-standard-response-codes.ts"],"names":[],"mappings":"AAmCA,eAAO,MAAM,4BAA4B;;;;yBAqDvC,CAAC"}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { createRule, fileRef, getService, isTemplateDeclarationOrInstance, paramMessage, } from "@typespec/compiler";
|
|
2
|
-
import { getHttpOperation } from "@typespec/http";
|
|
3
|
-
import { isStatusCodeAllowed, matchesStatusCode } from "./http-response-utils.js";
|
|
2
|
+
import { getHttpOperation, } from "@typespec/http";
|
|
4
3
|
import { isTemplatedInterfaceOperation } from "./utils.js";
|
|
5
|
-
const
|
|
6
|
-
|
|
4
|
+
const servicePolicies = {
|
|
5
|
+
get: {
|
|
6
|
+
messageId: "get",
|
|
7
|
+
allowed: [{ required: [200], optional: [202] }],
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
const allowedStatusCodes = new Set([200, 201, 202, 204]);
|
|
7
11
|
export const useStandardResponseCodesRule = createRule({
|
|
8
12
|
name: "use-standard-response-codes",
|
|
9
13
|
docs: fileRef.fromPackageRoot("src/rules/use-standard-response-codes.md"),
|
|
@@ -23,23 +27,23 @@ export const useStandardResponseCodesRule = createRule({
|
|
|
23
27
|
return;
|
|
24
28
|
}
|
|
25
29
|
const [httpOperation] = getHttpOperation(context.program, operation);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
if (httpOperation.verb === "get" && namespace) {
|
|
31
|
-
const has200 = httpOperation.responses.some(({ statusCodes }) => matchesStatusCode(statusCodes, 200));
|
|
32
|
-
const hasOnlyAllowedCodes = httpOperation.responses.every(({ statusCodes }) => isStatusCodeAllowed(statusCodes, allowedGetStatusCodes));
|
|
33
|
-
if (!has200 || !hasOnlyAllowedCodes) {
|
|
30
|
+
const policy = getResponsePolicy(context.program, operation, httpOperation);
|
|
31
|
+
if (policy) {
|
|
32
|
+
if (!matchesPolicy(httpOperation.responses, policy)) {
|
|
34
33
|
context.reportDiagnostic({
|
|
35
34
|
target: operation,
|
|
36
|
-
messageId: httpOperation.responses.length === 0
|
|
35
|
+
messageId: policy === servicePolicies.get && httpOperation.responses.length === 0
|
|
36
|
+
? "emptyGet"
|
|
37
|
+
: policy.messageId,
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
return;
|
|
40
41
|
}
|
|
41
42
|
for (const response of httpOperation.responses) {
|
|
42
|
-
if (
|
|
43
|
+
if (response.statusCodes === "*")
|
|
44
|
+
continue;
|
|
45
|
+
const code = getStatusCode(response.statusCodes);
|
|
46
|
+
if (code === undefined || !allowedStatusCodes.has(code)) {
|
|
43
47
|
context.reportDiagnostic({
|
|
44
48
|
target: operation,
|
|
45
49
|
format: {
|
|
@@ -53,10 +57,36 @@ export const useStandardResponseCodesRule = createRule({
|
|
|
53
57
|
};
|
|
54
58
|
},
|
|
55
59
|
});
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
function getResponsePolicy(program, operation, httpOperation) {
|
|
61
|
+
const policy = servicePolicies[httpOperation.verb];
|
|
62
|
+
if (!policy)
|
|
63
|
+
return undefined;
|
|
64
|
+
for (let ns = operation.namespace; ns; ns = ns.namespace) {
|
|
65
|
+
if (getService(program, ns))
|
|
66
|
+
return policy;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function matchesPolicy(responses, policy) {
|
|
71
|
+
const codes = new Set();
|
|
72
|
+
for (const response of responses) {
|
|
73
|
+
if (response.statusCodes === "*")
|
|
74
|
+
continue;
|
|
75
|
+
const code = getStatusCode(response.statusCodes);
|
|
76
|
+
if (code === undefined)
|
|
77
|
+
return false;
|
|
78
|
+
codes.add(code);
|
|
59
79
|
}
|
|
80
|
+
return policy.allowed.some(({ required, optional = [] }) => required.every((code) => codes.has(code)) &&
|
|
81
|
+
[...codes].every((code) => required.includes(code) || optional.includes(code)));
|
|
82
|
+
}
|
|
83
|
+
/** Returns the single status code, or `undefined` for a range that covers several codes. */
|
|
84
|
+
function getStatusCode(statusCode) {
|
|
85
|
+
if (typeof statusCode === "number")
|
|
86
|
+
return statusCode;
|
|
87
|
+
return statusCode.start === statusCode.end ? statusCode.start : undefined;
|
|
88
|
+
}
|
|
89
|
+
function formatStatusCode(statusCode) {
|
|
60
90
|
if (typeof statusCode === "number") {
|
|
61
91
|
return String(statusCode);
|
|
62
92
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-standard-response-codes.js","sourceRoot":"","sources":["../../../src/rules/use-standard-response-codes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,OAAO,EACP,UAAU,EACV,+BAA+B,EAC/B,YAAY,
|
|
1
|
+
{"version":3,"file":"use-standard-response-codes.js","sourceRoot":"","sources":["../../../src/rules/use-standard-response-codes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,OAAO,EACP,UAAU,EACV,+BAA+B,EAC/B,YAAY,GAGb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,gBAAgB,GAIjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAW3D,MAAM,eAAe,GAA2D;IAC9E,GAAG,EAAE;QACH,SAAS,EAAE,KAAK;QAChB,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC;KAChD;CACF,CAAC;AAEF,MAAM,kB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",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
5
|
"description": "TypeSpec Azure Resource Manager library",
|
|
6
6
|
"homepage": "https://azure.github.io/typespec-azure",
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@azure-tools/typespec-azure-core": "^0.74.0 || >= 0.75.0-dev.2",
|
|
51
|
+
"@typespec/compiler": "^1.18.0",
|
|
52
|
+
"@typespec/versioning": "^0.88.0",
|
|
51
53
|
"@typespec/http": "^1.18.0",
|
|
52
54
|
"@typespec/rest": "^0.88.0",
|
|
53
|
-
"@typespec/
|
|
54
|
-
"@typespec/openapi": "^1.18.0",
|
|
55
|
-
"@typespec/versioning": "^0.88.0"
|
|
55
|
+
"@typespec/openapi": "^1.18.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@azure-tools/typespec-azure-core": "^0.74.0 || >= 0.75.0-dev.2",
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"vitest": "^5.1.0",
|
|
66
66
|
"@typespec/compiler": "^1.18.0",
|
|
67
67
|
"@typespec/http": "^1.18.0",
|
|
68
|
+
"@typespec/library-linter": "^0.88.0",
|
|
68
69
|
"@typespec/tspd": "^0.78.0",
|
|
69
70
|
"@typespec/rest": "^0.88.0",
|
|
70
|
-
"@typespec/library-linter": "^0.88.0",
|
|
71
71
|
"@typespec/versioning": "^0.88.0",
|
|
72
72
|
"@typespec/openapi": "^1.18.0"
|
|
73
73
|
},
|