You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.2 KiB
103 lines
3.2 KiB
import {logger} from '../../service/logging/global.ts'
|
|
|
|
/**
|
|
* Type representing valid HTTP verbs.
|
|
*/
|
|
export type RouteVerb = 'get' | 'post' | 'patch' | 'delete' | 'head' | 'put' | 'connect' | 'options' | 'trace'
|
|
|
|
/**
|
|
* Type representing a route verb group from a router definition.
|
|
*/
|
|
export type RouteVerbGroup = { [key: string]: string | string[] }
|
|
|
|
/**
|
|
* Type representing a router definition.
|
|
*/
|
|
export interface RouterDefinition {
|
|
prefix?: string,
|
|
middleware?: string[],
|
|
get?: RouteVerbGroup,
|
|
post?: RouteVerbGroup,
|
|
patch?: RouteVerbGroup,
|
|
delete?: RouteVerbGroup,
|
|
head?: RouteVerbGroup,
|
|
put?: RouteVerbGroup,
|
|
connect?: RouteVerbGroup,
|
|
options?: RouteVerbGroup,
|
|
trace?: RouteVerbGroup,
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given value is a valid HTTP verb.
|
|
* @param something
|
|
* @return boolean
|
|
*/
|
|
export function isRouteVerb(something: any): something is RouteVerb {
|
|
const route_verbs = ['get', 'post', 'patch', 'delete', 'head', 'put', 'connect', 'options', 'trace']
|
|
return route_verbs.includes(something)
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given value is a valid route verb group definition.
|
|
* @param something
|
|
* @return boolean
|
|
*/
|
|
export function isRouteVerbGroup(something: any): something is RouteVerbGroup {
|
|
if ( !(typeof something === 'object' ) ) return false
|
|
for ( const key in something ) {
|
|
if ( !something.hasOwnProperty(key) ) continue
|
|
if ( typeof key !== 'string' ) {
|
|
logger.debug(`Route verb group key is not a string: ${key}`)
|
|
return false
|
|
}
|
|
if (
|
|
!(typeof something[key] === 'string')
|
|
&& !(Array.isArray(something[key]) && something[key].every((x: any) => typeof x === 'string'))
|
|
) {
|
|
logger.info(`Route verb group for key ${key} is not a string or array of strings.`)
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given value is a valid router definition.
|
|
* @param something
|
|
* @return boolean
|
|
*/
|
|
export function isRouterDefinition(something: any): something is RouterDefinition {
|
|
if ( !(typeof something === 'object') ) {
|
|
logger.debug('Routing definition is not an object.')
|
|
return false
|
|
}
|
|
for ( const key in something ) {
|
|
if (!something.hasOwnProperty(key)) continue
|
|
if ( key === 'prefix' ) {
|
|
if ( typeof something[key] !== 'string' ) {
|
|
logger.debug(`Invalid route prefix: ${something[key]}`)
|
|
return false
|
|
}
|
|
}
|
|
else if ( key === 'middleware' ) {
|
|
if ( !Array.isArray(something[key]) ) {
|
|
logger.debug('Middleware is not an array.')
|
|
return false
|
|
}
|
|
else if ( !something[key].every((x: any) => typeof x === 'string') ) {
|
|
logger.debug('Middleware array contains non-string values.')
|
|
return false
|
|
}
|
|
} else if ( isRouteVerb(key) ) {
|
|
if ( !isRouteVerbGroup(something[key as any]) ) {
|
|
logger.debug('Verb group value is not a valid route verb group.')
|
|
return false
|
|
}
|
|
} else {
|
|
logger.debug(`Invalid key: ${key}`)
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|