Skip to content

Commit

Permalink
Merge pull request #76 from bouwe77/openapi
Browse files Browse the repository at this point in the history
feat: OpenAPI spec generation when resources configured
  • Loading branch information
bouwe77 authored Sep 5, 2024
2 parents 37f93c9 + a424421 commit 5514702
Show file tree
Hide file tree
Showing 9 changed files with 788 additions and 9 deletions.
26 changes: 23 additions & 3 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "temba",
"version": "0.34.1",
"version": "0.35.0",
"description": "Get a simple REST API with zero coding in less than 30 seconds (seriously).",
"type": "module",
"main": "dist/src/index.js",
Expand Down Expand Up @@ -44,6 +44,7 @@
"etag": "^1.8.1",
"express": "^4.18.3",
"lowdb": "^7.0.1",
"morgan": "^1.10.0"
"morgan": "^1.10.0",
"openapi3-ts": "^4.4.0"
}
}
7 changes: 7 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Config = {
schemas: ConfiguredSchemas | null
allowDeleteCollection: boolean
etags: boolean
openapi: boolean
}

export type ConfigKey = keyof Config
Expand Down Expand Up @@ -50,6 +51,7 @@ export type UserConfig = {
schemas?: ConfiguredSchemas
allowDeleteCollection?: boolean
etags?: boolean
openapi?: boolean
}

const defaultConfig: Config = {
Expand All @@ -68,6 +70,7 @@ const defaultConfig: Config = {
schemas: null,
allowDeleteCollection: false,
etags: false,
openapi: false,
}

export const initConfig = (userConfig?: UserConfig): Config => {
Expand Down Expand Up @@ -169,6 +172,10 @@ export const initConfig = (userConfig?: UserConfig): Config => {
config.etags = userConfig.etags
}

if (!isUndefined(userConfig.openapi)) {
config.openapi = userConfig.openapi
}

return config
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createAuthMiddleware, isAuthEnabled } from './auth/auth'
import { initLogger } from './log/logger'
import { etag } from './etags/etags'
import type { StatsLike } from 'etag'
import { createOpenApiRouter } from './openapi/openapi'

// Route for handling not allowed methods.
const handleMethodNotAllowed = (_: Request, res: Response) => {
Expand Down Expand Up @@ -71,6 +72,9 @@ const createServer = (userConfig?: UserConfig) => {
app.use(config.customRouter)
}

app.use('/openapi.json', createOpenApiRouter('json', config))
app.use('/openapi.yaml', createOpenApiRouter('yaml', config))

// Temba supports the GET, POST, PUT, PATCH, DELETE, and HEAD methods for resource URLs.
// HEAD is not implemented here, because Express supports it out of the box.

Expand Down
Loading

0 comments on commit 5514702

Please sign in to comment.