typespec-hono 0.9.1 → 0.10.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/dist/src/app.d.ts +9 -0
- package/dist/src/app.js +15 -5
- package/dist/src/emitter.js +22 -3
- package/dist/src/lib.d.ts +11 -1
- package/dist/src/lib.js +19 -1
- package/package.json +2 -2
package/dist/src/app.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { type SecurityRequirement } from "./security.js";
|
|
2
2
|
import { type EmittedRoute, type EmittedService } from "typespec-http-zod";
|
|
3
|
+
/**
|
|
4
|
+
* The header every emitted file carries.
|
|
5
|
+
*
|
|
6
|
+
* **`DO NOT EDIT` says what not to do and not what to do instead.** Only the project knows the
|
|
7
|
+
* command that regenerates, so `regenerate-hint` supplies it and the generic line stands otherwise.
|
|
8
|
+
* The library honours the same option for the files it writes; a hint reaching three files out of
|
|
9
|
+
* five would be worse than none, because the two it missed are the two a reader opens first.
|
|
10
|
+
*/
|
|
11
|
+
export declare function generatedBanner(hint: string | undefined): string;
|
|
3
12
|
/**
|
|
4
13
|
* TypeSpec publishes `/widgets/{widget-id}`; Hono routes on `/widgets/:widget-id`.
|
|
5
14
|
*
|
package/dist/src/app.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import { renderSecurity } from "./security.js";
|
|
2
2
|
import { isRawBinaryMediaType, objectKey, } from "typespec-http-zod";
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The header every emitted file carries.
|
|
5
|
+
*
|
|
6
|
+
* **`DO NOT EDIT` says what not to do and not what to do instead.** Only the project knows the
|
|
7
|
+
* command that regenerates, so `regenerate-hint` supplies it and the generic line stands otherwise.
|
|
8
|
+
* The library honours the same option for the files it writes; a hint reaching three files out of
|
|
9
|
+
* five would be worse than none, because the two it missed are the two a reader opens first.
|
|
10
|
+
*/
|
|
11
|
+
export function generatedBanner(hint) {
|
|
12
|
+
const second = hint === undefined
|
|
13
|
+
? "// Recompile the spec that produced it; edits here are overwritten on the next run."
|
|
14
|
+
: `// Regenerate with: ${hint}`;
|
|
15
|
+
return `// GENERATED by typespec-hono from the TypeSpec service definition. DO NOT EDIT.\n${second}\n`;
|
|
16
|
+
}
|
|
7
17
|
/** A parameter name Hono can carry verbatim. Measured against Hono, not assumed. */
|
|
8
18
|
const PLAIN_PATH_PARAMETER = /^[A-Za-z0-9_.~-]+$/;
|
|
9
19
|
/**
|
|
@@ -755,7 +765,7 @@ securityFor) {
|
|
|
755
765
|
*/
|
|
756
766
|
const usesBasePath = basePaths.length > 0;
|
|
757
767
|
const needsHonoValue = subApps.size > 0 || usesBasePath;
|
|
758
|
-
return `${
|
|
768
|
+
return `${generatedBanner(emitted.options.regenerateHint)}
|
|
759
769
|
${validates ? 'import { zValidator } from "@hono/zod-validator";\n' : ""}${needsHonoValue ? 'import { Hono } from "hono";\nimport type { Context, Input } from "hono";' : 'import type { Context, Hono, Input } from "hono";'}
|
|
760
770
|
${usesZod ? 'import { z } from "zod";\n' : ""}import type { AppEnv, Awaitable, Ctx, Result, RouteDeps } from ${runtimeModule};${negotiates ? `\nimport { selectContentType } from ${runtimeModule};` : ""}${guardsHead ? `\nimport { headOnly } from ${runtimeModule};` : ""}${dispatchesBody ? `\nimport { byContentType } from ${runtimeModule};` : ""}
|
|
761
771
|
${imports}
|
package/dist/src/emitter.js
CHANGED
|
@@ -62,10 +62,15 @@ function targetFor(emitted, verb, path) {
|
|
|
62
62
|
* `test/adopter.test.ts` is the arm that now opens that branch.
|
|
63
63
|
*/
|
|
64
64
|
/** The header the emitted runtime carries, matching every other generated file. */
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
function generatedRuntimeBanner(hint) {
|
|
66
|
+
const second = hint === undefined
|
|
67
|
+
? "// A copy of this package's runtime, emitted so the generated code imports no package at run time."
|
|
68
|
+
: `// Regenerate with: ${hint}`;
|
|
69
|
+
return `// GENERATED by typespec-hono. DO NOT EDIT.
|
|
70
|
+
${second}
|
|
67
71
|
// Point \`runtime-module\` at a module of your own to replace it.
|
|
68
72
|
`;
|
|
73
|
+
}
|
|
69
74
|
/**
|
|
70
75
|
* The generated runtime lands beside the generated code, so nothing the emitter writes imports this
|
|
71
76
|
* package at run time.
|
|
@@ -93,7 +98,7 @@ export async function $onEmit(context) {
|
|
|
93
98
|
if (emitted.options.runtimeModule === DEFAULT_RUNTIME_MODULE) {
|
|
94
99
|
await emitFile(context.program, {
|
|
95
100
|
path: resolvePath(emitted.outputDir, "runtime.gen.ts"),
|
|
96
|
-
content: `${
|
|
101
|
+
content: `${generatedRuntimeBanner(emitted.options.regenerateHint)}${readFileSync(RUNTIME_SOURCE, "utf8")}`,
|
|
97
102
|
});
|
|
98
103
|
}
|
|
99
104
|
/**
|
|
@@ -101,6 +106,20 @@ export async function $onEmit(context) {
|
|
|
101
106
|
* its server, so `@server("/api/v1")` plus `/accounts` publishes `/api/v1/accounts`. Mounting at
|
|
102
107
|
* the root made every client generated from the document 404.
|
|
103
108
|
*/
|
|
109
|
+
/**
|
|
110
|
+
* **A service this project does not serve still belongs in the program.**
|
|
111
|
+
*
|
|
112
|
+
* One compile is what makes a shared vocabulary shared, so a surface not yet served is in the
|
|
113
|
+
* spec for its types and its validators. Its server is not wanted, and without a way to say so
|
|
114
|
+
* the emitter wrote an `app.gen.ts` the project had to keep honest with probe tests and never
|
|
115
|
+
* mounted.
|
|
116
|
+
*
|
|
117
|
+
* Only this file is withheld. Everything the library emits for that service is untouched,
|
|
118
|
+
* because those are the reason it is in the program at all.
|
|
119
|
+
*/
|
|
120
|
+
const serviceName = emitted.service.namespace.name;
|
|
121
|
+
if (context.options.services?.[serviceName]?.["emit-server"] === false)
|
|
122
|
+
continue;
|
|
104
123
|
const base = resolveBasePath(context.program, emitted.service.namespace);
|
|
105
124
|
await emitFile(context.program, {
|
|
106
125
|
path: resolvePath(emitted.outputDir, "app.gen.ts"),
|
package/dist/src/lib.d.ts
CHANGED
|
@@ -18,7 +18,17 @@ import { type EmitterOptions as HttpZodOptions } from "typespec-http-zod";
|
|
|
18
18
|
* because it carries anything today. The moment a Hono-only option appears it goes here, and the
|
|
19
19
|
* derivation keeps the rest honest.
|
|
20
20
|
*/
|
|
21
|
-
export type EmitterOptions = HttpZodOptions
|
|
21
|
+
export type EmitterOptions = HttpZodOptions & {
|
|
22
|
+
/**
|
|
23
|
+
* Per-service overrides this emitter adds on top of the library's.
|
|
24
|
+
*
|
|
25
|
+
* **The library's own `services` map is preserved**, because the type is an intersection: an
|
|
26
|
+
* option added there still arrives here, and this only widens what each entry may carry.
|
|
27
|
+
*/
|
|
28
|
+
services?: Record<string, {
|
|
29
|
+
"emit-server"?: boolean;
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
22
32
|
/**
|
|
23
33
|
* **A spread of the published schema, so a new key arrives for free.**
|
|
24
34
|
*
|
package/dist/src/lib.js
CHANGED
|
@@ -9,7 +9,25 @@ import { EmitterOptionsSchema as httpZodOptions, } from "typespec-http-zod";
|
|
|
9
9
|
*/
|
|
10
10
|
const EmitterOptionsSchema = {
|
|
11
11
|
...httpZodOptions,
|
|
12
|
-
properties: {
|
|
12
|
+
properties: {
|
|
13
|
+
...httpZodOptions.properties,
|
|
14
|
+
/**
|
|
15
|
+
* **Spread from the library's own entry rather than restated**, so a per-service option added
|
|
16
|
+
* there still validates here. Only `emit-server` is added.
|
|
17
|
+
*/
|
|
18
|
+
services: {
|
|
19
|
+
...httpZodOptions.properties.services,
|
|
20
|
+
additionalProperties: {
|
|
21
|
+
type: "object",
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
properties: {
|
|
24
|
+
...httpZodOptions.properties.services.additionalProperties.properties,
|
|
25
|
+
"emit-server": { type: "boolean", nullable: true },
|
|
26
|
+
},
|
|
27
|
+
required: [],
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
13
31
|
};
|
|
14
32
|
/**
|
|
15
33
|
* What this emitter refuses that the library does not.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typespec-hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "TypeSpec emitter: generate a Hono server, and the Zod validators it enforces, from an HTTP service definition, agreeing with the OpenAPI document @typespec/openapi3 publishes from the same source.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare-workers",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"provenance": true
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"typespec-http-zod": "^0.
|
|
47
|
+
"typespec-http-zod": "^0.12.1"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@hono/zod-openapi": "^1.4.0",
|