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. 91
      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';
const buildQueryAnvilDetail = ({
anvilsUUID,
anvilUUIDs,
isForProvisionServer,
}: {
anvilsUUID?: string[] | '*';
anvilUUIDs?: string[] | '*';
isForProvisionServer?: boolean;
}) => {
const condAnvilsUUID = join(anvilsUUID, {
const condAnvilsUUID = join(anvilUUIDs, {
beforeReturn: (toReturn) =>
toReturn ? `WHERE anv.anvil_uuid IN (${toReturn})` : '',
elementWrapper: "'",

@ -1,10 +1,14 @@
import { RequestHandler } from 'express';
import buildGetRequestHandler from '../buildGetRequestHandler';
import buildQueryAnvilDetail from './buildQueryAnvilDetail';
import { sanitizeQS } from '../../sanitizeQS';
const getAnvil = buildGetRequestHandler((request, buildQueryOptions) => {
const { anvilsUUID, isForProvisionServer } = request.body;
const getAnvil: RequestHandler = buildGetRequestHandler(
(request, buildQueryOptions) => {
const { anvilUUIDs, isForProvisionServer } = request.query;
let query = `
let query = `
SELECT
anv.anvil_name,
anv.anvil_uuid,
@ -19,56 +23,59 @@ const getAnvil = buildGetRequestHandler((request, buildQueryOptions) => {
)
ORDER BY anv.anvil_uuid;`;
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = (queryStdout) => {
let results = queryStdout;
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = (queryStdout) => {
let results = queryStdout;
if (queryStdout instanceof Array) {
let rowStage: AnvilOverview | undefined;
if (queryStdout instanceof Array) {
let rowStage: AnvilOverview | undefined;
results = queryStdout.reduce<AnvilOverview[]>(
(reducedRows, [anvilName, anvilUUID, hostName, hostUUID]) => {
if (!rowStage || anvilUUID !== rowStage.anvilUUID) {
{
rowStage = {
anvilName,
anvilUUID,
hosts: [],
};
results = queryStdout.reduce<AnvilOverview[]>(
(reducedRows, [anvilName, anvilUUID, hostName, hostUUID]) => {
if (!rowStage || anvilUUID !== rowStage.anvilUUID) {
{
rowStage = {
anvilName,
anvilUUID,
hosts: [],
};
reducedRows.push(rowStage);
reducedRows.push(rowStage);
}
}
}
rowStage.hosts.push({ hostName, hostUUID });
rowStage.hosts.push({ hostName, hostUUID });
return reducedRows;
},
[],
);
}
return reducedRows;
},
[],
);
}
return results;
};
}
return results;
};
}
if (anvilsUUID) {
const {
query: anvilDetailQuery,
afterQueryReturn: anvilDetailAfterQueryReturn,
} = buildQueryAnvilDetail({
anvilsUUID,
isForProvisionServer,
});
if (anvilUUIDs) {
const {
query: anvilDetailQuery,
afterQueryReturn: anvilDetailAfterQueryReturn,
} = buildQueryAnvilDetail({
anvilUUIDs: sanitizeQS(anvilUUIDs, { returnType: 'string[]' }),
isForProvisionServer: sanitizeQS(isForProvisionServer, {
returnType: 'boolean',
}),
});
query = anvilDetailQuery;
query = anvilDetailQuery;
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = anvilDetailAfterQueryReturn;
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = anvilDetailAfterQueryReturn;
}
}
}
return query;
});
return query;
},
);
export default getAnvil;

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

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

@ -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…
Cancel
Save