mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Enable strict TypeScript settings
This commit is contained in:
@@ -25,14 +25,14 @@ export class AcceptPreferenceParser extends PreferenceParser {
|
||||
|
||||
public async handle(input: HttpRequest): Promise<RepresentationPreferences> {
|
||||
const result: RepresentationPreferences = {};
|
||||
const headers: { [T in keyof RepresentationPreferences]: { val: string; func: (input: string) => AcceptHeader[] }} = {
|
||||
const headers: { [T in keyof RepresentationPreferences]: { val?: string; func: (input: string) => AcceptHeader[] }} = {
|
||||
type: { val: input.headers.accept, func: parseAccept },
|
||||
charset: { val: input.headers['accept-charset'] as string, func: parseAcceptCharset },
|
||||
encoding: { val: input.headers['accept-encoding'] as string, func: parseAcceptEncoding },
|
||||
language: { val: input.headers['accept-language'], func: parseAcceptLanguage },
|
||||
};
|
||||
Object.keys(headers).forEach((key: keyof RepresentationPreferences): void => {
|
||||
const preferences = this.parseHeader(headers[key].val, headers[key].func);
|
||||
(Object.keys(headers) as (keyof RepresentationPreferences)[]).forEach((key): void => {
|
||||
const preferences = this.parseHeader(headers[key]!.val, headers[key]!.func);
|
||||
if (preferences.length > 0) {
|
||||
result[key] = preferences;
|
||||
}
|
||||
@@ -53,7 +53,7 @@ export class AcceptPreferenceParser extends PreferenceParser {
|
||||
*
|
||||
* @returns A list of {@link RepresentationPreference}. Returns an empty list if input was not defined.
|
||||
*/
|
||||
private parseHeader(input: string, parseFunction: (input: string) => AcceptHeader[]): RepresentationPreference[] {
|
||||
private parseHeader(input: string | undefined, parseFunction: (input: string) => AcceptHeader[]): RepresentationPreference[] {
|
||||
if (!input) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ import { Representation } from '../representation/Representation';
|
||||
/**
|
||||
* Parses the body of an incoming {@link HttpRequest} and converts it to a {@link Representation}.
|
||||
*/
|
||||
export abstract class BodyParser extends AsyncHandler<HttpRequest, Representation> {}
|
||||
export abstract class BodyParser extends AsyncHandler<HttpRequest, Representation | undefined> {}
|
||||
|
||||
@@ -6,4 +6,4 @@ import { ResponseDescription } from '../operations/ResponseDescription';
|
||||
* Writes to the HttpResponse.
|
||||
* Response depends on the operation result and potentially which errors was thrown.
|
||||
*/
|
||||
export abstract class ResponseWriter extends AsyncHandler<{ response: HttpResponse; description?: ResponseDescription; error?: Error }> {}
|
||||
export abstract class ResponseWriter extends AsyncHandler<{ response: HttpResponse; result: ResponseDescription | Error }> {}
|
||||
|
||||
@@ -28,7 +28,9 @@ export class SimpleBodyParser extends BodyParser {
|
||||
}
|
||||
}
|
||||
|
||||
public async handle(input: HttpRequest): Promise<QuadRepresentation> {
|
||||
// Note that the only reason this is a union is in case the body is empty.
|
||||
// If this check gets moved away from the BodyParsers this union could be removed
|
||||
public async handle(input: HttpRequest): Promise<QuadRepresentation | undefined> {
|
||||
const contentType = input.headers['content-type'];
|
||||
|
||||
if (!contentType) {
|
||||
|
||||
@@ -19,9 +19,9 @@ export interface SimpleRequestParserArgs {
|
||||
* of a {@link TargetExtractor}, {@link PreferenceParser}, and {@link BodyParser}.
|
||||
*/
|
||||
export class SimpleRequestParser extends RequestParser {
|
||||
private readonly targetExtractor: TargetExtractor;
|
||||
private readonly preferenceParser: PreferenceParser;
|
||||
private readonly bodyParser: BodyParser;
|
||||
private readonly targetExtractor!: TargetExtractor;
|
||||
private readonly preferenceParser!: PreferenceParser;
|
||||
private readonly bodyParser!: BodyParser;
|
||||
|
||||
public constructor(args: SimpleRequestParserArgs) {
|
||||
super();
|
||||
@@ -42,6 +42,9 @@ export class SimpleRequestParser extends RequestParser {
|
||||
const preferences = await this.preferenceParser.handleSafe(input);
|
||||
const body = await this.bodyParser.handleSafe(input);
|
||||
|
||||
if (!input.method) {
|
||||
throw new Error('Missing method.');
|
||||
}
|
||||
return { method: input.method, target, preferences, body };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,40 +8,38 @@ import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
* Writes to an {@link HttpResponse} based on the incoming {@link ResponseDescription} or error.
|
||||
*/
|
||||
export class SimpleResponseWriter extends ResponseWriter {
|
||||
public async canHandle(input: { response: HttpResponse; description?: ResponseDescription; error?: Error }): Promise<void> {
|
||||
if (!input.description && !input.error) {
|
||||
throw new UnsupportedHttpError('Either a description or an error is required for output.');
|
||||
}
|
||||
if (input.description && input.description.body) {
|
||||
if (input.description.body.dataType !== 'binary' && input.description.body.dataType !== 'string') {
|
||||
public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
|
||||
if (!(input.result instanceof Error)) {
|
||||
const dataType = input.result.body?.dataType;
|
||||
if (dataType && dataType !== 'binary' && dataType !== 'string') {
|
||||
throw new UnsupportedHttpError('Only string or binary results are supported.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async handle(input: { response: HttpResponse; description?: ResponseDescription; error?: Error }): Promise<void> {
|
||||
if (input.description) {
|
||||
input.response.setHeader('location', input.description.identifier.path);
|
||||
if (input.description.body) {
|
||||
const contentType = input.description.body.metadata.contentType || 'text/plain';
|
||||
public async handle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
|
||||
if (input.result instanceof Error) {
|
||||
let code = 500;
|
||||
if (input.result instanceof HttpError) {
|
||||
code = input.result.statusCode;
|
||||
}
|
||||
input.response.setHeader('content-type', 'text/plain');
|
||||
input.response.writeHead(code);
|
||||
input.response.end(`${input.result.name}: ${input.result.message}\n${input.result.stack}`);
|
||||
} else {
|
||||
input.response.setHeader('location', input.result.identifier.path);
|
||||
if (input.result.body) {
|
||||
const contentType = input.result.body.metadata.contentType ?? 'text/plain';
|
||||
input.response.setHeader('content-type', contentType);
|
||||
input.description.body.data.pipe(input.response);
|
||||
input.result.body.data.pipe(input.response);
|
||||
}
|
||||
|
||||
input.response.writeHead(200);
|
||||
|
||||
if (!input.description.body) {
|
||||
if (!input.result.body) {
|
||||
// If there is an input body the response will end once the input stream ends
|
||||
input.response.end();
|
||||
}
|
||||
} else {
|
||||
let code = 500;
|
||||
if (input.error instanceof HttpError) {
|
||||
code = input.error.statusCode;
|
||||
}
|
||||
input.response.setHeader('content-type', 'text/plain');
|
||||
input.response.writeHead(code);
|
||||
input.response.end(`${input.error.name}: ${input.error.message}\n${input.error.stack}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user