mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: update eslint settings
This commit is contained in:
parent
3e2cfaf11e
commit
a07f440ab6
@ -13,7 +13,11 @@ module.exports = {
|
||||
],
|
||||
rules: {
|
||||
'@typescript-eslint/no-empty-interface': 'off',
|
||||
"sort-imports": "error",
|
||||
'@typescript-eslint/space-before-function-paren': [ 'error', 'never' ],
|
||||
'class-methods-use-this': 'off',
|
||||
'comma-dangle': ['error', 'always-multiline'],
|
||||
'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }],
|
||||
'padding-line-between-statements': 'off',
|
||||
'tsdoc/syntax': 'error',
|
||||
},
|
||||
};
|
||||
|
@ -46,22 +46,17 @@ export interface AuthenticatedLdpHandlerArgs {
|
||||
*/
|
||||
export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
private readonly requestParser: RequestParser;
|
||||
|
||||
private readonly credentialsExtractor: CredentialsExtractor;
|
||||
|
||||
private readonly permissionsExtractor: PermissionsExtractor;
|
||||
|
||||
private readonly authorizer: Authorizer;
|
||||
|
||||
private readonly operationHandler: OperationHandler;
|
||||
|
||||
private readonly responseWriter: ResponseWriter;
|
||||
|
||||
/**
|
||||
* Creates the handler.
|
||||
* @param args - The handlers required. None of them are optional.
|
||||
*/
|
||||
public constructor (args: AuthenticatedLdpHandlerArgs) {
|
||||
public constructor(args: AuthenticatedLdpHandlerArgs) {
|
||||
super();
|
||||
Object.assign(this, args);
|
||||
}
|
||||
@ -73,7 +68,7 @@ export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
*
|
||||
* @returns A promise resolving if this request can be handled, otherwise rejecting with an Error.
|
||||
*/
|
||||
public async canHandle (input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
public async canHandle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
return this.requestParser.canHandle(input.request);
|
||||
}
|
||||
|
||||
@ -90,7 +85,7 @@ export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
*
|
||||
* @returns A promise resolving when the handling is finished.
|
||||
*/
|
||||
public async handle (input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
let err: Error;
|
||||
let operation: Operation;
|
||||
|
||||
@ -112,15 +107,11 @@ export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
*
|
||||
* @returns A promise resolving to the generated Operation.
|
||||
*/
|
||||
private async runHandlers (request: HttpRequest): Promise<Operation> {
|
||||
private async runHandlers(request: HttpRequest): Promise<Operation> {
|
||||
const op: Operation = await this.requestParser.handleSafe(request);
|
||||
|
||||
const credentials: Credentials = await this.credentialsExtractor.handleSafe(request);
|
||||
|
||||
const permissions: PermissionSet = await this.permissionsExtractor.handleSafe(op);
|
||||
|
||||
await this.authorizer.handleSafe({ credentials, identifier: op.target, permissions });
|
||||
|
||||
await this.operationHandler.handleSafe(op);
|
||||
|
||||
return op;
|
||||
|
@ -27,7 +27,7 @@ export abstract class AsyncHandler<TInput, TOutput = void> {
|
||||
*
|
||||
* @returns The result of the handle function of the handler.
|
||||
*/
|
||||
public async handleSafe (data: TInput): Promise<TOutput> {
|
||||
public async handleSafe(data: TInput): Promise<TOutput> {
|
||||
await this.canHandle(data);
|
||||
|
||||
return this.handle(data);
|
||||
|
@ -12,7 +12,7 @@ export class CompositeAsyncHandler<TIn, TOut> implements AsyncHandler<TIn, TOut>
|
||||
* Creates a new CompositeAsyncHandler that stores the given handlers.
|
||||
* @param handlers - Handlers over which it will run.
|
||||
*/
|
||||
public constructor (handlers: AsyncHandler<TIn, TOut>[]) {
|
||||
public constructor(handlers: AsyncHandler<TIn, TOut>[]) {
|
||||
this.handlers = handlers;
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ export class CompositeAsyncHandler<TIn, TOut> implements AsyncHandler<TIn, TOut>
|
||||
*
|
||||
* @returns A promise resolving if at least 1 handler supports to input, or rejecting if none do.
|
||||
*/
|
||||
public async canHandle (input: TIn): Promise<void> {
|
||||
public async canHandle(input: TIn): Promise<void> {
|
||||
await this.findHandler(input);
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ export class CompositeAsyncHandler<TIn, TOut> implements AsyncHandler<TIn, TOut>
|
||||
* @returns A promise corresponding to the handle call of a handler that supports the input.
|
||||
* It rejects if no handlers support the given data.
|
||||
*/
|
||||
public async handle (input: TIn): Promise<TOut> {
|
||||
public async handle(input: TIn): Promise<TOut> {
|
||||
let handler: AsyncHandler<TIn, TOut>;
|
||||
|
||||
try {
|
||||
@ -52,7 +52,7 @@ export class CompositeAsyncHandler<TIn, TOut> implements AsyncHandler<TIn, TOut>
|
||||
* @returns A promise corresponding to the handle call of a handler that supports the input.
|
||||
* It rejects if no handlers support the given data.
|
||||
*/
|
||||
public async handleSafe (input: TIn): Promise<TOut> {
|
||||
public async handleSafe(input: TIn): Promise<TOut> {
|
||||
const handler = await this.findHandler(input);
|
||||
|
||||
return handler.handle(input);
|
||||
@ -66,7 +66,7 @@ export class CompositeAsyncHandler<TIn, TOut> implements AsyncHandler<TIn, TOut>
|
||||
*
|
||||
* @returns A promise resolving to a handler that supports the data or otherwise rejecting.
|
||||
*/
|
||||
private async findHandler (input: TIn): Promise<AsyncHandler<TIn, TOut>> {
|
||||
private async findHandler(input: TIn): Promise<AsyncHandler<TIn, TOut>> {
|
||||
const errors: Error[] = [];
|
||||
|
||||
for (const handler of this.handlers) {
|
||||
|
@ -11,7 +11,7 @@ export abstract class HttpError extends Error {
|
||||
* @param name - Error name. Useful for logging and stack tracing.
|
||||
* @param message - Message to be thrown.
|
||||
*/
|
||||
protected constructor (statusCode: number, name: string, message: string) {
|
||||
protected constructor(statusCode: number, name: string, message: string) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = name;
|
||||
|
@ -9,7 +9,7 @@ export class UnsupportedHttpError extends HttpError {
|
||||
* Default message is 'The given input is not supported by the server configuration.'.
|
||||
* @param message - Optional, more specific, message.
|
||||
*/
|
||||
public constructor (message?: string) {
|
||||
public constructor(message?: string) {
|
||||
super(400, 'UnsupportedHttpError', message || 'The given input is not supported by the server configuration.');
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
describe('A basic test', (): void => {
|
||||
it('to have something pass.', async (): Promise<void> => {
|
||||
it('to have something pass.', async(): Promise<void> => {
|
||||
expect(true).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
@ -11,7 +11,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
|
||||
let args: AuthenticatedLdpHandlerArgs;
|
||||
let responseFn: jest.Mock<Promise<void>, [any]>;
|
||||
|
||||
beforeEach(async (): Promise<void> => {
|
||||
beforeEach(async(): Promise<void> => {
|
||||
const requestParser: RequestParser = new StaticAsyncHandler(true, 'parser' as any);
|
||||
const credentialsExtractor: CredentialsExtractor = new StaticAsyncHandler(true, 'credentials' as any);
|
||||
const permissionsExtractor: PermissionsExtractor = new StaticAsyncHandler(true, 'permissions' as any);
|
||||
@ -19,7 +19,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
|
||||
const operationHandler: OperationHandler = new StaticAsyncHandler(true, 'operation' as any);
|
||||
const responseWriter: ResponseWriter = new StaticAsyncHandler(true, 'response' as any);
|
||||
|
||||
responseFn = jest.fn(async (input: any): Promise<void> => {
|
||||
responseFn = jest.fn(async(input: any): Promise<void> => {
|
||||
if (!input) {
|
||||
throw new Error('error');
|
||||
}
|
||||
@ -29,17 +29,17 @@ describe('An AuthenticatedLdpHandler', (): void => {
|
||||
args = { requestParser, credentialsExtractor, permissionsExtractor, authorizer, operationHandler, responseWriter };
|
||||
});
|
||||
|
||||
it('can be created.', async (): Promise<void> => {
|
||||
it('can be created.', async(): Promise<void> => {
|
||||
expect(new AuthenticatedLdpHandler(args)).toBeInstanceOf(AuthenticatedLdpHandler);
|
||||
});
|
||||
|
||||
it('can check if it handles input.', async (): Promise<void> => {
|
||||
it('can check if it handles input.', async(): Promise<void> => {
|
||||
const handler = new AuthenticatedLdpHandler(args);
|
||||
|
||||
await expect(handler.canHandle({ request: null, response: null })).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('can handle input.', async (): Promise<void> => {
|
||||
it('can handle input.', async(): Promise<void> => {
|
||||
const handler = new AuthenticatedLdpHandler(args);
|
||||
|
||||
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toEqual('response');
|
||||
@ -47,7 +47,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
|
||||
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', operation: 'parser' as any });
|
||||
});
|
||||
|
||||
it('sends an error to the output if a handler does not support the input.', async (): Promise<void> => {
|
||||
it('sends an error to the output if a handler does not support the input.', async(): Promise<void> => {
|
||||
args.requestParser = new StaticAsyncHandler(false, null);
|
||||
const handler = new AuthenticatedLdpHandler(args);
|
||||
|
||||
@ -56,7 +56,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
|
||||
expect(responseFn.mock.calls[0][0].error).toBeInstanceOf(Error);
|
||||
});
|
||||
|
||||
it('errors if the response writer does not support the result.', async (): Promise< void> => {
|
||||
it('errors if the response writer does not support the result.', async(): Promise< void> => {
|
||||
args.responseWriter = new StaticAsyncHandler(false, null);
|
||||
const handler = new AuthenticatedLdpHandler(args);
|
||||
|
||||
|
@ -2,10 +2,10 @@ import { AsyncHandler } from '../../../src/util/AsyncHandler';
|
||||
import { StaticAsyncHandler } from '../../util/StaticAsyncHandler';
|
||||
|
||||
describe('An AsyncHandler', (): void => {
|
||||
it('calls canHandle and handle when handleSafe is called.', async (): Promise<void> => {
|
||||
it('calls canHandle and handle when handleSafe is called.', async(): Promise<void> => {
|
||||
const handlerTrue: AsyncHandler<any, any> = new StaticAsyncHandler(true, null);
|
||||
const canHandleFn = jest.fn(async (input: any): Promise<void> => input);
|
||||
const handleFn = jest.fn(async (input: any): Promise<any> => input);
|
||||
const canHandleFn = jest.fn(async(input: any): Promise<void> => input);
|
||||
const handleFn = jest.fn(async(input: any): Promise<any> => input);
|
||||
|
||||
handlerTrue.canHandle = canHandleFn;
|
||||
handlerTrue.handle = handleFn;
|
||||
@ -14,12 +14,12 @@ describe('An AsyncHandler', (): void => {
|
||||
expect(handleFn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('does not call handle when canHandle errors during a handleSafe call.', async (): Promise<void> => {
|
||||
it('does not call handle when canHandle errors during a handleSafe call.', async(): Promise<void> => {
|
||||
const handlerFalse: AsyncHandler<any, any> = new StaticAsyncHandler(false, null);
|
||||
const canHandleFn = jest.fn(async (): Promise<void> => {
|
||||
const canHandleFn = jest.fn(async(): Promise<void> => {
|
||||
throw new Error('test');
|
||||
});
|
||||
const handleFn = jest.fn(async (input: any): Promise<any> => input);
|
||||
const handleFn = jest.fn(async(input: any): Promise<any> => input);
|
||||
|
||||
handlerFalse.canHandle = canHandleFn;
|
||||
handlerFalse.handle = handleFn;
|
||||
|
@ -4,13 +4,13 @@ import { StaticAsyncHandler } from '../../util/StaticAsyncHandler';
|
||||
|
||||
describe('A CompositeAsyncHandler', (): void => {
|
||||
describe('with no handlers', (): void => {
|
||||
it('can never handle data.', async (): Promise<void> => {
|
||||
it('can never handle data.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toThrow(Error);
|
||||
});
|
||||
|
||||
it('errors if its handle function is called.', async (): Promise<void> => {
|
||||
it('errors if its handle function is called.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([]);
|
||||
|
||||
await expect(handler.handle(null)).rejects.toThrow(Error);
|
||||
@ -23,29 +23,29 @@ describe('A CompositeAsyncHandler', (): void => {
|
||||
let canHandleFn: jest.Mock<Promise<void>, [any]>;
|
||||
let handleFn: jest.Mock<Promise<void>, [any]>;
|
||||
|
||||
beforeEach(async (): Promise<void> => {
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerTrue = new StaticAsyncHandler(true, null);
|
||||
handlerFalse = new StaticAsyncHandler(false, null);
|
||||
|
||||
canHandleFn = jest.fn(async (input: any): Promise<any> => input);
|
||||
handleFn = jest.fn(async (input: any): Promise<any> => input);
|
||||
canHandleFn = jest.fn(async(input: any): Promise<any> => input);
|
||||
handleFn = jest.fn(async(input: any): Promise<any> => input);
|
||||
handlerTrue.canHandle = canHandleFn;
|
||||
handlerTrue.handle = handleFn;
|
||||
});
|
||||
|
||||
it('can handle data if a handler supports it.', async (): Promise<void> => {
|
||||
it('can handle data if a handler supports it.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerTrue ]);
|
||||
|
||||
await expect(handler.canHandle(null)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('can not handle data if no handler supports it.', async (): Promise<void> => {
|
||||
it('can not handle data if no handler supports it.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerFalse ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toThrow('[Not supported., Not supported.]');
|
||||
});
|
||||
|
||||
it('handles data if a handler supports it.', async (): Promise<void> => {
|
||||
it('handles data if a handler supports it.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerTrue ]);
|
||||
|
||||
await expect(handler.handle('test')).resolves.toEqual('test');
|
||||
@ -53,13 +53,13 @@ describe('A CompositeAsyncHandler', (): void => {
|
||||
expect(handleFn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('errors if the handle function is called but no handler supports the data.', async (): Promise<void> => {
|
||||
it('errors if the handle function is called but no handler supports the data.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerFalse ]);
|
||||
|
||||
await expect(handler.handle('test')).rejects.toThrow('All handlers failed.');
|
||||
});
|
||||
|
||||
it('only calls the canHandle function once of its handlers when handleSafe is called.', async (): Promise<void> => {
|
||||
it('only calls the canHandle function once of its handlers when handleSafe is called.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerTrue ]);
|
||||
|
||||
await expect(handler.handleSafe('test')).resolves.toEqual('test');
|
||||
@ -67,7 +67,7 @@ describe('A CompositeAsyncHandler', (): void => {
|
||||
expect(handleFn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('throws the same error as canHandle when calling handleSafe if no handler supports the data.', async (): Promise<void> => {
|
||||
it('throws the same error as canHandle when calling handleSafe if no handler supports the data.', async(): Promise<void> => {
|
||||
const handler = new CompositeAsyncHandler([ handlerFalse, handlerFalse ]);
|
||||
|
||||
await expect(handler.handleSafe(null)).rejects.toThrow('[Not supported., Not supported.]');
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||
|
||||
describe('An UnsupportedHttpError', (): void => {
|
||||
it('has status code 400.', async (): Promise<void> => {
|
||||
it('has status code 400.', async(): Promise<void> => {
|
||||
const error = new UnsupportedHttpError('test');
|
||||
|
||||
expect(error.statusCode).toEqual(400);
|
||||
@ -9,7 +9,7 @@ describe('An UnsupportedHttpError', (): void => {
|
||||
expect(error.name).toEqual('UnsupportedHttpError');
|
||||
});
|
||||
|
||||
it('has a default message if none was provided.', async (): Promise<void> => {
|
||||
it('has a default message if none was provided.', async(): Promise<void> => {
|
||||
const error = new UnsupportedHttpError();
|
||||
|
||||
expect(error.message).toEqual('The given input is not supported by the server configuration.');
|
||||
|
@ -2,23 +2,22 @@ import { AsyncHandler } from '../../src/util/AsyncHandler';
|
||||
|
||||
export class StaticAsyncHandler<TOut> extends AsyncHandler<any, TOut> {
|
||||
private readonly canHandleStatic: boolean;
|
||||
|
||||
private readonly handleStatic: TOut;
|
||||
|
||||
public constructor (canHandleStatic: boolean, handleStatic: TOut) {
|
||||
public constructor(canHandleStatic: boolean, handleStatic: TOut) {
|
||||
super();
|
||||
this.canHandleStatic = canHandleStatic;
|
||||
this.handleStatic = handleStatic;
|
||||
}
|
||||
|
||||
public async canHandle (): Promise<void> {
|
||||
public async canHandle(): Promise<void> {
|
||||
if (this.canHandleStatic) {
|
||||
return;
|
||||
}
|
||||
throw new Error('Not supported.');
|
||||
}
|
||||
|
||||
public async handle (): Promise<TOut> {
|
||||
public async handle(): Promise<TOut> {
|
||||
return this.handleStatic;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user