sheridan-typespec-aspnet 0.2.1 → 0.4.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/README.md +11 -4
- package/dist/src/emitter.js +104 -5
- package/dist/src/lib.d.ts +47 -2
- package/dist/src/lib.js +18 -0
- package/dist/src/model/collect-types.d.ts +3 -1
- package/dist/src/model/collect-types.js +1 -1
- package/dist/src/model/collect.js +56 -6
- package/dist/src/model/types.d.ts +12 -0
- package/dist/src/naming/names.d.ts +1 -1
- package/dist/src/naming/names.js +13 -3
- package/dist/src/naming/services.d.ts +11 -1
- package/dist/src/naming/services.js +18 -3
- package/dist/src/render/controllers/controllers.d.ts +20 -0
- package/dist/src/render/controllers/controllers.js +559 -0
- package/dist/src/render/controllers/results.d.ts +10 -0
- package/dist/src/render/controllers/results.js +82 -0
- package/dist/src/render/minimal/endpoints.d.ts +14 -0
- package/dist/src/render/minimal/endpoints.js +161 -0
- package/dist/src/render/minimal/results.d.ts +11 -0
- package/dist/src/render/minimal/results.js +90 -0
- package/dist/src/render/shared/api-setup.d.ts +9 -1
- package/dist/src/render/shared/api-setup.js +165 -11
- package/dist/src/render/shared/binding.d.ts +43 -0
- package/dist/src/render/shared/binding.js +426 -0
- package/dist/src/render/shared/body-formats.d.ts +18 -0
- package/dist/src/render/shared/body-formats.js +561 -0
- package/dist/src/render/shared/converters.js +57 -4
- package/dist/src/render/shared/documentation.d.ts +18 -0
- package/dist/src/render/shared/documentation.js +112 -0
- package/dist/src/render/shared/headers.d.ts +7 -0
- package/dist/src/render/shared/headers.js +52 -0
- package/dist/src/render/shared/models.js +5 -23
- package/dist/src/render/shared/openapi.d.ts +1 -0
- package/dist/src/render/shared/openapi.js +333 -0
- package/dist/src/render/shared/parameters.d.ts +3 -0
- package/dist/src/render/shared/parameters.js +224 -0
- package/dist/src/render/shared/references.d.ts +3 -0
- package/dist/src/render/shared/references.js +18 -0
- package/dist/src/render/shared/request-selector.d.ts +1 -0
- package/dist/src/render/shared/request-selector.js +139 -0
- package/dist/src/render/shared/responses.d.ts +55 -0
- package/dist/src/render/shared/responses.js +130 -0
- package/dist/src/render/shared/routes.d.ts +37 -0
- package/dist/src/render/shared/routes.js +274 -0
- package/dist/src/render/shared/validation.d.ts +6 -1
- package/dist/src/render/shared/validation.js +60 -0
- package/dist/src/scaffold/projects.d.ts +1 -1
- package/dist/src/scaffold/projects.js +4 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
# Sheridan.Typespec.Aspnet
|
|
2
2
|
|
|
3
|
-
A [TypeSpec](https://typespec.io) emitter that
|
|
3
|
+
A [TypeSpec](https://typespec.io) emitter that generates ASP.NET Core servers in C#. It generates Minimal APIs or controllers.
|
|
4
4
|
|
|
5
|
-
**Status: early development.**
|
|
5
|
+
**Status: early development.** From a spec, the emitter writes:
|
|
6
|
+
|
|
7
|
+
- C# models, enums and unions, with JSON converters, one JSON setup and validation attributes;
|
|
8
|
+
- a service interface with result types for your code to implement, with a context that gives it the request;
|
|
9
|
+
- Minimal API routes or, with the `controllers` style, controllers, which bind and validate requests, call your services and send their results, with problem details for failures and an OpenAPI document in development;
|
|
10
|
+
- a solution with a host project that builds and runs, and a stub for each service, registered with dependency injection.
|
|
11
|
+
|
|
12
|
+
Authentication, versioning, pagination, files and streaming are not generated yet. See the [architecture](docs/architecture.md), the [plan](docs/plan.md) and [issues](https://github.com/two4suited/Sheridan.Typespec.Aspnet/issues).
|
|
6
13
|
|
|
7
14
|
Install it from npm (links to files and issues here need access to the repository):
|
|
8
15
|
|
|
@@ -10,11 +17,11 @@ Install it from npm (links to files and issues here need access to the repositor
|
|
|
10
17
|
npm install --save-dev sheridan-typespec-aspnet
|
|
11
18
|
```
|
|
12
19
|
|
|
13
|
-

|
|
14
21
|
|
|
15
22
|
## Options
|
|
16
23
|
|
|
17
|
-
Set these under `options: sheridan-typespec-aspnet:` in `tspconfig.yaml`. All are checked. Today `project-name`, `root-namespace`, `collection-type`, `host-output-dir`, `scaffold-host`, `scaffold-tests` and `mocks` change the output;
|
|
24
|
+
Set these under `options: sheridan-typespec-aspnet:` in `tspconfig.yaml`. All are checked. Today `style`, `openapi`, `project-name`, `root-namespace`, `collection-type`, `host-output-dir`, `scaffold-host`, `scaffold-tests` and `mocks` change the output; `skip-format` takes effect when formatting exists.
|
|
18
25
|
|
|
19
26
|
| Option | Values | Default |
|
|
20
27
|
|---|---|---|
|
package/dist/src/emitter.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { emitFile, getRelativePathFromDirectory, resolvePath, } from "@typespec/compiler";
|
|
2
2
|
import { collectModel } from "./model/collect.js";
|
|
3
3
|
import { assignNames } from "./naming/names.js";
|
|
4
|
+
import { renderActionBinding, renderActionResults, renderControllers, } from "./render/controllers/controllers.js";
|
|
5
|
+
import { actionResultWriter, responseAttributes } from "./render/controllers/results.js";
|
|
6
|
+
import { reportDiagnostic } from "./lib.js";
|
|
4
7
|
import { resolveOptions } from "./options.js";
|
|
8
|
+
import { renderEndpoints } from "./render/minimal/endpoints.js";
|
|
9
|
+
import { resultWriter } from "./render/minimal/results.js";
|
|
5
10
|
import { renderApiSetup } from "./render/shared/api-setup.js";
|
|
11
|
+
import { bindingWriter } from "./render/shared/binding.js";
|
|
12
|
+
import { bodyShapes } from "./render/shared/body-formats.js";
|
|
6
13
|
import { renderConverters } from "./render/shared/converters.js";
|
|
14
|
+
import { headerWriter } from "./render/shared/headers.js";
|
|
7
15
|
import { renderJsonSetup } from "./render/shared/json-setup.js";
|
|
8
16
|
import { renderModels } from "./render/shared/models.js";
|
|
17
|
+
import { documentationWriter } from "./render/shared/documentation.js";
|
|
18
|
+
import { renderOpenApi } from "./render/shared/openapi.js";
|
|
19
|
+
import { renderParameters, renderResponses } from "./render/shared/parameters.js";
|
|
9
20
|
import { renderHttpRequestContext, renderRequestContext } from "./render/shared/request-context.js";
|
|
21
|
+
import { renderRequestSelector } from "./render/shared/request-selector.js";
|
|
22
|
+
import { planRoutes } from "./render/shared/routes.js";
|
|
10
23
|
import { renderServices } from "./render/shared/services.js";
|
|
11
24
|
import { hasDeprecations, registrations, renderStubs } from "./render/shared/stubs.js";
|
|
12
25
|
import { renderValidation } from "./render/shared/validation.js";
|
|
@@ -25,9 +38,7 @@ export async function $onEmit(context) {
|
|
|
25
38
|
}
|
|
26
39
|
const model = collectModel(context.program);
|
|
27
40
|
const projects = projectNames(options.projectName);
|
|
28
|
-
const names = assignNames(context.program, model, options.rootNamespace, [
|
|
29
|
-
`${projects.api}ApiExtensions`,
|
|
30
|
-
]);
|
|
41
|
+
const names = assignNames(context.program, model, options.rootNamespace, [`${projects.api}ApiExtensions`], options.style);
|
|
31
42
|
const services = renderServices(context.program, model, names, options);
|
|
32
43
|
const files = [
|
|
33
44
|
{
|
|
@@ -45,15 +56,103 @@ export async function $onEmit(context) {
|
|
|
45
56
|
content: renderHttpRequestContext(options.rootNamespace),
|
|
46
57
|
});
|
|
47
58
|
}
|
|
59
|
+
const controllers = options.style === "controllers";
|
|
60
|
+
const routing = planRoutes(context.program, model);
|
|
61
|
+
const openapi = options.openapi;
|
|
62
|
+
const selector = routing.conditions.size > 0;
|
|
63
|
+
// A route that cannot be mapped leaves nothing to render.
|
|
64
|
+
if (context.program.hasError()) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const shapes = bodyShapes(context.program, model, options.rootNamespace);
|
|
68
|
+
const bind = bindingWriter(context.program, model, options.rootNamespace, services.typeOf, shapes, controllers);
|
|
69
|
+
const contracts = new Map(services.contracts.map((found) => [found.service, found]));
|
|
70
|
+
const headers = headerWriter(model, options.rootNamespace, (kind, header) => reportDiagnostic(context.program, {
|
|
71
|
+
code: "unsupported-type",
|
|
72
|
+
format: { kind },
|
|
73
|
+
target: header.source,
|
|
74
|
+
}));
|
|
75
|
+
const read = (service, index) => {
|
|
76
|
+
const found = contracts.get(service).operations[index];
|
|
77
|
+
const contract = `global::${service.namespace}.${service.name}`;
|
|
78
|
+
const writes = found.cases.some((c) => c.found.content.headers.length > 0);
|
|
79
|
+
return { found, binding: bind(found.operation, contract, found.parameters, writes) };
|
|
80
|
+
};
|
|
81
|
+
const serviceGroups = new Set(model.services.flatMap(({ namespace, groups }) => groups.filter((group) => group.kind === "namespace" &&
|
|
82
|
+
[...group.namespace, group.name].join(".") === namespace.join("."))));
|
|
83
|
+
const describe = documentationWriter(model, options.rootNamespace, openapi, controllers);
|
|
84
|
+
/** What is said of an operation, whichever style serves it. */
|
|
85
|
+
const documentationOf = (service, found, binding) => describe(serviceGroups.has(service.group) ? "" : service.group.name, found, binding, (routing.conditions.get(found.operation.operation)?.accepted.length ?? 0) > 0);
|
|
86
|
+
if (controllers) {
|
|
87
|
+
const respond = actionResultWriter(context.program, model, options.rootNamespace, headers);
|
|
88
|
+
files.push(...renderControllers(options.rootNamespace, names.services, routing, (service, index) => {
|
|
89
|
+
const { found, binding } = read(service, index);
|
|
90
|
+
return {
|
|
91
|
+
binding,
|
|
92
|
+
arms: respond(found, binding.found, binding.context, service.namespace),
|
|
93
|
+
responses: responseAttributes(model, found),
|
|
94
|
+
documentation: documentationOf(service, found, binding),
|
|
95
|
+
};
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const respond = resultWriter(context.program, model, options.rootNamespace, headers);
|
|
100
|
+
files.push(...renderEndpoints(options.rootNamespace, names.services, routing, (service, index) => {
|
|
101
|
+
const { found, binding } = read(service, index);
|
|
102
|
+
return {
|
|
103
|
+
binding,
|
|
104
|
+
result: respond(found, binding.found, binding.context, service.namespace),
|
|
105
|
+
documentation: documentationOf(service, found, binding),
|
|
106
|
+
};
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
const formats = shapes.render(controllers);
|
|
110
|
+
if (formats)
|
|
111
|
+
files.push({ path: "Requests/BodyFormats.cs", content: formats });
|
|
112
|
+
if (names.services.length > 0) {
|
|
113
|
+
files.push({ path: "Requests/Parameters.cs", content: renderParameters(options.rootNamespace) }, { path: "Responses/Results.cs", content: renderResponses(options.rootNamespace) });
|
|
114
|
+
if (controllers) {
|
|
115
|
+
files.push({
|
|
116
|
+
path: "Responses/ActionResults.cs",
|
|
117
|
+
content: renderActionResults(options.rootNamespace),
|
|
118
|
+
}, {
|
|
119
|
+
path: "Requests/ActionBinding.cs",
|
|
120
|
+
content: renderActionBinding(options.rootNamespace),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (openapi && names.services.length > 0) {
|
|
125
|
+
files.push({
|
|
126
|
+
path: "Documentation/OpenApi.cs",
|
|
127
|
+
content: renderOpenApi(options.rootNamespace, controllers),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (context.program.hasError()) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (selector) {
|
|
134
|
+
files.push({
|
|
135
|
+
path: "Routing/RequestSelector.cs",
|
|
136
|
+
content: renderRequestSelector(options.rootNamespace, controllers),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
48
139
|
files.push({
|
|
49
140
|
path: `${projects.api}ApiExtensions.cs`,
|
|
50
|
-
content: renderApiSetup(options.rootNamespace, projects.api, registrations(names, projects.host, options.scaffoldHost)
|
|
141
|
+
content: renderApiSetup(options.rootNamespace, projects.api, registrations(names, projects.host, options.scaffoldHost), {
|
|
142
|
+
calls: controllers
|
|
143
|
+
? []
|
|
144
|
+
: names.services.map(({ namespace, endpoints }) => `global::${namespace}.${endpoints.name}.${endpoints.map}`),
|
|
145
|
+
selector,
|
|
146
|
+
controllers: controllers && names.services.length > 0,
|
|
147
|
+
checks: names.services.length > 0,
|
|
148
|
+
openapi: openapi && names.services.length > 0,
|
|
149
|
+
}),
|
|
51
150
|
});
|
|
52
151
|
// Nothing is written when the spec holds something that cannot be generated.
|
|
53
152
|
if (context.program.hasError()) {
|
|
54
153
|
return;
|
|
55
154
|
}
|
|
56
|
-
const project = generatedProject(projects);
|
|
155
|
+
const project = generatedProject(projects, openapi);
|
|
57
156
|
const folder = resolvePath(context.emitterOutputDir, projects.generated);
|
|
58
157
|
const generated = [
|
|
59
158
|
{ ...project, path: project.path.slice(projects.generated.length + 1) },
|
package/dist/src/lib.d.ts
CHANGED
|
@@ -42,6 +42,15 @@ export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
|
|
|
42
42
|
"unsupported-type": {
|
|
43
43
|
readonly default: import("@typespec/compiler").CallableMessage<["kind"]>;
|
|
44
44
|
};
|
|
45
|
+
"unsupported-route": {
|
|
46
|
+
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
47
|
+
};
|
|
48
|
+
"server-path-ignored": {
|
|
49
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
50
|
+
};
|
|
51
|
+
"ambiguous-route": {
|
|
52
|
+
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
53
|
+
};
|
|
45
54
|
"versioning-unsupported": {
|
|
46
55
|
readonly default: "Versioned services cannot be generated yet.";
|
|
47
56
|
};
|
|
@@ -70,7 +79,7 @@ export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
|
|
|
70
79
|
readonly default: import("@typespec/compiler").CallableMessage<["option"]>;
|
|
71
80
|
};
|
|
72
81
|
}, AspNetEmitterOptions, never>;
|
|
73
|
-
export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-service-name" | "host-inside-generated" | "generated-inside-host" | "reserved-namespace" | "manifest-unreadable" | "stale-file-kept" | "host-reference-stale" | "solution-missing-project" | "unsupported-type" | "versioning-unsupported" | "project-name-required" | "default-project-name" | "name-collision" | "name-too-long" | "unsupported-encoding" | "default-not-applied" | "constraint-not-checked" | "requires-host", M extends keyof {
|
|
82
|
+
export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-service-name" | "host-inside-generated" | "generated-inside-host" | "reserved-namespace" | "manifest-unreadable" | "stale-file-kept" | "host-reference-stale" | "solution-missing-project" | "unsupported-type" | "unsupported-route" | "server-path-ignored" | "ambiguous-route" | "versioning-unsupported" | "project-name-required" | "default-project-name" | "name-collision" | "name-too-long" | "unsupported-encoding" | "default-not-applied" | "constraint-not-checked" | "requires-host", M extends keyof {
|
|
74
83
|
"invalid-name": {
|
|
75
84
|
readonly default: import("@typespec/compiler").CallableMessage<["option", "value"]>;
|
|
76
85
|
};
|
|
@@ -101,6 +110,15 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
101
110
|
"unsupported-type": {
|
|
102
111
|
readonly default: import("@typespec/compiler").CallableMessage<["kind"]>;
|
|
103
112
|
};
|
|
113
|
+
"unsupported-route": {
|
|
114
|
+
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
115
|
+
};
|
|
116
|
+
"server-path-ignored": {
|
|
117
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
118
|
+
};
|
|
119
|
+
"ambiguous-route": {
|
|
120
|
+
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
121
|
+
};
|
|
104
122
|
"versioning-unsupported": {
|
|
105
123
|
readonly default: "Versioned services cannot be generated yet.";
|
|
106
124
|
};
|
|
@@ -159,6 +177,15 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
159
177
|
"unsupported-type": {
|
|
160
178
|
readonly default: import("@typespec/compiler").CallableMessage<["kind"]>;
|
|
161
179
|
};
|
|
180
|
+
"unsupported-route": {
|
|
181
|
+
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
182
|
+
};
|
|
183
|
+
"server-path-ignored": {
|
|
184
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
185
|
+
};
|
|
186
|
+
"ambiguous-route": {
|
|
187
|
+
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
188
|
+
};
|
|
162
189
|
"versioning-unsupported": {
|
|
163
190
|
readonly default: "Versioned services cannot be generated yet.";
|
|
164
191
|
};
|
|
@@ -186,7 +213,7 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
186
213
|
"requires-host": {
|
|
187
214
|
readonly default: import("@typespec/compiler").CallableMessage<["option"]>;
|
|
188
215
|
};
|
|
189
|
-
}, C, M>) => void, createDiagnostic: <C extends "invalid-name" | "invalid-service-name" | "host-inside-generated" | "generated-inside-host" | "reserved-namespace" | "manifest-unreadable" | "stale-file-kept" | "host-reference-stale" | "solution-missing-project" | "unsupported-type" | "versioning-unsupported" | "project-name-required" | "default-project-name" | "name-collision" | "name-too-long" | "unsupported-encoding" | "default-not-applied" | "constraint-not-checked" | "requires-host", M extends keyof {
|
|
216
|
+
}, C, M>) => void, createDiagnostic: <C extends "invalid-name" | "invalid-service-name" | "host-inside-generated" | "generated-inside-host" | "reserved-namespace" | "manifest-unreadable" | "stale-file-kept" | "host-reference-stale" | "solution-missing-project" | "unsupported-type" | "unsupported-route" | "server-path-ignored" | "ambiguous-route" | "versioning-unsupported" | "project-name-required" | "default-project-name" | "name-collision" | "name-too-long" | "unsupported-encoding" | "default-not-applied" | "constraint-not-checked" | "requires-host", M extends keyof {
|
|
190
217
|
"invalid-name": {
|
|
191
218
|
readonly default: import("@typespec/compiler").CallableMessage<["option", "value"]>;
|
|
192
219
|
};
|
|
@@ -217,6 +244,15 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
217
244
|
"unsupported-type": {
|
|
218
245
|
readonly default: import("@typespec/compiler").CallableMessage<["kind"]>;
|
|
219
246
|
};
|
|
247
|
+
"unsupported-route": {
|
|
248
|
+
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
249
|
+
};
|
|
250
|
+
"server-path-ignored": {
|
|
251
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
252
|
+
};
|
|
253
|
+
"ambiguous-route": {
|
|
254
|
+
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
255
|
+
};
|
|
220
256
|
"versioning-unsupported": {
|
|
221
257
|
readonly default: "Versioned services cannot be generated yet.";
|
|
222
258
|
};
|
|
@@ -275,6 +311,15 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
275
311
|
"unsupported-type": {
|
|
276
312
|
readonly default: import("@typespec/compiler").CallableMessage<["kind"]>;
|
|
277
313
|
};
|
|
314
|
+
"unsupported-route": {
|
|
315
|
+
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
316
|
+
};
|
|
317
|
+
"server-path-ignored": {
|
|
318
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
319
|
+
};
|
|
320
|
+
"ambiguous-route": {
|
|
321
|
+
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
322
|
+
};
|
|
278
323
|
"versioning-unsupported": {
|
|
279
324
|
readonly default: "Versioned services cannot be generated yet.";
|
|
280
325
|
};
|
package/dist/src/lib.js
CHANGED
|
@@ -122,6 +122,24 @@ export const $lib = createTypeSpecLibrary({
|
|
|
122
122
|
default: paramMessage `This type cannot be generated: ${"kind"}.`,
|
|
123
123
|
},
|
|
124
124
|
},
|
|
125
|
+
"unsupported-route": {
|
|
126
|
+
severity: "error",
|
|
127
|
+
messages: {
|
|
128
|
+
default: paramMessage `This route cannot be generated: ${"reason"}.`,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
"server-path-ignored": {
|
|
132
|
+
severity: "warning",
|
|
133
|
+
messages: {
|
|
134
|
+
default: paramMessage `Routes start at "/", as the servers do not share one fixed path: ${"paths"}. Give every @server the same path without parameters, or add the path in your host.`,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
"ambiguous-route": {
|
|
138
|
+
severity: "error",
|
|
139
|
+
messages: {
|
|
140
|
+
default: paramMessage `A request cannot tell "${"operation"}" from "${"other"}", which has the same verb and route. Give each a different Content-Type on a required body, or a different fixed value of the same required query parameter or header.`,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
125
143
|
"versioning-unsupported": {
|
|
126
144
|
severity: "error",
|
|
127
145
|
messages: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Model, type ModelProperty, type Namespace, type Program, type Type } from "@typespec/compiler";
|
|
1
|
+
import { type Model, type ModelProperty, type Namespace, type Program, type Scalar, type Type } from "@typespec/compiler";
|
|
2
2
|
import type { Constraints, Documented, EncodingModel, PropertyModel, TypeDeclaration, TypeRef } from "./types.js";
|
|
3
3
|
export declare function namespacePath(namespace: Namespace | undefined): string[];
|
|
4
4
|
export declare function documentation(program: Program, type: Type): Documented;
|
|
@@ -10,6 +10,8 @@ export interface BodyRule {
|
|
|
10
10
|
/** The request phase, such as "Create"; undefined for responses. */
|
|
11
11
|
phase: string | undefined;
|
|
12
12
|
}
|
|
13
|
+
/** The scalar a type holds, looking through `T | null`, arrays and dictionaries. */
|
|
14
|
+
export declare function scalarOf(program: Program, type: Type): Scalar | undefined;
|
|
13
15
|
/** The `@encode` of a property, or of its scalar type or one that type derives from. */
|
|
14
16
|
export declare function encodingOf(program: Program, property: ModelProperty): EncodingModel | undefined;
|
|
15
17
|
/** The `@encode` of the scalar a type holds, or of one that scalar derives from. */
|
|
@@ -26,7 +26,7 @@ function isPlainRecord(program, model) {
|
|
|
26
26
|
return isRecordModelType(program, model) && model.properties.size === 0 && !model.baseModel;
|
|
27
27
|
}
|
|
28
28
|
/** The scalar a type holds, looking through `T | null`, arrays and dictionaries. */
|
|
29
|
-
function scalarOf(program, type) {
|
|
29
|
+
export function scalarOf(program, type) {
|
|
30
30
|
if (type.kind === "Scalar")
|
|
31
31
|
return type;
|
|
32
32
|
if (type.kind === "Model" && type.indexer) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { getFriendlyName, isErrorModel, serializeValueAsJson, } from "@typespec/compiler";
|
|
1
|
+
import { getAllTags, getFriendlyName, getSummary, getTags, isErrorModel, isVoidType, serializeValueAsJson, } from "@typespec/compiler";
|
|
2
2
|
import { getAllHttpServices, getPatchOptions, getServers, isBody, isVisible, resolveRequestVisibility, Visibility, isOverloadSameEndpoint, isSharedRoute, resolveAuthentication, } from "@typespec/http";
|
|
3
3
|
import { reportDiagnostic } from "../lib.js";
|
|
4
|
-
import { constraintsOf, documentation, encodingOf, isExcluded, namespacePath, TypeCollector, } from "./collect-types.js";
|
|
4
|
+
import { constraintsOf, documentation, encodingOf, heldConstraints, isExcluded, namespacePath, scalarOf, TypeCollector, } from "./collect-types.js";
|
|
5
5
|
import { exampleOf, operationExamples } from "./examples.js";
|
|
6
6
|
/** Reads a compiled program into the style-neutral model. Problems are reported as diagnostics. */
|
|
7
7
|
export function collectModel(program) {
|
|
@@ -34,6 +34,7 @@ function collectService(program, collector, service) {
|
|
|
34
34
|
namespace: namespacePath(container.namespace),
|
|
35
35
|
kind: container.kind === "Interface" ? "interface" : "namespace",
|
|
36
36
|
operations: [],
|
|
37
|
+
...tagList(getAllTags(program, container)),
|
|
37
38
|
source: container,
|
|
38
39
|
};
|
|
39
40
|
groups.set(container, group);
|
|
@@ -58,8 +59,12 @@ function collectService(program, collector, service) {
|
|
|
58
59
|
})),
|
|
59
60
|
authSchemes: authentication.schemes.map(collectScheme),
|
|
60
61
|
groups: [...groups.values()],
|
|
62
|
+
source: service.namespace,
|
|
61
63
|
};
|
|
62
64
|
}
|
|
65
|
+
function tagList(tags) {
|
|
66
|
+
return tags?.length ? { tags: [...tags] } : {};
|
|
67
|
+
}
|
|
63
68
|
function isVersioned(namespace) {
|
|
64
69
|
for (let current = namespace; current; current = current.namespace) {
|
|
65
70
|
if (current.decorators.some((applied) => applied.decorator.name === "$versioned"))
|
|
@@ -88,7 +93,8 @@ function collectScheme(auth) {
|
|
|
88
93
|
function collectOperation(program, collector, operation, group) {
|
|
89
94
|
const name = getFriendlyName(program, operation.operation) ?? operation.operation.name;
|
|
90
95
|
const usage = [group, name];
|
|
91
|
-
const
|
|
96
|
+
const declared = operation.parameters.body;
|
|
97
|
+
const body = sentBody(declared);
|
|
92
98
|
const outsideRequest = outsideBody(operation.parameters.properties);
|
|
93
99
|
const visibility = resolveRequestVisibility(program, operation.operation, operation.verb);
|
|
94
100
|
const merge = body?.contentTypes.includes("application/merge-patch+json") ?? false;
|
|
@@ -103,6 +109,10 @@ function collectOperation(program, collector, operation, group) {
|
|
|
103
109
|
return {
|
|
104
110
|
...documentation(program, operation.operation),
|
|
105
111
|
...operationExamples(program, operation.operation),
|
|
112
|
+
...tagList(getTags(program, operation.operation)),
|
|
113
|
+
...(getSummary(program, operation.operation) && {
|
|
114
|
+
summary: getSummary(program, operation.operation),
|
|
115
|
+
}),
|
|
106
116
|
name,
|
|
107
117
|
verb: operation.verb,
|
|
108
118
|
path: operation.path,
|
|
@@ -110,10 +120,14 @@ function collectOperation(program, collector, operation, group) {
|
|
|
110
120
|
sharedRoute: isSharedRoute(program, operation.operation),
|
|
111
121
|
implicitOptionality: operation.verb === "patch" &&
|
|
112
122
|
Boolean(getPatchOptions(program, operation.operation)?.implicitOptionality),
|
|
113
|
-
parameters: operation.parameters.parameters
|
|
123
|
+
parameters: operation.parameters.parameters
|
|
124
|
+
// The content type of a `void` body is not read, as in `@typespec/openapi3`.
|
|
125
|
+
.filter((parameter) => !declared || body || !isContentType(parameter))
|
|
126
|
+
.map((parameter) => collectParameter(program, collector, parameter, usage)),
|
|
114
127
|
body: requestBody && {
|
|
115
128
|
...requestBody,
|
|
116
129
|
optional: body.property?.optional ?? false,
|
|
130
|
+
...(body.property && withChecks(program, body.property)),
|
|
117
131
|
...(body.property && {
|
|
118
132
|
...documentation(program, body.property),
|
|
119
133
|
name: body.property.name,
|
|
@@ -130,7 +144,9 @@ function collectOperation(program, collector, operation, group) {
|
|
|
130
144
|
response.type,
|
|
131
145
|
...response.responses.map((content) => content.body && bodyType(content.body)),
|
|
132
146
|
].some((type) => type !== undefined && isErrorModel(program, type)),
|
|
133
|
-
contents: response.responses
|
|
147
|
+
contents: response.responses
|
|
148
|
+
.map(({ body, ...rest }) => ({ ...rest, body: sentBody(body) }))
|
|
149
|
+
.map((content) => ({
|
|
134
150
|
headers: Object.entries(content.headers ?? {}).map(([wireName, property]) => headerParameter(program, collector, wireName, property, [...usage, "response"])),
|
|
135
151
|
...(content.body?.property?.optional && { bodyOptional: true }),
|
|
136
152
|
...(content.body?.property && {
|
|
@@ -150,12 +166,38 @@ function collectOperation(program, collector, operation, group) {
|
|
|
150
166
|
function withEncoding(program, property) {
|
|
151
167
|
const encoding = encodingOf(program, property);
|
|
152
168
|
const constraints = constraintsOf(program, property);
|
|
153
|
-
|
|
169
|
+
const held = heldConstraints(program, property.type);
|
|
170
|
+
return {
|
|
171
|
+
...(encoding && { encoding }),
|
|
172
|
+
...(constraints && { constraints }),
|
|
173
|
+
...(held.length && { heldConstraints: held }),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/** A date and time in a header is an HTTP date unless the spec says otherwise. */
|
|
177
|
+
function headerEncoding(program, property) {
|
|
178
|
+
if (encodingOf(program, property))
|
|
179
|
+
return {};
|
|
180
|
+
for (let scalar = scalarOf(program, property.type); scalar; scalar = scalar.baseScalar) {
|
|
181
|
+
if (scalar.name === "utcDateTime" || scalar.name === "offsetDateTime") {
|
|
182
|
+
return { encoding: { name: "rfc7231", wireType: "string" } };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {};
|
|
186
|
+
}
|
|
187
|
+
/** The constraints on a property, apart from its encoding. */
|
|
188
|
+
function withChecks(program, property) {
|
|
189
|
+
const constraints = constraintsOf(program, property);
|
|
190
|
+
const held = heldConstraints(program, property.type);
|
|
191
|
+
return {
|
|
192
|
+
...(constraints && { constraints }),
|
|
193
|
+
...(held.length && { heldConstraints: held }),
|
|
194
|
+
};
|
|
154
195
|
}
|
|
155
196
|
function headerParameter(program, collector, wireName, property, usage) {
|
|
156
197
|
return {
|
|
157
198
|
...documentation(program, property),
|
|
158
199
|
...withEncoding(program, property),
|
|
200
|
+
...headerEncoding(program, property),
|
|
159
201
|
...exampleOf(program, property),
|
|
160
202
|
name: property.name,
|
|
161
203
|
wireName,
|
|
@@ -171,6 +213,7 @@ function collectParameter(program, collector, parameter, usage) {
|
|
|
171
213
|
return {
|
|
172
214
|
...documentation(program, property),
|
|
173
215
|
...withEncoding(program, property),
|
|
216
|
+
...(parameter.type === "header" && headerEncoding(program, property)),
|
|
174
217
|
...exampleOf(program, property),
|
|
175
218
|
name: property.name,
|
|
176
219
|
wireName: parameter.name,
|
|
@@ -248,6 +291,13 @@ function outsideBody(properties) {
|
|
|
248
291
|
const inBody = ["body", "bodyRoot", "bodyProperty", "multipartBody"];
|
|
249
292
|
return new Set(properties.filter((item) => !inBody.includes(item.kind)).map((item) => item.property));
|
|
250
293
|
}
|
|
294
|
+
/** `@body body: void` declares that nothing is sent, as it does for `@typespec/openapi3`. */
|
|
295
|
+
function sentBody(body) {
|
|
296
|
+
return body?.bodyKind === "single" && isVoidType(body.type) ? undefined : body;
|
|
297
|
+
}
|
|
298
|
+
function isContentType(parameter) {
|
|
299
|
+
return parameter.type === "header" && parameter.name.toLowerCase() === "content-type";
|
|
300
|
+
}
|
|
251
301
|
function bodyType(body) {
|
|
252
302
|
return body.bodyKind === "multipart" ? undefined : body.type;
|
|
253
303
|
}
|
|
@@ -197,6 +197,8 @@ export interface ParameterModel extends Documented {
|
|
|
197
197
|
explode: boolean;
|
|
198
198
|
encoding?: EncodingModel;
|
|
199
199
|
constraints?: Constraints;
|
|
200
|
+
/** Constraints on what an array parameter holds. */
|
|
201
|
+
heldConstraints?: string[];
|
|
200
202
|
example?: Sample;
|
|
201
203
|
/** Path parameters whose reserved characters, such as "/", are not escaped. */
|
|
202
204
|
allowReserved?: boolean;
|
|
@@ -283,12 +285,18 @@ export interface OperationModel extends Documented {
|
|
|
283
285
|
optional: boolean;
|
|
284
286
|
name?: string;
|
|
285
287
|
doc?: string;
|
|
288
|
+
constraints?: Constraints;
|
|
289
|
+
heldConstraints?: string[];
|
|
286
290
|
}) | undefined;
|
|
287
291
|
responses: ResponseModel[];
|
|
288
292
|
/** Alternatives, any one of which is enough. Empty when the spec says nothing. */
|
|
289
293
|
auth: AuthOptionModel[];
|
|
290
294
|
/** What each `@opExample` returns. */
|
|
291
295
|
examples?: Sample[];
|
|
296
|
+
/** The `@summary` of the operation. */
|
|
297
|
+
summary?: string;
|
|
298
|
+
/** Names given by `@tag` on the operation itself. */
|
|
299
|
+
tags?: string[];
|
|
292
300
|
source: Type;
|
|
293
301
|
}
|
|
294
302
|
/** The operations of one TypeSpec interface, or those declared directly in a namespace. */
|
|
@@ -297,6 +305,8 @@ export interface GroupModel extends Documented {
|
|
|
297
305
|
namespace: string[];
|
|
298
306
|
kind: "interface" | "namespace";
|
|
299
307
|
operations: OperationModel[];
|
|
308
|
+
/** Names given by `@tag` on the group and the namespaces around it. */
|
|
309
|
+
tags?: string[];
|
|
300
310
|
source: Type;
|
|
301
311
|
}
|
|
302
312
|
export interface ServiceModel extends Documented {
|
|
@@ -308,6 +318,8 @@ export interface ServiceModel extends Documented {
|
|
|
308
318
|
}[];
|
|
309
319
|
authSchemes: AuthSchemeModel[];
|
|
310
320
|
groups: GroupModel[];
|
|
321
|
+
/** The service namespace. */
|
|
322
|
+
source: Type;
|
|
311
323
|
}
|
|
312
324
|
export interface ApiModel {
|
|
313
325
|
services: ServiceModel[];
|
|
@@ -19,4 +19,4 @@ export interface Names {
|
|
|
19
19
|
* Chooses C# names. The result depends only on the model, so it is the same on every run. A name
|
|
20
20
|
* is numbered only when nothing better is free, and that is always reported.
|
|
21
21
|
*/
|
|
22
|
-
export declare function assignNames(program: Program, model: ApiModel, rootNamespace: string, setupNames?: string[]): Names;
|
|
22
|
+
export declare function assignNames(program: Program, model: ApiModel, rootNamespace: string, setupNames?: string[], style?: "minimal" | "controllers"): Names;
|
package/dist/src/naming/names.js
CHANGED
|
@@ -10,7 +10,14 @@ const frameworkTypes = ("Action Array Attribute Boolean Byte CancellationToken C
|
|
|
10
10
|
"Program ServiceCollection WebApplication " +
|
|
11
11
|
"Object Single String System Task TimeOnly TimeSpan Type Uri Value ValueTask").split(" ");
|
|
12
12
|
// Namespaces below the root that hold the emitter's own code.
|
|
13
|
-
const ownNamespaces = [
|
|
13
|
+
const ownNamespaces = [
|
|
14
|
+
"Serialization",
|
|
15
|
+
"Validation",
|
|
16
|
+
"Requests",
|
|
17
|
+
"Responses",
|
|
18
|
+
"Routing",
|
|
19
|
+
"Documentation",
|
|
20
|
+
];
|
|
14
21
|
// Names a record already has, which a property cannot take.
|
|
15
22
|
const recordMembers = new Set("Clone Deconstruct EqualityContract Equals GetHashCode GetType PrintMembers ToString".split(" "));
|
|
16
23
|
// File names that Windows does not allow.
|
|
@@ -37,7 +44,7 @@ function shorten(name) {
|
|
|
37
44
|
* Chooses C# names. The result depends only on the model, so it is the same on every run. A name
|
|
38
45
|
* is numbered only when nothing better is free, and that is always reported.
|
|
39
46
|
*/
|
|
40
|
-
export function assignNames(program, model, rootNamespace, setupNames = []) {
|
|
47
|
+
export function assignNames(program, model, rootNamespace, setupNames = [], style = "minimal") {
|
|
41
48
|
const declarations = [...model.types.values()].filter((type) => type.kind !== "scalar");
|
|
42
49
|
const types = new Map();
|
|
43
50
|
// Compared without case, because file names on macOS and Windows ignore it. A type cannot
|
|
@@ -60,6 +67,9 @@ export function assignNames(program, model, rootNamespace, setupNames = []) {
|
|
|
60
67
|
namespaces.add(`${rootNamespace}.Serialization`.toLowerCase());
|
|
61
68
|
namespaces.add(`${rootNamespace}.Validation`.toLowerCase());
|
|
62
69
|
namespaces.add(`${rootNamespace}.Requests`.toLowerCase());
|
|
70
|
+
namespaces.add(`${rootNamespace}.Routing`.toLowerCase());
|
|
71
|
+
namespaces.add(`${rootNamespace}.Responses`.toLowerCase());
|
|
72
|
+
namespaces.add(`${rootNamespace}.Documentation`.toLowerCase());
|
|
63
73
|
// A type named like part of the root namespace, or like a common .NET type, would hide it.
|
|
64
74
|
const reserved = new Set([...rootNamespace.split("."), ...frameworkTypes, ...setupNames].map((name) => name.toLowerCase()));
|
|
65
75
|
// Types written by name choose first, so a derived or inline type never takes their name.
|
|
@@ -189,7 +199,7 @@ export function assignNames(program, model, rootNamespace, setupNames = []) {
|
|
|
189
199
|
const describe = (ref) => ref.kind === "named"
|
|
190
200
|
? (types.get(ref.id)?.name ?? argumentName(model, ref))
|
|
191
201
|
: argumentName(model, ref);
|
|
192
|
-
const services = nameServices(program, model, rootNamespace, (namespace) => namespaceOf(model, rootNamespace, namespace), claim, describe);
|
|
202
|
+
const services = nameServices(program, model, rootNamespace, (namespace) => namespaceOf(model, rootNamespace, namespace), claim, describe, style);
|
|
193
203
|
// Report in table order whatever order the names were chosen in.
|
|
194
204
|
return { types: new Map(declarations.map((t) => [t.id, types.get(t.id)])), members, services };
|
|
195
205
|
}
|
|
@@ -28,6 +28,16 @@ export interface ServiceName {
|
|
|
28
28
|
name: string;
|
|
29
29
|
file: string;
|
|
30
30
|
operations: OperationName[];
|
|
31
|
+
endpoints: EndpointsName;
|
|
32
|
+
}
|
|
33
|
+
/** The class mapping the routes of a group, or its controller, and the names inside it. */
|
|
34
|
+
export interface EndpointsName {
|
|
35
|
+
name: string;
|
|
36
|
+
file: string;
|
|
37
|
+
/** The extension method mapping the group, which a controller does not have. */
|
|
38
|
+
map: string;
|
|
39
|
+
/** The handler for each of `operations`, in order. */
|
|
40
|
+
handlers: string[];
|
|
31
41
|
}
|
|
32
42
|
/**
|
|
33
43
|
* Names the service interfaces, their methods and their result unions. `claim` takes a type name in
|
|
@@ -36,4 +46,4 @@ export interface ServiceName {
|
|
|
36
46
|
export declare function nameServices(program: Program, model: ApiModel, rootNamespace: string, namespaceOf: (namespace: string[]) => string[], claim: (relative: string[], wanted: string, target: Type) => {
|
|
37
47
|
name: string;
|
|
38
48
|
file: string;
|
|
39
|
-
}, describe: (ref: TypeRef) => string): ServiceName[];
|
|
49
|
+
}, describe: (ref: TypeRef) => string, style?: "minimal" | "controllers"): ServiceName[];
|
|
@@ -98,8 +98,8 @@ function caseNames(program, operation, union, describe) {
|
|
|
98
98
|
* Names the service interfaces, their methods and their result unions. `claim` takes a type name in
|
|
99
99
|
* a namespace, avoiding every name already used there.
|
|
100
100
|
*/
|
|
101
|
-
export function nameServices(program, model, rootNamespace, namespaceOf, claim, describe) {
|
|
102
|
-
|
|
101
|
+
export function nameServices(program, model, rootNamespace, namespaceOf, claim, describe, style = "minimal") {
|
|
102
|
+
const services = model.services.flatMap((service) => service.groups.map((group) => {
|
|
103
103
|
const relative = namespaceOf(group.namespace);
|
|
104
104
|
// Operations outside any namespace have a group without a name.
|
|
105
105
|
const groupName = pascalCase(group.name || rootNamespace.split(".").at(-1));
|
|
@@ -123,6 +123,21 @@ export function nameServices(program, model, rootNamespace, namespaceOf, claim,
|
|
|
123
123
|
cases: caseNames(program, operation, result.name, describe),
|
|
124
124
|
};
|
|
125
125
|
});
|
|
126
|
-
|
|
126
|
+
const namespace = [rootNamespace, ...relative].join(".");
|
|
127
|
+
return { group, namespace, name, file, operations, relative, groupName };
|
|
127
128
|
}));
|
|
129
|
+
// Claimed last, so that no interface or result depends on the names of the endpoints.
|
|
130
|
+
return services.map(({ relative, groupName, ...service }) => {
|
|
131
|
+
const controllers = style === "controllers";
|
|
132
|
+
const suffix = controllers ? "Controller" : "Endpoints";
|
|
133
|
+
const claimed = claim(relative, `${groupName}${suffix}`, service.group.source);
|
|
134
|
+
const map = `Map${claimed.name}`;
|
|
135
|
+
// A controller's actions must not take the name of a method of its base class.
|
|
136
|
+
const handler = chooser(program, [
|
|
137
|
+
claimed.name,
|
|
138
|
+
...(controllers ? ["TryUpdateModelAsync"] : [map]),
|
|
139
|
+
]);
|
|
140
|
+
const handlers = service.operations.map((o) => handler(o.method, o.operation.source));
|
|
141
|
+
return { ...service, endpoints: { ...claimed, map, handlers } };
|
|
142
|
+
});
|
|
128
143
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ServiceName } from "../../naming/services.js";
|
|
2
|
+
import type { Binding } from "../shared/binding.js";
|
|
3
|
+
import type { Documentation } from "../shared/documentation.js";
|
|
4
|
+
import type { RenderedFile } from "../shared/models.js";
|
|
5
|
+
import type { Routing } from "../shared/routes.js";
|
|
6
|
+
/** What an action reads and answers. */
|
|
7
|
+
export interface Action {
|
|
8
|
+
binding: Binding;
|
|
9
|
+
/** The arms of the switch over the service's result. */
|
|
10
|
+
arms: string[];
|
|
11
|
+
/** The attributes declaring what the action answers. */
|
|
12
|
+
responses: string[];
|
|
13
|
+
documentation: Documentation;
|
|
14
|
+
}
|
|
15
|
+
/** Renders the controller of every group. */
|
|
16
|
+
export declare function renderControllers(rootNamespace: string, services: ServiceName[], routing: Routing, handle: (service: ServiceName, index: number) => Action): RenderedFile[];
|
|
17
|
+
/** The generated action results for what MVC's do not send. */
|
|
18
|
+
export declare function renderActionResults(rootNamespace: string): string;
|
|
19
|
+
/** The generated binders and filter, which bind and check requests as Minimal APIs do. */
|
|
20
|
+
export declare function renderActionBinding(rootNamespace: string): string;
|