create-daloy 0.1.5 → 0.1.7
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/package.json
CHANGED
|
@@ -16,6 +16,13 @@ curl http://localhost:3000/healthz
|
|
|
16
16
|
curl http://localhost:3000/books/1
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
## API documentation
|
|
20
|
+
|
|
21
|
+
- Swagger UI: <http://localhost:3000/docs>
|
|
22
|
+
- OpenAPI 3.1 JSON: <http://localhost:3000/openapi.json>
|
|
23
|
+
|
|
24
|
+
The spec is generated live from your routes, so it stays in sync with what is actually deployed.
|
|
25
|
+
|
|
19
26
|
## Generate OpenAPI + typed client
|
|
20
27
|
|
|
21
28
|
```bash
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
secureHeaders,
|
|
8
8
|
} from "@daloyjs/core";
|
|
9
9
|
import { serve } from "@daloyjs/core/node";
|
|
10
|
+
import { generateOpenAPI } from "@daloyjs/core/openapi";
|
|
11
|
+
import { htmlResponse, swaggerUiHtml } from "@daloyjs/core/docs";
|
|
10
12
|
|
|
11
13
|
const app = new App({
|
|
12
14
|
bodyLimitBytes: 1024 * 1024,
|
|
@@ -59,5 +61,45 @@ app.route({
|
|
|
59
61
|
});
|
|
60
62
|
|
|
61
63
|
const port = Number(process.env.PORT ?? 3000);
|
|
64
|
+
|
|
65
|
+
// --- API documentation -----------------------------------------------------
|
|
66
|
+
// `/openapi.json` returns the live OpenAPI 3.1 spec generated from the routes
|
|
67
|
+
// defined above. `/docs` serves a Swagger UI page that loads that spec.
|
|
68
|
+
|
|
69
|
+
app.route({
|
|
70
|
+
method: "GET",
|
|
71
|
+
path: "/openapi.json",
|
|
72
|
+
operationId: "getOpenAPI",
|
|
73
|
+
tags: ["Docs"],
|
|
74
|
+
responses: { 200: { description: "OpenAPI 3.1 document" } },
|
|
75
|
+
handler: async () => ({
|
|
76
|
+
status: 200 as const,
|
|
77
|
+
body: generateOpenAPI(app, {
|
|
78
|
+
info: { title: "My Daloy API", version: "0.0.1" },
|
|
79
|
+
servers: [{ url: `http://localhost:${port}` }],
|
|
80
|
+
}),
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
app.route({
|
|
85
|
+
method: "GET",
|
|
86
|
+
path: "/docs",
|
|
87
|
+
operationId: "docs",
|
|
88
|
+
tags: ["Docs"],
|
|
89
|
+
responses: { 200: { description: "API reference UI" } },
|
|
90
|
+
handler: async () => {
|
|
91
|
+
const html = swaggerUiHtml({ specUrl: "/openapi.json", title: "My Daloy API" });
|
|
92
|
+
const res = htmlResponse(html);
|
|
93
|
+
return {
|
|
94
|
+
status: 200 as const,
|
|
95
|
+
body: html,
|
|
96
|
+
headers: Object.fromEntries(res.headers),
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
62
101
|
serve(app, { port });
|
|
63
102
|
console.log(`DaloyJS listening on http://localhost:${port}`);
|
|
103
|
+
console.log(` Swagger UI: http://localhost:${port}/docs`);
|
|
104
|
+
console.log(` OpenAPI JSON: http://localhost:${port}/openapi.json`);
|
|
105
|
+
console.log(` Health: http://localhost:${port}/healthz`);
|
|
@@ -16,6 +16,13 @@ curl http://localhost:3000/healthz
|
|
|
16
16
|
curl http://localhost:3000/books/1
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
## API documentation
|
|
20
|
+
|
|
21
|
+
- Swagger UI: <http://localhost:3000/docs>
|
|
22
|
+
- OpenAPI 3.1 JSON: <http://localhost:3000/openapi.json>
|
|
23
|
+
|
|
24
|
+
After deploying, the same routes serve `/docs` and `/openapi.json` from your Vercel Edge URL.
|
|
25
|
+
|
|
19
26
|
## Deploy
|
|
20
27
|
|
|
21
28
|
```bash
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { App, NotFoundError, requestId, secureHeaders } from "@daloyjs/core";
|
|
3
3
|
import { toEdgeHandler } from "@daloyjs/core/vercel";
|
|
4
|
+
import { generateOpenAPI } from "@daloyjs/core/openapi";
|
|
5
|
+
import { htmlResponse, swaggerUiHtml } from "@daloyjs/core/docs";
|
|
4
6
|
|
|
5
7
|
export const config = { runtime: "edge" };
|
|
6
8
|
|
|
@@ -52,4 +54,39 @@ app.route({
|
|
|
52
54
|
},
|
|
53
55
|
});
|
|
54
56
|
|
|
57
|
+
// --- API documentation -----------------------------------------------------
|
|
58
|
+
// `/openapi.json` returns the OpenAPI 3.1 spec generated from the routes above.
|
|
59
|
+
// `/docs` serves a Swagger UI page that loads that spec.
|
|
60
|
+
|
|
61
|
+
app.route({
|
|
62
|
+
method: "GET",
|
|
63
|
+
path: "/openapi.json",
|
|
64
|
+
operationId: "getOpenAPI",
|
|
65
|
+
tags: ["Docs"],
|
|
66
|
+
responses: { 200: { description: "OpenAPI 3.1 document" } },
|
|
67
|
+
handler: async () => ({
|
|
68
|
+
status: 200 as const,
|
|
69
|
+
body: generateOpenAPI(app, {
|
|
70
|
+
info: { title: "My Daloy Edge API", version: "0.0.1" },
|
|
71
|
+
}),
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
app.route({
|
|
76
|
+
method: "GET",
|
|
77
|
+
path: "/docs",
|
|
78
|
+
operationId: "docs",
|
|
79
|
+
tags: ["Docs"],
|
|
80
|
+
responses: { 200: { description: "API reference UI" } },
|
|
81
|
+
handler: async () => {
|
|
82
|
+
const html = swaggerUiHtml({ specUrl: "/openapi.json", title: "My Daloy Edge API" });
|
|
83
|
+
const res = htmlResponse(html);
|
|
84
|
+
return {
|
|
85
|
+
status: 200 as const,
|
|
86
|
+
body: html,
|
|
87
|
+
headers: Object.fromEntries(res.headers),
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
55
92
|
export default toEdgeHandler(app);
|