Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {ResponseFactory} from "./ResponseFactory"
|
||||
import {Rehydratable} from "../../util"
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {Rehydratable} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Helper function that creates a DehydratedStateResponseFactory.
|
||||
@@ -15,10 +15,12 @@ export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
|
||||
*/
|
||||
export class DehydratedStateResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly rehydratable: Rehydratable
|
||||
) { super() }
|
||||
public readonly rehydratable: Rehydratable,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
|
||||
request.response.setHeader('Content-Type', 'application/json')
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import {ResponseFactory} from "./ResponseFactory"
|
||||
import {ErrorWithContext, HTTPStatus} from "../../util"
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import * as api from "./api"
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {ErrorWithContext, HTTPStatus} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
import * as api from './api'
|
||||
|
||||
/**
|
||||
* Helper to create a new ErrorResponseFactory, with the given HTTP status and output format.
|
||||
* @param error
|
||||
* @param thrownError
|
||||
* @param status
|
||||
* @param output
|
||||
*/
|
||||
export function error(
|
||||
error: Error | string,
|
||||
thrownError: Error | string,
|
||||
status: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
output: 'json' | 'html' | 'auto' = 'auto'
|
||||
output: 'json' | 'html' | 'auto' = 'auto',
|
||||
): ErrorResponseFactory {
|
||||
if ( typeof error === 'string' ) error = new Error(error)
|
||||
return new ErrorResponseFactory(error, status, output)
|
||||
if ( typeof thrownError === 'string' ) {
|
||||
thrownError = new Error(thrownError)
|
||||
}
|
||||
return new ErrorResponseFactory(thrownError, status, output)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,9 +27,9 @@ export class ErrorResponseFactory extends ResponseFactory {
|
||||
protected targetMode: 'json' | 'html' | 'auto' = 'auto'
|
||||
|
||||
constructor(
|
||||
public readonly error: Error,
|
||||
public readonly thrownError: Error,
|
||||
status: HTTPStatus,
|
||||
output: 'json' | 'html' | 'auto' = 'auto'
|
||||
output: 'json' | 'html' | 'auto' = 'auto',
|
||||
) {
|
||||
super()
|
||||
this.status(status)
|
||||
@@ -39,16 +41,16 @@ export class ErrorResponseFactory extends ResponseFactory {
|
||||
return this
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
const wants = request.wants()
|
||||
|
||||
if ( this.targetMode === 'json' || (this.targetMode === 'auto' && wants === 'json') ) {
|
||||
request.response.setHeader('Content-Type', 'application/json')
|
||||
request.response.body = this.buildJSON(this.error)
|
||||
request.response.body = this.buildJSON(this.thrownError)
|
||||
} else if ( this.targetMode === 'html' || (this.targetMode === 'auto' && (wants === 'html' || wants === 'unknown')) ) {
|
||||
request.response.setHeader('Content-Type', 'text/html')
|
||||
request.response.body = this.buildHTML(this.error)
|
||||
request.response.body = this.buildHTML(this.thrownError)
|
||||
}
|
||||
|
||||
// FIXME XML support
|
||||
@@ -61,12 +63,12 @@ export class ErrorResponseFactory extends ResponseFactory {
|
||||
* @param {Error} error
|
||||
* @return string
|
||||
*/
|
||||
protected buildHTML(error: Error) {
|
||||
protected buildHTML(thrownError: Error): string {
|
||||
let context: any
|
||||
if ( error instanceof ErrorWithContext ) {
|
||||
context = error.context
|
||||
if ( error.originalError ) {
|
||||
error = error.originalError
|
||||
if ( thrownError instanceof ErrorWithContext ) {
|
||||
context = thrownError.context
|
||||
if ( thrownError.originalError ) {
|
||||
thrownError = thrownError.originalError
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,10 +76,11 @@ export class ErrorResponseFactory extends ResponseFactory {
|
||||
<b>Sorry, an unexpected error occurred while processing your request.</b>
|
||||
<br>
|
||||
<pre><code>
|
||||
Name: ${error.name}
|
||||
Message: ${error.message}
|
||||
Name: ${thrownError.name}
|
||||
Message: ${thrownError.message}
|
||||
Stack trace:
|
||||
- ${error.stack ? error.stack.split(/\s+at\s+/).slice(1).join('<br> - ') : 'none'}
|
||||
- ${thrownError.stack ? thrownError.stack.split(/\s+at\s+/).slice(1)
|
||||
.join('<br> - ') : 'none'}
|
||||
</code></pre>
|
||||
`
|
||||
|
||||
@@ -85,7 +88,8 @@ Stack trace:
|
||||
str += `
|
||||
<pre><code>
|
||||
Context:
|
||||
${Object.keys(context).map(key => ` - ${key} : ${context[key]}`).join('\n')}
|
||||
${Object.keys(context).map(key => ` - ${key} : ${context[key]}`)
|
||||
.join('\n')}
|
||||
</code></pre>
|
||||
`
|
||||
}
|
||||
@@ -93,7 +97,7 @@ ${Object.keys(context).map(key => ` - ${key} : ${context[key]}`).join('\n')}
|
||||
return str
|
||||
}
|
||||
|
||||
protected buildJSON(error: Error) {
|
||||
return JSON.stringify(api.error(error))
|
||||
protected buildJSON(thrownError: Error): string {
|
||||
return JSON.stringify(api.error(thrownError))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Helper function that creates a new HTMLResponseFactory.
|
||||
@@ -15,9 +15,11 @@ export function html(value: string): HTMLResponseFactory {
|
||||
export class HTMLResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly value: string,
|
||||
) { super() }
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
request.response.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
request.response.body = this.value
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {ErrorResponseFactory} from "./ErrorResponseFactory";
|
||||
import {HTTPError} from "../HTTPError";
|
||||
import {HTTPStatus} from "../../util"
|
||||
import {ErrorResponseFactory} from './ErrorResponseFactory'
|
||||
import {HTTPError} from '../HTTPError'
|
||||
import {HTTPStatus} from '../../util'
|
||||
|
||||
/**
|
||||
* Helper that generates a new HTTPErrorResponseFactory given the HTTP status and message.
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Helper function to create a new JSONResponseFactory of the given value.
|
||||
* @param value
|
||||
*/
|
||||
export function json(value: any): JSONResponseFactory {
|
||||
export function json(value: unknown): JSONResponseFactory {
|
||||
return new JSONResponseFactory(value)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,12 @@ export function json(value: any): JSONResponseFactory {
|
||||
*/
|
||||
export class JSONResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly value: any
|
||||
) { super() }
|
||||
public readonly value: unknown,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
request.response.setHeader('Content-Type', 'application/json')
|
||||
request.response.body = JSON.stringify(this.value)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {HTTPStatus} from "../../util"
|
||||
import {Request} from "../lifecycle/Request"
|
||||
import {HTTPStatus} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Abstract class that defines "factory" that knows how to write a particular
|
||||
@@ -19,7 +19,7 @@ export abstract class ResponseFactory {
|
||||
}
|
||||
|
||||
/** Set the target status of this factory. */
|
||||
public status(status: HTTPStatus) {
|
||||
public status(status: HTTPStatus): this {
|
||||
this.targetStatus = status
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Helper function that creates a new StringResponseFactory for the given string value.
|
||||
@@ -16,9 +16,11 @@ export class StringResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
/** The string to write as the body. */
|
||||
public readonly value: string,
|
||||
) { super() }
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
request.response.setHeader('Content-Type', 'text/plain')
|
||||
request.response.body = this.value
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {HTTPStatus} from "../../util";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {HTTPStatus} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Helper function to create a new TemporaryRedirectResponseFactory to the given destination.
|
||||
@@ -18,10 +18,12 @@ export class TemporaryRedirectResponseFactory extends ResponseFactory {
|
||||
|
||||
constructor(
|
||||
/** THe URL where the client should redirect to. */
|
||||
public readonly destination: string
|
||||
) { super() }
|
||||
public readonly destination: string,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request = await super.write(request)
|
||||
request.response.setHeader('Location', this.destination)
|
||||
return request
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {Container} from "../../di";
|
||||
import {ResponseFactory} from "./ResponseFactory";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {ViewEngine} from "../../views/ViewEngine";
|
||||
import {Container} from '../../di'
|
||||
import {ResponseFactory} from './ResponseFactory'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
import {ViewEngine} from '../../views/ViewEngine'
|
||||
|
||||
/**
|
||||
* Helper function that creates a new ViewResponseFactory to render the given view
|
||||
@@ -22,10 +22,12 @@ export class ViewResponseFactory extends ResponseFactory {
|
||||
/** The name of the view to render. */
|
||||
public readonly viewName: string,
|
||||
/** Optional data that should be passed to the view engine as params. */
|
||||
public readonly data?: {[key: string]: any}
|
||||
) { super() }
|
||||
public readonly data?: {[key: string]: any},
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
public async write(request: Request): Promise<Request> {
|
||||
const viewEngine = <ViewEngine> Container.getContainer().make(ViewEngine)
|
||||
request.response.body = await viewEngine.renderByName(this.viewName, this.data || {})
|
||||
request.response.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
|
||||
@@ -14,13 +14,13 @@ export interface APIResponse {
|
||||
|
||||
/**
|
||||
* Formats a mesage as a successful API response.
|
||||
* @param {string} message
|
||||
* @param {string} displayMessage
|
||||
* @return APIResponse
|
||||
*/
|
||||
export function message(message: string): APIResponse {
|
||||
export function message(displayMessage: string): APIResponse {
|
||||
return {
|
||||
success: true,
|
||||
message,
|
||||
message: displayMessage,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export function message(message: string): APIResponse {
|
||||
* @param record
|
||||
* @return APIResponse
|
||||
*/
|
||||
export function one(record: any): APIResponse {
|
||||
export function one(record: unknown): APIResponse {
|
||||
return {
|
||||
success: true,
|
||||
data: record,
|
||||
@@ -53,23 +53,23 @@ export function many(records: any[]): APIResponse {
|
||||
|
||||
/**
|
||||
* Formats an error message or Error instance as an API response.
|
||||
* @param {string|Error} error
|
||||
* @return APIResponse
|
||||
* @param thrownError
|
||||
*/
|
||||
export function error(error: string | Error): APIResponse {
|
||||
if ( typeof error === 'string' ) {
|
||||
export function error(thrownError: string | Error): APIResponse {
|
||||
if ( typeof thrownError === 'string' ) {
|
||||
return {
|
||||
success: false,
|
||||
message: error,
|
||||
message: thrownError,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: error.message,
|
||||
message: thrownError.message,
|
||||
error: {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack ? error.stack.split(/\s+at\s+/).slice(1) : [],
|
||||
name: thrownError.name,
|
||||
message: thrownError.message,
|
||||
stack: thrownError.stack ? thrownError.stack.split(/\s+at\s+/).slice(1) : [],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user