typespec-typescript-emitter 1.1.0 → 1.1.1
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/CHANGELOG.md +12 -9
- package/README.md +128 -22
- package/dist/src/emit_mapped_types.js +63 -20
- package/dist/src/emit_mapped_types.js.map +1 -1
- package/dist/src/emit_routes.js +9 -6
- package/dist/src/emit_routes.js.map +1 -1
- package/dist/src/emit_types.js +18 -3
- package/dist/src/emit_types.js.map +1 -1
- package/dist/src/emit_types_resolve.d.ts +17 -7
- package/dist/src/emit_types_resolve.js +47 -31
- package/dist/src/emit_types_resolve.js.map +1 -1
- package/package.json +1 -1
- package/src/emit_mapped_types.ts +86 -31
- package/src/emit_routes.ts +15 -9
- package/src/emit_types.ts +19 -3
- package/src/emit_types_resolve.ts +66 -74
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## [1.1.1](https://github.com/crowbait/typespec-typescript-emitter/compare/v1.1.0...v1.1.1) (2025-09-26)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* routed typemap respects visibility modifiers ([13c0a45](https://github.com/crowbait/typespec-typescript-emitter/commit/13c0a45f38b949a36ad2e8b6c40a1829ff3b54da))
|
|
7
|
+
* routed-typemap handling of multiple verbs on same path ([f33e6d9](https://github.com/crowbait/typespec-typescript-emitter/commit/f33e6d94d78523206813ab4191feee29cb6c3b2e))
|
|
8
|
+
* routes / routed typemap emitters now resolve ops inside interfaces ([21504bc](https://github.com/crowbait/typespec-typescript-emitter/commit/21504bc74cc4f77aeac1f1bcb4e52a42917d9146))
|
|
9
|
+
* thorough resolution of nested types in typemap ([95af299](https://github.com/crowbait/typespec-typescript-emitter/commit/95af299b662dbcba0d21b56aa8ebe2f91c6e6e92))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
1
13
|
# [1.1.0](https://github.com/crowbait/typespec-typescript-emitter/compare/v1.0.0...v1.1.0) (2025-05-23)
|
|
2
14
|
|
|
3
15
|
|
|
@@ -37,12 +49,3 @@
|
|
|
37
49
|
|
|
38
50
|
|
|
39
51
|
|
|
40
|
-
## [0.3.5](https://github.com/crowbait/typespec-typescript-emitter/compare/v0.3.4...v0.3.5) (2025-02-23)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
### Bug Fixes
|
|
44
|
-
|
|
45
|
-
* ci (no skip on chore-only) ([75b2e5d](https://github.com/crowbait/typespec-typescript-emitter/commit/75b2e5d4346fd17de5c7baf689de6e4cac259542))
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
package/README.md
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
# typespec-typescript-emitter
|
|
2
2
|
|
|
3
|
-
This is a [TypeSpec](https://typespec.io) library aiming to provide
|
|
4
|
-
TypeScript output to a TypeSpec project.
|
|
3
|
+
This is a [TypeSpec](https://typespec.io) library aiming to provide TypeScript output to a TypeSpec project.
|
|
5
4
|
|
|
6
|
-
Currently, this library is tailored to my specific use case, which is defining
|
|
7
|
-
|
|
5
|
+
Currently, this library is tailored to my specific use case, which is defining HTTP APIs.
|
|
6
|
+
The 'routes'-emitter will only work on HTTP operations. **However**, exporting all models as types is independent of HTTP, and so may also benefit projects with a different usage scenario.
|
|
8
7
|
|
|
9
8
|
It can the following things:
|
|
10
9
|
|
|
11
|
-
- ts files exporting every model present in
|
|
12
|
-
- 1 file for each
|
|
10
|
+
- ts files exporting every model present in a namespace
|
|
11
|
+
- 1 file for each nested namespace
|
|
13
12
|
- exports models, enums and unions
|
|
14
13
|
- does NOT export aliases (see below)
|
|
15
14
|
- optional typeguards, *if* type export is enabled
|
|
16
|
-
|
|
17
|
-
- for `TypeSpec.Http`:
|
|
18
|
-
- this can be imported at runtime to provide a robust way of eg. accessing URLs
|
|
15
|
+
- for `TypeSpec.Http`: ts file containing a nested object (by namespace-opname) containing information about every route (eg. url-from-parameters, method, etc.)
|
|
16
|
+
- for `TypeSpec.Http`: "routed typemap" mapping types to their routes (path and verb) (respects Lifecycle visibility)
|
|
19
17
|
|
|
20
18
|
## Content <!-- omit from toc -->
|
|
21
19
|
|
|
22
|
-
- [
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
25
|
-
- [
|
|
26
|
-
- [
|
|
20
|
+
- [typespec-typescript-emitter](#typespec-typescript-emitter)
|
|
21
|
+
- [Installation](#installation)
|
|
22
|
+
- [Configuration](#configuration)
|
|
23
|
+
- [Types Emitter](#types-emitter)
|
|
24
|
+
- [Aliases](#aliases)
|
|
25
|
+
- [Routes Emitter](#routes-emitter)
|
|
26
|
+
- [Routed Typemap](#routed-typemap)
|
|
27
27
|
|
|
28
28
|
## Installation
|
|
29
29
|
|
|
@@ -45,6 +45,7 @@ options:
|
|
|
45
45
|
enable-types: true
|
|
46
46
|
enable-typeguards: false
|
|
47
47
|
enable-routes: false
|
|
48
|
+
enable-routed-typemap: false
|
|
48
49
|
```
|
|
49
50
|
|
|
50
51
|
The following options are available:
|
|
@@ -54,15 +55,21 @@ The following options are available:
|
|
|
54
55
|
- `enable-types` (default: true): enables output of TypeScript types.
|
|
55
56
|
- `enable-typeguards` (default: false): enables output of typeguards, *IF* type-output is enabled.
|
|
56
57
|
- `enable-routes` (default: false): enables output of the HTTP-routes object.
|
|
58
|
+
- `enable-routed-typemap` (default: false): enables output of an indexable type mapping paths and HTTP verbs to request and response bodies.
|
|
57
59
|
|
|
58
|
-
## Types
|
|
60
|
+
## Types Emitter
|
|
59
61
|
|
|
60
62
|
This emitter will traverse your configured root namespace and all nested namespaces, generating a `{namespace-name}.ts`-file.
|
|
61
63
|
|
|
62
|
-
The emitter can handle `Model`s, `Enum`s and `Union`s. ~~`Alias`'s~~ are
|
|
64
|
+
The emitter can handle `Model`s, `Enum`s and `Union`s. ~~`Alias`'s~~ are *not* emitted - more on that [later](#aliases). It will also preserve docs as JSDoc-style comments.
|
|
63
65
|
|
|
64
66
|
The emitter should be able to handle most basic TS contructs, like scalars, literals, object, arrays, tuples and intrinsics (eg. `null`).
|
|
65
67
|
|
|
68
|
+
> [!IMPORTANT]
|
|
69
|
+
> These types do not respect most transformative decorators, notable `@visibility`.
|
|
70
|
+
> This is because it's non-trivial to do, has unexpected problems and the functionality is *somewhat* already there, in the form of [Routed Typemaps](#routed-typemap).
|
|
71
|
+
> For additional information, see [#7](https://github.com/crowbait/typespec-typescript-emitter/issues/7).
|
|
72
|
+
|
|
66
73
|
Example:
|
|
67
74
|
|
|
68
75
|
```ts
|
|
@@ -126,9 +133,9 @@ export function isBook(arg: any): arg is Book {
|
|
|
126
133
|
Typeguards *should* create comprehensive checks that adhere as strictly to the source model as possible.
|
|
127
134
|
If you find a case where the typeguard is looser than it needs to be, please report that as a bug.
|
|
128
135
|
|
|
129
|
-
###
|
|
136
|
+
### Aliases
|
|
130
137
|
|
|
131
|
-
There seems to be no way to extract aliases from TypeSpec's emitter framework. Because of that, `Alias`
|
|
138
|
+
There seems to be no way to extract aliases from TypeSpec's emitter framework. Because of that, `Alias`es are ignored by the emitter (or, to be more precise: `Alias`es reach the emitter already resolved. They won't be exported as their own type but directly substituted where they're needed).
|
|
132
139
|
|
|
133
140
|
That means, if you want something to be emitted, it can't be an alias:
|
|
134
141
|
|
|
@@ -145,16 +152,17 @@ alias Derived1 = OmitProperties<Demo, "prop1">;
|
|
|
145
152
|
model Derived2 {...OmitProperties<Demo, "prop1">};
|
|
146
153
|
```
|
|
147
154
|
|
|
148
|
-
## Routes
|
|
155
|
+
## Routes Emitter
|
|
149
156
|
|
|
150
157
|
**This emitter depends on your use of the `TypeSpec.Http` library**.
|
|
151
158
|
|
|
152
159
|
If you're using `TypeSpec.Http` to define your API routes and endpoints, this library offers an emitter to export a `routes` object.
|
|
153
160
|
It will generate a nested object containing information about every `op` you have defined, nested by namespace.
|
|
154
161
|
I contains the following data (per `op`):
|
|
162
|
+
|
|
155
163
|
- `method`: HTTP method
|
|
156
|
-
- `path`: Path (as defined
|
|
157
|
-
- `getUrl`: Function for generating a valid URL to this `op`; if the path has parameters, this function will have
|
|
164
|
+
- `path`: Path (as defined in the `route` string; parameters are not substituted)
|
|
165
|
+
- `getUrl`: Function for generating a valid URL to this `op`; if the path has parameters, this function will have matching parameters
|
|
158
166
|
- `auth`: Array of valid authentication schemes (or `[null]`, if none)
|
|
159
167
|
Just as the types emitter, this emitter will also preserve docs as JSDoc-style comments.
|
|
160
168
|
|
|
@@ -212,4 +220,102 @@ export const routes_myProject = {
|
|
|
212
220
|
}
|
|
213
221
|
}
|
|
214
222
|
} as const;
|
|
215
|
-
```
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Routed Typemap
|
|
226
|
+
|
|
227
|
+
This emitter produces a Typemap (a typescript type indexed by string keys mapping other types) based on your HTTP routes and verbs.
|
|
228
|
+
In short, this allows you to select a type *used in a body of your HTTP ops* using it's path and verb. This includes request and response bodies. Path parameters are not relevant for this emitter; those are already handled in the Routes object.
|
|
229
|
+
This can be helpful when, for example, building a wrapper around your API.
|
|
230
|
+
|
|
231
|
+
> [!NOTE]
|
|
232
|
+
> The Typemap is not nested! This means that, let's say the route "/user/account" will not be mapped to `{user: {account: /* ... */}}` (somewhat similar to how the Routes Emitter works), but to `{"/user/account": /* ... */}`. Crucially, those map keys are *the same as the `path` property in the Routes object* emitted from the Routes Emitter.
|
|
233
|
+
> This means you can select from the Typemap *using* the structured Routes object.
|
|
234
|
+
|
|
235
|
+
Example:
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
@route("/typemap")
|
|
239
|
+
namespace namespaceA.typemap {
|
|
240
|
+
model ModelA {
|
|
241
|
+
id: int32,
|
|
242
|
+
name: string
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
@get
|
|
246
|
+
op getAll(): {@body body: ModelA[]} | {@statusCode status: 418, @body body: "Me teapot"};
|
|
247
|
+
|
|
248
|
+
@post
|
|
249
|
+
op add(@body body: ModelA): OkResponse;
|
|
250
|
+
|
|
251
|
+
@post
|
|
252
|
+
@route("{id}")
|
|
253
|
+
op getOne(
|
|
254
|
+
@path id: int32
|
|
255
|
+
): {@body body: ModelA}| NotFoundResponse | {@statusCode status: 418} | {@statusCode status: 419, @body body: null};
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
...will be transformed into:
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
/* /path/to/outdir/routedTypemap_{root-namespace}.ts */
|
|
263
|
+
export type types_namespaceA = {
|
|
264
|
+
['/typemap']: {
|
|
265
|
+
['GET']: {
|
|
266
|
+
request: null
|
|
267
|
+
response: {status: 200, body: {
|
|
268
|
+
id: number,
|
|
269
|
+
name: string
|
|
270
|
+
}[]} | {status: 418, body: 'Me teapot'}
|
|
271
|
+
},
|
|
272
|
+
['POST']: {
|
|
273
|
+
request: {
|
|
274
|
+
id: number,
|
|
275
|
+
name: string
|
|
276
|
+
}
|
|
277
|
+
response: {status: 200, body: {
|
|
278
|
+
/** The status code. */
|
|
279
|
+
statusCode: 200
|
|
280
|
+
}}
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
['/typemap/{id}']: {
|
|
284
|
+
['POST']: {
|
|
285
|
+
request: null
|
|
286
|
+
response: {status: 200, body: {
|
|
287
|
+
id: number,
|
|
288
|
+
name: string
|
|
289
|
+
}} | {status: 404, body: {
|
|
290
|
+
/** The status code. */
|
|
291
|
+
statusCode: 404
|
|
292
|
+
}} | {status: 418, body: {
|
|
293
|
+
status: 418
|
|
294
|
+
}} | {status: 419, body: null}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
...which can be accessed like this:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
// Accessing type of response body directly by knowing path and verb
|
|
304
|
+
type T_update1 = types_namespaceA['/typemap']['POST']['response']['body']
|
|
305
|
+
|
|
306
|
+
// Accessing type of request body by indexing Routes object
|
|
307
|
+
// namespace "namespaceA.typemap", op "add"
|
|
308
|
+
type T_update2 = types_namespaceA[typeof routes_namespaceA.typemap.add.path]['POST']['request']
|
|
309
|
+
// One could also use `typeof routes_namespace.testSimple.update.method` instead of 'POST'.
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
> [!NOTE]
|
|
313
|
+
> Observe how the emitter
|
|
314
|
+
>
|
|
315
|
+
> - assumes a `200` status code for an op's response type if you didn't define any (first responses on `getAll` and `getOne` ops)
|
|
316
|
+
> - assumes the entire response type is the body of no body is explicitely decorated (418 on `getOne` op) (this is non-standard; I have seen quite a few projects not realizing the response definition should have a body *in* it and treating the whole thing as a body; so while technically "wrong", this accomodates those projects. You can easily define a truly empty body using `{}`, `""`, `null`..., see 419 on `getOne` op)
|
|
317
|
+
|
|
318
|
+
Additional notes:
|
|
319
|
+
|
|
320
|
+
- There is currently no built-in way of accessing typeguards from paths their types may be associated with.
|
|
321
|
+
- Models are not reused in or imported by this emitter. Reasoning involves "no runtime overhead either way", "simpler code", "self-contained emitter modules" and "you're not supposed to rummage around in the generated files anyway, just import them"; this has been touched upon in [#4](https://github.com/crowbait/typespec-typescript-emitter/issues/4#issuecomment-2720955282) and [#6](https://github.com/crowbait/typespec-typescript-emitter/issues/6#issuecomment-3049999155).
|
|
@@ -1,22 +1,32 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isTemplateDeclaration, } from "@typespec/compiler";
|
|
2
|
+
import { getHttpOperation, resolveRequestVisibility, Visibility, } from "@typespec/http";
|
|
2
3
|
import { resolveType } from "./emit_types_resolve.js";
|
|
3
4
|
export const emitRoutedTypemap = (context, namespace) => {
|
|
4
5
|
const ops = {};
|
|
5
6
|
const traverseNamespace = (n) => {
|
|
6
7
|
// operations
|
|
7
|
-
|
|
8
|
+
const processOp = (op) => {
|
|
8
9
|
const httpOp = getHttpOperation(context.program, op);
|
|
9
|
-
const
|
|
10
|
-
|
|
10
|
+
const path = httpOp[0].path;
|
|
11
|
+
const verb = httpOp[0].verb.toUpperCase();
|
|
12
|
+
if (!ops[path])
|
|
13
|
+
ops[path] = {};
|
|
14
|
+
ops[path][verb] = {
|
|
11
15
|
request: "null",
|
|
12
|
-
response: [{ status: 200, body: "
|
|
16
|
+
response: [{ status: 200, body: "{}" }],
|
|
13
17
|
};
|
|
14
18
|
// request
|
|
15
19
|
let request = "null";
|
|
16
|
-
if (
|
|
17
|
-
request = resolveType(
|
|
20
|
+
if (httpOp[0].parameters.body) {
|
|
21
|
+
request = resolveType(httpOp[0].parameters.body.type, {
|
|
22
|
+
nestlevel: 2,
|
|
23
|
+
currentNamespace: namespace,
|
|
24
|
+
context,
|
|
25
|
+
visibility: resolveRequestVisibility(context.program, op, httpOp[0].verb),
|
|
26
|
+
resolveEvenWithName: true,
|
|
27
|
+
}).replaceAll("\n", "\n ");
|
|
18
28
|
}
|
|
19
|
-
ops[
|
|
29
|
+
ops[path][verb].request = request;
|
|
20
30
|
// response
|
|
21
31
|
if (op.returnType && op.returnType.kind) {
|
|
22
32
|
const getReturnType = (t) => {
|
|
@@ -25,7 +35,7 @@ export const emitRoutedTypemap = (context, namespace) => {
|
|
|
25
35
|
// if the return type is a model, it may have a fully qualified body
|
|
26
36
|
const modelret = {
|
|
27
37
|
status: 200,
|
|
28
|
-
body: "
|
|
38
|
+
body: "{}",
|
|
29
39
|
};
|
|
30
40
|
let wasQualifiedBody = false;
|
|
31
41
|
t.properties.forEach((prop) => {
|
|
@@ -36,14 +46,26 @@ export const emitRoutedTypemap = (context, namespace) => {
|
|
|
36
46
|
modelret.status = prop.type.value;
|
|
37
47
|
// one of the properties may be the body definition
|
|
38
48
|
if (dec.definition?.name === "@body") {
|
|
39
|
-
modelret.body = resolveType(prop.type,
|
|
49
|
+
modelret.body = resolveType(prop.type, {
|
|
50
|
+
nestlevel: 2,
|
|
51
|
+
currentNamespace: namespace,
|
|
52
|
+
context,
|
|
53
|
+
visibility: Visibility.Read,
|
|
54
|
+
resolveEvenWithName: true,
|
|
55
|
+
}).replaceAll("\n", "\n ");
|
|
40
56
|
wasQualifiedBody = true;
|
|
41
57
|
}
|
|
42
58
|
});
|
|
43
59
|
});
|
|
44
60
|
// ... if not, we assume status 200 and treat the model as the body
|
|
45
61
|
if (!wasQualifiedBody) {
|
|
46
|
-
modelret.body = resolveType(t,
|
|
62
|
+
modelret.body = resolveType(t, {
|
|
63
|
+
nestlevel: 2,
|
|
64
|
+
currentNamespace: namespace,
|
|
65
|
+
context,
|
|
66
|
+
visibility: Visibility.Read,
|
|
67
|
+
resolveEvenWithName: true,
|
|
68
|
+
}).replaceAll("\n", "\n ");
|
|
47
69
|
}
|
|
48
70
|
ret.push(modelret);
|
|
49
71
|
}
|
|
@@ -55,24 +77,45 @@ export const emitRoutedTypemap = (context, namespace) => {
|
|
|
55
77
|
});
|
|
56
78
|
}
|
|
57
79
|
else
|
|
58
|
-
ret.push({
|
|
80
|
+
ret.push({
|
|
81
|
+
status: 200,
|
|
82
|
+
body: resolveType(t, {
|
|
83
|
+
nestlevel: 1,
|
|
84
|
+
currentNamespace: namespace,
|
|
85
|
+
context,
|
|
86
|
+
visibility: Visibility.Read,
|
|
87
|
+
resolveEvenWithName: true,
|
|
88
|
+
}),
|
|
89
|
+
});
|
|
59
90
|
return ret;
|
|
60
91
|
};
|
|
61
|
-
ops[
|
|
92
|
+
ops[path][verb].response = getReturnType(op.returnType);
|
|
62
93
|
}
|
|
63
|
-
}
|
|
94
|
+
}; // end operations
|
|
95
|
+
n.operations.forEach(processOp);
|
|
96
|
+
n.interfaces.forEach((itf) => {
|
|
97
|
+
if (!isTemplateDeclaration(itf))
|
|
98
|
+
itf.operations.forEach(processOp);
|
|
99
|
+
});
|
|
64
100
|
// get and traverse all namespaces
|
|
65
101
|
n.namespaces.forEach((ns) => traverseNamespace(ns));
|
|
66
102
|
};
|
|
67
103
|
traverseNamespace(namespace);
|
|
68
104
|
let out = `export type types_${context.options["root-namespace"]} = {\n`;
|
|
69
105
|
out += Object.entries(ops)
|
|
70
|
-
.map((
|
|
71
|
-
let
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
106
|
+
.map((path) => {
|
|
107
|
+
let pathret = ` ['${path[0]}']: {\n`;
|
|
108
|
+
pathret += Object.entries(path[1])
|
|
109
|
+
.map((verb) => {
|
|
110
|
+
let verbret = ` ['${verb[0]}']: {\n`;
|
|
111
|
+
verbret += ` request: ${verb[1].request}\n`;
|
|
112
|
+
verbret += ` response: ${verb[1].response.map((res) => `{status: ${res.status}, body: ${res.body}}`).join(" | ")}\n`;
|
|
113
|
+
verbret += " }";
|
|
114
|
+
return verbret;
|
|
115
|
+
})
|
|
116
|
+
.join(",\n");
|
|
117
|
+
pathret += "\n }";
|
|
118
|
+
return pathret;
|
|
76
119
|
})
|
|
77
120
|
.join(",\n");
|
|
78
121
|
out += "\n};\n";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emit_mapped_types.js","sourceRoot":"","sources":["../../src/emit_mapped_types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"emit_mapped_types.js","sourceRoot":"","sources":["../../src/emit_mapped_types.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,qBAAqB,GAItB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,wBAAwB,EACxB,UAAU,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,OAAoB,EACpB,SAAoB,EACZ,EAAE;IACV,MAAM,GAAG,GAQL,EAAE,CAAC;IAEP,MAAM,iBAAiB,GAAG,CAAC,CAAY,EAAQ,EAAE;QAC/C,aAAa;QACb,MAAM,SAAS,GAAG,CAAC,EAAa,EAAE,EAAE;YAClC,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACrD,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,IA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C,OAAO,CAAC,gBAAgB,CAAC,QAAQ,CAAC;IACzE,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;SACvB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACZ,IAAI,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;QACtC,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aAC/B,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;YACZ,IAAI,OAAO,GAAG,SAAS,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;YACxC,OAAO,IAAI,kBAAkB,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,CAAC;YACjD,OAAO,IAAI,mBAAmB,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,YAAY,GAAG,CAAC,MAAM,WAAW,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;YAC1H,OAAO,IAAI,OAAO,CAAC;YACnB,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC;aACD,IAAI,CAAC,KAAK,CAAC,CAAC;QACf,OAAO,IAAI,OAAO,CAAC;QACnB,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;SACD,IAAI,CAAC,KAAK,CAAC,CAAC;IACf,GAAG,IAAI,QAAQ,CAAC;IAChB,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
package/dist/src/emit_routes.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { getDoc } from "@typespec/compiler";
|
|
1
|
+
import { getDoc, isTemplateDeclaration, } from "@typespec/compiler";
|
|
2
2
|
import { getAuthentication, getHttpOperation } from "@typespec/http";
|
|
3
3
|
export const emitRoutes = (context, namespace) => {
|
|
4
4
|
let out = `export const routes_${context.options["root-namespace"]} = {\n`;
|
|
5
5
|
const traverseNamespace = (n, nestLevel) => {
|
|
6
6
|
// operations
|
|
7
|
-
|
|
8
|
-
n.operations.forEach((op) => {
|
|
9
|
-
opnum++;
|
|
7
|
+
const processOp = (op) => {
|
|
10
8
|
const httpOp = getHttpOperation(context.program, op);
|
|
11
9
|
// jsdoc comment
|
|
12
10
|
const doc = getDoc(context.program, op);
|
|
@@ -58,8 +56,13 @@ export const emitRoutes = (context, namespace) => {
|
|
|
58
56
|
.join(", ")}}`)
|
|
59
57
|
.join(", ")}]`, nestLevel + 2);
|
|
60
58
|
// finalize route entry
|
|
61
|
-
out = out.addLine(
|
|
62
|
-
}
|
|
59
|
+
out = out.addLine("},", nestLevel + 1);
|
|
60
|
+
}; // end operations
|
|
61
|
+
n.operations.forEach(processOp);
|
|
62
|
+
n.interfaces.forEach((itf) => {
|
|
63
|
+
if (!isTemplateDeclaration(itf))
|
|
64
|
+
itf.operations.forEach(processOp);
|
|
65
|
+
});
|
|
63
66
|
// get and traverse all namespaces
|
|
64
67
|
let nsnum = 0;
|
|
65
68
|
n.namespaces.forEach((ns) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emit_routes.js","sourceRoot":"","sources":["../../src/emit_routes.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"emit_routes.js","sourceRoot":"","sources":["../../src/emit_routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EACN,qBAAqB,GAGtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAErE,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,OAAoB,EACpB,SAAoB,EACZ,EAAE;IACV,IAAI,GAAG,GAAG,uBAAuB,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,QAAQ,CAAC;IAE3E,MAAM,iBAAiB,GAAG,CAAC,CAAY,EAAE,SAAiB,EAAQ,EAAE;QAClE,aA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AAE,CAAC;YACR,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACxC,IAAI,GAAG;gBAAE,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,GAAG,KAAK,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC;YAC3D,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,KAAK,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC;YAClD,iBAAiB,CAAC,EAAE,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC;YACrC,GAAG,GAAG,GAAG,CAAC,OAAO,CACf,IAAI,KAAK,GAAG,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EAC1C,SAAS,GAAG,CAAC,CACd,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,iBAAiB,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IAChC,GAAG,IAAI,eAAe,CAAC;IACvB,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
package/dist/src/emit_types.js
CHANGED
|
@@ -7,7 +7,12 @@ const emitTypes = (context, namespace, options) => {
|
|
|
7
7
|
let file = "";
|
|
8
8
|
n.enums.forEach((e) => {
|
|
9
9
|
if (options["enable-types"]) {
|
|
10
|
-
const resolved = resolveEnum(e,
|
|
10
|
+
const resolved = resolveEnum(e, {
|
|
11
|
+
context: context,
|
|
12
|
+
currentNamespace: n,
|
|
13
|
+
nestlevel: 0,
|
|
14
|
+
isNamespaceRoot: true,
|
|
15
|
+
});
|
|
11
16
|
if (resolved) {
|
|
12
17
|
const doc = getDoc(context.program, e);
|
|
13
18
|
if (doc)
|
|
@@ -18,7 +23,12 @@ const emitTypes = (context, namespace, options) => {
|
|
|
18
23
|
});
|
|
19
24
|
n.unions.forEach((u) => {
|
|
20
25
|
if (options["enable-types"]) {
|
|
21
|
-
const resolved = resolveUnion(u,
|
|
26
|
+
const resolved = resolveUnion(u, {
|
|
27
|
+
currentNamespace: n,
|
|
28
|
+
context,
|
|
29
|
+
nestlevel: 0,
|
|
30
|
+
isNamespaceRoot: true,
|
|
31
|
+
});
|
|
22
32
|
if (resolved) {
|
|
23
33
|
const doc = getDoc(context.program, u);
|
|
24
34
|
if (doc)
|
|
@@ -29,7 +39,12 @@ const emitTypes = (context, namespace, options) => {
|
|
|
29
39
|
});
|
|
30
40
|
n.models.forEach((m) => {
|
|
31
41
|
if (options["enable-types"]) {
|
|
32
|
-
const resolved = resolveModel(m,
|
|
42
|
+
const resolved = resolveModel(m, {
|
|
43
|
+
nestlevel: 0,
|
|
44
|
+
currentNamespace: n,
|
|
45
|
+
context,
|
|
46
|
+
isNamespaceRoot: true,
|
|
47
|
+
});
|
|
33
48
|
if (resolved) {
|
|
34
49
|
const doc = getDoc(context.program, m);
|
|
35
50
|
if (doc)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emit_types.js","sourceRoot":"","sources":["../../src/emit_types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,MAAM,EAAa,MAAM,oBAAoB,CAAC;AACpE,OAAO,EACL,WAAW,EACX,YAAY,EACZ,YAAY,GACb,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAG/D,MAAM,SAAS,GAAG,CAChB,OAAoB,EACpB,SAAoB,EACpB,OAAuB,EAIvB,EAAE;IACF,MAAM,GAAG,GAAiC,EAAE,KAAK,EAAE,EAAE,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC;IAE9E,MAAM,iBAAiB,GAAG,CAAC,CAAY,EAAQ,EAAE;QAC/C,IAAI,IAAI,GAAG,EAAE,CAAC;QAEd,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACpB,IAAI,OAAO,CAAC,cAAc,CAAC,EAAE,C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,CAAC,CAAC,CAAC;IACtD,CAAC,CAAC;IACF,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAE7B,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,eAAe,SAAS,CAAC"}
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import { ArrayModelType, EmitContext, Enum, Model, Namespace, RecordModelType, Scalar, Tuple, Type, Union } from "@typespec/compiler";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { Visibility } from "@typespec/http";
|
|
3
|
+
type CommonOptions = {
|
|
4
|
+
nestlevel: number;
|
|
5
|
+
currentNamespace: Namespace;
|
|
6
|
+
context: EmitContext;
|
|
7
|
+
visibility?: Visibility;
|
|
8
|
+
resolveEvenWithName?: boolean;
|
|
9
|
+
isNamespaceRoot?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare const resolveType: (t: Type, opts: CommonOptions) => string;
|
|
12
|
+
export declare const resolveArray: (a: ArrayModelType, opts: CommonOptions) => string;
|
|
13
|
+
export declare const resolveRecord: (a: RecordModelType, opts: CommonOptions) => string;
|
|
14
|
+
export declare const resolveEnum: (e: Enum, opts: CommonOptions) => string;
|
|
15
|
+
export declare const resolveTuple: (t: Tuple, opts: CommonOptions) => string;
|
|
16
|
+
export declare const resolveUnion: (u: Union, opts: CommonOptions) => string;
|
|
8
17
|
export declare const resolveScalar: (s: Scalar) => string;
|
|
9
|
-
export declare const resolveModel: (m: Model,
|
|
18
|
+
export declare const resolveModel: (m: Model, opts: CommonOptions) => string;
|
|
19
|
+
export {};
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { getDoc, } from "@typespec/compiler";
|
|
2
|
-
|
|
2
|
+
import { isVisible } from "@typespec/http";
|
|
3
|
+
export const resolveType = (t, opts) => {
|
|
3
4
|
let typeStr = "unknown";
|
|
4
5
|
switch (t.kind) {
|
|
5
6
|
case "Model":
|
|
6
7
|
if (t.name === "Array") {
|
|
7
|
-
typeStr = resolveArray(t,
|
|
8
|
+
typeStr = resolveArray(t, opts);
|
|
8
9
|
}
|
|
9
10
|
else if (t.name === "Record") {
|
|
10
|
-
typeStr = resolveRecord(t,
|
|
11
|
+
typeStr = resolveRecord(t, opts);
|
|
11
12
|
}
|
|
12
13
|
else
|
|
13
|
-
typeStr = resolveModel(t,
|
|
14
|
+
typeStr = resolveModel(t, opts);
|
|
14
15
|
break;
|
|
15
16
|
case "Boolean":
|
|
16
17
|
typeStr = "boolean";
|
|
17
18
|
break;
|
|
18
19
|
case "Enum":
|
|
19
|
-
typeStr = resolveEnum(t,
|
|
20
|
+
typeStr = resolveEnum(t, opts);
|
|
20
21
|
break;
|
|
21
22
|
case "Intrinsic":
|
|
22
23
|
typeStr = t.name;
|
|
@@ -31,28 +32,31 @@ export const resolveType = (t, nestlevel, currentNamespace, context) => {
|
|
|
31
32
|
typeStr = `'${t.value}'`;
|
|
32
33
|
break;
|
|
33
34
|
case "Tuple":
|
|
34
|
-
typeStr = resolveTuple(t,
|
|
35
|
+
typeStr = resolveTuple(t, opts);
|
|
35
36
|
break;
|
|
36
37
|
case "Union":
|
|
37
|
-
typeStr = resolveUnion(t,
|
|
38
|
+
typeStr = resolveUnion(t, opts);
|
|
38
39
|
break;
|
|
39
40
|
default:
|
|
40
41
|
console.warn("Could not resolve type:", t.kind);
|
|
41
42
|
}
|
|
42
43
|
return typeStr;
|
|
43
44
|
};
|
|
44
|
-
export const resolveArray = (a,
|
|
45
|
+
export const resolveArray = (a, opts) => {
|
|
45
46
|
if (a.name !== "Array")
|
|
46
47
|
throw new Error(`Trying to parse model ${a.name} as Array`);
|
|
47
|
-
return
|
|
48
|
+
return `(${resolveType(a.indexer.value, opts)})[]`;
|
|
48
49
|
};
|
|
49
|
-
export const resolveRecord = (a,
|
|
50
|
+
export const resolveRecord = (a, opts) => {
|
|
50
51
|
if (a.name !== "Record")
|
|
51
52
|
throw new Error(`Trying to parse model ${a.name} as Record`);
|
|
52
|
-
return `{[k: string]: ${resolveType(a.indexer.value,
|
|
53
|
+
return `{[k: string]: ${resolveType(a.indexer.value, opts)}}`;
|
|
53
54
|
};
|
|
54
|
-
export const resolveEnum = (e,
|
|
55
|
-
if (e.name &&
|
|
55
|
+
export const resolveEnum = (e, opts) => {
|
|
56
|
+
if (e.name &&
|
|
57
|
+
!opts.isNamespaceRoot &&
|
|
58
|
+
e.namespace?.enums.has(e.name) &&
|
|
59
|
+
!opts.resolveEvenWithName)
|
|
56
60
|
return e.name;
|
|
57
61
|
let ret = "{\n";
|
|
58
62
|
let i = 1;
|
|
@@ -61,20 +65,23 @@ export const resolveEnum = (e, nestlevel, isNamespaceRoot) => {
|
|
|
61
65
|
? ""
|
|
62
66
|
: " = " +
|
|
63
67
|
(typeof p.value === "string" ? `'${p.value}'` : p.value.toString());
|
|
64
|
-
ret = ret.addLine(`${p.name.includes("-") ? `'${p.name}'` : p.name}${val}${i < e.members.size ? "," : ""}`, nestlevel + 1);
|
|
68
|
+
ret = ret.addLine(`${p.name.includes("-") ? `'${p.name}'` : p.name}${val}${i < e.members.size ? "," : ""}`, opts.nestlevel + 1);
|
|
65
69
|
i++;
|
|
66
70
|
});
|
|
67
|
-
ret = ret.addLine("}", nestlevel, true);
|
|
71
|
+
ret = ret.addLine("}", opts.nestlevel, true);
|
|
68
72
|
return ret;
|
|
69
73
|
};
|
|
70
|
-
export const resolveTuple = (t,
|
|
71
|
-
return `[${t.values.map((v) => resolveType(v,
|
|
74
|
+
export const resolveTuple = (t, opts) => {
|
|
75
|
+
return `[${t.values.map((v) => resolveType(v, opts)).join(", ")}]`;
|
|
72
76
|
};
|
|
73
|
-
export const resolveUnion = (u,
|
|
74
|
-
if (u.name &&
|
|
77
|
+
export const resolveUnion = (u, opts) => {
|
|
78
|
+
if (u.name &&
|
|
79
|
+
!opts.isNamespaceRoot &&
|
|
80
|
+
u.namespace?.unions.has(u.name) &&
|
|
81
|
+
!opts.resolveEvenWithName)
|
|
75
82
|
return u.name;
|
|
76
83
|
return Array.from(u.variants)
|
|
77
|
-
.map((v) => resolveType(v[1].type,
|
|
84
|
+
.map((v) => resolveType(v[1].type, opts))
|
|
78
85
|
.join(" | ");
|
|
79
86
|
};
|
|
80
87
|
export const resolveScalar = (s) => {
|
|
@@ -108,24 +115,33 @@ export const resolveScalar = (s) => {
|
|
|
108
115
|
}
|
|
109
116
|
return s.baseScalar ? resolveScalar(s.baseScalar) : ret;
|
|
110
117
|
};
|
|
111
|
-
export const resolveModel = (m,
|
|
112
|
-
if (m.name &&
|
|
118
|
+
export const resolveModel = (m, opts) => {
|
|
119
|
+
if (m.name &&
|
|
120
|
+
!opts.isNamespaceRoot &&
|
|
121
|
+
opts.currentNamespace.namespace === m.namespace &&
|
|
122
|
+
!opts.resolveEvenWithName)
|
|
113
123
|
return m.name;
|
|
114
124
|
let ret = "{\n";
|
|
115
125
|
let i = 1;
|
|
116
126
|
m.properties.forEach((p) => {
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
if (
|
|
120
|
-
|
|
127
|
+
if (opts.visibility === undefined ||
|
|
128
|
+
isVisible(opts.context.program, p, opts.visibility)) {
|
|
129
|
+
if (opts.context) {
|
|
130
|
+
const doc = getDoc(opts.context.program, p);
|
|
131
|
+
if (doc)
|
|
132
|
+
ret = ret.addLine(`/** ${doc} */`, opts.nestlevel + 1);
|
|
133
|
+
}
|
|
134
|
+
const typeStr = resolveType(p.type, {
|
|
135
|
+
...opts,
|
|
136
|
+
nestlevel: opts.nestlevel + 1,
|
|
137
|
+
});
|
|
138
|
+
if (typeStr.includes("unknown"))
|
|
139
|
+
console.warn(`Could not resolve property ${p.name} on ${m.name}`);
|
|
140
|
+
ret = ret.addLine(`${p.name}${p.optional ? "?" : ""}: ${typeStr}${i < m.properties.size ? "," : ""}`, opts.nestlevel + 1);
|
|
121
141
|
}
|
|
122
|
-
const typeStr = resolveType(p.type, nestlevel, currentNamespace, context);
|
|
123
|
-
if (typeStr.includes("unknown"))
|
|
124
|
-
console.warn(`Could not resolve property ${p.name} on ${m.name}`);
|
|
125
|
-
ret = ret.addLine(`${p.name}${p.optional ? "?" : ""}: ${typeStr}${i < m.properties.size ? "," : ""}`, nestlevel + 1);
|
|
126
142
|
i++;
|
|
127
143
|
});
|
|
128
|
-
ret = ret.addLine("}", nestlevel, true);
|
|
144
|
+
ret = ret.addLine("}", opts.nestlevel, true);
|
|
129
145
|
return ret;
|
|
130
146
|
};
|
|
131
147
|
//# sourceMappingURL=emit_types_resolve.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emit_types_resolve.js","sourceRoot":"","sources":["../../src/emit_types_resolve.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,GAQP,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"emit_types_resolve.js","sourceRoot":"","sources":["../../src/emit_types_resolve.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,GAQP,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAc,MAAM,gBAAgB,CAAC;AAWvD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAO,EAAE,IAAmB,EAAU,EAAE;IAClE,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;QACf,KAAK,OAAO;YACV,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBACvB,OAAO,GAAG,YAAY,CAAC,CAAmB,EAAE,IAAI,CAAC,CAAC;YACpD,CAAC;iBAAM,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC/B,OAAO,GAAG,aAAa,CAAC,CAAoB,EAAE,IAAI,CAAC,CAAC;YACtD,CAAC;;gBAAM,OAAO,GAAG,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;YACvC,MAAM;QACR,KAAK,SAAS;YACZ,OAAO,GAAG,SAAS,CAAC;YACpB,MAAM;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AAE,IAAI,CAAC,SAAU,GAAG,CAAC,CAAC,CAAC;YACnE,CAAC;YACD,MAAM,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,EAAE;gBAClC,GAAG,IAAI;gBACP,SAAS,EAAE,IAAI,CAAC,SAAS,GAAG,CAAC;aAC9B,CAAC,CAAC;YACH,IAAI,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;gBAC7B,OAAO,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;YACpE,GAAG,GAAG,GAAG,CAAC,OAAO,CACf,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EAClF,IAAI,CAAC,SAAS,GAAG,CAAC,CACnB,CAAC;QACJ,CAAC;QACD,CAAC,EAAE,CAAC;IACN,CAAC,CAAC,CAAC;IACH,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAC7C,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typespec-typescript-emitter",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A TypeSpec library providing an emitter that generates TypeScript types and a structured routes object for robust importing",
|
|
5
5
|
"homepage": "https://github.com/crowbait/typespec-typescript-emitter#readme",
|
|
6
6
|
"bugs": "https://github.com/crowbait/typespec-typescript-emitter/issues",
|
package/src/emit_mapped_types.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
EmitContext,
|
|
3
|
+
isTemplateDeclaration,
|
|
4
|
+
Namespace,
|
|
5
|
+
Operation,
|
|
6
|
+
Type,
|
|
7
|
+
} from "@typespec/compiler";
|
|
8
|
+
import {
|
|
9
|
+
getHttpOperation,
|
|
10
|
+
resolveRequestVisibility,
|
|
11
|
+
Visibility,
|
|
12
|
+
} from "@typespec/http";
|
|
3
13
|
import { resolveType } from "./emit_types_resolve.js";
|
|
4
14
|
|
|
5
15
|
export const emitRoutedTypemap = (
|
|
@@ -7,44 +17,55 @@ export const emitRoutedTypemap = (
|
|
|
7
17
|
namespace: Namespace,
|
|
8
18
|
): string => {
|
|
9
19
|
const ops: {
|
|
10
|
-
[
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
[path: string]: {
|
|
21
|
+
[verb: string]: {
|
|
22
|
+
// "string" in these does not refer to the type "string"! It's the typescript code as string.
|
|
23
|
+
request: string;
|
|
24
|
+
response: Array<{ status: number | "unknown"; body: string }>;
|
|
25
|
+
};
|
|
14
26
|
};
|
|
15
27
|
} = {};
|
|
16
28
|
|
|
17
29
|
const traverseNamespace = (n: Namespace): void => {
|
|
18
30
|
// operations
|
|
19
|
-
|
|
31
|
+
const processOp = (op: Operation) => {
|
|
20
32
|
const httpOp = getHttpOperation(context.program, op);
|
|
21
|
-
const
|
|
22
|
-
|
|
33
|
+
const path = httpOp[0].path;
|
|
34
|
+
const verb = httpOp[0].verb.toUpperCase();
|
|
35
|
+
if (!ops[path]) ops[path] = {};
|
|
36
|
+
ops[path][verb] = {
|
|
23
37
|
request: "null",
|
|
24
|
-
response: [{ status: 200, body: "
|
|
38
|
+
response: [{ status: 200, body: "{}" }],
|
|
25
39
|
};
|
|
26
40
|
|
|
27
41
|
// request
|
|
28
42
|
let request = "null";
|
|
29
|
-
if (
|
|
30
|
-
request = resolveType(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
namespace,
|
|
43
|
+
if (httpOp[0].parameters.body) {
|
|
44
|
+
request = resolveType(httpOp[0].parameters.body.type, {
|
|
45
|
+
nestlevel: 2,
|
|
46
|
+
currentNamespace: namespace,
|
|
34
47
|
context,
|
|
35
|
-
|
|
48
|
+
visibility: resolveRequestVisibility(
|
|
49
|
+
context.program,
|
|
50
|
+
op,
|
|
51
|
+
httpOp[0].verb,
|
|
52
|
+
),
|
|
53
|
+
resolveEvenWithName: true,
|
|
54
|
+
}).replaceAll("\n", "\n ");
|
|
36
55
|
}
|
|
37
|
-
ops[
|
|
56
|
+
ops[path][verb].request = request;
|
|
38
57
|
|
|
39
58
|
// response
|
|
40
59
|
if (op.returnType && op.returnType.kind) {
|
|
41
|
-
const getReturnType = (
|
|
42
|
-
|
|
60
|
+
const getReturnType = (
|
|
61
|
+
t: Type,
|
|
62
|
+
): (typeof ops)[string][string]["response"] => {
|
|
63
|
+
const ret: (typeof ops)[string][string]["response"] = [];
|
|
43
64
|
if (t.kind === "Model") {
|
|
44
65
|
// if the return type is a model, it may have a fully qualified body
|
|
45
66
|
const modelret: (typeof ret)[number] = {
|
|
46
67
|
status: 200,
|
|
47
|
-
body: "
|
|
68
|
+
body: "{}",
|
|
48
69
|
};
|
|
49
70
|
let wasQualifiedBody = false;
|
|
50
71
|
t.properties.forEach((prop) => {
|
|
@@ -57,14 +78,26 @@ export const emitRoutedTypemap = (
|
|
|
57
78
|
modelret.status = prop.type.value;
|
|
58
79
|
// one of the properties may be the body definition
|
|
59
80
|
if (dec.definition?.name === "@body") {
|
|
60
|
-
modelret.body = resolveType(prop.type,
|
|
81
|
+
modelret.body = resolveType(prop.type, {
|
|
82
|
+
nestlevel: 2,
|
|
83
|
+
currentNamespace: namespace,
|
|
84
|
+
context,
|
|
85
|
+
visibility: Visibility.Read,
|
|
86
|
+
resolveEvenWithName: true,
|
|
87
|
+
}).replaceAll("\n", "\n ");
|
|
61
88
|
wasQualifiedBody = true;
|
|
62
89
|
}
|
|
63
90
|
});
|
|
64
91
|
});
|
|
65
92
|
// ... if not, we assume status 200 and treat the model as the body
|
|
66
93
|
if (!wasQualifiedBody) {
|
|
67
|
-
modelret.body = resolveType(t,
|
|
94
|
+
modelret.body = resolveType(t, {
|
|
95
|
+
nestlevel: 2,
|
|
96
|
+
currentNamespace: namespace,
|
|
97
|
+
context,
|
|
98
|
+
visibility: Visibility.Read,
|
|
99
|
+
resolveEvenWithName: true,
|
|
100
|
+
}).replaceAll("\n", "\n ");
|
|
68
101
|
}
|
|
69
102
|
ret.push(modelret);
|
|
70
103
|
} else if (t.kind === "Union") {
|
|
@@ -73,12 +106,27 @@ export const emitRoutedTypemap = (
|
|
|
73
106
|
t.variants.forEach((variant) => {
|
|
74
107
|
ret.push(...getReturnType(variant.type));
|
|
75
108
|
});
|
|
76
|
-
} else
|
|
109
|
+
} else
|
|
110
|
+
ret.push({
|
|
111
|
+
status: 200,
|
|
112
|
+
body: resolveType(t, {
|
|
113
|
+
nestlevel: 1,
|
|
114
|
+
currentNamespace: namespace,
|
|
115
|
+
context,
|
|
116
|
+
visibility: Visibility.Read,
|
|
117
|
+
resolveEvenWithName: true,
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
77
120
|
return ret;
|
|
78
121
|
};
|
|
79
|
-
ops[
|
|
122
|
+
ops[path][verb].response = getReturnType(op.returnType);
|
|
80
123
|
}
|
|
81
|
-
}
|
|
124
|
+
}; // end operations
|
|
125
|
+
|
|
126
|
+
n.operations.forEach(processOp);
|
|
127
|
+
n.interfaces.forEach((itf) => {
|
|
128
|
+
if (!isTemplateDeclaration(itf)) itf.operations.forEach(processOp);
|
|
129
|
+
});
|
|
82
130
|
|
|
83
131
|
// get and traverse all namespaces
|
|
84
132
|
n.namespaces.forEach((ns) => traverseNamespace(ns));
|
|
@@ -87,12 +135,19 @@ export const emitRoutedTypemap = (
|
|
|
87
135
|
traverseNamespace(namespace);
|
|
88
136
|
let out = `export type types_${context.options["root-namespace"]} = {\n`;
|
|
89
137
|
out += Object.entries(ops)
|
|
90
|
-
.map((
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
138
|
+
.map((path) => {
|
|
139
|
+
let pathret = ` ['${path[0]}']: {\n`;
|
|
140
|
+
pathret += Object.entries(path[1])
|
|
141
|
+
.map((verb) => {
|
|
142
|
+
let verbret = ` ['${verb[0]}']: {\n`;
|
|
143
|
+
verbret += ` request: ${verb[1].request}\n`;
|
|
144
|
+
verbret += ` response: ${verb[1].response.map((res) => `{status: ${res.status}, body: ${res.body}}`).join(" | ")}\n`;
|
|
145
|
+
verbret += " }";
|
|
146
|
+
return verbret;
|
|
147
|
+
})
|
|
148
|
+
.join(",\n");
|
|
149
|
+
pathret += "\n }";
|
|
150
|
+
return pathret;
|
|
96
151
|
})
|
|
97
152
|
.join(",\n");
|
|
98
153
|
out += "\n};\n";
|
package/src/emit_routes.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
EmitContext,
|
|
3
|
+
getDoc,
|
|
4
|
+
isTemplateDeclaration,
|
|
5
|
+
Namespace,
|
|
6
|
+
Operation,
|
|
7
|
+
} from "@typespec/compiler";
|
|
2
8
|
import { getAuthentication, getHttpOperation } from "@typespec/http";
|
|
3
9
|
|
|
4
10
|
export const emitRoutes = (
|
|
@@ -9,9 +15,7 @@ export const emitRoutes = (
|
|
|
9
15
|
|
|
10
16
|
const traverseNamespace = (n: Namespace, nestLevel: number): void => {
|
|
11
17
|
// operations
|
|
12
|
-
|
|
13
|
-
n.operations.forEach((op) => {
|
|
14
|
-
opnum++;
|
|
18
|
+
const processOp = (op: Operation) => {
|
|
15
19
|
const httpOp = getHttpOperation(context.program, op);
|
|
16
20
|
|
|
17
21
|
// jsdoc comment
|
|
@@ -90,11 +94,13 @@ export const emitRoutes = (
|
|
|
90
94
|
);
|
|
91
95
|
|
|
92
96
|
// finalize route entry
|
|
93
|
-
out = out.addLine(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
97
|
+
out = out.addLine("},", nestLevel + 1);
|
|
98
|
+
}; // end operations
|
|
99
|
+
|
|
100
|
+
n.operations.forEach(processOp);
|
|
101
|
+
n.interfaces.forEach((itf) => {
|
|
102
|
+
if (!isTemplateDeclaration(itf)) itf.operations.forEach(processOp);
|
|
103
|
+
});
|
|
98
104
|
|
|
99
105
|
// get and traverse all namespaces
|
|
100
106
|
let nsnum = 0;
|
package/src/emit_types.ts
CHANGED
|
@@ -22,7 +22,12 @@ const emitTypes = (
|
|
|
22
22
|
|
|
23
23
|
n.enums.forEach((e) => {
|
|
24
24
|
if (options["enable-types"]) {
|
|
25
|
-
const resolved = resolveEnum(e,
|
|
25
|
+
const resolved = resolveEnum(e, {
|
|
26
|
+
context: context,
|
|
27
|
+
currentNamespace: n,
|
|
28
|
+
nestlevel: 0,
|
|
29
|
+
isNamespaceRoot: true,
|
|
30
|
+
});
|
|
26
31
|
if (resolved) {
|
|
27
32
|
const doc = getDoc(context.program, e);
|
|
28
33
|
if (doc) file = file.addLine(`/** ${doc} */`);
|
|
@@ -32,7 +37,12 @@ const emitTypes = (
|
|
|
32
37
|
});
|
|
33
38
|
n.unions.forEach((u) => {
|
|
34
39
|
if (options["enable-types"]) {
|
|
35
|
-
const resolved = resolveUnion(u,
|
|
40
|
+
const resolved = resolveUnion(u, {
|
|
41
|
+
currentNamespace: n,
|
|
42
|
+
context,
|
|
43
|
+
nestlevel: 0,
|
|
44
|
+
isNamespaceRoot: true,
|
|
45
|
+
});
|
|
36
46
|
if (resolved) {
|
|
37
47
|
const doc = getDoc(context.program, u);
|
|
38
48
|
if (doc) file = file.addLine(`/** ${doc} */`);
|
|
@@ -42,13 +52,19 @@ const emitTypes = (
|
|
|
42
52
|
});
|
|
43
53
|
n.models.forEach((m) => {
|
|
44
54
|
if (options["enable-types"]) {
|
|
45
|
-
const resolved = resolveModel(m,
|
|
55
|
+
const resolved = resolveModel(m, {
|
|
56
|
+
nestlevel: 0,
|
|
57
|
+
currentNamespace: n,
|
|
58
|
+
context,
|
|
59
|
+
isNamespaceRoot: true,
|
|
60
|
+
});
|
|
46
61
|
if (resolved) {
|
|
47
62
|
const doc = getDoc(context.program, m);
|
|
48
63
|
if (doc) file = file.addLine(`/** ${doc} */`);
|
|
49
64
|
file = file.addLine(`export interface ${m.name} ${resolved};`);
|
|
50
65
|
}
|
|
51
66
|
}
|
|
67
|
+
|
|
52
68
|
if (options["enable-typeguards"]) {
|
|
53
69
|
file = file.addLine(
|
|
54
70
|
`export function is${m.name}(arg: any): arg is ${m.name} {`,
|
|
@@ -11,38 +11,32 @@ import {
|
|
|
11
11
|
Type,
|
|
12
12
|
Union,
|
|
13
13
|
} from "@typespec/compiler";
|
|
14
|
+
import { isVisible, Visibility } from "@typespec/http";
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
type CommonOptions = {
|
|
17
|
+
nestlevel: number;
|
|
18
|
+
currentNamespace: Namespace;
|
|
19
|
+
context: EmitContext;
|
|
20
|
+
visibility?: Visibility;
|
|
21
|
+
resolveEvenWithName?: boolean;
|
|
22
|
+
isNamespaceRoot?: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const resolveType = (t: Type, opts: CommonOptions): string => {
|
|
21
26
|
let typeStr = "unknown";
|
|
22
27
|
switch (t.kind) {
|
|
23
28
|
case "Model":
|
|
24
29
|
if (t.name === "Array") {
|
|
25
|
-
typeStr = resolveArray(
|
|
26
|
-
t as ArrayModelType,
|
|
27
|
-
nestlevel,
|
|
28
|
-
currentNamespace,
|
|
29
|
-
context,
|
|
30
|
-
);
|
|
30
|
+
typeStr = resolveArray(t as ArrayModelType, opts);
|
|
31
31
|
} else if (t.name === "Record") {
|
|
32
|
-
typeStr = resolveRecord(
|
|
33
|
-
|
|
34
|
-
nestlevel,
|
|
35
|
-
currentNamespace,
|
|
36
|
-
context,
|
|
37
|
-
);
|
|
38
|
-
} else
|
|
39
|
-
typeStr = resolveModel(t, nestlevel + 1, currentNamespace, context);
|
|
32
|
+
typeStr = resolveRecord(t as RecordModelType, opts);
|
|
33
|
+
} else typeStr = resolveModel(t, opts);
|
|
40
34
|
break;
|
|
41
35
|
case "Boolean":
|
|
42
36
|
typeStr = "boolean";
|
|
43
37
|
break;
|
|
44
38
|
case "Enum":
|
|
45
|
-
typeStr = resolveEnum(t,
|
|
39
|
+
typeStr = resolveEnum(t, opts);
|
|
46
40
|
break;
|
|
47
41
|
case "Intrinsic":
|
|
48
42
|
typeStr = t.name;
|
|
@@ -57,10 +51,10 @@ export const resolveType = (
|
|
|
57
51
|
typeStr = `'${t.value}'`;
|
|
58
52
|
break;
|
|
59
53
|
case "Tuple":
|
|
60
|
-
typeStr = resolveTuple(t,
|
|
54
|
+
typeStr = resolveTuple(t, opts);
|
|
61
55
|
break;
|
|
62
56
|
case "Union":
|
|
63
|
-
typeStr = resolveUnion(t,
|
|
57
|
+
typeStr = resolveUnion(t, opts);
|
|
64
58
|
break;
|
|
65
59
|
default:
|
|
66
60
|
console.warn("Could not resolve type:", t.kind);
|
|
@@ -70,32 +64,29 @@ export const resolveType = (
|
|
|
70
64
|
|
|
71
65
|
export const resolveArray = (
|
|
72
66
|
a: ArrayModelType,
|
|
73
|
-
|
|
74
|
-
currentNamespace: Namespace,
|
|
75
|
-
context?: EmitContext,
|
|
67
|
+
opts: CommonOptions,
|
|
76
68
|
): string => {
|
|
77
69
|
if (a.name !== "Array")
|
|
78
70
|
throw new Error(`Trying to parse model ${a.name} as Array`);
|
|
79
|
-
return
|
|
71
|
+
return `(${resolveType(a.indexer.value, opts)})[]`;
|
|
80
72
|
};
|
|
81
73
|
|
|
82
74
|
export const resolveRecord = (
|
|
83
75
|
a: RecordModelType,
|
|
84
|
-
|
|
85
|
-
currentNamespace: Namespace,
|
|
86
|
-
context?: EmitContext,
|
|
76
|
+
opts: CommonOptions,
|
|
87
77
|
): string => {
|
|
88
78
|
if (a.name !== "Record")
|
|
89
79
|
throw new Error(`Trying to parse model ${a.name} as Record`);
|
|
90
|
-
return `{[k: string]: ${resolveType(a.indexer.value,
|
|
80
|
+
return `{[k: string]: ${resolveType(a.indexer.value, opts)}}`;
|
|
91
81
|
};
|
|
92
82
|
|
|
93
|
-
export const resolveEnum = (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
)
|
|
98
|
-
|
|
83
|
+
export const resolveEnum = (e: Enum, opts: CommonOptions): string => {
|
|
84
|
+
if (
|
|
85
|
+
e.name &&
|
|
86
|
+
!opts.isNamespaceRoot &&
|
|
87
|
+
e.namespace?.enums.has(e.name) &&
|
|
88
|
+
!opts.resolveEvenWithName
|
|
89
|
+
)
|
|
99
90
|
return e.name;
|
|
100
91
|
let ret = "{\n";
|
|
101
92
|
let i = 1;
|
|
@@ -107,34 +98,28 @@ export const resolveEnum = (
|
|
|
107
98
|
(typeof p.value === "string" ? `'${p.value}'` : p.value.toString());
|
|
108
99
|
ret = ret.addLine(
|
|
109
100
|
`${p.name.includes("-") ? `'${p.name}'` : p.name}${val}${i < e.members.size ? "," : ""}`,
|
|
110
|
-
nestlevel + 1,
|
|
101
|
+
opts.nestlevel + 1,
|
|
111
102
|
);
|
|
112
103
|
i++;
|
|
113
104
|
});
|
|
114
|
-
ret = ret.addLine("}", nestlevel, true);
|
|
105
|
+
ret = ret.addLine("}", opts.nestlevel, true);
|
|
115
106
|
return ret;
|
|
116
107
|
};
|
|
117
108
|
|
|
118
|
-
export const resolveTuple = (
|
|
119
|
-
t
|
|
120
|
-
nestlevel: number,
|
|
121
|
-
currentNamespace: Namespace,
|
|
122
|
-
context?: EmitContext,
|
|
123
|
-
): string => {
|
|
124
|
-
return `[${t.values.map((v) => resolveType(v, nestlevel, currentNamespace, context)).join(", ")}]`;
|
|
109
|
+
export const resolveTuple = (t: Tuple, opts: CommonOptions): string => {
|
|
110
|
+
return `[${t.values.map((v) => resolveType(v, opts)).join(", ")}]`;
|
|
125
111
|
};
|
|
126
112
|
|
|
127
|
-
export const resolveUnion = (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
)
|
|
134
|
-
if (u.name && !isNamespaceRoot && u.namespace?.unions.has(u.name))
|
|
113
|
+
export const resolveUnion = (u: Union, opts: CommonOptions): string => {
|
|
114
|
+
if (
|
|
115
|
+
u.name &&
|
|
116
|
+
!opts.isNamespaceRoot &&
|
|
117
|
+
u.namespace?.unions.has(u.name) &&
|
|
118
|
+
!opts.resolveEvenWithName
|
|
119
|
+
)
|
|
135
120
|
return u.name;
|
|
136
121
|
return Array.from(u.variants)
|
|
137
|
-
.map((v) => resolveType(v[1].type,
|
|
122
|
+
.map((v) => resolveType(v[1].type, opts))
|
|
138
123
|
.join(" | ");
|
|
139
124
|
};
|
|
140
125
|
export const resolveScalar = (s: Scalar): string => {
|
|
@@ -169,31 +154,38 @@ export const resolveScalar = (s: Scalar): string => {
|
|
|
169
154
|
return s.baseScalar ? resolveScalar(s.baseScalar) : ret;
|
|
170
155
|
};
|
|
171
156
|
|
|
172
|
-
export const resolveModel = (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
)
|
|
179
|
-
if (m.name && !isNamespaceRoot && currentNamespace.namespace === m.namespace)
|
|
157
|
+
export const resolveModel = (m: Model, opts: CommonOptions): string => {
|
|
158
|
+
if (
|
|
159
|
+
m.name &&
|
|
160
|
+
!opts.isNamespaceRoot &&
|
|
161
|
+
opts.currentNamespace.namespace === m.namespace &&
|
|
162
|
+
!opts.resolveEvenWithName
|
|
163
|
+
)
|
|
180
164
|
return m.name;
|
|
181
165
|
let ret = "{\n";
|
|
182
166
|
let i = 1;
|
|
183
167
|
m.properties.forEach((p) => {
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
|
|
168
|
+
if (
|
|
169
|
+
opts.visibility === undefined ||
|
|
170
|
+
isVisible(opts.context.program, p, opts.visibility)
|
|
171
|
+
) {
|
|
172
|
+
if (opts.context) {
|
|
173
|
+
const doc = getDoc(opts.context.program, p);
|
|
174
|
+
if (doc) ret = ret.addLine(`/** ${doc} */`, opts.nestlevel! + 1);
|
|
175
|
+
}
|
|
176
|
+
const typeStr = resolveType(p.type, {
|
|
177
|
+
...opts,
|
|
178
|
+
nestlevel: opts.nestlevel + 1,
|
|
179
|
+
});
|
|
180
|
+
if (typeStr.includes("unknown"))
|
|
181
|
+
console.warn(`Could not resolve property ${p.name} on ${m.name}`);
|
|
182
|
+
ret = ret.addLine(
|
|
183
|
+
`${p.name}${p.optional ? "?" : ""}: ${typeStr}${i < m.properties.size ? "," : ""}`,
|
|
184
|
+
opts.nestlevel + 1,
|
|
185
|
+
);
|
|
187
186
|
}
|
|
188
|
-
const typeStr = resolveType(p.type, nestlevel, currentNamespace, context);
|
|
189
|
-
if (typeStr.includes("unknown"))
|
|
190
|
-
console.warn(`Could not resolve property ${p.name} on ${m.name}`);
|
|
191
|
-
ret = ret.addLine(
|
|
192
|
-
`${p.name}${p.optional ? "?" : ""}: ${typeStr}${i < m.properties.size ? "," : ""}`,
|
|
193
|
-
nestlevel + 1,
|
|
194
|
-
);
|
|
195
187
|
i++;
|
|
196
188
|
});
|
|
197
|
-
ret = ret.addLine("}", nestlevel, true);
|
|
189
|
+
ret = ret.addLine("}", opts.nestlevel, true);
|
|
198
190
|
return ret;
|
|
199
191
|
};
|