From e38864e2563a06b542b182bfd9288916ed450451 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 20 Oct 2022 21:53:24 -0400 Subject: [PATCH] fix(striker-ui-api): allow replace modifier in sanitizeQS --- .../lib/request_handlers/anvil/getAnvil.ts | 2 +- .../src/lib/request_handlers/file/getFile.ts | 2 +- striker-ui-api/src/lib/sanitizeQS.ts | 44 ++++++++++++++----- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts b/striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts index 73475fc2..ba2d2f4f 100644 --- a/striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts +++ b/striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts @@ -62,7 +62,7 @@ const getAnvil: RequestHandler = buildGetRequestHandler( afterQueryReturn: anvilDetailAfterQueryReturn, } = buildQueryAnvilDetail({ anvilUUIDs: sanitizeQS(anvilUUIDs, { - isForSQL: true, + modifierType: 'sql', returnType: 'string[]', }), isForProvisionServer: sanitizeQS(isForProvisionServer, { diff --git a/striker-ui-api/src/lib/request_handlers/file/getFile.ts b/striker-ui-api/src/lib/request_handlers/file/getFile.ts index b1eae8fd..d6e8dc62 100644 --- a/striker-ui-api/src/lib/request_handlers/file/getFile.ts +++ b/striker-ui-api/src/lib/request_handlers/file/getFile.ts @@ -20,7 +20,7 @@ const getFile: RequestHandler = buildGetRequestHandler((request) => { if (fileUUIDs) { query = buildQueryFileDetail({ fileUUIDs: sanitizeQS(fileUUIDs, { - isForSQL: true, + modifierType: 'sql', returnType: 'string[]', }), }); diff --git a/striker-ui-api/src/lib/sanitizeQS.ts b/striker-ui-api/src/lib/sanitizeQS.ts index 1234b5b8..4eb7678d 100644 --- a/striker-ui-api/src/lib/sanitizeQS.ts +++ b/striker-ui-api/src/lib/sanitizeQS.ts @@ -1,3 +1,4 @@ +import call from './call'; import { sanitizeSQLParam } from './sanitizeSQLParam'; type MapToReturnType = { @@ -10,27 +11,39 @@ type MapToReturnType = { type MapToReturnFunction = { [ReturnTypeName in keyof MapToReturnType]: ( qs: unknown, - modSQL: (value: string) => string, + modifier: (value: unknown) => string, ) => MapToReturnType[ReturnTypeName]; }; +type ModifierFunction = (value: string) => string; + +type MapToModifierFunction = { + none: undefined; + sql: ModifierFunction; +}; + +const MAP_TO_MODIFIER_FUNCTION: MapToModifierFunction = { + none: undefined, + sql: sanitizeSQLParam, +}; + const MAP_TO_RETURN_FUNCTION: MapToReturnFunction = { boolean: (qs) => qs !== undefined, number: (qs) => parseFloat(String(qs)) || 0, - string: (qs, modSQL) => (qs ? modSQL(String(qs)) : ''), - 'string[]': (qs, modSQL) => { + string: (qs, mod) => (qs ? mod(qs) : ''), + 'string[]': (qs, mod) => { let result: string[] = []; if (qs instanceof Array) { result = qs.reduce((reduceContainer, element) => { if (element) { - reduceContainer.push(modSQL(String(element))); + reduceContainer.push(mod(element)); } return reduceContainer; }, []); } else if (qs) { - result = modSQL(String(qs)).split(/[,;]/); + result = mod(qs).split(/[,;]/); } return result; @@ -40,11 +53,20 @@ const MAP_TO_RETURN_FUNCTION: MapToReturnFunction = { export const sanitizeQS = ( qs: unknown, { - isForSQL = false, + modifierType = 'none', + modifier = MAP_TO_MODIFIER_FUNCTION[modifierType], returnType = 'string', - }: { isForSQL?: boolean; returnType?: ReturnTypeName | 'string' } = {}, + }: { + modifier?: ModifierFunction; + modifierType?: keyof MapToModifierFunction; + returnType?: ReturnTypeName | 'string'; + } = {}, ): MapToReturnType[ReturnTypeName] => - MAP_TO_RETURN_FUNCTION[returnType]( - qs, - isForSQL ? sanitizeSQLParam : (value: string) => value, - ) as MapToReturnType[ReturnTypeName]; + MAP_TO_RETURN_FUNCTION[returnType](qs, (value: unknown) => { + const input = String(value); + + return call(modifier, { + notCallableReturn: input, + parameters: [input], + }); + }) as MapToReturnType[ReturnTypeName];