@contractkit/plugin-typescript 0.34.1 → 0.36.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.
@@ -0,0 +1,8 @@
1
+ import type { ServerFramework } from './server-framework.js';
2
+ /**
3
+ * ServerKit on Fastify: `ServerKitRouter()` collects routes the way a Koa app reads and mounts them
4
+ * as a Fastify plugin, handlers take `(request, reply)` where the request *is* the ServerKit
5
+ * context, and a response is sent by returning `reply.send(...)` rather than by assignment.
6
+ */
7
+ export declare const FASTIFY_SERVER_FRAMEWORK: ServerFramework;
8
+ //# sourceMappingURL=server-framework-fastify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-framework-fastify.d.ts","sourceRoot":"","sources":["../src/server-framework-fastify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAc7D;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,EAAE,eA4GtC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { ServerFramework } from './server-framework.js';
2
+ /**
3
+ * ServerKit on Koa: the router is a `@koa/router` instance, handlers take a single `ctx`, and a
4
+ * response is written by assigning to `ctx.status` / `ctx.type` / `ctx.body` rather than returned.
5
+ */
6
+ export declare const KOA_SERVER_FRAMEWORK: ServerFramework;
7
+ //# sourceMappingURL=server-framework-koa.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-framework-koa.d.ts","sourceRoot":"","sources":["../src/server-framework-koa.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAW7D;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,eAkGlC,CAAC"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * HTTP frameworks the server sub-generator can target. Adding a name here without adding an adapter
3
+ * to {@link SERVER_FRAMEWORKS} fails to compile, which is the point of keeping the two in step.
4
+ */
5
+ export declare const SERVER_FRAMEWORK_NAMES: readonly ["koa", "fastify"];
6
+ /** One of {@link SERVER_FRAMEWORK_NAMES}. */
7
+ export type ServerFrameworkName = (typeof SERVER_FRAMEWORK_NAMES)[number];
8
+ /** The framework assumed when a config names none. */
9
+ export declare const DEFAULT_SERVER_FRAMEWORK_NAME: ServerFrameworkName;
10
+ /**
11
+ * Every framework-specific string the router and MCP router generators emit.
12
+ *
13
+ * Granularity is one statement (or one fragment) per method, so the shared codegen keeps ownership
14
+ * of control flow — which branches exist, what order they run in, and which values reach them — and
15
+ * an adapter only decides how a given step is spelled. Anything an adapter cannot express as a
16
+ * statement, such as ending a response, is returned as a list of lines so it can also be empty.
17
+ */
18
+ export interface ServerFramework {
19
+ readonly name: ServerFrameworkName;
20
+ /**
21
+ * Import lines for the framework runtime, already filtered down to what the generated body uses.
22
+ *
23
+ * The adapter applies `uses` itself rather than declaring a symbol list, because a framework may
24
+ * need more than one import line, and because only names the adapter chooses ever go through the
25
+ * word-boundary probe — a handler-local identifier can never be mistaken for an import.
26
+ */
27
+ imports(uses: (symbol: string) => boolean): string[];
28
+ /** The module-level router value every handler attaches to. */
29
+ routerDeclaration(routerName: string): string;
30
+ /** Placeholder syntax for one path parameter, given a name already mapped to a valid identifier. */
31
+ pathParam(identifier: string): string;
32
+ /**
33
+ * Identifiers the handler signature itself binds — `ctx`, or `request` and `reply`. A path
34
+ * parameter is destructured into the handler body, so one declared with the same name would
35
+ * shadow the handler's own parameter: a redeclaration under `tsc`, and a temporal-dead-zone
36
+ * `ReferenceError` at runtime. Codegen renames the local binding to avoid these.
37
+ */
38
+ readonly handlerLocals: readonly string[];
39
+ /** Opening line of a handler, including its middleware and the handler function's parameters. */
40
+ routeOpen(routerName: string, method: string, path: string, middlewares: readonly string[]): string;
41
+ /** Lines that close a handler opened by {@link routeOpen}. */
42
+ routeClose(): string[];
43
+ /** Route middleware factory calls, rendered as expressions for {@link routeOpen}. */
44
+ readonly middleware: {
45
+ policy(args: string): string;
46
+ bodyParser(tokensExpr: string): string;
47
+ signature(args: string): string;
48
+ };
49
+ /** Expressions a handler reads the request through. */
50
+ readonly request: {
51
+ params: string;
52
+ query: string;
53
+ headers: string;
54
+ /** The body already parsed by the body-parser middleware. */
55
+ parsedBody: string;
56
+ /**
57
+ * The request's media type with any parameters stripped. It is matched against declared MIME
58
+ * literals, so an adapter whose framework exposes only the raw header must normalise it here
59
+ * — a `; charset=utf-8` left on the end matches nothing.
60
+ */
61
+ contentType: string;
62
+ };
63
+ /** Expression resolving a service class out of the request-scoped DI container. */
64
+ resolveService(className: string): string;
65
+ /** Statements a handler writes the response with. */
66
+ readonly response: {
67
+ status(expr: string): string;
68
+ /**
69
+ * One statement setting a response header. It is emitted bare or behind an `if` guard for an
70
+ * optional header, so it must stay a single statement.
71
+ */
72
+ header(name: string, valueExpr: string): string;
73
+ type(expr: string): string;
74
+ /**
75
+ * The terminal write for a response, or for one without a body when `bodyExpr` is undefined.
76
+ * A framework that ends a response by returning needs a statement in both cases; Koa, which
77
+ * ends it by assignment, emits nothing for a bodyless one.
78
+ */
79
+ send(bodyExpr: string | undefined): string[];
80
+ /** What closes one `case` of the multi-status switch, after that status has been written. */
81
+ caseEnd(): string[];
82
+ };
83
+ /** The whole `mcp.router.ts` file, which is boilerplate rather than a per-operation render. */
84
+ mcpRouter(options: {
85
+ path: string;
86
+ }): string;
87
+ }
88
+ /**
89
+ * Every supported framework, keyed by name. The annotation is what ties this to
90
+ * {@link SERVER_FRAMEWORK_NAMES}: adding a name without an adapter is a compile error.
91
+ */
92
+ export declare const SERVER_FRAMEWORKS: Readonly<Record<ServerFrameworkName, ServerFramework>>;
93
+ /**
94
+ * Resolve a configured framework name to its adapter.
95
+ *
96
+ * @param name The `server.framework` value, or undefined for {@link DEFAULT_SERVER_FRAMEWORK_NAME}.
97
+ * @throws When `name` is not a supported framework. Config arrives as JSON, so this is a runtime
98
+ * check and not something the `ServerFrameworkName` type can enforce on its own.
99
+ */
100
+ export declare function resolveServerFramework(name: string | undefined): ServerFramework;
101
+ //# sourceMappingURL=server-framework.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-framework.d.ts","sourceRoot":"","sources":["../src/server-framework.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,eAAO,MAAM,sBAAsB,6BAA8B,CAAC;AAElE,6CAA6C;AAC7C,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E,sDAAsD;AACtD,eAAO,MAAM,6BAA6B,EAAE,mBAA2B,CAAC;AAExE;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IAEnC;;;;;;OAMG;IACH,OAAO,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM,EAAE,CAAC;IAErD,+DAA+D;IAC/D,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IAE9C,oGAAoG;IACpG,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IAEtC;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;IAE1C,iGAAiG;IACjG,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;IAEpG,8DAA8D;IAC9D,UAAU,IAAI,MAAM,EAAE,CAAC;IAEvB,qFAAqF;IACrF,QAAQ,CAAC,UAAU,EAAE;QACjB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;QAC7B,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;QACvC,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;KACnC,CAAC;IAEF,uDAAuD;IACvD,QAAQ,CAAC,OAAO,EAAE;QACd,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,6DAA6D;QAC7D,UAAU,EAAE,MAAM,CAAC;QACnB;;;;WAIG;QACH,WAAW,EAAE,MAAM,CAAC;KACvB,CAAC;IAEF,mFAAmF;IACnF,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IAE1C,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE;QACf,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;QAC7B;;;WAGG;QACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;QAChD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;QAC3B;;;;WAIG;QACH,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,EAAE,CAAC;QAC7C,6FAA6F;QAC7F,OAAO,IAAI,MAAM,EAAE,CAAC;KACvB,CAAC;IAEF,+FAA+F;IAC/F,SAAS,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC;CAChD;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAGpF,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,eAAe,CAShF"}
package/llms.txt CHANGED
@@ -56,7 +56,7 @@ Path templates accept `{filename}`, `{dir}`, `{area}`, and — in `sdk.output.sd
56
56
 
57
57
  | Key | Emits |
58
58
  | --- | --- |
59
- | `server` | Koa routers from `operation` declarations, plus the type or Zod files they import |
59
+ | `server` | Server routers from `operation` declarations, plus the type or Zod files they import. `framework` selects the HTTP framework: `koa` or `fastify` |
60
60
  | `sdk` | The SDK class, per-area operation clients, and their types |
61
61
  | `zod` | Standalone Zod schemas, independent of `server` and `sdk` |
62
62
  | `types` | Standalone plain TypeScript types, independent of `server` and `sdk` |
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contractkit/plugin-typescript",
3
- "version": "0.34.1",
4
- "description": "ContractKit built-in plugin: TypeScript codegen (SDK clients, Koa routers, Zod schemas, plain types)",
3
+ "version": "0.36.0",
4
+ "description": "ContractKit built-in plugin: TypeScript codegen (SDK clients, Koa or Fastify routers, Zod schemas, plain types)",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Marooned Software",
@@ -5,6 +5,8 @@ import { inferService, deriveModulePath, buildArgs, deriveBaseName } from './cod
5
5
  import { quoteKey, escapeSingleQuoted, sourceLink } from './ts-render.js';
6
6
  import { DECIMAL_IMPORT, DECIMAL_PRELUDE_LINES } from './decimal-runtime.js';
7
7
  import { basename, dirname, relative } from 'node:path';
8
+ import type { ServerFramework } from './server-framework.js';
9
+ import { KOA_SERVER_FRAMEWORK } from './server-framework-koa.js';
8
10
 
9
11
  // ─── Options ────────────────────────────────────────────────────────────────
10
12
 
@@ -498,29 +500,13 @@ export function generateMcpAggregator(entries: McpAggregatorEntry[]): string {
498
500
  return lines.join('\n') + '\n';
499
501
  }
500
502
 
501
- /** Generate the optional `mcp.router.ts` — the standard ServerKit route wiring for the dispatcher. */
502
- export function generateMcpRouter(options: { path?: string } = {}): string {
503
- const path = options.path ?? '/mcp';
504
- return `import { ServerKitRouter, bodyParserMiddleware, requireSignature } from '@maroonedsoftware/koa';
505
- import { McpDispatcher, createMcpRequestContext, MCP_AUTH_POLICY } from '@maroonedsoftware/mcp';
506
-
507
- /** Mount the MCP endpoint onto a ServerKit router. Bind \`registerMcpTools\` to the \`McpToolHandlerMap\` token. */
508
- export function mountMcp(router: ReturnType<typeof ServerKitRouter>): void {
509
- router.post('${path}', bodyParserMiddleware(['json']), requireSignature('mcp', { policy: MCP_AUTH_POLICY }), async (ctx) => {
510
- const dispatcher = ctx.container.get(McpDispatcher);
511
- const context = createMcpRequestContext({ requestId: ctx.requestId, logger: ctx.logger });
512
- if (dispatcher.sessionMode === 'stateful') {
513
- ctx.respond = false;
514
- await dispatcher.dispatchStateful(
515
- { req: ctx.req, res: ctx.res, body: ctx.parsedBody, sessionId: ctx.get('mcp-session-id') },
516
- context,
517
- );
518
- } else {
519
- const response = await dispatcher.dispatch(JSON.parse(String(ctx.rawBody)), context);
520
- if (response) ctx.body = response;
521
- else ctx.status = 202; // a notification — nothing to return
522
- }
523
- });
524
- }
525
- `;
503
+ /**
504
+ * Generate the optional `mcp.router.ts` the standard ServerKit route wiring for the dispatcher.
505
+ *
506
+ * The whole file is framework-specific boilerplate rather than a per-operation render, so the
507
+ * adapter owns the template. Defaults to Koa, matching the router generator.
508
+ */
509
+ export function generateMcpRouter(options: { path?: string; framework?: ServerFramework } = {}): string {
510
+ const framework = options.framework ?? KOA_SERVER_FRAMEWORK;
511
+ return framework.mcpRouter({ path: options.path ?? '/mcp' });
526
512
  }