sheridan-typespec-aspnet 0.3.0 → 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 +4 -4
- package/dist/src/emitter.js +72 -41
- package/dist/src/lib.d.ts +17 -2
- package/dist/src/lib.js +6 -0
- package/dist/src/model/collect.js +18 -4
- package/dist/src/model/types.d.ts +2 -0
- package/dist/src/naming/names.d.ts +1 -1
- package/dist/src/naming/names.js +2 -2
- package/dist/src/naming/services.d.ts +3 -3
- package/dist/src/naming/services.js +9 -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 +2 -2
- package/dist/src/render/minimal/endpoints.js +23 -1
- package/dist/src/render/minimal/results.js +33 -88
- package/dist/src/render/shared/api-setup.d.ts +4 -1
- package/dist/src/render/shared/api-setup.js +58 -24
- package/dist/src/render/shared/binding.d.ts +11 -1
- package/dist/src/render/shared/binding.js +52 -21
- 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/openapi.d.ts +1 -1
- package/dist/src/render/shared/openapi.js +88 -3
- package/dist/src/render/shared/request-selector.d.ts +1 -1
- package/dist/src/render/shared/request-selector.js +58 -5
- 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.js +36 -3
- package/package.json +1 -1
- package/dist/src/render/minimal/documentation.d.ts +0 -4
- package/dist/src/render/minimal/documentation.js +0 -107
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# Sheridan.Typespec.Aspnet
|
|
2
2
|
|
|
3
|
-
A [TypeSpec](https://typespec.io) emitter that generates ASP.NET Core servers in C#. It generates Minimal APIs
|
|
3
|
+
A [TypeSpec](https://typespec.io) emitter that generates ASP.NET Core servers in C#. It generates Minimal APIs or controllers.
|
|
4
4
|
|
|
5
5
|
**Status: early development.** From a spec, the emitter writes:
|
|
6
6
|
|
|
7
7
|
- C# models, enums and unions, with JSON converters, one JSON setup and validation attributes;
|
|
8
8
|
- a service interface with result types for your code to implement, with a context that gives it the request;
|
|
9
|
-
- with the `
|
|
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
10
|
- a solution with a host project that builds and runs, and a stub for each service, registered with dependency injection.
|
|
11
11
|
|
|
12
|
-
|
|
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).
|
|
13
13
|
|
|
14
14
|
Install it from npm (links to files and issues here need access to the repository):
|
|
15
15
|
|
|
@@ -17,7 +17,7 @@ Install it from npm (links to files and issues here need access to the repositor
|
|
|
17
17
|
npm install --save-dev sheridan-typespec-aspnet
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-

|
|
21
21
|
|
|
22
22
|
## Options
|
|
23
23
|
|
package/dist/src/emitter.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
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";
|
|
4
6
|
import { reportDiagnostic } from "./lib.js";
|
|
5
7
|
import { resolveOptions } from "./options.js";
|
|
6
|
-
import { documentationWriter } from "./render/minimal/documentation.js";
|
|
7
8
|
import { renderEndpoints } from "./render/minimal/endpoints.js";
|
|
8
9
|
import { resultWriter } from "./render/minimal/results.js";
|
|
9
10
|
import { renderApiSetup } from "./render/shared/api-setup.js";
|
|
10
11
|
import { bindingWriter } from "./render/shared/binding.js";
|
|
12
|
+
import { bodyShapes } from "./render/shared/body-formats.js";
|
|
11
13
|
import { renderConverters } from "./render/shared/converters.js";
|
|
12
14
|
import { headerWriter } from "./render/shared/headers.js";
|
|
13
15
|
import { renderJsonSetup } from "./render/shared/json-setup.js";
|
|
14
16
|
import { renderModels } from "./render/shared/models.js";
|
|
17
|
+
import { documentationWriter } from "./render/shared/documentation.js";
|
|
15
18
|
import { renderOpenApi } from "./render/shared/openapi.js";
|
|
16
19
|
import { renderParameters, renderResponses } from "./render/shared/parameters.js";
|
|
17
20
|
import { renderHttpRequestContext, renderRequestContext } from "./render/shared/request-context.js";
|
|
@@ -35,9 +38,7 @@ export async function $onEmit(context) {
|
|
|
35
38
|
}
|
|
36
39
|
const model = collectModel(context.program);
|
|
37
40
|
const projects = projectNames(options.projectName);
|
|
38
|
-
const names = assignNames(context.program, model, options.rootNamespace, [
|
|
39
|
-
`${projects.api}ApiExtensions`,
|
|
40
|
-
]);
|
|
41
|
+
const names = assignNames(context.program, model, options.rootNamespace, [`${projects.api}ApiExtensions`], options.style);
|
|
41
42
|
const services = renderServices(context.program, model, names, options);
|
|
42
43
|
const files = [
|
|
43
44
|
{
|
|
@@ -55,73 +56,103 @@ export async function $onEmit(context) {
|
|
|
55
56
|
content: renderHttpRequestContext(options.rootNamespace),
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
-
const routing =
|
|
60
|
-
const
|
|
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;
|
|
61
63
|
// A route that cannot be mapped leaves nothing to render.
|
|
62
64
|
if (context.program.hasError()) {
|
|
63
65
|
return;
|
|
64
66
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
};
|
|
72
96
|
}));
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const describe = documentationWriter(model, options.rootNamespace, options.openapi);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
76
99
|
const respond = resultWriter(context.program, model, options.rootNamespace, headers);
|
|
77
100
|
files.push(...renderEndpoints(options.rootNamespace, names.services, routing, (service, index) => {
|
|
78
|
-
const found =
|
|
79
|
-
const contract = `global::${service.namespace}.${service.name}`;
|
|
80
|
-
const writes = found.cases.some((c) => c.found.content.headers.length > 0);
|
|
81
|
-
const binding = bind(found.operation, contract, found.parameters, writes);
|
|
82
|
-
const contextName = binding.parameters.find((p) => p.source === "context")?.name;
|
|
101
|
+
const { found, binding } = read(service, index);
|
|
83
102
|
return {
|
|
84
103
|
binding,
|
|
85
|
-
result: respond(found, binding.found,
|
|
86
|
-
|
|
87
|
-
describe: (endpoint) => describe(root.has(service.group) ? "" : service.group.name, found, binding, endpoint),
|
|
104
|
+
result: respond(found, binding.found, binding.context, service.namespace),
|
|
105
|
+
documentation: documentationOf(service, found, binding),
|
|
88
106
|
};
|
|
89
107
|
}));
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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) {
|
|
94
115
|
files.push({
|
|
95
|
-
path: "
|
|
96
|
-
content:
|
|
116
|
+
path: "Responses/ActionResults.cs",
|
|
117
|
+
content: renderActionResults(options.rootNamespace),
|
|
118
|
+
}, {
|
|
119
|
+
path: "Requests/ActionBinding.cs",
|
|
120
|
+
content: renderActionBinding(options.rootNamespace),
|
|
97
121
|
});
|
|
98
122
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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;
|
|
102
132
|
}
|
|
103
133
|
if (selector) {
|
|
104
134
|
files.push({
|
|
105
135
|
path: "Routing/RequestSelector.cs",
|
|
106
|
-
content: renderRequestSelector(options.rootNamespace),
|
|
136
|
+
content: renderRequestSelector(options.rootNamespace, controllers),
|
|
107
137
|
});
|
|
108
138
|
}
|
|
109
139
|
files.push({
|
|
110
140
|
path: `${projects.api}ApiExtensions.cs`,
|
|
111
141
|
content: renderApiSetup(options.rootNamespace, projects.api, registrations(names, projects.host, options.scaffoldHost), {
|
|
112
|
-
calls:
|
|
113
|
-
?
|
|
114
|
-
:
|
|
142
|
+
calls: controllers
|
|
143
|
+
? []
|
|
144
|
+
: names.services.map(({ namespace, endpoints }) => `global::${namespace}.${endpoints.name}.${endpoints.map}`),
|
|
115
145
|
selector,
|
|
116
|
-
|
|
117
|
-
|
|
146
|
+
controllers: controllers && names.services.length > 0,
|
|
147
|
+
checks: names.services.length > 0,
|
|
148
|
+
openapi: openapi && names.services.length > 0,
|
|
118
149
|
}),
|
|
119
150
|
});
|
|
120
151
|
// Nothing is written when the spec holds something that cannot be generated.
|
|
121
152
|
if (context.program.hasError()) {
|
|
122
153
|
return;
|
|
123
154
|
}
|
|
124
|
-
const project = generatedProject(projects,
|
|
155
|
+
const project = generatedProject(projects, openapi);
|
|
125
156
|
const folder = resolvePath(context.emitterOutputDir, projects.generated);
|
|
126
157
|
const generated = [
|
|
127
158
|
{ ...project, path: project.path.slice(projects.generated.length + 1) },
|
package/dist/src/lib.d.ts
CHANGED
|
@@ -45,6 +45,9 @@ export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
|
|
|
45
45
|
"unsupported-route": {
|
|
46
46
|
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
47
47
|
};
|
|
48
|
+
"server-path-ignored": {
|
|
49
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
50
|
+
};
|
|
48
51
|
"ambiguous-route": {
|
|
49
52
|
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
50
53
|
};
|
|
@@ -76,7 +79,7 @@ export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
|
|
|
76
79
|
readonly default: import("@typespec/compiler").CallableMessage<["option"]>;
|
|
77
80
|
};
|
|
78
81
|
}, AspNetEmitterOptions, never>;
|
|
79
|
-
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" | "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 {
|
|
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 {
|
|
80
83
|
"invalid-name": {
|
|
81
84
|
readonly default: import("@typespec/compiler").CallableMessage<["option", "value"]>;
|
|
82
85
|
};
|
|
@@ -110,6 +113,9 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
110
113
|
"unsupported-route": {
|
|
111
114
|
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
112
115
|
};
|
|
116
|
+
"server-path-ignored": {
|
|
117
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
118
|
+
};
|
|
113
119
|
"ambiguous-route": {
|
|
114
120
|
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
115
121
|
};
|
|
@@ -174,6 +180,9 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
174
180
|
"unsupported-route": {
|
|
175
181
|
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
176
182
|
};
|
|
183
|
+
"server-path-ignored": {
|
|
184
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
185
|
+
};
|
|
177
186
|
"ambiguous-route": {
|
|
178
187
|
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
179
188
|
};
|
|
@@ -204,7 +213,7 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
204
213
|
"requires-host": {
|
|
205
214
|
readonly default: import("@typespec/compiler").CallableMessage<["option"]>;
|
|
206
215
|
};
|
|
207
|
-
}, 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" | "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 {
|
|
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 {
|
|
208
217
|
"invalid-name": {
|
|
209
218
|
readonly default: import("@typespec/compiler").CallableMessage<["option", "value"]>;
|
|
210
219
|
};
|
|
@@ -238,6 +247,9 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
238
247
|
"unsupported-route": {
|
|
239
248
|
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
240
249
|
};
|
|
250
|
+
"server-path-ignored": {
|
|
251
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
252
|
+
};
|
|
241
253
|
"ambiguous-route": {
|
|
242
254
|
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
243
255
|
};
|
|
@@ -302,6 +314,9 @@ export declare const reportDiagnostic: <C extends "invalid-name" | "invalid-serv
|
|
|
302
314
|
"unsupported-route": {
|
|
303
315
|
readonly default: import("@typespec/compiler").CallableMessage<["reason"]>;
|
|
304
316
|
};
|
|
317
|
+
"server-path-ignored": {
|
|
318
|
+
readonly default: import("@typespec/compiler").CallableMessage<["paths"]>;
|
|
319
|
+
};
|
|
305
320
|
"ambiguous-route": {
|
|
306
321
|
readonly default: import("@typespec/compiler").CallableMessage<["operation", "other"]>;
|
|
307
322
|
};
|
package/dist/src/lib.js
CHANGED
|
@@ -128,6 +128,12 @@ export const $lib = createTypeSpecLibrary({
|
|
|
128
128
|
default: paramMessage `This route cannot be generated: ${"reason"}.`,
|
|
129
129
|
},
|
|
130
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
|
+
},
|
|
131
137
|
"ambiguous-route": {
|
|
132
138
|
severity: "error",
|
|
133
139
|
messages: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getAllTags, getFriendlyName, getSummary, getTags, 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
4
|
import { constraintsOf, documentation, encodingOf, heldConstraints, isExcluded, namespacePath, scalarOf, TypeCollector, } from "./collect-types.js";
|
|
@@ -59,6 +59,7 @@ function collectService(program, collector, service) {
|
|
|
59
59
|
})),
|
|
60
60
|
authSchemes: authentication.schemes.map(collectScheme),
|
|
61
61
|
groups: [...groups.values()],
|
|
62
|
+
source: service.namespace,
|
|
62
63
|
};
|
|
63
64
|
}
|
|
64
65
|
function tagList(tags) {
|
|
@@ -92,7 +93,8 @@ function collectScheme(auth) {
|
|
|
92
93
|
function collectOperation(program, collector, operation, group) {
|
|
93
94
|
const name = getFriendlyName(program, operation.operation) ?? operation.operation.name;
|
|
94
95
|
const usage = [group, name];
|
|
95
|
-
const
|
|
96
|
+
const declared = operation.parameters.body;
|
|
97
|
+
const body = sentBody(declared);
|
|
96
98
|
const outsideRequest = outsideBody(operation.parameters.properties);
|
|
97
99
|
const visibility = resolveRequestVisibility(program, operation.operation, operation.verb);
|
|
98
100
|
const merge = body?.contentTypes.includes("application/merge-patch+json") ?? false;
|
|
@@ -118,7 +120,10 @@ function collectOperation(program, collector, operation, group) {
|
|
|
118
120
|
sharedRoute: isSharedRoute(program, operation.operation),
|
|
119
121
|
implicitOptionality: operation.verb === "patch" &&
|
|
120
122
|
Boolean(getPatchOptions(program, operation.operation)?.implicitOptionality),
|
|
121
|
-
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)),
|
|
122
127
|
body: requestBody && {
|
|
123
128
|
...requestBody,
|
|
124
129
|
optional: body.property?.optional ?? false,
|
|
@@ -139,7 +144,9 @@ function collectOperation(program, collector, operation, group) {
|
|
|
139
144
|
response.type,
|
|
140
145
|
...response.responses.map((content) => content.body && bodyType(content.body)),
|
|
141
146
|
].some((type) => type !== undefined && isErrorModel(program, type)),
|
|
142
|
-
contents: response.responses
|
|
147
|
+
contents: response.responses
|
|
148
|
+
.map(({ body, ...rest }) => ({ ...rest, body: sentBody(body) }))
|
|
149
|
+
.map((content) => ({
|
|
143
150
|
headers: Object.entries(content.headers ?? {}).map(([wireName, property]) => headerParameter(program, collector, wireName, property, [...usage, "response"])),
|
|
144
151
|
...(content.body?.property?.optional && { bodyOptional: true }),
|
|
145
152
|
...(content.body?.property && {
|
|
@@ -284,6 +291,13 @@ function outsideBody(properties) {
|
|
|
284
291
|
const inBody = ["body", "bodyRoot", "bodyProperty", "multipartBody"];
|
|
285
292
|
return new Set(properties.filter((item) => !inBody.includes(item.kind)).map((item) => item.property));
|
|
286
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
|
+
}
|
|
287
301
|
function bodyType(body) {
|
|
288
302
|
return body.bodyKind === "multipart" ? undefined : body.type;
|
|
289
303
|
}
|
|
@@ -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
|
@@ -44,7 +44,7 @@ function shorten(name) {
|
|
|
44
44
|
* Chooses C# names. The result depends only on the model, so it is the same on every run. A name
|
|
45
45
|
* is numbered only when nothing better is free, and that is always reported.
|
|
46
46
|
*/
|
|
47
|
-
export function assignNames(program, model, rootNamespace, setupNames = []) {
|
|
47
|
+
export function assignNames(program, model, rootNamespace, setupNames = [], style = "minimal") {
|
|
48
48
|
const declarations = [...model.types.values()].filter((type) => type.kind !== "scalar");
|
|
49
49
|
const types = new Map();
|
|
50
50
|
// Compared without case, because file names on macOS and Windows ignore it. A type cannot
|
|
@@ -199,7 +199,7 @@ export function assignNames(program, model, rootNamespace, setupNames = []) {
|
|
|
199
199
|
const describe = (ref) => ref.kind === "named"
|
|
200
200
|
? (types.get(ref.id)?.name ?? argumentName(model, ref))
|
|
201
201
|
: argumentName(model, ref);
|
|
202
|
-
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);
|
|
203
203
|
// Report in table order whatever order the names were chosen in.
|
|
204
204
|
return { types: new Map(declarations.map((t) => [t.id, types.get(t.id)])), members, services };
|
|
205
205
|
}
|
|
@@ -30,11 +30,11 @@ export interface ServiceName {
|
|
|
30
30
|
operations: OperationName[];
|
|
31
31
|
endpoints: EndpointsName;
|
|
32
32
|
}
|
|
33
|
-
/** The class mapping the routes of a group, and the names inside it. */
|
|
33
|
+
/** The class mapping the routes of a group, or its controller, and the names inside it. */
|
|
34
34
|
export interface EndpointsName {
|
|
35
35
|
name: string;
|
|
36
36
|
file: string;
|
|
37
|
-
/** The extension method mapping the group. */
|
|
37
|
+
/** The extension method mapping the group, which a controller does not have. */
|
|
38
38
|
map: string;
|
|
39
39
|
/** The handler for each of `operations`, in order. */
|
|
40
40
|
handlers: string[];
|
|
@@ -46,4 +46,4 @@ export interface EndpointsName {
|
|
|
46
46
|
export declare function nameServices(program: Program, model: ApiModel, rootNamespace: string, namespaceOf: (namespace: string[]) => string[], claim: (relative: string[], wanted: string, target: Type) => {
|
|
47
47
|
name: string;
|
|
48
48
|
file: string;
|
|
49
|
-
}, describe: (ref: TypeRef) => string): ServiceName[];
|
|
49
|
+
}, describe: (ref: TypeRef) => string, style?: "minimal" | "controllers"): ServiceName[];
|
|
@@ -98,7 +98,7 @@ 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) {
|
|
101
|
+
export function nameServices(program, model, rootNamespace, namespaceOf, claim, describe, style = "minimal") {
|
|
102
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.
|
|
@@ -128,9 +128,15 @@ export function nameServices(program, model, rootNamespace, namespaceOf, claim,
|
|
|
128
128
|
}));
|
|
129
129
|
// Claimed last, so that no interface or result depends on the names of the endpoints.
|
|
130
130
|
return services.map(({ relative, groupName, ...service }) => {
|
|
131
|
-
const
|
|
131
|
+
const controllers = style === "controllers";
|
|
132
|
+
const suffix = controllers ? "Controller" : "Endpoints";
|
|
133
|
+
const claimed = claim(relative, `${groupName}${suffix}`, service.group.source);
|
|
132
134
|
const map = `Map${claimed.name}`;
|
|
133
|
-
|
|
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
|
+
]);
|
|
134
140
|
const handlers = service.operations.map((o) => handler(o.method, o.operation.source));
|
|
135
141
|
return { ...service, endpoints: { ...claimed, map, handlers } };
|
|
136
142
|
});
|
|
@@ -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;
|