typespec-typescript-emitter 0.4.1 → 1.0.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/CHANGELOG.md +17 -9
- package/README.md +19 -24
- package/dist/src/emit_mapped_types.d.ts +2 -0
- package/dist/src/emit_mapped_types.js +81 -0
- package/dist/src/emit_mapped_types.js.map +1 -0
- package/dist/src/emit_routes.d.ts +1 -6
- package/dist/src/emit_routes.js +49 -99
- package/dist/src/emit_routes.js.map +1 -1
- package/dist/src/emit_types.js +2 -5
- package/dist/src/emit_types.js.map +1 -1
- package/dist/src/emit_types_resolve.d.ts +6 -6
- package/dist/src/emit_types_resolve.js +21 -19
- package/dist/src/emit_types_resolve.js.map +1 -1
- package/dist/src/emitter.js +20 -12
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/lib.d.ts +1 -2
- package/dist/src/lib.js +1 -2
- package/dist/src/lib.js.map +1 -1
- package/package.json +1 -1
- package/src/emit_mapped_types.ts +100 -0
- package/src/emit_routes.ts +67 -142
- package/src/emit_types.ts +2 -5
- package/src/emit_types_resolve.ts +20 -18
- package/src/emitter.ts +25 -21
- package/src/lib.ts +2 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
# [1.0.0](https://github.com/crowbait/typespec-typescript-emitter/compare/v0.4.1...v1.0.0) (2025-03-13)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
* feat!: complete rework of routes emitter ([2f07e7f](https://github.com/crowbait/typespec-typescript-emitter/commit/2f07e7f487f0e6531b89e66f7de4d7d09382e4e5))
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
* routed typemap ([0a57f43](https://github.com/crowbait/typespec-typescript-emitter/commit/0a57f439e6f483c35b00824c725aecc66d3a80df))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### BREAKING CHANGES
|
|
13
|
+
|
|
14
|
+
* This breaks existing configurations and - depending on how it was used - usage of the emitted routes object.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
1
18
|
## [0.4.1](https://github.com/crowbait/typespec-typescript-emitter/compare/v0.4.0...v0.4.1) (2025-02-25)
|
|
2
19
|
|
|
3
20
|
|
|
@@ -36,12 +53,3 @@
|
|
|
36
53
|
|
|
37
54
|
|
|
38
55
|
|
|
39
|
-
## [0.3.3](https://github.com/crowbait/typespec-typescript-emitter/compare/v0.3.2...v0.3.3) (2025-01-02)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
### Bug Fixes
|
|
43
|
-
|
|
44
|
-
* parsing of Record-type models ([b6e1dd9](https://github.com/crowbait/typespec-typescript-emitter/commit/b6e1dd91b94e39ad657d584f49051bc8e9e83608)), closes [#2](https://github.com/crowbait/typespec-typescript-emitter/issues/2)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
package/README.md
CHANGED
|
@@ -45,8 +45,6 @@ options:
|
|
|
45
45
|
enable-types: true
|
|
46
46
|
enable-typeguards: false
|
|
47
47
|
enable-routes: false
|
|
48
|
-
relative-routes: false
|
|
49
|
-
typeguards-in-routes: true
|
|
50
48
|
```
|
|
51
49
|
|
|
52
50
|
The following options are available:
|
|
@@ -56,8 +54,6 @@ The following options are available:
|
|
|
56
54
|
- `enable-types` (default: true): enables output of TypeScript types.
|
|
57
55
|
- `enable-typeguards` (default: false): enables output of typeguards, *IF* type-output is enabled.
|
|
58
56
|
- `enable-routes` (default: false): enables output of the HTTP-routes object.
|
|
59
|
-
- `relative-routes` (default: false): emits URLs in the routes object relative to the server's address (as opposed to a fully qualified URL when set to false).
|
|
60
|
-
- `typeguards-in-routes` (default: false) **Experimental**: generates or references typeguards in the routes object, *IF* types, typeguards *and* routes are enabled.
|
|
61
57
|
|
|
62
58
|
## Types emitter
|
|
63
59
|
|
|
@@ -154,7 +150,12 @@ model Derived2 {...OmitProperties<Demo, "prop1">};
|
|
|
154
150
|
**This emitter depends on your use of the `TypeSpec.Http` library**.
|
|
155
151
|
|
|
156
152
|
If you're using `TypeSpec.Http` to define your API routes and endpoints, this library offers an emitter to export a `routes` object.
|
|
157
|
-
|
|
153
|
+
It will generate a nested object containing information about every `op` you have defined, nested by namespace.
|
|
154
|
+
I contains the following data (per `op`):
|
|
155
|
+
- `method`: HTTP method
|
|
156
|
+
- `path`: Path (as defined as `route`-string; parameters are not substituted)
|
|
157
|
+
- `getUrl`: Function for generating a valid URL to this `op`; if the path has parameters, this function will have analogue parameters
|
|
158
|
+
- `auth`: Array of valid authentication schemes (or `[null]`, if none)
|
|
158
159
|
Just as the types emitter, this emitter will also preserve docs as JSDoc-style comments.
|
|
159
160
|
|
|
160
161
|
Example:
|
|
@@ -176,7 +177,7 @@ namespace myProject { // remember to set in config!
|
|
|
176
177
|
namespace sub {
|
|
177
178
|
@post
|
|
178
179
|
@route("post/{post_param}")
|
|
179
|
-
@useAuth(
|
|
180
|
+
@useAuth(BearerAuth)
|
|
180
181
|
op postSomething(
|
|
181
182
|
@path post_param: int32,
|
|
182
183
|
@body body: string
|
|
@@ -191,29 +192,23 @@ namespace myProject { // remember to set in config!
|
|
|
191
192
|
/* /path/to/outdir/routes_{root-namespace}.ts */
|
|
192
193
|
export const routes_myProject = {
|
|
193
194
|
getSomething: {
|
|
194
|
-
method: '
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
isRequestType: null,
|
|
199
|
-
isResponseType: (arg: any): boolean => typeof arg === 'string'
|
|
195
|
+
method: 'GET',
|
|
196
|
+
path: '/',
|
|
197
|
+
getUrl: (): string => `/`,
|
|
198
|
+
auth: [null]
|
|
200
199
|
},
|
|
201
200
|
getSmthElse: {
|
|
202
|
-
method: '
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
isRequestType: null,
|
|
207
|
-
isResponseType: (arg: any): boolean => typeof arg === 'string'
|
|
201
|
+
method: 'GET',
|
|
202
|
+
path: '/{param}',
|
|
203
|
+
getUrl: (params: {param: string}): string => `/${params.param}`,
|
|
204
|
+
auth: [null, "BASIC"]
|
|
208
205
|
},
|
|
209
206
|
sub: {
|
|
210
207
|
postSomething: {
|
|
211
|
-
method: '
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
isRequestType: (arg: any): boolean => typeof arg === 'string',
|
|
216
|
-
isResponseType: (arg: any): boolean => typeof arg === 'string'
|
|
208
|
+
method: 'POST',
|
|
209
|
+
path: '/post/{post_param}',
|
|
210
|
+
getUrl: (params: {post_param: string}): string => `/post/${params.post_param}`,
|
|
211
|
+
auth: ["BEARER"]
|
|
217
212
|
}
|
|
218
213
|
}
|
|
219
214
|
} as const;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getHttpOperation } from "@typespec/http";
|
|
2
|
+
import { resolveType } from "./emit_types_resolve.js";
|
|
3
|
+
export const emitRoutedTypemap = (context, namespace) => {
|
|
4
|
+
const ops = {};
|
|
5
|
+
const traverseNamespace = (n) => {
|
|
6
|
+
// operations
|
|
7
|
+
n.operations.forEach((op) => {
|
|
8
|
+
const httpOp = getHttpOperation(context.program, op);
|
|
9
|
+
const identifier = httpOp[0].path;
|
|
10
|
+
ops[identifier] = {
|
|
11
|
+
request: "null",
|
|
12
|
+
response: [{ status: 200, body: "unknown" }],
|
|
13
|
+
};
|
|
14
|
+
// request
|
|
15
|
+
let request = "null";
|
|
16
|
+
if (op.parameters.properties.has("body")) {
|
|
17
|
+
request = resolveType(op.parameters.properties.get("body").type, 1, namespace, context);
|
|
18
|
+
}
|
|
19
|
+
ops[identifier].request = request;
|
|
20
|
+
// response
|
|
21
|
+
if (op.returnType && op.returnType.kind) {
|
|
22
|
+
const getReturnType = (t) => {
|
|
23
|
+
const ret = [];
|
|
24
|
+
if (t.kind === "Model") {
|
|
25
|
+
// if the return type is a model, it may have a fully qualified body
|
|
26
|
+
const modelret = {
|
|
27
|
+
status: 200,
|
|
28
|
+
body: "unknown",
|
|
29
|
+
};
|
|
30
|
+
let wasQualifiedBody = false;
|
|
31
|
+
t.properties.forEach((prop) => {
|
|
32
|
+
prop.decorators.forEach((dec) => {
|
|
33
|
+
// one of the properties may be the status code
|
|
34
|
+
if (dec.definition?.name === "@statusCode" &&
|
|
35
|
+
prop.type.kind === "Number")
|
|
36
|
+
modelret.status = prop.type.value;
|
|
37
|
+
// one of the properties may be the body definition
|
|
38
|
+
if (dec.definition?.name === "@body") {
|
|
39
|
+
modelret.body = resolveType(prop.type, 1, namespace, context);
|
|
40
|
+
wasQualifiedBody = true;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
// ... if not, we assume status 200 and treat the model as the body
|
|
45
|
+
if (!wasQualifiedBody) {
|
|
46
|
+
modelret.body = resolveType(t, 1, namespace, context);
|
|
47
|
+
}
|
|
48
|
+
ret.push(modelret);
|
|
49
|
+
}
|
|
50
|
+
else if (t.kind === "Union") {
|
|
51
|
+
// if the return type is a union, we have to check and resolve all variants
|
|
52
|
+
// the union could either be a body-only definition or fully qualified (see above)
|
|
53
|
+
t.variants.forEach((variant) => {
|
|
54
|
+
ret.push(...getReturnType(variant.type));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
ret.push({ status: 200, body: resolveType(t, 1, namespace) });
|
|
59
|
+
return ret;
|
|
60
|
+
};
|
|
61
|
+
ops[identifier].response = getReturnType(op.returnType);
|
|
62
|
+
}
|
|
63
|
+
}); // end operations
|
|
64
|
+
// get and traverse all namespaces
|
|
65
|
+
n.namespaces.forEach((ns) => traverseNamespace(ns));
|
|
66
|
+
};
|
|
67
|
+
traverseNamespace(namespace);
|
|
68
|
+
let out = `export type types_${context.options["root-namespace"]} = {\n`;
|
|
69
|
+
out += Object.entries(ops)
|
|
70
|
+
.map((op) => {
|
|
71
|
+
let ret = ` ['${op[0]}']: {\n`;
|
|
72
|
+
ret += ` request: ${op[1].request}\n`;
|
|
73
|
+
ret += ` response: ${op[1].response.map((res) => `{status: ${res.status}, body: ${res.body}}`).join(" | ")}\n`;
|
|
74
|
+
ret += " }";
|
|
75
|
+
return ret;
|
|
76
|
+
})
|
|
77
|
+
.join(",\n");
|
|
78
|
+
out += "\n};\n";
|
|
79
|
+
return out;
|
|
80
|
+
};
|
|
81
|
+
//# sourceMappingURL=emit_mapped_types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emit_mapped_types.js","sourceRoot":"","sources":["../../src/emit_mapped_types.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,OAAoB,EACpB,SAAoB,EACZ,EAAE;IACV,MAAM,GAAG,GAML,EAAE,CAAC;IAEP,MAAM,iBAAiB,GAAG,CAAC,CAAY,EAAQ,EAAE;QAC/C,aAAa;QACb,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;YAC1B,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACrD,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAClC,GAAG,CAAC,UAAU,CAAC,GAAG;gBAChB,OAAO,EAAE,MAAM;gBACf,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;aAC7C,CAAC;YAEF,UAAU;YACV,IAAI,OAAO,GAAG,MAAM,CAAC;YACrB,IAAI,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;gBACzC,OAAO,GAAG,WAAW,CACnB,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,IAAI,EAC1C,CAAC,EACD,SAAS,EACT,OAAO,CACR,CAAC;YACJ,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Q,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;QAClH,GAAG,IAAI,KAAK,CAAC;QACb,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;SACD,IAAI,CAAC,KAAK,CAAC,CAAC;IACf,GAAG,IAAI,QAAQ,CAAC;IAChB,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
|
@@ -1,7 +1,2 @@
|
|
|
1
1
|
import { EmitContext, Namespace } from "@typespec/compiler";
|
|
2
|
-
|
|
3
|
-
declare const emitRoutes: (context: EmitContext, namespace: Namespace, rootServer: string, options: EmitterOptions, knownTypeguards: Array<{
|
|
4
|
-
filename: string;
|
|
5
|
-
name: string;
|
|
6
|
-
}>) => string;
|
|
7
|
-
export default emitRoutes;
|
|
2
|
+
export declare const emitRoutes: (context: EmitContext, namespace: Namespace) => string;
|
package/dist/src/emit_routes.js
CHANGED
|
@@ -1,129 +1,79 @@
|
|
|
1
|
-
import { getDoc
|
|
1
|
+
import { getDoc } from "@typespec/compiler";
|
|
2
2
|
import { getAuthentication, getHttpOperation } from "@typespec/http";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const emitRoutes = (context, namespace, rootServer, options, knownTypeguards) => {
|
|
6
|
-
const rootNode = `routes_${context.options["root-namespace"]}`;
|
|
7
|
-
const imports = [];
|
|
8
|
-
let out = autogenerateWarning;
|
|
9
|
-
out += `const ${rootNode} = {\n`;
|
|
3
|
+
export const emitRoutes = (context, namespace) => {
|
|
4
|
+
let out = `export const routes_${context.options["root-namespace"]} = {\n`;
|
|
10
5
|
const traverseNamespace = (n, nestLevel) => {
|
|
11
6
|
// operations
|
|
12
|
-
let
|
|
7
|
+
let opnum = 0;
|
|
13
8
|
n.operations.forEach((op) => {
|
|
14
|
-
|
|
9
|
+
opnum++;
|
|
15
10
|
const httpOp = getHttpOperation(context.program, op);
|
|
16
11
|
// jsdoc comment
|
|
17
12
|
const doc = getDoc(context.program, op);
|
|
18
13
|
if (doc)
|
|
19
14
|
out = out.addLine(`/** ${doc} */`, nestLevel + 1);
|
|
15
|
+
// start op body
|
|
20
16
|
out = out.addLine(`${op.name}: {`, nestLevel + 1);
|
|
21
|
-
//
|
|
17
|
+
// HTTP method
|
|
22
18
|
out = out.addLine(`method: '${httpOp[0].verb.toUpperCase()}',`, nestLevel + 2);
|
|
23
|
-
//
|
|
19
|
+
// path
|
|
20
|
+
out = out.addLine(`path: '${httpOp[0].path}',`, nestLevel + 2);
|
|
21
|
+
// getUrl
|
|
24
22
|
const pathParams = httpOp[0].parameters.parameters.filter((p) => p.type === "path");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
fn += pathParams.reduce((sum, cur) => sum.replaceAll(`{${cur.name}}`, `${"$"}{p.${cur.name}}`), `\`${!options["relative-routes"] ? rootServer : ""}${httpOp[0].path}\`,`);
|
|
32
|
-
out = out.addLine(fn, nestLevel + 2);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
out = out.addLine(`getUrl: () => '${!options["relative-routes"] ? rootServer : ""}${httpOp[0].path}',`, nestLevel + 2);
|
|
36
|
-
}
|
|
23
|
+
const pathParamsArgsString = pathParams
|
|
24
|
+
.map((p) => `${p.name}: string`)
|
|
25
|
+
.join(", ");
|
|
26
|
+
const pathParamsArg = `params: {${pathParamsArgsString}}`;
|
|
27
|
+
const pathParamsOutString = pathParams.reduce((sum, cur) => sum.replaceAll(`{${cur.name}}`, `${"$"}{params.${cur.name}}`), `\`${httpOp[0].path}\``);
|
|
28
|
+
out = out.addLine(`getUrl: (${pathParams.length > 0 ? pathParamsArg : ""}): string => ${pathParamsOutString},`, nestLevel + 2);
|
|
37
29
|
// auth
|
|
38
|
-
let auth =
|
|
30
|
+
let auth = [];
|
|
39
31
|
const opAuth = getAuthentication(context.program, op);
|
|
40
32
|
if (opAuth) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
includesSome && includesNone
|
|
51
|
-
? "varies"
|
|
52
|
-
: includesSome && !includesNone
|
|
53
|
-
? true
|
|
54
|
-
: false;
|
|
55
|
-
}
|
|
56
|
-
out = out.addLine(`auth: ${typeof auth === "string" ? `'${auth}'` : auth.toString()}${options["typeguards-in-routes"] ? "," : ""}`, nestLevel + 2);
|
|
57
|
-
// typeguards
|
|
58
|
-
const typeguardLines = (t) => {
|
|
59
|
-
const guard = getTypeguard(t, "arg", 0, n, knownTypeguards);
|
|
60
|
-
imports.push(...guard[1]);
|
|
61
|
-
return guard[0].split("\n");
|
|
62
|
-
};
|
|
63
|
-
if (options["typeguards-in-routes"]) {
|
|
64
|
-
if (!knownTypeguards) {
|
|
65
|
-
console.warn("Typeguard Names List was empty when it shouldn't have been.");
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (op.parameters.properties.has("body")) {
|
|
69
|
-
const lines = typeguardLines(op.parameters.properties.get("body").type);
|
|
70
|
-
out = out.addLine(`isRequestType: ${lines.length === 0 ? "null" : `(arg: any): boolean => ${lines.shift()}`}${lines.length < 1 ? "," : ""}`, nestLevel + 2, lines.length === 1);
|
|
71
|
-
if (lines.length > 0) {
|
|
72
|
-
lines[lines.length - 1] += ",";
|
|
73
|
-
lines.forEach((line, i, arr) => {
|
|
74
|
-
out = out.addLine(line, lines.length > 1 ? nestLevel + (i < arr.length - 1 ? 3 : 2) : 0);
|
|
33
|
+
opAuth.options.forEach((authOption) => authOption.schemes.forEach((authScheme) => {
|
|
34
|
+
if (authScheme.type === "noAuth")
|
|
35
|
+
auth.push(null);
|
|
36
|
+
if (authScheme.type === "http")
|
|
37
|
+
auth.push(authScheme.scheme.toUpperCase());
|
|
38
|
+
if (authScheme.type === "apiKey")
|
|
39
|
+
auth.push({
|
|
40
|
+
apiKeyLocation: authScheme.in.toUpperCase(),
|
|
41
|
+
apiKeyName: authScheme.name,
|
|
75
42
|
});
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
op.returnType.kind &&
|
|
82
|
-
op.returnType.kind !== "Intrinsic") {
|
|
83
|
-
const lines = [];
|
|
84
|
-
const addModelLines = (m) => {
|
|
85
|
-
if (m.properties.has("body")) {
|
|
86
|
-
lines.push(...typeguardLines(m.properties.get("body").type));
|
|
87
|
-
// else (no "body" prop): stays empty -> 'null'
|
|
88
|
-
// why?: unnamed model return type is likely to be / should be
|
|
89
|
-
// headers'n'stuff, so if there is no "body" property, play it safe
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
if (op.returnType.kind === "Union") {
|
|
93
|
-
op.returnType.variants.forEach((v) => {
|
|
94
|
-
if (v.type.kind === "Model")
|
|
95
|
-
addModelLines(v.type);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
if (op.returnType.kind === "Model")
|
|
99
|
-
addModelLines(op.returnType);
|
|
100
|
-
out = out.addLine(`isResponseType: ${lines.length === 0 ? "null" : `(arg: any): boolean => ${lines.shift()}`}${lines.length < 1 ? "," : ""}`, nestLevel + 2, lines.length === 1);
|
|
101
|
-
lines.forEach((line, i, arr) => {
|
|
102
|
-
out = out.addLine(line, lines.length > 1 ? nestLevel + (i < arr.length - 1 ? 3 : 2) : 0);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
else
|
|
106
|
-
out = out.addLine("isResponseType: null", nestLevel + 2);
|
|
43
|
+
if (authScheme.type === "oauth2")
|
|
44
|
+
authScheme.flows.forEach((oauthFlow) => auth.push(`OAuth_${oauthFlow.type}`));
|
|
45
|
+
if (authScheme.type === "openIdConnect")
|
|
46
|
+
auth.push("OPENID");
|
|
47
|
+
}));
|
|
107
48
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
49
|
+
else
|
|
50
|
+
auth = [null];
|
|
51
|
+
out = out.addLine(`auth: [${auth
|
|
52
|
+
.map((authEntry) => !authEntry
|
|
53
|
+
? "null"
|
|
54
|
+
: typeof authEntry === "string"
|
|
55
|
+
? `'${authEntry}'`
|
|
56
|
+
: `{${Object.entries(authEntry)
|
|
57
|
+
.map((entry) => `${entry[0]}: '${entry[1]}'`)
|
|
58
|
+
.join(", ")}}`)
|
|
59
|
+
.join(", ")}]`, nestLevel + 2);
|
|
60
|
+
// finalize route entry
|
|
61
|
+
out = out.addLine(`}${opnum < n.operations.size || n.namespaces.size > 0 ? "," : ""}`, nestLevel + 1);
|
|
62
|
+
}); // end operations
|
|
63
|
+
// get and traverse all namespaces
|
|
64
|
+
let nsnum = 0;
|
|
112
65
|
n.namespaces.forEach((ns) => {
|
|
113
|
-
|
|
66
|
+
nsnum++;
|
|
114
67
|
const doc = getDoc(context.program, ns);
|
|
115
68
|
if (doc)
|
|
116
69
|
out = out.addLine(`/** ${doc} */`, nestLevel + 1);
|
|
117
70
|
out = out.addLine(`${ns.name}: {`, nestLevel + 1);
|
|
118
71
|
traverseNamespace(ns, nestLevel + 1);
|
|
119
|
-
out = out.addLine(`}${
|
|
72
|
+
out = out.addLine(`}${nsnum < n.namespaces.size ? "," : ""}`, nestLevel + 1);
|
|
120
73
|
});
|
|
121
74
|
};
|
|
122
75
|
traverseNamespace(namespace, 0);
|
|
123
76
|
out += "} as const;\n";
|
|
124
|
-
out += `export default ${rootNode};\n`;
|
|
125
|
-
out = `${imports.filter((x, i, arr) => arr.indexOf(x) === i).join("\n")}\n\n${out}`;
|
|
126
77
|
return out;
|
|
127
78
|
};
|
|
128
|
-
export default emitRoutes;
|
|
129
79
|
//# sourceMappingURL=emit_routes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emit_routes.js","sourceRoot":"","sources":["../../src/emit_routes.ts"],"names":[],"mappings":"AAAA,O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veModel, resolveUnion, } from "./emit_types_resolve.js";
|
|
3
3
|
import { getTypeguardModel } from "./emit_types_typeguards.js";
|
|
4
|
-
import autogenerateWarning from "./helper_autogenerateWarning.js";
|
|
5
4
|
const emitTypes = (context, namespace, options) => {
|
|
6
5
|
const out = { files: {}, typeguardedNames: [] };
|
|
7
6
|
const traverseNamespace = (n) => {
|
|
@@ -19,7 +18,7 @@ const emitTypes = (context, namespace, options) => {
|
|
|
19
18
|
});
|
|
20
19
|
n.unions.forEach((u) => {
|
|
21
20
|
if (options["enable-types"]) {
|
|
22
|
-
const resolved = resolveUnion(
|
|
21
|
+
const resolved = resolveUnion(u, 0, n, context, true);
|
|
23
22
|
if (resolved) {
|
|
24
23
|
const doc = getDoc(context.program, u);
|
|
25
24
|
if (doc)
|
|
@@ -30,7 +29,7 @@ const emitTypes = (context, namespace, options) => {
|
|
|
30
29
|
});
|
|
31
30
|
n.models.forEach((m) => {
|
|
32
31
|
if (options["enable-types"]) {
|
|
33
|
-
const resolved = resolveModel(
|
|
32
|
+
const resolved = resolveModel(m, 0, n, context, true);
|
|
34
33
|
if (resolved) {
|
|
35
34
|
const doc = getDoc(context.program, m);
|
|
36
35
|
if (doc)
|
|
@@ -55,8 +54,6 @@ const emitTypes = (context, namespace, options) => {
|
|
|
55
54
|
}
|
|
56
55
|
file += "\n";
|
|
57
56
|
});
|
|
58
|
-
if (file)
|
|
59
|
-
file = autogenerateWarning + file;
|
|
60
57
|
// set output for this namespace
|
|
61
58
|
out.files[n.name.charAt(0).toUpperCase() + n.name.slice(1)] = file;
|
|
62
59
|
// recursively iterate child namespaces
|
|
@@ -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,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";
|
|
2
|
-
export declare const resolveType: (
|
|
3
|
-
export declare const resolveArray: (
|
|
4
|
-
export declare const resolveRecord: (
|
|
2
|
+
export declare const resolveType: (t: Type, nestlevel: number, currentNamespace: Namespace, context?: EmitContext) => string;
|
|
3
|
+
export declare const resolveArray: (a: ArrayModelType, nestlevel: number, currentNamespace: Namespace, context?: EmitContext) => string;
|
|
4
|
+
export declare const resolveRecord: (a: RecordModelType, nestlevel: number, currentNamespace: Namespace, context?: EmitContext) => string;
|
|
5
5
|
export declare const resolveEnum: (e: Enum, nestlevel: number, isNamespaceRoot?: boolean) => string;
|
|
6
|
-
export declare const resolveTuple: (
|
|
7
|
-
export declare const resolveUnion: (
|
|
6
|
+
export declare const resolveTuple: (t: Tuple, nestlevel: number, currentNamespace: Namespace, context?: EmitContext) => string;
|
|
7
|
+
export declare const resolveUnion: (u: Union, nestlevel: number, currentNamespace: Namespace, context?: EmitContext, isNamespaceRoot?: boolean) => string;
|
|
8
8
|
export declare const resolveScalar: (s: Scalar) => string;
|
|
9
|
-
export declare const resolveModel: (
|
|
9
|
+
export declare const resolveModel: (m: Model, nestlevel: number | undefined, currentNamespace: Namespace, context?: EmitContext, isNamespaceRoot?: boolean) => string;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { getDoc, } from "@typespec/compiler";
|
|
2
|
-
export const resolveType = (
|
|
2
|
+
export const resolveType = (t, nestlevel, currentNamespace, context) => {
|
|
3
3
|
let typeStr = "unknown";
|
|
4
4
|
switch (t.kind) {
|
|
5
5
|
case "Model":
|
|
6
6
|
if (t.name === "Array") {
|
|
7
|
-
typeStr = resolveArray(
|
|
7
|
+
typeStr = resolveArray(t, nestlevel, currentNamespace, context);
|
|
8
8
|
}
|
|
9
9
|
else if (t.name === "Record") {
|
|
10
|
-
typeStr = resolveRecord(
|
|
10
|
+
typeStr = resolveRecord(t, nestlevel, currentNamespace, context);
|
|
11
11
|
}
|
|
12
12
|
else
|
|
13
|
-
typeStr = resolveModel(
|
|
13
|
+
typeStr = resolveModel(t, nestlevel + 1, currentNamespace, context);
|
|
14
14
|
break;
|
|
15
15
|
case "Boolean":
|
|
16
16
|
typeStr = "boolean";
|
|
@@ -31,25 +31,25 @@ export const resolveType = (context, t, nestlevel, currentNamespace) => {
|
|
|
31
31
|
typeStr = `'${t.value}'`;
|
|
32
32
|
break;
|
|
33
33
|
case "Tuple":
|
|
34
|
-
typeStr = resolveTuple(
|
|
34
|
+
typeStr = resolveTuple(t, nestlevel, currentNamespace, context);
|
|
35
35
|
break;
|
|
36
36
|
case "Union":
|
|
37
|
-
typeStr = resolveUnion(
|
|
37
|
+
typeStr = resolveUnion(t, nestlevel, currentNamespace, context);
|
|
38
38
|
break;
|
|
39
39
|
default:
|
|
40
40
|
console.warn("Could not resolve type:", t.kind);
|
|
41
41
|
}
|
|
42
42
|
return typeStr;
|
|
43
43
|
};
|
|
44
|
-
export const resolveArray = (
|
|
44
|
+
export const resolveArray = (a, nestlevel, currentNamespace, context) => {
|
|
45
45
|
if (a.name !== "Array")
|
|
46
46
|
throw new Error(`Trying to parse model ${a.name} as Array`);
|
|
47
|
-
return `${resolveType(
|
|
47
|
+
return `${resolveType(a.indexer.value, nestlevel, currentNamespace, context)}[]`;
|
|
48
48
|
};
|
|
49
|
-
export const resolveRecord = (
|
|
49
|
+
export const resolveRecord = (a, nestlevel, currentNamespace, context) => {
|
|
50
50
|
if (a.name !== "Record")
|
|
51
51
|
throw new Error(`Trying to parse model ${a.name} as Record`);
|
|
52
|
-
return `{[k: string]: ${resolveType(
|
|
52
|
+
return `{[k: string]: ${resolveType(a.indexer.value, nestlevel, currentNamespace, context)}}`;
|
|
53
53
|
};
|
|
54
54
|
export const resolveEnum = (e, nestlevel, isNamespaceRoot) => {
|
|
55
55
|
if (e.name && !isNamespaceRoot && e.namespace?.enums.has(e.name))
|
|
@@ -67,14 +67,14 @@ export const resolveEnum = (e, nestlevel, isNamespaceRoot) => {
|
|
|
67
67
|
ret = ret.addLine("}", nestlevel, true);
|
|
68
68
|
return ret;
|
|
69
69
|
};
|
|
70
|
-
export const resolveTuple = (
|
|
71
|
-
return `[${t.values.map((v) => resolveType(
|
|
70
|
+
export const resolveTuple = (t, nestlevel, currentNamespace, context) => {
|
|
71
|
+
return `[${t.values.map((v) => resolveType(v, nestlevel, currentNamespace, context)).join(", ")}]`;
|
|
72
72
|
};
|
|
73
|
-
export const resolveUnion = (
|
|
73
|
+
export const resolveUnion = (u, nestlevel, currentNamespace, context, isNamespaceRoot) => {
|
|
74
74
|
if (u.name && !isNamespaceRoot && u.namespace?.unions.has(u.name))
|
|
75
75
|
return u.name;
|
|
76
76
|
return Array.from(u.variants)
|
|
77
|
-
.map((v) => resolveType(
|
|
77
|
+
.map((v) => resolveType(v[1].type, nestlevel, currentNamespace, context))
|
|
78
78
|
.join(" | ");
|
|
79
79
|
};
|
|
80
80
|
export const resolveScalar = (s) => {
|
|
@@ -108,16 +108,18 @@ export const resolveScalar = (s) => {
|
|
|
108
108
|
}
|
|
109
109
|
return s.baseScalar ? resolveScalar(s.baseScalar) : ret;
|
|
110
110
|
};
|
|
111
|
-
export const resolveModel = (
|
|
111
|
+
export const resolveModel = (m, nestlevel = 0, currentNamespace, context, isNamespaceRoot) => {
|
|
112
112
|
if (m.name && !isNamespaceRoot && currentNamespace.namespace === m.namespace)
|
|
113
113
|
return m.name;
|
|
114
114
|
let ret = "{\n";
|
|
115
115
|
let i = 1;
|
|
116
116
|
m.properties.forEach((p) => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
if (context) {
|
|
118
|
+
const doc = getDoc(context.program, p);
|
|
119
|
+
if (doc)
|
|
120
|
+
ret = ret.addLine(`/** ${doc} */`, nestlevel + 1);
|
|
121
|
+
}
|
|
122
|
+
const typeStr = resolveType(p.type, nestlevel, currentNamespace, context);
|
|
121
123
|
if (typeStr.includes("unknown"))
|
|
122
124
|
console.warn(`Could not resolve property ${p.name} on ${m.name}`);
|
|
123
125
|
ret = ret.addLine(`${p.name}${p.optional ? "?" : ""}: ${typeStr}${i < m.properties.size ? "," : ""}`, nestlevel + 1);
|