parent
70f06e47e5
commit
2c4a025cf8
6 changed files with 98 additions and 56 deletions
@ -1,8 +1,10 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import buildQueryFileDetail from './buildQueryFileDetail'; |
||||
|
||||
const getFileDetail = buildGetRequestHandler((request) => |
||||
buildQueryFileDetail({ filesUUID: [request.params.fileUUID] }), |
||||
const getFileDetail: RequestHandler = buildGetRequestHandler((request) => |
||||
buildQueryFileDetail({ fileUUIDs: [request.params.fileUUID] }), |
||||
); |
||||
|
||||
export default getFileDetail; |
||||
|
@ -0,0 +1,28 @@ |
||||
type MapToReturnType = { |
||||
boolean: boolean; |
||||
string: string; |
||||
'string[]': string[]; |
||||
}; |
||||
|
||||
type MapToReturnFunction = { |
||||
[ReturnTypeName in keyof MapToReturnType]: ( |
||||
qs: unknown, |
||||
) => MapToReturnType[ReturnTypeName]; |
||||
}; |
||||
|
||||
const MAP_TO_RETURN_FUNCTION: MapToReturnFunction = { |
||||
boolean: (qs) => qs !== undefined, |
||||
string: (qs) => String(qs), |
||||
'string[]': (qs) => |
||||
qs instanceof Array |
||||
? qs.map((element) => String(element)) |
||||
: String(qs).split(/[,;]/), |
||||
}; |
||||
|
||||
const sanitizeQS = <ReturnTypeName extends keyof MapToReturnType>( |
||||
qs: unknown, |
||||
{ returnType = 'string' }: { returnType?: ReturnTypeName | 'string' } = {}, |
||||
): MapToReturnType[ReturnTypeName] => |
||||
MAP_TO_RETURN_FUNCTION[returnType](qs) as MapToReturnType[ReturnTypeName]; |
||||
|
||||
export { sanitizeQS }; |
Loading…
Reference in new issue