applications endpoints

This commit is contained in:
Erik 2023-05-08 17:34:40 +03:00
parent 8b20ae60bf
commit b1dfec1196
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
5 changed files with 64 additions and 7 deletions

View File

@ -93,6 +93,9 @@
"ignoreTemplateLiterals": true,
"ignoreRegExpLiterals": true
}],
"max-lines-per-function": ["warn", 100],
"max-depth": ["warn", 3],
"new-parens": "warn",
"no-alert": "warn",
"no-array-constructor": "warn",

View File

@ -58,6 +58,7 @@ class Server extends EventEmitter {
#logger: LoggerClient;
// eslint-disable-next-line max-lines-per-function
constructor (options: ServerOptions) {
super();
@ -195,7 +196,6 @@ class Server extends EventEmitter {
// Not sure why binding is a problem
this.#_app.use(this.#attachMisc.bind(this) as never);
this.#_app.use(this.#logRequest.bind(this) as never); // Logs every request
this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500
this.#_app.use(this.#ready.bind(this) as never); // denies requests before the server is ready
process.on('message', this._handleMessage.bind(this));
@ -222,6 +222,9 @@ class Server extends EventEmitter {
this.#logger.info('Loading endpoints');
await this.#_registry.loadEndpoints();
this.#logger.debug(this.#_registry.print);
// Must come last, after loading endpoints
this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500
this.#logger.info('Creating http server');
this.#_server = http.createServer(this.#_options.http, this.app).listen(this.port);

View File

@ -0,0 +1,55 @@
import { Request, Response } from "../../../../../@types/Server.js";
import PermissionValidationError from "../../../../util/errors/PermissionValidationError.js";
import Server from "../../../Server.js";
import ApiEndpoint from "../../../interfaces/ApiEndpoint.js";
import UserDatabaseInterface from "../../../interfaces/UserDatabaseInterface.js";
class ApplicationsEndpoint extends ApiEndpoint {
#users: UserDatabaseInterface;
constructor (server: Server) {
super(server, {
name: 'Applications',
path: '/applications',
auth: true
});
this.#users = server.users;
this.subpaths = [
[ 'get', '/:id', this.getApp.bind(this), [ server.auth.createAuthoriser('administrator:applications', 5) ]],
[ 'post', '/:id/permissions', this.updatePermissions.bind(this), [ server.auth.createAuthoriser('administrator:applications', 5) ]]
];
}
async getApp (req: Request, res: Response): Promise<void> {
const { id } = req.params;
const app = await this.#users.fetchApplication(id);
if (!app)
return void res.status(404).send('Invalid ID');
res.json(app.json);
}
async updatePermissions (req: Request, res: Response): Promise<void> {
const { params, body } = req;
const { id } = params;
const app = await this.#users.fetchApplication(id);
if (!app)
return void res.status(404).send('Invalid ID');
try {
await app.updatePermissions(body);
} catch (err) {
const error = err as Error;
if (err instanceof PermissionValidationError)
return void res.status(400).send(error.message);
this.logger.error(error.stack || error.message);
return void res.status(500).end();
}
res.end();
}
}
export default ApplicationsEndpoint;

View File

@ -1,7 +1,3 @@
// const { ApiEndpoint } = require("../../../interfaces");
// const { PermissionValidationError } = require('../../../../util/errors');
// const path = require('node:path');
import path from 'path';
import Server from '../../../Server.js';
import { ApiEndpoint, UserDatabaseInterface } from '../../../interfaces/index.js';

View File

@ -42,7 +42,7 @@ class Entity {
this.#_name = name;
this.#_disabled = disabled ?? false;
this.#_permissions = PermissionManager.merge(permissions || {}, PermissionManager.DefaultPermissions);
this.#_createdTimestamp = createdTimestamp || Date.now();
this.#_createdTimestamp = createdTimestamp ?? Date.now();
this.#_cachedTimestamp = Date.now();
this.#_note = note ?? null;
@ -125,7 +125,7 @@ class Entity {
}
get createdTimestamp () {
return this.#_cachedTimestamp;
return this.#_createdTimestamp;
}
get cachedTimestamp () {