@ahmedrowaihi/openapi-ts-paths
v1.0.1
Published
Plugin for @hey-api/openapi-ts — emit per-operation route consts (spec template, URLPattern, method, operationId) for tree-shakable runtime routing and matching
Maintainers
Readme
@ahmedrowaihi/openapi-ts-paths
Per-operation route consts emitted from an OpenAPI spec — spec template, URLPattern-style pattern, method, and operationId, ready for tree-shakable runtime routing and matching. Plugin for @hey-api/openapi-ts.
Install
npm install -D @ahmedrowaihi/openapi-ts-paths @hey-api/openapi-tsUse
import { defineConfig } from "@hey-api/openapi-ts";
import { defineConfig as definePathsConfig } from "@ahmedrowaihi/openapi-ts-paths";
export default defineConfig({
input: "openapi.yaml",
output: { path: "./generated" },
plugins: [
"@hey-api/typescript",
definePathsConfig({
output: "paths", // → generated/paths.ts
naming: { casing: "camelCase", suffix: "Route" },
}),
],
});Output
For each operation that has a path and an operationId, the plugin emits:
export const getPetByIdRoute = {
spec: "/pets/{id}",
pattern: "/pets/:id",
method: "get",
operationId: "getPetById",
} as const;spec— the original{param}template, useful when re-issuing the request.pattern—:paramform, ready to feedURLPattern,path-to-regexp, your router of choice.method— lower-case verb.operationId— the OpenAPI id when present.
as const keeps every value literal-typed so downstream code can narrow on route.method, etc.
Why route consts (instead of runtime routing)
Tree-shakable: only the routes you import end up in the bundle. Pair with the matchers in @ahmedrowaihi/openapi-tools for client-side dispatch:
import { match } from "@ahmedrowaihi/openapi-tools/match";
import { getPetByIdRoute } from "./generated/paths";
const params = match(getPetByIdRoute.pattern, "/pets/42"); // { id: "42" }Or feed them to a router framework that takes pattern strings.
Options
definePathsConfig({
output: "paths", // filename (no extension). Default: "paths"
naming: {
casing: "camelCase", // any @hey-api/shared NamingRule | Casing
suffix: "Route", // appended to each const name
},
});License
MIT
