typespec-hono 0.6.0 → 0.7.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.
Files changed (2) hide show
  1. package/dist/src/app.js +25 -5
  2. package/package.json +2 -2
package/dist/src/app.js CHANGED
@@ -248,7 +248,7 @@ basePaths = [],
248
248
  * audience is wider.
249
249
  */
250
250
  securityFor) {
251
- const entries = emitted.routes.flatMap((route) => {
251
+ const mounted = emitted.routes.flatMap((route) => {
252
252
  let dispatched;
253
253
  const names = emitted.schemaNames.get(route.operationId);
254
254
  // The library declares a `Responses` const for every operation, so a missing entry is a bug in
@@ -291,6 +291,7 @@ securityFor) {
291
291
  }
292
292
  return [{ route, names, validators, dispatched }];
293
293
  });
294
+ const entries = mounted;
294
295
  /**
295
296
  * **One registration per verb+path, not per operation.**
296
297
  *
@@ -703,6 +704,27 @@ securityFor) {
703
704
  * check cannot be wrong in the direction that breaks a build.
704
705
  */
705
706
  const dispatchesBody = entries.some((entry) => entry.dispatched !== undefined);
707
+ /**
708
+ * **Imported only where a route actually validates something, like every other value import here.**
709
+ *
710
+ * This one was unconditional while the three around it were not, and the comment above them stated
711
+ * the rule it was breaking. A service whose operations declare no parameters at all -- two bare
712
+ * `GET`s, which is where a health check starts and therefore where a new consumer starts -- emitted
713
+ * an import nothing used, and `tsp compile` reported success:
714
+ * `TS6133: 'zValidator' is declared but its value is never read`.
715
+ *
716
+ * Counted from the same filtered list the middleware is rendered from, so it cannot disagree with
717
+ * what was emitted: a dispatched body's validator is `byContentType`, not a `zValidator`.
718
+ */
719
+ /**
720
+ * **`z` is only ever reached through `z.infer`**, which appears where an operation has an input
721
+ * type or a response body. A service whose every operation takes nothing and returns `void` names
722
+ * neither, so the import was written and never used:
723
+ * `TS6133: 'z' is declared but its value is never read`. Same shape as the `zValidator` one above,
724
+ * one step further along.
725
+ */
726
+ const usesZod = entries.some((entry) => inputTypeOf(entry) !== undefined || entry.names.response !== undefined);
727
+ const validates = entries.some((entry) => entry.validators.filter(([target]) => entry.dispatched === undefined || target !== VALIDATOR_TARGET.body).length > 0);
706
728
  const runtimeModule = JSON.stringify(emitted.options.runtimeModule);
707
729
  /**
708
730
  * **One base sub-app, mounted with `app.route()`. Hono's own nesting, not a rewritten path on
@@ -714,10 +736,8 @@ securityFor) {
714
736
  const usesBasePath = basePaths.length > 0;
715
737
  const needsHonoValue = subApps.size > 0 || usesBasePath;
716
738
  return `${GENERATED_BANNER}
717
- import { zValidator } from "@hono/zod-validator";
718
- ${needsHonoValue ? 'import { Hono } from "hono";\nimport type { Context, Input } from "hono";' : 'import type { Context, Hono, Input } from "hono";'}
719
- import { z } from "zod";
720
- import type { AppEnv, Awaitable, Ctx, Result, RouteDeps } from ${runtimeModule};${negotiates ? `\nimport { selectContentType } from ${runtimeModule};` : ""}${guardsHead ? `\nimport { headOnly } from ${runtimeModule};` : ""}${dispatchesBody ? `\nimport { byContentType } from ${runtimeModule};` : ""}
739
+ ${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";'}
740
+ ${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};` : ""}
721
741
  ${imports}
722
742
  /**
723
743
  * One method per operation, each concretely typed from the schemas it validates against.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typespec-hono",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
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.6.0"
47
+ "typespec-http-zod": "^0.9.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@hono/zod-openapi": "^1.4.0",