create-backon 0.6.1 → 0.9.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/README.md +102 -12
- package/dist/cli.js +1240 -0
- package/package.json +10 -6
- package/src/add-method.ts +101 -0
- package/src/add-route.ts +11 -9
- package/src/cli.ts +145 -3
- package/src/colors.ts +25 -0
- package/src/generate.ts +17 -6
- package/src/prompts.ts +26 -0
- package/src/route-methods.ts +76 -0
- package/src/templates/base.ts +103 -7
- package/src/templates/route.ts +143 -64
package/src/templates/base.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Answers, Framework, Language } from "../prompts";
|
|
1
|
+
import type { Answers, DeployTarget, Framework, Language } from "../prompts";
|
|
2
2
|
import { camelCase } from "./route";
|
|
3
3
|
import { dbDependencies, dbEnvVars, dbReadmeNote } from "./db";
|
|
4
4
|
|
|
@@ -12,16 +12,44 @@ import { dbDependencies, dbEnvVars, dbReadmeNote } from "./db";
|
|
|
12
12
|
|
|
13
13
|
export function renderIndexTs(framework: Framework): string {
|
|
14
14
|
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
15
|
+
// The Hono adapter's serve() is async as of @mednours/backon 0.7.0 (it
|
|
16
|
+
// dynamically imports Bun.serve()/@hono/node-server depending which
|
|
17
|
+
// runtime it's actually on) — top-level await works fine here since
|
|
18
|
+
// generated projects are ESM ("type": "module" in their package.json).
|
|
19
|
+
// The Express adapter's serve() stays synchronous (always app.listen()
|
|
20
|
+
// under the hood, no runtime branching to speak of), so it's left
|
|
21
|
+
// un-awaited rather than implying an async contract that isn't real.
|
|
22
|
+
const call = framework === "express" ? "serve(app);" : "await serve(app);";
|
|
15
23
|
return `import { serve } from "${backonImport}";
|
|
16
24
|
import { app } from "./src/app";
|
|
17
25
|
|
|
18
|
-
|
|
26
|
+
${call}
|
|
19
27
|
`;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
// serveApiDocs() is called last, after every route is mounted — it reads
|
|
31
|
+
// the route table as it stands at call time, so anything registered after
|
|
32
|
+
// this line would be missing from /docs. Same live route this project's
|
|
33
|
+
// bun run docs script generates a static copy of; see generate-docs.<ext>.
|
|
34
|
+
//
|
|
35
|
+
// The default export (Vercel target only) is additive, not a replacement
|
|
36
|
+
// for index.ts's serve(app) — Vercel's zero-config Hono/Express support
|
|
37
|
+
// imports this file directly and calls the exported app's own request
|
|
38
|
+
// handler, bypassing serve()/index.ts entirely, while index.ts (and its
|
|
39
|
+
// serve() call) keeps working exactly as before for `bun run dev`/`start`
|
|
40
|
+
// anywhere else. src/app.<ext> is already one of Vercel's recognized entry
|
|
41
|
+
// paths — this export is the only thing it was missing.
|
|
42
|
+
export function renderAppTs(projectName: string, framework: Framework, deployTarget?: DeployTarget): string {
|
|
43
|
+
const defaultExport =
|
|
44
|
+
deployTarget === "vercel"
|
|
45
|
+
? `
|
|
46
|
+
// Vercel deploys straight from this export — see vercel.json.
|
|
47
|
+
export default app;
|
|
48
|
+
`
|
|
49
|
+
: "";
|
|
50
|
+
|
|
23
51
|
if (framework === "express") {
|
|
24
|
-
return `import { createApp } from "@mednours/backon/express";
|
|
52
|
+
return `import { createApp, serveApiDocs } from "@mednours/backon/express";
|
|
25
53
|
import { endpoints } from "./endpoints";
|
|
26
54
|
import { exampleMiddleware } from "./middleware/example.middleware";
|
|
27
55
|
|
|
@@ -29,9 +57,11 @@ export const app = createApp();
|
|
|
29
57
|
|
|
30
58
|
app.use(exampleMiddleware);
|
|
31
59
|
app.use("/", endpoints);
|
|
32
|
-
|
|
60
|
+
|
|
61
|
+
serveApiDocs(app, "/docs", { title: "${projectName}" });
|
|
62
|
+
${defaultExport}`;
|
|
33
63
|
}
|
|
34
|
-
return `import { createApp } from "@mednours/backon";
|
|
64
|
+
return `import { createApp, serveApiDocs } from "@mednours/backon";
|
|
35
65
|
import { endpoints } from "./endpoints";
|
|
36
66
|
import { exampleMiddleware } from "./middleware/example.middleware";
|
|
37
67
|
|
|
@@ -39,6 +69,23 @@ export const app = createApp();
|
|
|
39
69
|
|
|
40
70
|
app.use("*", exampleMiddleware);
|
|
41
71
|
app.route("/", endpoints);
|
|
72
|
+
|
|
73
|
+
serveApiDocs(app, "/docs", { title: "${projectName}" });
|
|
74
|
+
${defaultExport}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Vercel target only. bunVersion pins the Bun *major* version Vercel runs
|
|
78
|
+
// the deployed function on (currently "1.x" is the only valid value; minor/
|
|
79
|
+
// patch are managed by Vercel automatically) — kept in sync with this
|
|
80
|
+
// being a Bun-first framework, so the deployed function behaves the same
|
|
81
|
+
// runtime as local dev. Not required for the zero-config Hono/Express
|
|
82
|
+
// detection itself (that works on Vercel's default Node runtime too), but
|
|
83
|
+
// there's no reason to switch runtimes between local and deployed.
|
|
84
|
+
export function renderVercelJson(): string {
|
|
85
|
+
return `{
|
|
86
|
+
"$schema": "https://openapi.vercel.sh/vercel.json",
|
|
87
|
+
"bunVersion": "1.x"
|
|
88
|
+
}
|
|
42
89
|
`;
|
|
43
90
|
}
|
|
44
91
|
|
|
@@ -276,6 +323,7 @@ export function renderPackageJson(
|
|
|
276
323
|
// arguments (like the route name) straight through without needing
|
|
277
324
|
// a `--` separator.
|
|
278
325
|
"add-route": "bunx create-backon add-route",
|
|
326
|
+
"add-method": "bunx create-backon add-method",
|
|
279
327
|
"remove-route": "bunx create-backon remove-route",
|
|
280
328
|
"list-routes": "bunx create-backon list-routes",
|
|
281
329
|
doctor: "bunx create-backon doctor",
|
|
@@ -298,6 +346,48 @@ export function renderPackageJson(
|
|
|
298
346
|
return `${JSON.stringify(pkg, null, 2)}\n`;
|
|
299
347
|
}
|
|
300
348
|
|
|
349
|
+
function renderDeploySection(deployTarget: DeployTarget | undefined, ext: string): string {
|
|
350
|
+
switch (deployTarget) {
|
|
351
|
+
case "vercel":
|
|
352
|
+
return `## Deploying
|
|
353
|
+
|
|
354
|
+
Set up for [Vercel](https://vercel.com)'s zero-config Hono/Express support:
|
|
355
|
+
\`src/app.${ext}\` has a default export (Vercel deploys straight from it,
|
|
356
|
+
bypassing \`index.${ext}\`/\`serve()\` — those keep working locally exactly as
|
|
357
|
+
before), and \`vercel.json\` pins the Bun runtime so the deployed function
|
|
358
|
+
matches local dev. Connect the repo on [vercel.com/new](https://vercel.com/new)
|
|
359
|
+
or run \`vercel deploy\` — no other configuration needed. \`vercel dev\` runs it
|
|
360
|
+
locally the same way it'll run once deployed.
|
|
361
|
+
`;
|
|
362
|
+
case "node":
|
|
363
|
+
return `## Deploying
|
|
364
|
+
|
|
365
|
+
No deploy-specific files here — \`index.${ext}\` calls \`serve(app)\`, which
|
|
366
|
+
listens on \`PORT\` (see \`.env.example\`) wherever it's run. That's what
|
|
367
|
+
Render, Railway, Fly, a plain VPS, or any other Node/Bun host expects: point
|
|
368
|
+
the platform's start command at \`bun run start\` (or \`bun run index.${ext}\`)
|
|
369
|
+
and set \`PORT\`/your database env vars in its dashboard.
|
|
370
|
+
`;
|
|
371
|
+
case "local":
|
|
372
|
+
return `## Deploying
|
|
373
|
+
|
|
374
|
+
Not set up for a specific host — \`bun run start\` runs \`index.${ext}\`, which
|
|
375
|
+
listens on \`PORT\` (see \`.env.example\`). Point any Node/Bun-capable host's
|
|
376
|
+
start command at that when you're ready.
|
|
377
|
+
`;
|
|
378
|
+
default:
|
|
379
|
+
return `## Deploying
|
|
380
|
+
|
|
381
|
+
Not decided yet — \`index.${ext}\` already calls \`serve(app)\`, listening on
|
|
382
|
+
\`PORT\` (see \`.env.example\`), which is all most Node/Bun hosts need. For
|
|
383
|
+
[Vercel](https://vercel.com) specifically, add a default export to
|
|
384
|
+
\`src/app.${ext}\` (\`export default app;\`) and a \`vercel.json\` with
|
|
385
|
+
\`{ "bunVersion": "1.x" }\` — or re-run \`create-backon\` and pick Vercel to get
|
|
386
|
+
both for free.
|
|
387
|
+
`;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
301
391
|
export function renderReadme(answers: Answers): string {
|
|
302
392
|
const routeList = answers.routes.length
|
|
303
393
|
? answers.routes.map((route) => `- \`src/endpoints/${route}/\``).join("\n")
|
|
@@ -305,6 +395,7 @@ export function renderReadme(answers: Answers): string {
|
|
|
305
395
|
|
|
306
396
|
const backonImport = answers.framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
307
397
|
const languageLabel = answers.language === "javascript" ? "JavaScript" : "TypeScript";
|
|
398
|
+
const ext = answers.language === "javascript" ? "js" : "ts";
|
|
308
399
|
|
|
309
400
|
return `# ${answers.projectName}
|
|
310
401
|
|
|
@@ -318,6 +409,10 @@ cp .env.example .env # fill in real values
|
|
|
318
409
|
bun run dev
|
|
319
410
|
\`\`\`
|
|
320
411
|
|
|
412
|
+
Once running, \`http://localhost:3000/docs\` has a live API reference for
|
|
413
|
+
every route added via \`defineRoute\` — \`bun run docs\` also writes the same
|
|
414
|
+
document to \`openapi.json\`/\`docs.html\` on disk, for CI or static hosting.
|
|
415
|
+
|
|
321
416
|
## Database: ${answers.db}
|
|
322
417
|
|
|
323
418
|
${dbReadmeNote(answers.db)}
|
|
@@ -330,5 +425,6 @@ Each route folder follows the same shape: \`*.routes.${answers.language === "jav
|
|
|
330
425
|
methods to controller functions, \`*.controller.${answers.language === "javascript" ? "js" : "ts"}\` validates the
|
|
331
426
|
request (via \`defineRoute\` from \`${backonImport}\`) and calls into
|
|
332
427
|
\`*.services.${answers.language === "javascript" ? "js" : "ts"}\`, which is where real data access belongs.
|
|
333
|
-
|
|
428
|
+
|
|
429
|
+
${renderDeploySection(answers.deployTarget, ext)}`;
|
|
334
430
|
}
|
package/src/templates/route.ts
CHANGED
|
@@ -1,107 +1,186 @@
|
|
|
1
1
|
import type { Framework, Language } from "../prompts";
|
|
2
2
|
|
|
3
|
-
function pascalCase(name: string): string {
|
|
3
|
+
export function pascalCase(name: string): string {
|
|
4
4
|
return name
|
|
5
5
|
.split("-")
|
|
6
6
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
7
7
|
.join("");
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// file is already valid JavaScript as-is. Only the file extension changes.
|
|
14
|
-
export function renderController(route: string, framework: Framework): string {
|
|
15
|
-
const Route = pascalCase(route);
|
|
16
|
-
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
17
|
-
return `import { z } from "zod";
|
|
18
|
-
import { defineRoute } from "${backonImport}";
|
|
19
|
-
import * as service from "./${route}.services";
|
|
20
|
-
|
|
21
|
-
export const list = defineRoute({}, async (c) => {
|
|
22
|
-
const items = await service.list();
|
|
23
|
-
return c.json(items);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// TODO: replace with the real shape of a ${Route}
|
|
27
|
-
const create${Route}Schema = z.object({});
|
|
28
|
-
|
|
29
|
-
export const create = defineRoute({ body: create${Route}Schema }, async (c, { body }) => {
|
|
30
|
-
const item = await service.create(body);
|
|
31
|
-
return c.json(item, 201);
|
|
32
|
-
});
|
|
33
|
-
`;
|
|
10
|
+
export function camelCase(name: string): string {
|
|
11
|
+
const [first, ...rest] = name.split("-");
|
|
12
|
+
return [first, ...rest.map((part) => part.charAt(0).toUpperCase() + part.slice(1))].join("");
|
|
34
13
|
}
|
|
35
14
|
|
|
36
|
-
export
|
|
15
|
+
export type HttpVerb = "get" | "post" | "put" | "patch" | "delete";
|
|
16
|
+
|
|
17
|
+
export const HTTP_VERBS: readonly HttpVerb[] = ["get", "post", "put", "patch", "delete"];
|
|
18
|
+
|
|
19
|
+
export interface MethodSpec {
|
|
20
|
+
verb: HttpVerb;
|
|
21
|
+
/** e.g. "/" or "/:id" — path params come straight from this, nothing else derives them. */
|
|
22
|
+
path: string;
|
|
23
|
+
/** Identifier for the controller export and the matching service function. Caller's job to keep it unique. */
|
|
24
|
+
name: string;
|
|
25
|
+
needsBody: boolean;
|
|
26
|
+
needsQuery: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Path params come straight from the URL pattern — no guessing needed, unlike body/query shape. */
|
|
30
|
+
export function paramNamesFromPath(path: string): string[] {
|
|
31
|
+
return [...path.matchAll(/:([A-Za-z0-9_]+)/g)]
|
|
32
|
+
.map((match) => match[1])
|
|
33
|
+
.filter((name): name is string => name !== undefined);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Blank endpoint — an endpoint folder with zero methods wired yet. This is
|
|
38
|
+
// what every route starts as now, whether it comes from the initial
|
|
39
|
+
// `bun create backon` route prompt or a later `add-route`: methods are
|
|
40
|
+
// always a deliberate second step via `bun run add-method`, rather than
|
|
41
|
+
// guessing that every route wants a GET-list/POST-create pair.
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
const PLACEHOLDER_COMMENT = "// No methods yet — run `bun run add-method` to add one.";
|
|
45
|
+
|
|
46
|
+
export function renderBlankController(): string {
|
|
47
|
+
return `${PLACEHOLDER_COMMENT}\n`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function renderBlankRoutes(route: string, framework: Framework, language: Language): string {
|
|
51
|
+
const varName = `${camelCase(route)}Routes`;
|
|
52
|
+
|
|
37
53
|
if (framework === "express") {
|
|
38
|
-
// Router() takes no generic parameter either way — same content for
|
|
39
|
-
// both languages, same reasoning as renderController above.
|
|
40
54
|
return `import { Router } from "@mednours/backon/express";
|
|
41
|
-
import * as controller from "./${route}.controller";
|
|
42
55
|
|
|
43
|
-
export const ${
|
|
56
|
+
export const ${varName} = Router();
|
|
44
57
|
|
|
45
|
-
${
|
|
46
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
58
|
+
${PLACEHOLDER_COMMENT}
|
|
47
59
|
`;
|
|
48
60
|
}
|
|
49
61
|
|
|
50
62
|
if (language === "javascript") {
|
|
51
|
-
// `new Hono<Env>()` is TypeScript-only syntax (a generic type
|
|
52
|
-
// argument) — not valid JavaScript, so the JS variant drops both the
|
|
53
|
-
// `type Env` import and the `<Env>` argument. The Env generic only
|
|
54
|
-
// affects c.get()/c.set() typing anyway, which doesn't exist in plain
|
|
55
|
-
// JS regardless.
|
|
56
63
|
return `import { Hono } from "@mednours/backon";
|
|
57
|
-
import * as controller from "./${route}.controller";
|
|
58
64
|
|
|
59
|
-
export const ${
|
|
65
|
+
export const ${varName} = new Hono();
|
|
60
66
|
|
|
61
|
-
${
|
|
62
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
67
|
+
${PLACEHOLDER_COMMENT}
|
|
63
68
|
`;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
return `import { Hono, type Env } from "@mednours/backon";
|
|
67
|
-
import * as controller from "./${route}.controller";
|
|
68
72
|
|
|
69
|
-
export const ${
|
|
73
|
+
export const ${varName} = new Hono<Env>();
|
|
70
74
|
|
|
71
|
-
${
|
|
72
|
-
${camelCase(route)}Routes.post("/", controller.create);
|
|
75
|
+
${PLACEHOLDER_COMMENT}
|
|
73
76
|
`;
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
export function
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
export function renderBlankServices(): string {
|
|
80
|
+
return `// No routes yet — service functions land here as methods are added via \`bun run add-method\`.\n`;
|
|
81
|
+
}
|
|
79
82
|
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// Per-method codegen — appended to an existing endpoint's three files by
|
|
85
|
+
// add-method.ts, one HTTP method at a time. Handler signature is always
|
|
86
|
+
// `(c, { body, query, params })`, regardless of which schemas this method
|
|
87
|
+
// actually declares (defineRoute always hands back all three, unused ones
|
|
88
|
+
// just come through as undefined) — keeping it uniform means no conditional
|
|
89
|
+
// destructuring to get right, and the generated tsconfig has neither
|
|
90
|
+
// noUnusedLocals nor noUnusedParameters on, so an unused destructured name
|
|
91
|
+
// is not a compile error.
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
function defaultServiceReturn(spec: MethodSpec): string {
|
|
95
|
+
const { verb, needsBody } = spec;
|
|
96
|
+
const hasParams = paramNamesFromPath(spec.path).length > 0;
|
|
97
|
+
|
|
98
|
+
if (verb === "get" && !hasParams) return "return [];";
|
|
99
|
+
if (verb === "get") return "return null; // TODO: fetch the real record";
|
|
100
|
+
if (verb === "post") return needsBody ? "return { id: crypto.randomUUID(), ...body };" : "return { id: crypto.randomUUID() };";
|
|
101
|
+
if (verb === "put" || verb === "patch") return needsBody ? "return { ...params, ...body };" : "return { ...params };";
|
|
102
|
+
return "return { success: true };"; // delete
|
|
82
103
|
}
|
|
83
104
|
|
|
84
|
-
|
|
85
|
-
|
|
105
|
+
/** Positional args the controller passes to the matching service function — only what this method actually declared. */
|
|
106
|
+
function serviceCallArgs(spec: MethodSpec): string[] {
|
|
107
|
+
const args: string[] = [];
|
|
108
|
+
if (paramNamesFromPath(spec.path).length > 0) args.push("params");
|
|
109
|
+
if (spec.needsBody) args.push("body");
|
|
110
|
+
if (spec.needsQuery) args.push("query");
|
|
111
|
+
return args;
|
|
86
112
|
}
|
|
87
|
-
`;
|
|
88
|
-
}
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
/** Matching typed parameter list for the service function itself (TS only — see renderServiceMethod). */
|
|
115
|
+
function serviceParamList(spec: MethodSpec): string {
|
|
116
|
+
const parts: string[] = [];
|
|
117
|
+
if (paramNamesFromPath(spec.path).length > 0) parts.push(`params: z.infer<typeof ${spec.name}ParamsSchema>`);
|
|
118
|
+
if (spec.needsBody) parts.push(`body: z.infer<typeof ${spec.name}BodySchema>`);
|
|
119
|
+
if (spec.needsQuery) parts.push(`query: z.infer<typeof ${spec.name}QuerySchema>`);
|
|
120
|
+
return parts.join(", ");
|
|
121
|
+
}
|
|
91
122
|
|
|
92
|
-
export
|
|
93
|
-
|
|
123
|
+
export function controllerImports(route: string, framework: Framework): string[] {
|
|
124
|
+
const backonImport = framework === "express" ? "@mednours/backon/express" : "@mednours/backon";
|
|
125
|
+
return [`import { z } from "zod";`, `import { defineRoute } from "${backonImport}";`, `import * as service from "./${route}.services";`];
|
|
94
126
|
}
|
|
95
127
|
|
|
96
|
-
export
|
|
97
|
-
|
|
128
|
+
export function renderControllerMethod(spec: MethodSpec): string {
|
|
129
|
+
const paramNames = paramNamesFromPath(spec.path);
|
|
130
|
+
const lines: string[] = [];
|
|
131
|
+
|
|
132
|
+
if (paramNames.length > 0) {
|
|
133
|
+
const shape = paramNames.map((name) => `${name}: z.string()`).join(", ");
|
|
134
|
+
lines.push(`export const ${spec.name}ParamsSchema = z.object({ ${shape} });`);
|
|
135
|
+
}
|
|
136
|
+
if (spec.needsBody) {
|
|
137
|
+
lines.push(`// TODO: replace with the real shape of "${spec.name}"'s request body`);
|
|
138
|
+
lines.push(`export const ${spec.name}BodySchema = z.object({});`);
|
|
139
|
+
}
|
|
140
|
+
if (spec.needsQuery) {
|
|
141
|
+
lines.push(`// TODO: replace with the real shape of "${spec.name}"'s query params`);
|
|
142
|
+
lines.push(`export const ${spec.name}QuerySchema = z.object({});`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const schemaEntries: string[] = [];
|
|
146
|
+
if (paramNames.length > 0) schemaEntries.push(`params: ${spec.name}ParamsSchema`);
|
|
147
|
+
if (spec.needsBody) schemaEntries.push(`body: ${spec.name}BodySchema`);
|
|
148
|
+
if (spec.needsQuery) schemaEntries.push(`query: ${spec.name}QuerySchema`);
|
|
149
|
+
const schemaObject = schemaEntries.length > 0 ? `{ ${schemaEntries.join(", ")} }` : "{}";
|
|
150
|
+
|
|
151
|
+
const callArgs = serviceCallArgs(spec).join(", ");
|
|
152
|
+
const status = spec.verb === "post" ? ", 201" : "";
|
|
153
|
+
|
|
154
|
+
lines.push(
|
|
155
|
+
`export const ${spec.name} = defineRoute(${schemaObject}, async (c, { body, query, params }) => {`,
|
|
156
|
+
` const result = await service.${spec.name}(${callArgs});`,
|
|
157
|
+
` return c.json(result${status});`,
|
|
158
|
+
`});`
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return lines.join("\n");
|
|
98
162
|
}
|
|
99
|
-
|
|
163
|
+
|
|
164
|
+
export function routeMountLine(route: string, spec: MethodSpec): string {
|
|
165
|
+
return `${camelCase(route)}Routes.${spec.verb}("${spec.path}", controller.${spec.name});`;
|
|
100
166
|
}
|
|
101
167
|
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
168
|
+
export function serviceImports(route: string, spec: MethodSpec, language: Language): string[] {
|
|
169
|
+
if (language === "javascript") return [];
|
|
170
|
+
const imports: string[] = [`import type { z } from "zod";`];
|
|
171
|
+
const named: string[] = [];
|
|
172
|
+
if (paramNamesFromPath(spec.path).length > 0) named.push(`${spec.name}ParamsSchema`);
|
|
173
|
+
if (spec.needsBody) named.push(`${spec.name}BodySchema`);
|
|
174
|
+
if (spec.needsQuery) named.push(`${spec.name}QuerySchema`);
|
|
175
|
+
if (named.length > 0) imports.push(`import type { ${named.join(", ")} } from "./${route}.controller";`);
|
|
176
|
+
return imports;
|
|
105
177
|
}
|
|
106
178
|
|
|
107
|
-
export
|
|
179
|
+
export function renderServiceMethod(spec: MethodSpec, language: Language): string {
|
|
180
|
+
const ext = language === "javascript" ? "js" : "ts";
|
|
181
|
+
const argList = language === "javascript" ? serviceCallArgs(spec).join(", ") : serviceParamList(spec);
|
|
182
|
+
return `// TODO: replace with real data access (see src/config/db.${ext})
|
|
183
|
+
export async function ${spec.name}(${argList}) {
|
|
184
|
+
${defaultServiceReturn(spec)}
|
|
185
|
+
}`;
|
|
186
|
+
}
|