typespec-fastify 0.0.0 → 0.1.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/dist/src/components/AppFile.d.ts +4 -0
- package/dist/src/components/AppFile.js +46 -0
- package/dist/src/components/AppFile.js.map +1 -0
- package/dist/src/components/AppFile.jsx +19 -0
- package/dist/src/components/AppFile.jsx.map +1 -0
- package/dist/src/components/ModelsDirectory.d.ts +9 -0
- package/dist/src/components/ModelsDirectory.js +27 -0
- package/dist/src/components/ModelsDirectory.js.map +1 -0
- package/dist/src/components/ModelsDirectory.jsx +14 -0
- package/dist/src/components/ModelsDirectory.jsx.map +1 -0
- package/dist/src/components/OperationInterface.d.ts +18 -0
- package/dist/src/components/OperationInterface.js +124 -0
- package/dist/src/components/OperationInterface.js.map +1 -0
- package/dist/src/components/OperationInterface.jsx +84 -0
- package/dist/src/components/OperationInterface.jsx.map +1 -0
- package/dist/src/components/OperationsDirectory.d.ts +8 -0
- package/dist/src/components/OperationsDirectory.js +33 -0
- package/dist/src/components/OperationsDirectory.js.map +1 -0
- package/dist/src/components/OperationsDirectory.jsx +18 -0
- package/dist/src/components/OperationsDirectory.jsx.map +1 -0
- package/dist/src/components/RouteRegistration.d.ts +16 -0
- package/dist/src/components/RouteRegistration.js +165 -0
- package/dist/src/components/RouteRegistration.js.map +1 -0
- package/dist/src/components/RouteRegistration.jsx +115 -0
- package/dist/src/components/RouteRegistration.jsx.map +1 -0
- package/dist/src/components/RouterFile.d.ts +12 -0
- package/dist/src/components/RouterFile.js +56 -0
- package/dist/src/components/RouterFile.js.map +1 -0
- package/dist/src/components/RouterFile.jsx +31 -0
- package/dist/src/components/RouterFile.jsx.map +1 -0
- package/dist/src/components/RoutesDirectory.d.ts +10 -0
- package/dist/src/components/RoutesDirectory.js +92 -0
- package/dist/src/components/RoutesDirectory.js.map +1 -0
- package/dist/src/components/RoutesDirectory.jsx +54 -0
- package/dist/src/components/RoutesDirectory.jsx.map +1 -0
- package/dist/src/components/TsTypes.d.ts +10 -0
- package/dist/src/components/TsTypes.js +31 -0
- package/dist/src/components/TsTypes.js.map +1 -0
- package/dist/src/components/TsTypes.jsx +25 -0
- package/dist/src/components/TsTypes.jsx.map +1 -0
- package/dist/src/emitter.d.ts +3 -0
- package/dist/src/emitter.js +51 -0
- package/dist/src/emitter.js.map +1 -0
- package/dist/src/emitter.jsx +28 -0
- package/dist/src/emitter.jsx.map +1 -0
- package/dist/src/external-packages/fastify.d.ts +18 -0
- package/dist/src/external-packages/fastify.js +18 -0
- package/dist/src/external-packages/fastify.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib.d.ts +38 -0
- package/dist/src/lib.js +32 -0
- package/dist/src/lib.js.map +1 -0
- package/dist/src/testing/index.d.ts +2 -0
- package/dist/src/testing/index.js +6 -0
- package/dist/src/testing/index.js.map +1 -0
- package/dist/src/utils/http-helpers.d.ts +35 -0
- package/dist/src/utils/http-helpers.js +78 -0
- package/dist/src/utils/http-helpers.js.map +1 -0
- package/dist/src/utils/type-discovery.d.ts +7 -0
- package/dist/src/utils/type-discovery.js +87 -0
- package/dist/src/utils/type-discovery.js.map +1 -0
- package/dist/test/hello.test.d.ts +1 -0
- package/dist/test/hello.test.js +187 -0
- package/dist/test/hello.test.js.map +1 -0
- package/dist/test/test-host.d.ts +5 -0
- package/dist/test/test-host.js +52 -0
- package/dist/test/test-host.js.map +1 -0
- package/package.json +57 -10
- package/index.js +0 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { List } from "@alloy-js/core";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { fastifyLib } from "../external-packages/fastify.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generates the app.ts file which creates and configures a Fastify instance.
|
|
8
|
+
*/
|
|
9
|
+
export function AppFile() {
|
|
10
|
+
return _$createComponent(ts.SourceFile, {
|
|
11
|
+
path: "app.ts",
|
|
12
|
+
get children() {
|
|
13
|
+
return _$createComponent(ts.FunctionDeclaration, {
|
|
14
|
+
name: "createApp",
|
|
15
|
+
"export": true,
|
|
16
|
+
get returnType() {
|
|
17
|
+
return fastifyLib.FastifyInstance;
|
|
18
|
+
},
|
|
19
|
+
get children() {
|
|
20
|
+
return _$createComponent(List, {
|
|
21
|
+
get children() {
|
|
22
|
+
return [_$createComponent(ts.VarDeclaration, {
|
|
23
|
+
name: "server",
|
|
24
|
+
get children() {
|
|
25
|
+
return _$createComponent(ts.FunctionCallExpression, {
|
|
26
|
+
get target() {
|
|
27
|
+
return fastifyLib.fastify;
|
|
28
|
+
},
|
|
29
|
+
get args() {
|
|
30
|
+
return [_$createComponent(ts.ObjectExpression, {
|
|
31
|
+
jsValue: {
|
|
32
|
+
logger: true
|
|
33
|
+
}
|
|
34
|
+
})];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}), ["return server;"]];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=AppFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["List","ts","fastifyLib","AppFile","_$createComponent","SourceFile","path","children","FunctionDeclaration","name","returnType","FastifyInstance","VarDeclaration","FunctionCallExpression","target","fastify","args","ObjectExpression","jsValue","logger"],"sources":["../../../src/components/AppFile.tsx"],"sourcesContent":[null],"mappings":";AAAA,SAASA,IAAI,QAAQ,gBAAgB;AACrC,OAAO,KAAKC,EAAE,MAAM,sBAAsB;AAC1C,SAASC,UAAU,QAAQ,iCAAiC;;AAE5D;AACA;AACA;AACA,OAAO,SAASC,OAAOA,CAAA,EAAG;EACxB,OAAAC,iBAAA,CACGH,EAAE,CAACI,UAAU;IAACC,IAAI;IAAA,IAAAC,SAAA;MAAA,OAAAH,iBAAA,CAChBH,EAAE,CAACO,mBAAmB;QACrBC,IAAI;QAAA;QAAA,IAEJC,UAAUA,CAAA;UAAA,OAAER,UAAU,CAACS,eAAe;QAAA;QAAA,IAAAJ,SAAA;UAAA,OAAAH,iBAAA,CAErCJ,IAAI;YAAA,IAAAO,SAAA;cAAA,QAAAH,iBAAA,CACFH,EAAE,CAACW,cAAc;gBAACH,IAAI;gBAAA,IAAAF,SAAA;kBAAA,OAAAH,iBAAA,CACpBH,EAAE,CAACY,sBAAsB;oBAAA,IACxBC,MAAMA,CAAA;sBAAA,OAAEZ,UAAU,CAACa,OAAO;oBAAA;oBAAA,IAC1BC,IAAIA,CAAA;sBAAA,OAAE,CAAAZ,iBAAA,CAAEH,EAAE,CAACgB,gBAAgB;wBAACC,OAAO,EAAE;0BAAEC,MAAM,EAAE;wBAAK;sBAAC,GAAK;oBAAA;kBAAA;gBAAA;cAAA;YAAA;UAAA;QAAA;MAAA;IAAA;EAAA;AAQxE","ignoreList":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { List } from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { fastifyLib } from "../external-packages/fastify.js";
|
|
4
|
+
/**
|
|
5
|
+
* Generates the app.ts file which creates and configures a Fastify instance.
|
|
6
|
+
*/
|
|
7
|
+
export function AppFile() {
|
|
8
|
+
return (<ts.SourceFile path="app.ts">
|
|
9
|
+
<ts.FunctionDeclaration name="createApp" export returnType={fastifyLib.FastifyInstance}>
|
|
10
|
+
<List>
|
|
11
|
+
<ts.VarDeclaration name="server">
|
|
12
|
+
<ts.FunctionCallExpression target={fastifyLib.fastify} args={[<ts.ObjectExpression jsValue={{ logger: true }}/>]}/>
|
|
13
|
+
</ts.VarDeclaration>
|
|
14
|
+
<>return server;</>
|
|
15
|
+
</List>
|
|
16
|
+
</ts.FunctionDeclaration>
|
|
17
|
+
</ts.SourceFile>);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=AppFile.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AppFile.jsx","sourceRoot":"","sources":["../../../src/components/AppFile.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D;;GAEG;AACH,MAAM,UAAU,OAAO;IACrB,OAAO,CACL,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAC1B;MAAA,CAAC,EAAE,CAAC,mBAAmB,CACrB,IAAI,CAAC,WAAW,CAChB,MAAM,CACN,UAAU,CAAC,CAAC,UAAU,CAAC,eAAe,CAAC,CAEvC;QAAA,CAAC,IAAI,CACH;UAAA,CAAC,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,QAAQ,CAC9B;YAAA,CAAC,EAAE,CAAC,sBAAsB,CACxB,MAAM,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAC3B,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAG,CAAC,CAAC,EAE/D;UAAA,EAAE,EAAE,CAAC,cAAc,CACnB;UAAA,EAAE,cAAc,GAClB;QAAA,EAAE,IAAI,CACR;MAAA,EAAE,EAAE,CAAC,mBAAmB,CAC1B;IAAA,EAAE,EAAE,CAAC,UAAU,CAAC,CACjB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Type } from "@typespec/compiler";
|
|
2
|
+
export interface ModelsDirectoryProps {
|
|
3
|
+
namespace: string;
|
|
4
|
+
types: Type[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Generates the models directory with TypeSpec type declarations.
|
|
8
|
+
*/
|
|
9
|
+
export declare function ModelsDirectory(props: ModelsDirectoryProps): import("@alloy-js/core").Children;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { SourceDirectory } from "@alloy-js/core";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { TsTypes } from "./TsTypes.js";
|
|
5
|
+
/**
|
|
6
|
+
* Generates the models directory with TypeSpec type declarations.
|
|
7
|
+
*/
|
|
8
|
+
export function ModelsDirectory(props) {
|
|
9
|
+
return _$createComponent(SourceDirectory, {
|
|
10
|
+
path: "models",
|
|
11
|
+
get children() {
|
|
12
|
+
return _$createComponent(ts.SourceFile, {
|
|
13
|
+
get path() {
|
|
14
|
+
return `${props.namespace.toLowerCase()}.ts`;
|
|
15
|
+
},
|
|
16
|
+
get children() {
|
|
17
|
+
return _$createComponent(TsTypes, {
|
|
18
|
+
get types() {
|
|
19
|
+
return props.types;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=ModelsDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["SourceDirectory","ts","TsTypes","ModelsDirectory","props","_$createComponent","path","children","SourceFile","namespace","toLowerCase","types"],"sources":["../../../src/components/ModelsDirectory.tsx"],"sourcesContent":[null],"mappings":";AAAA,SAASA,eAAe,QAAQ,gBAAgB;AAChD,OAAO,KAAKC,EAAE,MAAM,sBAAsB;AAE1C,SAASC,OAAO,QAAQ,cAAc;AAOtC;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAACC,KAA2B,EAAE;EAC3D,OAAAC,iBAAA,CACGL,eAAe;IAACM,IAAI;IAAA,IAAAC,SAAA;MAAA,OAAAF,iBAAA,CAClBJ,EAAE,CAACO,UAAU;QAAA,IAACF,IAAIA,CAAA;UAAA,OAAE,GAAGF,KAAK,CAACK,SAAS,CAACC,WAAW,CAAC,CAAC,KAAK;QAAA;QAAA,IAAAH,SAAA;UAAA,OAAAF,iBAAA,CACvDH,OAAO;YAAA,IAACS,KAAKA,CAAA;cAAA,OAAEP,KAAK,CAACO,KAAK;YAAA;UAAA;QAAA;MAAA;IAAA;EAAA;AAInC","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SourceDirectory } from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { TsTypes } from "./TsTypes.js";
|
|
4
|
+
/**
|
|
5
|
+
* Generates the models directory with TypeSpec type declarations.
|
|
6
|
+
*/
|
|
7
|
+
export function ModelsDirectory(props) {
|
|
8
|
+
return (<SourceDirectory path="models">
|
|
9
|
+
<ts.SourceFile path={`${props.namespace.toLowerCase()}.ts`}>
|
|
10
|
+
<TsTypes types={props.types}/>
|
|
11
|
+
</ts.SourceFile>
|
|
12
|
+
</SourceDirectory>);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=ModelsDirectory.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModelsDirectory.jsx","sourceRoot":"","sources":["../../../src/components/ModelsDirectory.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAOvC;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,KAA2B;IACzD,OAAO,CACL,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAC5B;MAAA,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,KAAK,CAAC,CACzD;QAAA,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAC9B;MAAA,EAAE,EAAE,CAAC,UAAU,CACjB;IAAA,EAAE,eAAe,CAAC,CACnB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Refkey, type Children } from "@alloy-js/core";
|
|
2
|
+
import type { HttpOperation } from "@typespec/http";
|
|
3
|
+
export interface OperationInterfaceProps {
|
|
4
|
+
containerName: string;
|
|
5
|
+
operations: HttpOperation[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Creates a refkey for an operation interface.
|
|
9
|
+
* This allows other components to reference this interface and
|
|
10
|
+
* Alloy will automatically generate the import.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getOperationInterfaceRef(containerName: string): Refkey;
|
|
13
|
+
/**
|
|
14
|
+
* Generates a TypeScript interface for a group of HTTP operations.
|
|
15
|
+
* Following the http-server-js pattern, this creates an interface with methods
|
|
16
|
+
* for each operation that the user will implement.
|
|
17
|
+
*/
|
|
18
|
+
export declare function OperationInterface(props: OperationInterfaceProps): Children;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { For, List, refkey } from "@alloy-js/core";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { TypeExpression } from "@typespec/emitter-framework/typescript";
|
|
5
|
+
import { toCamelCase } from "../utils/http-helpers.js";
|
|
6
|
+
/**
|
|
7
|
+
* Creates a refkey for an operation interface.
|
|
8
|
+
* This allows other components to reference this interface and
|
|
9
|
+
* Alloy will automatically generate the import.
|
|
10
|
+
*/
|
|
11
|
+
export function getOperationInterfaceRef(containerName) {
|
|
12
|
+
return refkey(containerName, "operation-interface");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generates a TypeScript interface for a group of HTTP operations.
|
|
17
|
+
* Following the http-server-js pattern, this creates an interface with methods
|
|
18
|
+
* for each operation that the user will implement.
|
|
19
|
+
*/
|
|
20
|
+
export function OperationInterface(props) {
|
|
21
|
+
const {
|
|
22
|
+
containerName,
|
|
23
|
+
operations
|
|
24
|
+
} = props;
|
|
25
|
+
const interfaceRef = getOperationInterfaceRef(containerName);
|
|
26
|
+
return _$createComponent(ts.InterfaceDeclaration, {
|
|
27
|
+
name: containerName,
|
|
28
|
+
"export": true,
|
|
29
|
+
refkey: interfaceRef,
|
|
30
|
+
get children() {
|
|
31
|
+
return _$createComponent(List, {
|
|
32
|
+
get children() {
|
|
33
|
+
return _$createComponent(For, {
|
|
34
|
+
each: operations,
|
|
35
|
+
hardline: true,
|
|
36
|
+
semicolon: true,
|
|
37
|
+
children: function renderOperationMethod(operation) {
|
|
38
|
+
const opName = toCamelCase(operation.operation.name);
|
|
39
|
+
const parameters = [];
|
|
40
|
+
for (const param of operation.parameters.parameters) {
|
|
41
|
+
if (param.type === "path") {
|
|
42
|
+
const paramName = toCamelCase(param.param.name);
|
|
43
|
+
parameters.push({
|
|
44
|
+
name: paramName,
|
|
45
|
+
type: _$createComponent(TypeExpression, {
|
|
46
|
+
get type() {
|
|
47
|
+
return param.param.type;
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (operation.parameters.body) {
|
|
54
|
+
parameters.push({
|
|
55
|
+
name: "body",
|
|
56
|
+
type: _$createComponent(TypeExpression, {
|
|
57
|
+
get type() {
|
|
58
|
+
return operation.parameters.body.type;
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
for (const param of operation.parameters.parameters) {
|
|
64
|
+
if (param.type === "header") {
|
|
65
|
+
const paramName = toCamelCase(param.param.name);
|
|
66
|
+
parameters.push({
|
|
67
|
+
name: paramName,
|
|
68
|
+
type: _$createComponent(TypeExpression, {
|
|
69
|
+
get type() {
|
|
70
|
+
return param.param.type;
|
|
71
|
+
}
|
|
72
|
+
}),
|
|
73
|
+
optional: param.param.optional
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const queryParams = operation.parameters.parameters.filter(function isQuery(p) {
|
|
78
|
+
return p.type === "query";
|
|
79
|
+
});
|
|
80
|
+
if (queryParams.length > 0) {
|
|
81
|
+
const optionsType = _$createComponent(ts.InterfaceExpression, {
|
|
82
|
+
get children() {
|
|
83
|
+
return queryParams.map(function renderQueryParam(param) {
|
|
84
|
+
const paramName = toCamelCase(param.param.name);
|
|
85
|
+
return [_$createComponent(ts.InterfaceMember, {
|
|
86
|
+
name: paramName,
|
|
87
|
+
get optional() {
|
|
88
|
+
return param.param.optional;
|
|
89
|
+
},
|
|
90
|
+
get children() {
|
|
91
|
+
return _$createComponent(TypeExpression, {
|
|
92
|
+
get type() {
|
|
93
|
+
return param.param.type;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}), "; "];
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
parameters.push({
|
|
102
|
+
name: "options",
|
|
103
|
+
type: optionsType,
|
|
104
|
+
optional: true
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const returnType = ["Promise<", _$createComponent(TypeExpression, {
|
|
108
|
+
get type() {
|
|
109
|
+
return operation.operation.returnType;
|
|
110
|
+
}
|
|
111
|
+
}), ">"];
|
|
112
|
+
return _$createComponent(ts.InterfaceMethod, {
|
|
113
|
+
name: opName,
|
|
114
|
+
parameters: parameters,
|
|
115
|
+
returnType: returnType
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=OperationInterface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["For","List","refkey","ts","TypeExpression","toCamelCase","getOperationInterfaceRef","containerName","OperationInterface","props","operations","interfaceRef","_$createComponent","InterfaceDeclaration","name","children","each","hardline","semicolon","renderOperationMethod","operation","opName","parameters","param","type","paramName","push","body","optional","queryParams","filter","isQuery","p","length","optionsType","InterfaceExpression","map","renderQueryParam","InterfaceMember","returnType","InterfaceMethod"],"sources":["../../../src/components/OperationInterface.tsx"],"sourcesContent":[null],"mappings":";AAAA,SAASA,GAAG,EAAEC,IAAI,EAAEC,MAAM,QAAoC,gBAAgB;AAC9E,OAAO,KAAKC,EAAE,MAAM,sBAAsB;AAE1C,SAASC,cAAc,QAAQ,wCAAwC;AACvE,SAASC,WAAW,QAAQ,0BAA0B;AAOtD;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,wBAAwBA,CAACC,aAAqB,EAAU;EACtE,OAAOL,MAAM,CAACK,aAAa,EAAE,qBAAqB,CAAC;AACrD;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,kBAAkBA,CAACC,KAA8B,EAAE;EACjE,MAAM;IAAEF,aAAa;IAAEG;EAAW,CAAC,GAAGD,KAAK;EAC3C,MAAME,YAAY,GAAGL,wBAAwB,CAACC,aAAa,CAAC;EAE5D,OAAAK,iBAAA,CACGT,EAAE,CAACU,oBAAoB;IAACC,IAAI,EAAEP,aAAa;IAAA;IAASL,MAAM,EAAES,YAAY;IAAA,IAAAI,SAAA;MAAA,OAAAH,iBAAA,CACtEX,IAAI;QAAA,IAAAc,SAAA;UAAA,OAAAH,iBAAA,CACFZ,GAAG;YAACgB,IAAI,EAAEN,UAAU;YAAEO,QAAQ;YAACC,SAAS;YAAAH,QAAA,EACtC,SAASI,qBAAqBA,CAACC,SAAS,EAAE;cACzC,MAAMC,MAAM,GAAGhB,WAAW,CAACe,SAAS,CAACA,SAAS,CAACN,IAAI,CAAC;cACpD,MAAMQ,UAIH,GAAG,EAAE;cAER,KAAK,MAAMC,KAAK,IAAIH,SAAS,CAACE,UAAU,CAACA,UAAU,EAAE;gBACnD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@alloy-js/typescript";
|
|
3
|
+
import { TypeExpression } from "@typespec/emitter-framework/typescript";
|
|
4
|
+
import { toCamelCase } from "../utils/http-helpers.js";
|
|
5
|
+
/**
|
|
6
|
+
* Creates a refkey for an operation interface.
|
|
7
|
+
* This allows other components to reference this interface and
|
|
8
|
+
* Alloy will automatically generate the import.
|
|
9
|
+
*/
|
|
10
|
+
export function getOperationInterfaceRef(containerName) {
|
|
11
|
+
return refkey(containerName, "operation-interface");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generates a TypeScript interface for a group of HTTP operations.
|
|
15
|
+
* Following the http-server-js pattern, this creates an interface with methods
|
|
16
|
+
* for each operation that the user will implement.
|
|
17
|
+
*/
|
|
18
|
+
export function OperationInterface(props) {
|
|
19
|
+
const { containerName, operations } = props;
|
|
20
|
+
const interfaceRef = getOperationInterfaceRef(containerName);
|
|
21
|
+
return (<ts.InterfaceDeclaration name={containerName} export refkey={interfaceRef}>
|
|
22
|
+
<List>
|
|
23
|
+
<For each={operations} hardline semicolon>
|
|
24
|
+
{function renderOperationMethod(operation) {
|
|
25
|
+
const opName = toCamelCase(operation.operation.name);
|
|
26
|
+
const parameters = [];
|
|
27
|
+
for (const param of operation.parameters.parameters) {
|
|
28
|
+
if (param.type === "path") {
|
|
29
|
+
const paramName = toCamelCase(param.param.name);
|
|
30
|
+
parameters.push({
|
|
31
|
+
name: paramName,
|
|
32
|
+
type: <TypeExpression type={param.param.type}/>,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (operation.parameters.body) {
|
|
37
|
+
parameters.push({
|
|
38
|
+
name: "body",
|
|
39
|
+
type: <TypeExpression type={operation.parameters.body.type}/>,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
for (const param of operation.parameters.parameters) {
|
|
43
|
+
if (param.type === "header") {
|
|
44
|
+
const paramName = toCamelCase(param.param.name);
|
|
45
|
+
parameters.push({
|
|
46
|
+
name: paramName,
|
|
47
|
+
type: <TypeExpression type={param.param.type}/>,
|
|
48
|
+
optional: param.param.optional,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const queryParams = operation.parameters.parameters.filter(function isQuery(p) {
|
|
53
|
+
return p.type === "query";
|
|
54
|
+
});
|
|
55
|
+
if (queryParams.length > 0) {
|
|
56
|
+
const optionsType = (<ts.InterfaceExpression>
|
|
57
|
+
{queryParams.map(function renderQueryParam(param) {
|
|
58
|
+
const paramName = toCamelCase(param.param.name);
|
|
59
|
+
return (<>
|
|
60
|
+
<ts.InterfaceMember name={paramName} optional={param.param.optional}>
|
|
61
|
+
<TypeExpression type={param.param.type}/>
|
|
62
|
+
</ts.InterfaceMember>
|
|
63
|
+
{"; "}
|
|
64
|
+
</>);
|
|
65
|
+
})}
|
|
66
|
+
</ts.InterfaceExpression>);
|
|
67
|
+
parameters.push({
|
|
68
|
+
name: "options",
|
|
69
|
+
type: optionsType,
|
|
70
|
+
optional: true,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const returnType = (<>
|
|
74
|
+
Promise<
|
|
75
|
+
<TypeExpression type={operation.operation.returnType}/>
|
|
76
|
+
>
|
|
77
|
+
</>);
|
|
78
|
+
return (<ts.InterfaceMethod name={opName} parameters={parameters} returnType={returnType}/>);
|
|
79
|
+
}}
|
|
80
|
+
</For>
|
|
81
|
+
</List>
|
|
82
|
+
</ts.InterfaceDeclaration>);
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=OperationInterface.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationInterface.jsx","sourceRoot":"","sources":["../../../src/components/OperationInterface.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAA8B,MAAM,gBAAgB,CAAC;AAC/E,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3C,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC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AC,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,EACzC;wBAAA,EAAE,EAAE,CAAC,eAAe,CACpB;wBAAA,CAAC,IAAI,CACP;sBAAA,GAAG,CACJ,CAAC;oBACJ,CAAC,CAAC,CACJ;gBAAA,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAC1B,CAAC;gBACF,UAAU,CAAC,IAAI,CAAC;oBACd,IAAI,EAAE,SAAS;oBACf,IAAI,EAAE,WAAW;oBACjB,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;YACL,CAAC;YAED,MAAM,UAAU,GAAG,CACjB,EACE;;gBACA,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,UAAU,CAAC,EACrD;;cACF,GAAG,CACJ,CAAC;YAEF,OAAO,CACL,CAAC,EAAE,CAAC,eAAe,CACjB,IAAI,CAAC,CAAC,MAAM,CAAC,CACb,UAAU,CAAC,CAAC,UAAU,CAAC,CACvB,UAAU,CAAC,CAAC,UAAU,CAAC,EACvB,CACH,CAAC;QACJ,CAAC,CACH;QAAA,EAAE,GAAG,CACP;MAAA,EAAE,IAAI,CACR;IAAA,EAAE,EAAE,CAAC,oBAAoB,CAAC,CAC3B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HttpOperation } from "@typespec/http";
|
|
2
|
+
export interface OperationsDirectoryProps {
|
|
3
|
+
groupedOperations: Map<string, HttpOperation[]>;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Generates the operations directory with operation interface files.
|
|
7
|
+
*/
|
|
8
|
+
export declare function OperationsDirectory(props: OperationsDirectoryProps): import("@alloy-js/core").Children;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { For, SourceDirectory } from "@alloy-js/core";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { OperationInterface } from "./OperationInterface.js";
|
|
5
|
+
/**
|
|
6
|
+
* Generates the operations directory with operation interface files.
|
|
7
|
+
*/
|
|
8
|
+
export function OperationsDirectory(props) {
|
|
9
|
+
return _$createComponent(SourceDirectory, {
|
|
10
|
+
path: "operations",
|
|
11
|
+
get children() {
|
|
12
|
+
return _$createComponent(For, {
|
|
13
|
+
get each() {
|
|
14
|
+
return Array.from(props.groupedOperations.entries());
|
|
15
|
+
},
|
|
16
|
+
children: function renderOperationInterface([containerName, operations]) {
|
|
17
|
+
return _$createComponent(ts.SourceFile, {
|
|
18
|
+
get path() {
|
|
19
|
+
return `${containerName.toLowerCase()}.ts`;
|
|
20
|
+
},
|
|
21
|
+
get children() {
|
|
22
|
+
return _$createComponent(OperationInterface, {
|
|
23
|
+
containerName: containerName,
|
|
24
|
+
operations: operations
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=OperationsDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["For","SourceDirectory","ts","OperationInterface","OperationsDirectory","props","_$createComponent","path","children","each","Array","from","groupedOperations","entries","renderOperationInterface","containerName","operations","SourceFile","toLowerCase"],"sources":["../../../src/components/OperationsDirectory.tsx"],"sourcesContent":[null],"mappings":";AAAA,SAASA,GAAG,EAAEC,eAAe,QAAQ,gBAAgB;AACrD,OAAO,KAAKC,EAAE,MAAM,sBAAsB;AAE1C,SAASC,kBAAkB,QAAQ,yBAAyB;AAM5D;AACA;AACA;AACA,OAAO,SAASC,mBAAmBA,CAACC,KAA+B,EAAE;EACnE,OAAAC,iBAAA,CACGL,eAAe;IAACM,IAAI;IAAA,IAAAC,SAAA;MAAA,OAAAF,iBAAA,CAClBN,GAAG;QAAA,IAACS,IAAIA,CAAA;UAAA,OAAEC,KAAK,CAACC,IAAI,CAACN,KAAK,CAACO,iBAAiB,CAACC,OAAO,CAAC,CAAC,CAAC;QAAA;QAAAL,QAAA,EACrD,SAASM,wBAAwBA,CAAC,CAACC,aAAa,EAAEC,UAAU,CAAC,EAAE;UAC9D,OAAAV,iBAAA,CACGJ,EAAE,CAACe,UAAU;YAAA,IAACV,IAAIA,CAAA;cAAA,OAAE,GAAGQ,aAAa,CAACG,WAAW,CAAC,CAAC,KAAK;YAAA;YAAA,IAAAV,SAAA;cAAA,OAAAF,iBAAA,CACrDH,kBAAkB;gBACjBY,aAAa,EAAEA,aAAa;gBAC5BC,UAAU,EAAEA;cAAU;YAAA;UAAA;QAI9B;MAAC;IAAA;EAAA;AAIT","ignoreList":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { For, SourceDirectory } from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { OperationInterface } from "./OperationInterface.js";
|
|
4
|
+
/**
|
|
5
|
+
* Generates the operations directory with operation interface files.
|
|
6
|
+
*/
|
|
7
|
+
export function OperationsDirectory(props) {
|
|
8
|
+
return (<SourceDirectory path="operations">
|
|
9
|
+
<For each={Array.from(props.groupedOperations.entries())}>
|
|
10
|
+
{function renderOperationInterface([containerName, operations]) {
|
|
11
|
+
return (<ts.SourceFile path={`${containerName.toLowerCase()}.ts`}>
|
|
12
|
+
<OperationInterface containerName={containerName} operations={operations}/>
|
|
13
|
+
</ts.SourceFile>);
|
|
14
|
+
}}
|
|
15
|
+
</For>
|
|
16
|
+
</SourceDirectory>);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=OperationsDirectory.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationsDirectory.jsx","sourceRoot":"","sources":["../../../src/components/OperationsDirectory.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAM7D;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAA+B;IACjE,OAAO,CACL,CAAC,eAAe,CAAC,IAAI,CAAC,YAAY,CAChC;MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,OAAO,EAAE,CAAC,CAAC,CACvD;QAAA,CAAC,SAAS,wBAAwB,CAAC,CAAC,aAAa,EAAE,UAAU,CAAC;YAC5D,OAAO,CACL,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,aAAa,CAAC,WAAW,EAAE,KAAK,CAAC,CACvD;cAAA,CAAC,kBAAkB,CACjB,aAAa,CAAC,CAAC,aAAa,CAAC,CAC7B,UAAU,CAAC,CAAC,UAAU,CAAC,EAE3B;YAAA,EAAE,EAAE,CAAC,UAAU,CAAC,CACjB,CAAC;QACJ,CAAC,CACH;MAAA,EAAE,GAAG,CACP;IAAA,EAAE,eAAe,CAAC,CACnB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Refkey, type Children } from "@alloy-js/core";
|
|
2
|
+
import type { HttpOperation } from "@typespec/http";
|
|
3
|
+
export interface RouteRegistrationProps {
|
|
4
|
+
containerName: string;
|
|
5
|
+
operations: HttpOperation[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Creates a refkey for a route registration function.
|
|
9
|
+
* This allows the loadRoutes function to reference it with automatic imports.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getRouteRegistrationRef(containerName: string): Refkey;
|
|
12
|
+
/**
|
|
13
|
+
* Generates Fastify route registration functions.
|
|
14
|
+
* This creates a function that registers all routes for a resource.
|
|
15
|
+
*/
|
|
16
|
+
export declare function RouteRegistration(props: RouteRegistrationProps): Children;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { For, List, refkey } from "@alloy-js/core";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { toCamelCase, toPascalCase, getHttpVerb } from "../utils/http-helpers.js";
|
|
5
|
+
import { fastifyLib } from "../external-packages/fastify.js";
|
|
6
|
+
import { getOperationInterfaceRef } from "./OperationInterface.js";
|
|
7
|
+
/**
|
|
8
|
+
* Creates a refkey for a route registration function.
|
|
9
|
+
* This allows the loadRoutes function to reference it with automatic imports.
|
|
10
|
+
*/
|
|
11
|
+
export function getRouteRegistrationRef(containerName) {
|
|
12
|
+
return refkey(containerName, "route-registration");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generates Fastify route registration functions.
|
|
17
|
+
* This creates a function that registers all routes for a resource.
|
|
18
|
+
*/
|
|
19
|
+
export function RouteRegistration(props) {
|
|
20
|
+
const {
|
|
21
|
+
containerName,
|
|
22
|
+
operations
|
|
23
|
+
} = props;
|
|
24
|
+
const functionName = `register${toPascalCase(containerName)}Routes`;
|
|
25
|
+
const interfaceRef = getOperationInterfaceRef(containerName);
|
|
26
|
+
const routeRegRef = getRouteRegistrationRef(containerName);
|
|
27
|
+
return _$createComponent(ts.FunctionDeclaration, {
|
|
28
|
+
name: functionName,
|
|
29
|
+
"export": true,
|
|
30
|
+
refkey: routeRegRef,
|
|
31
|
+
async: true,
|
|
32
|
+
get parameters() {
|
|
33
|
+
return [{
|
|
34
|
+
name: "fastify",
|
|
35
|
+
type: fastifyLib.FastifyInstance
|
|
36
|
+
}, {
|
|
37
|
+
name: "operations",
|
|
38
|
+
type: interfaceRef
|
|
39
|
+
}];
|
|
40
|
+
},
|
|
41
|
+
returnType: "void",
|
|
42
|
+
get children() {
|
|
43
|
+
return _$createComponent(List, {
|
|
44
|
+
get children() {
|
|
45
|
+
return _$createComponent(For, {
|
|
46
|
+
each: operations,
|
|
47
|
+
hardline: true,
|
|
48
|
+
children: function renderRoute(operation) {
|
|
49
|
+
const verb = getHttpVerb(operation);
|
|
50
|
+
const path = operation.path;
|
|
51
|
+
const opName = toCamelCase(operation.operation.name);
|
|
52
|
+
return [_$createComponent(ts.FunctionCallExpression, {
|
|
53
|
+
get target() {
|
|
54
|
+
return ["fastify.", verb];
|
|
55
|
+
},
|
|
56
|
+
get args() {
|
|
57
|
+
return [["'", path, "'"], generateRouteHandler(operation, opName)];
|
|
58
|
+
}
|
|
59
|
+
}), ";"];
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function generateRouteHandler(operation, opName) {
|
|
68
|
+
const callArgs = [];
|
|
69
|
+
for (const param of operation.parameters.parameters) {
|
|
70
|
+
if (param.type === "path") {
|
|
71
|
+
const paramName = toCamelCase(param.param.name);
|
|
72
|
+
callArgs.push(["(request.params as any).", paramName]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (operation.parameters.body) {
|
|
76
|
+
callArgs.push(["request.body as any"]);
|
|
77
|
+
}
|
|
78
|
+
for (const param of operation.parameters.parameters) {
|
|
79
|
+
if (param.type === "header") {
|
|
80
|
+
const headerKey = param.name.toLowerCase();
|
|
81
|
+
const headerAccess = ["request.headers['", headerKey, "']"];
|
|
82
|
+
callArgs.push(["Array.isArray(", headerAccess, ") ? ", headerAccess, "[0] : ", headerAccess]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const queryParams = operation.parameters.parameters.filter(function isQuery(p) {
|
|
86
|
+
return p.type === "query";
|
|
87
|
+
});
|
|
88
|
+
if (queryParams.length > 0) {
|
|
89
|
+
const optionsObj = _$createComponent(ts.ObjectExpression, {
|
|
90
|
+
get children() {
|
|
91
|
+
return queryParams.map(function renderQueryProp(param, index) {
|
|
92
|
+
const paramName = toCamelCase(param.param.name);
|
|
93
|
+
return [index > 0 && ", ", paramName, ": (request.query as any).", paramName];
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
callArgs.push(optionsObj);
|
|
98
|
+
}
|
|
99
|
+
return _$createComponent(ts.FunctionExpression, {
|
|
100
|
+
async: true,
|
|
101
|
+
get parameters() {
|
|
102
|
+
return [{
|
|
103
|
+
name: "request",
|
|
104
|
+
type: fastifyLib.FastifyRequest
|
|
105
|
+
}, {
|
|
106
|
+
name: "reply",
|
|
107
|
+
type: fastifyLib.FastifyReply
|
|
108
|
+
}];
|
|
109
|
+
},
|
|
110
|
+
get children() {
|
|
111
|
+
return _$createComponent(List, {
|
|
112
|
+
get children() {
|
|
113
|
+
return [["try ", "{"], _$createComponent(List, {
|
|
114
|
+
get children() {
|
|
115
|
+
return [_$createComponent(ts.VarDeclaration, {
|
|
116
|
+
name: "result",
|
|
117
|
+
get children() {
|
|
118
|
+
return ["await", " ", _$createComponent(ts.FunctionCallExpression, {
|
|
119
|
+
get target() {
|
|
120
|
+
return ["operations.", opName];
|
|
121
|
+
},
|
|
122
|
+
args: callArgs
|
|
123
|
+
})];
|
|
124
|
+
}
|
|
125
|
+
}), [_$createComponent(ts.FunctionCallExpression, {
|
|
126
|
+
get target() {
|
|
127
|
+
return ["reply.code"];
|
|
128
|
+
},
|
|
129
|
+
get args() {
|
|
130
|
+
return [["200"]];
|
|
131
|
+
}
|
|
132
|
+
}), ".", _$createComponent(ts.FunctionCallExpression, {
|
|
133
|
+
get target() {
|
|
134
|
+
return ["send"];
|
|
135
|
+
},
|
|
136
|
+
get args() {
|
|
137
|
+
return [["result"]];
|
|
138
|
+
}
|
|
139
|
+
}), ";"]];
|
|
140
|
+
}
|
|
141
|
+
}), "}", " catch (error) ", "{", _$createComponent(List, {
|
|
142
|
+
get children() {
|
|
143
|
+
return [_$createComponent(ts.FunctionCallExpression, {
|
|
144
|
+
get target() {
|
|
145
|
+
return ["reply.code"];
|
|
146
|
+
},
|
|
147
|
+
get args() {
|
|
148
|
+
return [["500"]];
|
|
149
|
+
}
|
|
150
|
+
}), ".", _$createComponent(ts.FunctionCallExpression, {
|
|
151
|
+
get target() {
|
|
152
|
+
return ["send"];
|
|
153
|
+
},
|
|
154
|
+
get args() {
|
|
155
|
+
return [["{", " error: error instanceof Error ? error.message : 'Internal server error' ", "}"]];
|
|
156
|
+
}
|
|
157
|
+
}), ";"];
|
|
158
|
+
}
|
|
159
|
+
}), "}"];
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=RouteRegistration.js.map
|