mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Expose swagger ui in GET /{version}/docs
This commit is contained in:
parent
104db9c438
commit
02d7958ea4
|
@ -19,7 +19,7 @@
|
|||
"bin": "n8n"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc && cp -r ./src/UserManagement/email/templates ./dist/src/UserManagement/email; rsync -a --include='*/' --include='*.yml' --exclude='*' ./src/PublicApi/ ./dist/src/PublicApi/",
|
||||
"build": "tsc && cp -r ./src/UserManagement/email/templates ./dist/src/UserManagement/email; rsync -a --include='*/' --include={'*.yml','*.css'} --exclude='*' ./src/PublicApi/ ./dist/src/PublicApi/",
|
||||
"dev": "concurrently -k -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold\" \"npm run watch\" \"nodemon\"",
|
||||
"format": "cd ../.. && node_modules/prettier/bin-prettier.js packages/cli/**/**.ts --write",
|
||||
"lint": "cd ../.. && node_modules/eslint/bin/eslint.js packages/cli",
|
||||
|
@ -97,6 +97,8 @@
|
|||
"@rudderstack/rudder-sdk-node": "1.0.6",
|
||||
"@types/json-diff": "^0.5.1",
|
||||
"@types/jsonwebtoken": "^8.5.2",
|
||||
"@types/swagger-ui-express": "^4.1.3",
|
||||
"@types/yamljs": "^0.2.31",
|
||||
"basic-auth": "^2.0.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"body-parser": "^1.18.3",
|
||||
|
@ -143,10 +145,12 @@
|
|||
"request-promise-native": "^1.0.7",
|
||||
"sqlite3": "^5.0.2",
|
||||
"sse-channel": "^3.1.1",
|
||||
"swagger-ui-express": "^4.3.0",
|
||||
"tslib": "1.14.1",
|
||||
"typeorm": "0.2.30",
|
||||
"uuid": "^8.3.0",
|
||||
"validator": "13.7.0",
|
||||
"winston": "^3.3.3"
|
||||
"winston": "^3.3.3",
|
||||
"yamljs": "^0.3.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,15 +8,29 @@ import * as OpenApiValidator from 'express-openapi-validator';
|
|||
import { HttpError } from 'express-openapi-validator/dist/framework/types';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import * as swaggerUi from 'swagger-ui-express';
|
||||
import * as YAML from 'yamljs';
|
||||
import config from '../../config';
|
||||
import { authenticationHandler, specFormats } from './helpers';
|
||||
|
||||
function createApiRouter(
|
||||
version: string,
|
||||
openApiSpecPath: string,
|
||||
hanldersDirectory: string,
|
||||
swaggerThemeCss: string,
|
||||
): Router {
|
||||
const n8nPath = config.getEnv('path');
|
||||
const swaggerDocument = YAML.load(openApiSpecPath) as swaggerUi.JsonObject;
|
||||
const apiController = express.Router();
|
||||
apiController.use(`/${version}/spec`, express.static(openApiSpecPath));
|
||||
apiController.use(
|
||||
`/${version}/docs`,
|
||||
swaggerUi.serveFiles(swaggerDocument),
|
||||
swaggerUi.setup(swaggerDocument, {
|
||||
customCss: swaggerThemeCss,
|
||||
customSiteTitle: 'n8n Public API UI',
|
||||
customfavIcon: `${n8nPath}favicon.ico`,
|
||||
}),
|
||||
);
|
||||
apiController.use(`/${version}`, express.json());
|
||||
apiController.use(
|
||||
`/${version}`,
|
||||
|
@ -44,12 +58,14 @@ function createApiRouter(
|
|||
}
|
||||
|
||||
export const loadPublicApiVersions = async (): Promise<express.Router[]> => {
|
||||
const data = await fs.readdir(__dirname);
|
||||
const versions = data.filter((folderName) => folderName.startsWith('v'));
|
||||
const swaggerThemePath = path.join(__dirname, 'swaggerTheme.css');
|
||||
const folders = await fs.readdir(__dirname);
|
||||
const css = (await fs.readFile(swaggerThemePath)).toString();
|
||||
const versions = folders.filter((folderName) => folderName.startsWith('v'));
|
||||
const apiRouters: express.Router[] = [];
|
||||
for (const version of versions) {
|
||||
const openApiPath = path.join(__dirname, version, 'openapi.yml');
|
||||
apiRouters.push(createApiRouter(version, openApiPath, __dirname));
|
||||
apiRouters.push(createApiRouter(version, openApiPath, __dirname, css));
|
||||
}
|
||||
return apiRouters;
|
||||
};
|
||||
|
|
26
packages/cli/src/PublicApi/swaggerTheme.css
Normal file
26
packages/cli/src/PublicApi/swaggerTheme.css
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Public n8n API
|
||||
title: n8n Public API
|
||||
description: n8n Public API
|
||||
termsOfService: https://n8n.io/legal/terms
|
||||
contact:
|
||||
|
@ -9,26 +9,22 @@ info:
|
|||
license:
|
||||
name: Sustainable Use License
|
||||
url: https://github.com/n8n-io/n8n/blob/master/packages/cli/LICENSE.md
|
||||
version: 1.0.0
|
||||
version: v1
|
||||
externalDocs:
|
||||
description: n8n API documentation
|
||||
url: https://docs.n8n.io/api/
|
||||
servers:
|
||||
# Added by API Auto Mocking Plugin
|
||||
- url: /api/v1
|
||||
tags:
|
||||
- name: users
|
||||
- name: User
|
||||
description: Operations about user
|
||||
externalDocs:
|
||||
description: Find out more about our store
|
||||
url: http://swagger.io
|
||||
paths:
|
||||
/users:
|
||||
get:
|
||||
x-eov-operation-id: getUsers
|
||||
x-eov-operation-handler: v1/handlers/Users
|
||||
tags:
|
||||
- users
|
||||
- User
|
||||
summary: Retrieve all users
|
||||
description: Retrieve all users from your instance. Only available for the instance owner.
|
||||
parameters:
|
||||
|
@ -74,7 +70,7 @@ paths:
|
|||
x-eov-operation-id: createUsers
|
||||
x-eov-operation-handler: v1/handlers/Users
|
||||
tags:
|
||||
- users
|
||||
- User
|
||||
summary: Invite a user
|
||||
description: Invites a user to your instance. Only available for the instance owner.
|
||||
operationId: createUser
|
||||
|
@ -107,7 +103,7 @@ paths:
|
|||
x-eov-operation-id: getUser
|
||||
x-eov-operation-handler: v1/handlers/Users
|
||||
tags:
|
||||
- users
|
||||
- User
|
||||
summary: Get user by ID/Email
|
||||
description: Retrieve a user from your instance. Only available for the instance owner.
|
||||
operationId: getUser
|
||||
|
@ -142,7 +138,7 @@ paths:
|
|||
x-eov-operation-id: deleteUser
|
||||
x-eov-operation-handler: v1/handlers/Users
|
||||
tags:
|
||||
- users
|
||||
- User
|
||||
summary: Delete user by ID/Email
|
||||
description: Deletes a user from your instance. Only available for the instance owner.
|
||||
operationId: deleteUser
|
||||
|
|
Loading…
Reference in a new issue