mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 18:41:14 +00:00
refactor(core): Post-release refactorings of Public API (#3495)
* ⚡ Post-release refactorings * 🧪 Add `--forceExit` * 🛠 typing refactor (#3486) * 🐛 Fix middleware arguments * 👕 Fix lint * ⚡ Restore commented out block Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
This commit is contained in:
@@ -1,38 +1,38 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable import/no-cycle */
|
||||
import express, { Router } from 'express';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
import * as OpenApiValidator from 'express-openapi-validator';
|
||||
import { HttpError } from 'express-openapi-validator/dist/framework/types';
|
||||
import fs from 'fs/promises';
|
||||
import { OpenAPIV3 } from 'openapi-types';
|
||||
import path from 'path';
|
||||
import * as swaggerUi from 'swagger-ui-express';
|
||||
import swaggerUi from 'swagger-ui-express';
|
||||
import validator from 'validator';
|
||||
import * as YAML from 'yamljs';
|
||||
import { Db, InternalHooksManager } from '..';
|
||||
import YAML from 'yamljs';
|
||||
|
||||
import config from '../../config';
|
||||
import { Db, InternalHooksManager } from '..';
|
||||
import { getInstanceBaseUrl } from '../UserManagement/UserManagementHelper';
|
||||
|
||||
function createApiRouter(
|
||||
version: string,
|
||||
openApiSpecPath: string,
|
||||
hanldersDirectory: string,
|
||||
handlersDirectory: string,
|
||||
swaggerThemeCss: string,
|
||||
publicApiEndpoint: string,
|
||||
): Router {
|
||||
const n8nPath = config.getEnv('path');
|
||||
const swaggerDocument = YAML.load(openApiSpecPath) as swaggerUi.JsonObject;
|
||||
// add the server depeding on the config so the user can interact with the API
|
||||
// from the swagger UI
|
||||
// from the Swagger UI
|
||||
swaggerDocument.server = [
|
||||
{
|
||||
url: `${getInstanceBaseUrl()}/${publicApiEndpoint}/${version}}`,
|
||||
},
|
||||
];
|
||||
const apiController = express.Router();
|
||||
|
||||
apiController.use(
|
||||
`/${publicApiEndpoint}/${version}/docs`,
|
||||
swaggerUi.serveFiles(swaggerDocument),
|
||||
@@ -42,12 +42,14 @@ function createApiRouter(
|
||||
customfavIcon: `${n8nPath}favicon.ico`,
|
||||
}),
|
||||
);
|
||||
|
||||
apiController.use(`/${publicApiEndpoint}/${version}`, express.json());
|
||||
|
||||
apiController.use(
|
||||
`/${publicApiEndpoint}/${version}`,
|
||||
OpenApiValidator.middleware({
|
||||
apiSpec: openApiSpecPath,
|
||||
operationHandlers: hanldersDirectory,
|
||||
operationHandlers: handlersDirectory,
|
||||
validateRequests: true,
|
||||
validateApiSpec: true,
|
||||
formats: [
|
||||
@@ -71,16 +73,12 @@ function createApiRouter(
|
||||
schema: OpenAPIV3.ApiKeySecurityScheme,
|
||||
): Promise<boolean> => {
|
||||
const apiKey = req.headers[schema.name.toLowerCase()];
|
||||
const user = await Db.collections.User?.findOne({
|
||||
where: {
|
||||
apiKey,
|
||||
},
|
||||
const user = await Db.collections.User.findOne({
|
||||
where: { apiKey },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return false;
|
||||
}
|
||||
if (!user) return false;
|
||||
|
||||
void InternalHooksManager.getInstance().onUserInvokedApi({
|
||||
user_id: user.id,
|
||||
@@ -97,13 +95,20 @@ function createApiRouter(
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
apiController.use(
|
||||
(error: HttpError, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
(
|
||||
error: HttpError,
|
||||
_req: express.Request,
|
||||
res: express.Response,
|
||||
_next: express.NextFunction,
|
||||
) => {
|
||||
return res.status(error.status || 400).json({
|
||||
message: error.message,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
return apiController;
|
||||
}
|
||||
|
||||
@@ -114,11 +119,12 @@ export const loadPublicApiVersions = async (
|
||||
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 apiRouters = versions.map((version) => {
|
||||
const openApiPath = path.join(__dirname, version, 'openapi.yml');
|
||||
apiRouters.push(createApiRouter(version, openApiPath, __dirname, css, publicApiEndpoint));
|
||||
}
|
||||
return createApiRouter(version, openApiPath, __dirname, css, publicApiEndpoint);
|
||||
});
|
||||
|
||||
return {
|
||||
apiRouters,
|
||||
apiLatestVersion: Number(versions.pop()?.charAt(1)) ?? 1,
|
||||
|
||||
Reference in New Issue
Block a user