Merge branch 'dev' into analytics

This commit is contained in:
Mike Cao 2023-07-29 00:00:42 -07:00
commit f68cd970f6

View File

@ -54,7 +54,11 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
const { type, payload } = getJsonBody<CollectRequestBody>(req);
validateBody(res, { type, payload });
const error = validateBody({ type, payload });
if (error) {
return badRequest(res, error);
}
if (await hasBlockedIp(req)) {
return forbidden(res);
@ -114,17 +118,19 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
return send(res, token);
};
function validateBody(res: NextApiResponse, { type, payload }: CollectRequestBody) {
const { data } = payload || {};
// Validate type
if (type !== COLLECTION_TYPE.event && type !== COLLECTION_TYPE.identify) {
return badRequest(res, 'Wrong payload type.');
function validateBody({ type, payload }: CollectRequestBody) {
if (!type || !payload) {
return 'Invalid payload.';
}
// Validate eventData is JSON
if (type !== COLLECTION_TYPE.event && type !== COLLECTION_TYPE.identify) {
return 'Wrong payload type.';
}
const { data } = payload;
if (data && !(typeof data === 'object' && !Array.isArray(data))) {
return badRequest(res, 'Invalid event data.');
return 'Invalid event data.';
}
}