feat: create PodHttpHandler with default interfaces

This commit is contained in:
Joachim Van Herwegen
2020-11-27 13:42:18 +01:00
parent ecfe3cfc46
commit 39745ccf22
6 changed files with 183 additions and 1 deletions

15
src/pods/PodManager.ts Normal file
View File

@@ -0,0 +1,15 @@
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import type { Agent } from './agent/Agent';
/**
* Covers all functions related to pod management.
* In the future this should also include delete, and potentially recovery functions.
*/
export interface PodManager {
/**
* Creates a pod for the given agent data.
* @param agent - Data of the agent that needs a pod.
* @returns {@link ResourceIdentifier} of the newly created pod.
*/
createPod: (agent: Agent) => Promise<ResourceIdentifier>;
}

View File

@@ -0,0 +1,69 @@
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 { InternalServerError } from '../util/errors/InternalServerError';
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
import type { AgentParser } from './agent/AgentParser';
import type { PodManager } from './PodManager';
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 an Agent. */
agentParser: AgentParser;
/** 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 agentParser!: AgentParser;
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 agent = await this.agentParser.handleSafe(op.body);
const id = await this.manager.createPod(agent);
await this.responseWriter.handleSafe({ response, result: new CreatedResponseDescription(id) });
} catch (error: unknown) {
if (error instanceof Error) {
await this.responseWriter.handleSafe({ response, result: error });
} else {
await this.responseWriter.handleSafe({ response, result: new InternalServerError('Unexpected error') });
}
}
}
}

9
src/pods/agent/Agent.ts Normal file
View File

@@ -0,0 +1,9 @@
/**
* Agent metadata related to pod generation.
*/
export type Agent = {
login: string;
webId: string;
name?: string;
email?: string;
};

View File

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