Fix route CLI commands

This commit is contained in:
2022-01-20 00:54:55 -06:00
parent dc16dfdb81
commit 32050cb2ce
3 changed files with 84 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
import {Collection, Either, ErrorWithContext, Pipeline, PrefixTypeArray, right} from '../../util'
import {Collection, Either, ErrorWithContext, Maybe, Pipeline, PrefixTypeArray, right} from '../../util'
import {ResponseFactory} from '../response/ResponseFactory'
import {HTTPMethod, Request} from '../lifecycle/Request'
import {constructable, Constructable, Container, Instantiable, isInstantiableOf, TypedDependencyKey} from '../../di'
@@ -180,6 +180,8 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
handler?: Constructable<(...x: THandlerParams) => TReturn>
protected displays: Collection<{stage: 'pre'|'post'|'handler', display: string}> = new Collection()
constructor(
protected method: HTTPMethod | HTTPMethod[],
protected route: string,
@@ -230,6 +232,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return this.parameters.clone()
}
public getDisplays(): Collection<{ stage: 'pre'|'handler'|'post', display: string }> {
return this.displays.clone()
}
public getHandlerDisplay(): Maybe<string> {
return this.displays.firstWhere('stage', '=', 'handler')?.display
}
/**
* Returns true if this route matches the given HTTP verb and request path.
* @param method
@@ -313,6 +323,7 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
this.preflight = other.preflight.clone()
this.postflight = other.postflight.clone()
this.aliases = other.aliases.clone()
this.displays = other.displays.clone()
}
public calls<TKey>(
@@ -322,6 +333,11 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
this.handler = constructable<TKey>(key)
.tap(inst => Function.prototype.bind.call(selector(inst), inst as any) as ((...params: THandlerParams) => TReturn))
this.displays.push({
stage: 'handler',
display: `${key.name}(${selector})`,
})
Route.registeredRoutes.push(this as unknown as Route<unknown, unknown[]>) // man this is stupid
return this as HandledRoute<TReturn, THandlerParams>
}
@@ -332,25 +348,45 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
this.handler = Pipeline.id<Container>()
.tap(() => handler)
this.displays.push({
stage: 'handler',
display: `(closure)`,
})
Route.registeredRoutes.push(this as unknown as Route<unknown, unknown[]>)
return this as HandledRoute<TReturn, THandlerParams>
}
public pre(middleware: Instantiable<Middleware>): this {
this.preflight.push(request => request.make<Middleware>(middleware, request).apply())
this.displays.push({
stage: 'pre',
display: `${middleware.name}`,
})
return this
}
public post(middleware: Instantiable<Middleware>): this {
this.postflight.push(request => request.make<Middleware>(middleware, request).apply())
this.displays.push({
stage: 'pre',
display: `${middleware.name}`,
})
return this
}
public input<T extends Validator<T>>(validator: ValidatorFactory<T>): Route<TReturn, PrefixTypeArray<Valid<T>, THandlerParams>> {
public input<T>(validator: ValidatorFactory<T>): Route<TReturn, PrefixTypeArray<Valid<T>, THandlerParams>> {
if ( !(validator instanceof Validator) ) {
validator = validator()
}
this.displays.push({
stage: 'pre',
display: `input(${validator.constructor.name})`,
})
return this.parameterMiddleware(validateMiddleware(validator))
}
@@ -372,6 +408,18 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return `${method.join('|')} -> ${this.route}`
}
/**
* Return a new Pipe of this collection.
*/
pipeTo<TOut>(pipeline: Pipeline<this, TOut>): TOut {
return pipeline.apply(this)
}
/** Build and apply a pipeline. */
pipe<TOut>(builder: (pipeline: Pipeline<this, this>) => Pipeline<this, TOut>): TOut {
return builder(Pipeline.id()).apply(this)
}
/** Prefix the route's path with the given prefix, normalizing `/` characters. */
private prepend(prefix: string): this {
if ( !prefix.endsWith('/') ) {