fix(striker-ui-api): allow replace modifier in sanitizeQS

main
Tsu-ba-me 2 years ago
parent bbb0fa503e
commit e38864e256
  1. 2
      striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts
  2. 2
      striker-ui-api/src/lib/request_handlers/file/getFile.ts
  3. 44
      striker-ui-api/src/lib/sanitizeQS.ts

@ -62,7 +62,7 @@ const getAnvil: RequestHandler = buildGetRequestHandler(
afterQueryReturn: anvilDetailAfterQueryReturn, afterQueryReturn: anvilDetailAfterQueryReturn,
} = buildQueryAnvilDetail({ } = buildQueryAnvilDetail({
anvilUUIDs: sanitizeQS(anvilUUIDs, { anvilUUIDs: sanitizeQS(anvilUUIDs, {
isForSQL: true, modifierType: 'sql',
returnType: 'string[]', returnType: 'string[]',
}), }),
isForProvisionServer: sanitizeQS(isForProvisionServer, { isForProvisionServer: sanitizeQS(isForProvisionServer, {

@ -20,7 +20,7 @@ const getFile: RequestHandler = buildGetRequestHandler((request) => {
if (fileUUIDs) { if (fileUUIDs) {
query = buildQueryFileDetail({ query = buildQueryFileDetail({
fileUUIDs: sanitizeQS(fileUUIDs, { fileUUIDs: sanitizeQS(fileUUIDs, {
isForSQL: true, modifierType: 'sql',
returnType: 'string[]', returnType: 'string[]',
}), }),
}); });

@ -1,3 +1,4 @@
import call from './call';
import { sanitizeSQLParam } from './sanitizeSQLParam'; import { sanitizeSQLParam } from './sanitizeSQLParam';
type MapToReturnType = { type MapToReturnType = {
@ -10,27 +11,39 @@ type MapToReturnType = {
type MapToReturnFunction = { type MapToReturnFunction = {
[ReturnTypeName in keyof MapToReturnType]: ( [ReturnTypeName in keyof MapToReturnType]: (
qs: unknown, qs: unknown,
modSQL: (value: string) => string, modifier: (value: unknown) => string,
) => MapToReturnType[ReturnTypeName]; ) => 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 = { const MAP_TO_RETURN_FUNCTION: MapToReturnFunction = {
boolean: (qs) => qs !== undefined, boolean: (qs) => qs !== undefined,
number: (qs) => parseFloat(String(qs)) || 0, number: (qs) => parseFloat(String(qs)) || 0,
string: (qs, modSQL) => (qs ? modSQL(String(qs)) : ''), string: (qs, mod) => (qs ? mod(qs) : ''),
'string[]': (qs, modSQL) => { 'string[]': (qs, mod) => {
let result: string[] = []; let result: string[] = [];
if (qs instanceof Array) { if (qs instanceof Array) {
result = qs.reduce<string[]>((reduceContainer, element) => { result = qs.reduce<string[]>((reduceContainer, element) => {
if (element) { if (element) {
reduceContainer.push(modSQL(String(element))); reduceContainer.push(mod(element));
} }
return reduceContainer; return reduceContainer;
}, []); }, []);
} else if (qs) { } else if (qs) {
result = modSQL(String(qs)).split(/[,;]/); result = mod(qs).split(/[,;]/);
} }
return result; return result;
@ -40,11 +53,20 @@ const MAP_TO_RETURN_FUNCTION: MapToReturnFunction = {
export const sanitizeQS = <ReturnTypeName extends keyof MapToReturnType>( export const sanitizeQS = <ReturnTypeName extends keyof MapToReturnType>(
qs: unknown, qs: unknown,
{ {
isForSQL = false, modifierType = 'none',
modifier = MAP_TO_MODIFIER_FUNCTION[modifierType],
returnType = 'string', returnType = 'string',
}: { isForSQL?: boolean; returnType?: ReturnTypeName | 'string' } = {}, }: {
modifier?: ModifierFunction;
modifierType?: keyof MapToModifierFunction;
returnType?: ReturnTypeName | 'string';
} = {},
): MapToReturnType[ReturnTypeName] => ): MapToReturnType[ReturnTypeName] =>
MAP_TO_RETURN_FUNCTION[returnType]( MAP_TO_RETURN_FUNCTION[returnType](qs, (value: unknown) => {
qs, const input = String(value);
isForSQL ? sanitizeSQLParam : (value: string) => value,
) as MapToReturnType[ReturnTypeName]; return call<string>(modifier, {
notCallableReturn: input,
parameters: [input],
});
}) as MapToReturnType[ReturnTypeName];

Loading…
Cancel
Save