Skip to content
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

feat(@xen-orchestra/rest-api): initial setup for swagger #8316

Merged
merged 6 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/open-api/*
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/.npmignore
6 changes: 6 additions & 0 deletions @xen-orchestra/rest-api/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-check

import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'

export default tseslint.config(eslint.configs.recommended, tseslint.configs.recommended)
40 changes: 40 additions & 0 deletions @xen-orchestra/rest-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"main": "./dist/index.mjs",
"name": "@xen-orchestra/rest-api",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/rest-api",
"version": "0.0.0",
"description": "REST API to manage your XOA",
"license": "AGPL-3.0-or-later",
"private": false,
"type": "module",
"engines": {
"node": ">=20.18"
},
"scripts": {
"build": "tsoa spec-and-routes && tsc",
"dev": "tsoa spec-and-routes && tsc --watch",
"prepublishOnly": "npm run build",
"postversion": "npm publish --access public"
},
"devDependencies": {
"@eslint/js": "^9.19.0",
"@types/express": "^5.0.0",
"@types/swagger-ui-express": "^4.1.7",
"typescript": "~5.6.3",
"typescript-eslint": "^8.23.0"
},
"dependencies": {
"swagger-ui-express": "^5.0.1",
"tsoa": "^6.6.0"
},
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@xen-orchestra/rest-api",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
}
}
18 changes: 18 additions & 0 deletions @xen-orchestra/rest-api/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as swaggerUi from 'swagger-ui-express'
import { createRequire } from 'module'
import type { Express } from 'express'

import { RegisterRoutes } from './open-api/routes/routes.js'

// Avoid using "import from" to import a json file as this requires assert/with and will break compatibility with recent node versions
// https://github.com/nodejs/node/issues/51622
const require = createRequire(import.meta.url)
const swaggerOpenApiSpec = require('../open-api/spec/swagger.json')

export default function setupRestApi(express: Express) {
RegisterRoutes(express)

// do not register the doc at the root level, or it may lead to unwated behaviour
express.get('/rest/v0', (_req, res) => res.redirect('/rest/v0/docs'))
express.use('/rest/v0/docs', swaggerUi.serve, swaggerUi.setup(swaggerOpenApiSpec))
}
4 changes: 4 additions & 0 deletions @xen-orchestra/rest-api/src/vms/vm.controller.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller, Route } from 'tsoa'

@Route('vms')
export class VmController extends Controller {}
17 changes: 17 additions & 0 deletions @xen-orchestra/rest-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",

"strict": true,
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2023",
"skipLibCheck": true,

/* Experimental options */
"experimentalDecorators": true // used by the TSOA framework
},
"include": ["./src/**/*"],
"exclude": ["./node_modules", "./dist"]
}
16 changes: 16 additions & 0 deletions @xen-orchestra/rest-api/tsoa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"entryFile": "./src/index.mts",
"noImplicitAdditionalProperties": "throw-on-extras",
"controllerPathGlobs": ["./src/**/*.controller.mts"],
"spec": {
"outputDirectory": "./open-api/spec",
"specVersion": 3,
"basePath": "/rest/v0"
},
"routes": {
"routesDir": "./src/open-api/routes",
"esm": true,
"middleware": "express",
"basePath": "/rest/v0"
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
> Users must be able to say: “Nice enhancement, I'm eager to test it”

- [Plugin/backup-reports] Add VM Description to the backup report. (contribution made by [@truongtx8](https://github.com/truongtx8)) (PR [#8253](https://github.com/vatesfr/xen-orchestra/pull/8253))
- [REST API] Swagger interface available on `/rest/v0` endpoint. Endpoint documentation will be added step by step (PR [#8316](https://github.com/vatesfr/xen-orchestra/pull/8316))

### Bug fixes

Expand All @@ -34,6 +35,7 @@
<!--packages-start-->

- @vates/types major
- @xen-orchestra/rest-api minor
- @xen-orchestra/web-core minor
- xo-server-auth-oidc patch
- xo-server-backup-reports minor
Expand Down
1 change: 1 addition & 0 deletions packages/xo-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@xen-orchestra/log": "^0.7.1",
"@xen-orchestra/mixin": "^0.2.0",
"@xen-orchestra/mixins": "^0.16.2",
"@xen-orchestra/rest-api": "^0.0.0",
"@xen-orchestra/self-signed": "^0.2.1",
"@xen-orchestra/template": "^0.1.0",
"@xen-orchestra/vmware-explorer": "^0.9.0",
Expand Down
8 changes: 3 additions & 5 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import setupRestApi from '@xen-orchestra/rest-api'
import { asyncEach } from '@vates/async-each'
import { createGzip } from 'node:zlib'
import { defer } from 'golike-defer'
Expand Down Expand Up @@ -1139,11 +1140,6 @@ export default class RestApi {
}
})

api.get(
'/',
wrap((req, res) => sendObjects(Object.values(collections), req, res))
)

// For compatibility redirect from /backups* to /backup
api.get('/backups*', (req, res) => {
res.redirect(308, req.baseUrl + '/backup' + req.params[0])
Expand Down Expand Up @@ -1514,6 +1510,8 @@ export default class RestApi {
res.sendStatus(200)
})
)

setupRestApi(express)
}

registerRestApi(spec, base = '/') {
Expand Down
1 change: 1 addition & 0 deletions scripts/npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ __snapshots__/

/docs/
/src/
eslint.config.mjs
Loading
Loading