Make new routing system the default
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-01-19 13:24:59 -06:00
parent 8cf19792a6
commit dc16dfdb81
17 changed files with 298 additions and 535 deletions

View File

@@ -8,6 +8,8 @@ import {Logging} from '../service/Logging'
/** Type tag for a validated runtime type. */
export type Valid<T> = TypeTag<'@extollo/lib:Valid'> & T
export type ValidatorFactory<T extends Validator<T>> = T | (() => T)
/**
* Error thrown if the schema for a validator cannot be located.
*/

View File

@@ -0,0 +1,22 @@
import {Request} from '../http/lifecycle/Request'
import {Validator} from './Validator'
import {ZodError} from 'zod'
import {HTTPStatus, left, right} from '../util'
import {json} from '../http/response/JSONResponseFactory'
export function validateMiddleware<T>(validator: Validator<T>) {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
return (request: Request) => {
try {
const data = validator.parse(request.parsedInput)
return right(data)
} catch (e) {
if ( e instanceof ZodError ) {
// FIXME render this better
return left(json(e.formErrors).status(HTTPStatus.BAD_REQUEST))
}
throw e
}
}
}