Skip to content

feat: typescript-express server template #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 5, 2025
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and API server scaffolding (routing, validation, serialization) from api specifi
Currently, [OpenAPI 3.0](https://swagger.io/specification/v3), [OpenAPI 3.1](https://swagger.io/specification/),
and [TypeSpec](https://typespec.io/) are supported as input specifications.

With typescript templates for [koa](https://openapi-code-generator.nahkies.co.nz/guides/server-templates/typescript-koa), [fetch](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-fetch), [axios](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-axios), and [angular](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-angular) currently available.
With typescript templates for [koa](https://openapi-code-generator.nahkies.co.nz/guides/server-templates/typescript-koa), [express](https://openapi-code-generator.nahkies.co.nz/guides/server-templates/typescript-express), [fetch](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-fetch), [axios](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-axios), and [angular](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-angular) currently available.

The [fetch](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-fetch) and [axios](https://openapi-code-generator.nahkies.co.nz/guides/client-templates/typescript-axios) templates work great in conjunction with [react-query](https://tanstack.com/query/latest)

Expand Down Expand Up @@ -39,6 +39,7 @@ The repository is structured as a mono repo of several npm packages that work to

- [openapi-code-generator](./packages/openapi-code-generator)
- [typescript-axios-runtime](./packages/typescript-axios-runtime)
- [typescript-express-runtime](./packages/typescript-express-runtime)
- [typescript-fetch-runtime](./packages/typescript-fetch-runtime)
- [typescript-koa-runtime](./packages/typescript-koa-runtime)

Expand Down
8 changes: 8 additions & 0 deletions e2e/scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ yarn openapi-code-generator \
--extract-inline-schemas \
--grouping-strategy first-tag

yarn openapi-code-generator \
--input ./openapi.yaml \
--output ./src/generated/server/express \
--template typescript-express \
--schema-builder "$SCHEMA_BUILDER" \
--extract-inline-schemas \
--grouping-strategy first-tag

yarn openapi-code-generator \
--input ./openapi.yaml \
--output ./src/generated/client/fetch \
Expand Down
57 changes: 57 additions & 0 deletions e2e/src/express.entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {type NextFunction, type Request, type Response, Router} from "express"
import {bootstrap} from "./generated/server/express"
import {createEscapeHatchesRouter} from "./routes/express/escape-hatches"
import {createRequestHeadersRouter} from "./routes/express/request-headers"
import {createValidationRouter} from "./routes/express/validation"
import {createErrorResponse} from "./shared"

function createRouter() {
const router = Router()

const requestHeadersRouter = createRequestHeadersRouter()
const validationRouter = createValidationRouter()
const escapeHatchesRouter = createEscapeHatchesRouter()

router.use(requestHeadersRouter)
router.use(validationRouter)
router.use(escapeHatchesRouter)

return router
}

export async function startExpressServer() {
const {app, server, address} = await bootstrap({
cors: {
credentials: true,
allowedHeaders: ["Authorization", "Content-Type"],
methods: ["GET", "OPTIONS"],
origin: "http://example.com",
},
router: createRouter(),
notFoundHandler: (req: Request, res: Response, next: NextFunction) => {
res.status(404).json({code: 404, message: "route not found"})
},
errorHandler: (
err: Error,
req: Request,
res: Response,
next: NextFunction,
) => {
if (res.headersSent) {
return next(err)
}

const {status, body} = createErrorResponse(err)
res.status(status).json(body)
},
})

return {app, server, address}
}

if (require.main === module) {
startExpressServer().catch((err) => {
console.error("fatal error", err)
process.exit(1)
})
}
13 changes: 13 additions & 0 deletions e2e/src/generated/server/express/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions e2e/src/generated/server/express/models.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions e2e/src/generated/server/express/routes/escape-hatches.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading