2.2 KiB
2.2 KiB
build-routes.ts
Purpose
Builds the 6 OpenAPI route entries the plugin mounts on the host Hono app: single-shot POST /, chunked-init POST /initiate, chunk append PUT /{uploadId}/chunks/{chunkIndex}, finalise POST /{uploadId}/complete, abort DELETE /{uploadId}, and status GET /{uploadId}. Each route uses RouteBuilder from @suaveplan/api-openapi so request/response schemas flow through end-to-end, keeping the served /openapi.json document accurate against the live wire surface.
Features
- Six routes registered with typed request bodies, params, and responses.
- Single shared service handle (
FileUploadService) wired into every handler, so the routes do not own any per-request state. - Reuses the same
SingleShotResponseSchemaacrossPOST /andPOST /{uploadId}/completeso clients have a uniform success shape regardless of which upload mode they used.
Usage: Basic
import { buildUploadRoutes } from "@suaveplan/api-plugin-file-upload/routes/routes";
import { definePluginRoutes } from "@suaveplan/api-openapi";
definePluginRoutes(buildUploadRoutes(service), {
app,
registry: openAPIRegistry,
pluginName: "file-upload",
});
Usage: Advanced
import { buildUploadRoutes } from "@suaveplan/api-plugin-file-upload";
const entries = buildUploadRoutes(service);
for (const entry of entries) {
console.log(entry.route.method, entry.route.path);
}
// → post /
// → post /initiate
// → put /{uploadId}/chunks/{chunkIndex}
// → post /{uploadId}/complete
// → delete /{uploadId}
// → get /{uploadId}
API Reference
buildUploadRoutes(service: FileUploadService): PluginRouteEntry[]— returns the 6 ready-to-mount entries.
Implementation Notes
- The paths use OpenAPI
{name}placeholders (not Hono's:name) becauseRouteBuilderemits OpenAPI 3.1 path templates.@hono/zod-openapitranslates back to Hono's parameter syntax at registration. - The single-shot route uses
POST /— Hono mounts it underbasePath, so the final URL isPOST {basePath}(defaultPOST /uploads). - All routes return JSON. Multipart parsing for the single-shot path happens INSIDE the handler so the route table stays declarative.