fix: Remove unneeded pod files

This commit is contained in:
Joachim Van Herwegen
2021-05-28 16:39:28 +02:00
parent 4d7d939dc4
commit 130a91fdfb
11 changed files with 4 additions and 354 deletions

View File

@@ -1,70 +0,0 @@
import type { RequestParser } from '../ldp/http/RequestParser';
import { CreatedResponseDescription } from '../ldp/http/response/CreatedResponseDescription';
import type { ResponseWriter } from '../ldp/http/ResponseWriter';
import { HttpHandler } from '../server/HttpHandler';
import type { HttpRequest } from '../server/HttpRequest';
import type { HttpResponse } from '../server/HttpResponse';
import { BadRequestHttpError } from '../util/errors/BadRequestHttpError';
import { isNativeError } from '../util/errors/ErrorUtil';
import { InternalServerError } from '../util/errors/InternalServerError';
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
import type { PodManager } from './PodManager';
import type { PodSettingsParser } from './settings/PodSettingsParser';
export interface PodHttpHandlerArgs {
/** The path on which this handler should intercept requests. Should start with a slash. */
requestPath: string;
/** Parses the incoming request. */
requestParser: RequestParser;
/** Parses the data stream to PodSettings. */
podSettingsParser: PodSettingsParser;
/** Handles the pod management. */
manager: PodManager;
/** Writes the outgoing response. */
responseWriter: ResponseWriter;
}
/**
* An HTTP handler that listens to requests to a specific path for pod related requests.
* Handles everything related to pod management from input request to output response.
*/
export class PodManagerHttpHandler extends HttpHandler {
private readonly requestPath!: string;
private readonly requestParser!: RequestParser;
private readonly podSettingsParser!: PodSettingsParser;
private readonly manager!: PodManager;
private readonly responseWriter!: ResponseWriter;
public constructor(args: PodHttpHandlerArgs) {
super();
Object.assign(this, args);
}
public async canHandle({ request }: { request: HttpRequest }): Promise<void> {
if (request.url !== this.requestPath) {
throw new NotImplementedHttpError(`Only requests to ${this.requestPath} are accepted`);
}
}
public async handle({ request, response }: { request: HttpRequest; response: HttpResponse }): Promise<void> {
try {
if (request.method !== 'POST') {
throw new NotImplementedHttpError('Only POST requests are supported');
}
const op = await this.requestParser.handleSafe(request);
if (!op.body) {
throw new BadRequestHttpError('A body is required to create a pod');
}
const settings = await this.podSettingsParser.handleSafe(op.body);
const id = await this.manager.createPod(settings);
await this.responseWriter.handleSafe({ response, result: new CreatedResponseDescription(id) });
} catch (error: unknown) {
if (isNativeError(error)) {
await this.responseWriter.handleSafe({ response, result: error });
} else {
await this.responseWriter.handleSafe({ response, result: new InternalServerError('Unexpected error') });
}
}
}
}

View File

@@ -1,41 +0,0 @@
import type { Representation } from '../../ldp/representation/Representation';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { readableToString } from '../../util/StreamUtil';
import type { PodSettings } from './PodSettings';
import { PodSettingsParser } from './PodSettingsParser';
import Dict = NodeJS.Dict;
const requiredKeys: (keyof PodSettings)[] = [ 'login', 'webId' ];
/**
* A parser that extracts PodSettings data from a JSON body.
*/
export class PodSettingsJsonParser extends PodSettingsParser {
public async canHandle(input: Representation): Promise<void> {
if (!input.metadata.contentType || !this.isJSON(input.metadata.contentType)) {
throw new NotImplementedHttpError('Only JSON data is supported');
}
}
public async handle(input: Representation): Promise<PodSettings> {
const result = JSON.parse(await readableToString(input.data));
this.isValid(result);
return result;
}
private isJSON(mediaType: string): boolean {
return mediaType === 'application/json' || mediaType.endsWith('+json');
}
/**
* Checks if all the required PodSettings keys are there.
*/
private isValid(data: Dict<string>): asserts data is PodSettings {
for (const key of requiredKeys) {
if (!data[key]) {
throw new BadRequestHttpError(`Input data is missing key ${key}`);
}
}
}
}

View File

@@ -1,8 +0,0 @@
import type { Representation } from '../../ldp/representation/Representation';
import { AsyncHandler } from '../../util/handlers/AsyncHandler';
import type { PodSettings } from './PodSettings';
/**
* Parser that generates a {@link PodSettings} from the data in the given {@link Representation}.
*/
export abstract class PodSettingsParser extends AsyncHandler<Representation, PodSettings> { }