fix(striker-ui-api): use query strings instead of body for GETs

main
Tsu-ba-me 3 years ago
parent 70f06e47e5
commit 2c4a025cf8
  1. 6
      striker-ui-api/src/lib/request_handlers/anvil/buildQueryAnvilDetail.ts
  2. 19
      striker-ui-api/src/lib/request_handlers/anvil/getAnvil.ts
  3. 10
      striker-ui-api/src/lib/request_handlers/file/buildQueryFileDetail.ts
  4. 13
      striker-ui-api/src/lib/request_handlers/file/getFile.ts
  5. 6
      striker-ui-api/src/lib/request_handlers/file/getFileDetail.ts
  6. 28
      striker-ui-api/src/lib/sanitizeQS.ts

@ -6,13 +6,13 @@ import SERVER_PATHS from '../../consts/SERVER_PATHS';
import join from '../../join'; import join from '../../join';
const buildQueryAnvilDetail = ({ const buildQueryAnvilDetail = ({
anvilsUUID, anvilUUIDs,
isForProvisionServer, isForProvisionServer,
}: { }: {
anvilsUUID?: string[] | '*'; anvilUUIDs?: string[] | '*';
isForProvisionServer?: boolean; isForProvisionServer?: boolean;
}) => { }) => {
const condAnvilsUUID = join(anvilsUUID, { const condAnvilsUUID = join(anvilUUIDs, {
beforeReturn: (toReturn) => beforeReturn: (toReturn) =>
toReturn ? `WHERE anv.anvil_uuid IN (${toReturn})` : '', toReturn ? `WHERE anv.anvil_uuid IN (${toReturn})` : '',
elementWrapper: "'", elementWrapper: "'",

@ -1,8 +1,12 @@
import { RequestHandler } from 'express';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryAnvilDetail from './buildQueryAnvilDetail'; import buildQueryAnvilDetail from './buildQueryAnvilDetail';
import { sanitizeQS } from '../../sanitizeQS';
const getAnvil = buildGetRequestHandler((request, buildQueryOptions) => { const getAnvil: RequestHandler = buildGetRequestHandler(
const { anvilsUUID, isForProvisionServer } = request.body; (request, buildQueryOptions) => {
const { anvilUUIDs, isForProvisionServer } = request.query;
let query = ` let query = `
SELECT SELECT
@ -52,13 +56,15 @@ const getAnvil = buildGetRequestHandler((request, buildQueryOptions) => {
}; };
} }
if (anvilsUUID) { if (anvilUUIDs) {
const { const {
query: anvilDetailQuery, query: anvilDetailQuery,
afterQueryReturn: anvilDetailAfterQueryReturn, afterQueryReturn: anvilDetailAfterQueryReturn,
} = buildQueryAnvilDetail({ } = buildQueryAnvilDetail({
anvilsUUID, anvilUUIDs: sanitizeQS(anvilUUIDs, { returnType: 'string[]' }),
isForProvisionServer, isForProvisionServer: sanitizeQS(isForProvisionServer, {
returnType: 'boolean',
}),
}); });
query = anvilDetailQuery; query = anvilDetailQuery;
@ -69,6 +75,7 @@ const getAnvil = buildGetRequestHandler((request, buildQueryOptions) => {
} }
return query; return query;
}); },
);
export default getAnvil; export default getAnvil;

@ -1,18 +1,18 @@
import join from '../../join'; import join from '../../join';
const buildQueryFileDetail = ({ const buildQueryFileDetail = ({
filesUUID, fileUUIDs,
}: { }: {
filesUUID?: string[] | '*'; fileUUIDs?: string[] | '*';
}) => { }) => {
const condFilesUUID = join(filesUUID, { const condFileUUIDs = join(fileUUIDs, {
beforeReturn: (toReturn) => beforeReturn: (toReturn) =>
toReturn ? `AND fil.file_uuid IN (${toReturn})` : '', toReturn ? `AND fil.file_uuid IN (${toReturn})` : '',
elementWrapper: "'", elementWrapper: "'",
separator: ', ', separator: ', ',
}); });
console.log(`condFilesUUID=[${condFilesUUID}]`); console.log(`condFilesUUID=[${condFileUUIDs}]`);
return ` return `
SELECT SELECT
@ -32,7 +32,7 @@ const buildQueryFileDetail = ({
JOIN anvils AS anv JOIN anvils AS anv
ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid
WHERE fil.file_type != 'DELETED' WHERE fil.file_type != 'DELETED'
${condFilesUUID};`; ${condFileUUIDs};`;
}; };
export default buildQueryFileDetail; export default buildQueryFileDetail;

@ -1,8 +1,11 @@
import { RequestHandler } from 'express';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryFileDetail from './buildQueryFileDetail'; import buildQueryFileDetail from './buildQueryFileDetail';
import { sanitizeQS } from '../../sanitizeQS';
const getFile = buildGetRequestHandler((request) => { const getFile: RequestHandler = buildGetRequestHandler((request) => {
const { filesUUID } = request.body; const { fileUUIDs } = request.query;
let query = ` let query = `
SELECT SELECT
@ -14,8 +17,10 @@ const getFile = buildGetRequestHandler((request) => {
FROM files FROM files
WHERE file_type != 'DELETED';`; WHERE file_type != 'DELETED';`;
if (filesUUID) { if (fileUUIDs) {
query = buildQueryFileDetail({ filesUUID }); query = buildQueryFileDetail({
fileUUIDs: sanitizeQS(fileUUIDs, { returnType: 'string[]' }),
});
} }
return query; return query;

@ -1,8 +1,10 @@
import { RequestHandler } from 'express';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryFileDetail from './buildQueryFileDetail'; import buildQueryFileDetail from './buildQueryFileDetail';
const getFileDetail = buildGetRequestHandler((request) => const getFileDetail: RequestHandler = buildGetRequestHandler((request) =>
buildQueryFileDetail({ filesUUID: [request.params.fileUUID] }), buildQueryFileDetail({ fileUUIDs: [request.params.fileUUID] }),
); );
export default getFileDetail; 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…
Cancel
Save