Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Create unit test.
Brian Caoe6eb9a487e9e
diff --git a/src/lib/constants.ts b/src/lib/constants.tsindex 888c14843..a548826ad 100644--- a/src/lib/constants.ts+++ b/src/lib/constants.ts@@ -30,6 +30,8 @@ export const FILTER_RANGE = 'filter-range';export const FILTER_REFERRERS = 'filter-referrers';export const FILTER_PAGES = 'filter-pages';+export const UNIT_TYPES = ['year', 'month', 'hour', 'day'];+export const USER_FILTER_TYPES = {all: 'All',username: 'Username',diff --git a/src/lib/yup.ts b/src/lib/yup.tsindex 8b2eceee1..a2ea46d8e 100644--- a/src/lib/yup.ts+++ b/src/lib/yup.ts@@ -1,5 +1,6 @@import moment from 'moment';import * as yup from 'yup';+import { UNIT_TYPES } from './constants';export const DateRangeValidation = {startAt: yup.number().integer().required(),@@ -20,5 +21,11 @@ export function getFilterValidation(matchRegex) {export const TimezoneTest = yup.string().test('timezone',() => `Invalid timezone`,- value => !moment.tz.zone(value),+ value => !value || !moment.tz.zone(value),+);++export const UnitTypeTest = yup.string().test(+ 'unit',+ () => `Invalid unit`,+ value => !value || !UNIT_TYPES.includes(value),);diff --git a/src/pages/api/websites/[id]/events.ts b/src/pages/api/websites/[id]/events.tsindex 422200f80..32288aa52 100644--- a/src/pages/api/websites/[id]/events.ts+++ b/src/pages/api/websites/[id]/events.ts@@ -1,22 +1,19 @@-import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';import { canViewWebsite } from 'lib/auth';import { useAuth, useCors, useValidate } from 'lib/middleware';-import moment from 'moment-timezone';+import { parseDateRangeQuery } from 'lib/query';+import { NextApiRequestQueryBody, WebsiteMetric } from 'lib/types';+import { TimezoneTest, UnitTypeTest } from 'lib/yup';import { NextApiResponse } from 'next';-import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';+import { methodNotAllowed, ok, unauthorized } from 'next-basics';import { getEventMetrics } from 'queries';-import { parseDateRangeQuery } from 'lib/query';import * as yup from 'yup';-import { TimezoneTest } from 'lib/yup';--const unitTypes = ['year', 'month', 'hour', 'day'];export interface WebsiteEventsRequestQuery {id: string;startAt: string;endAt: string;- unit: string;- timezone: string;+ unit?: string;+ timezone?: string;url: string;}@@ -25,8 +22,8 @@ const schema = {id: yup.string().uuid().required(),startAt: yup.number().integer().required(),endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),- unit: yup.string().required(),- timezone: TimezoneTest.required(),+ unit: UnitTypeTest,+ timezone: TimezoneTest,url: yup.string(),}),};@@ -49,10 +46,6 @@ export default async (return unauthorized(res);}- if (!moment.tz.zone(timezone) || !unitTypes.includes(unit)) {- return badRequest(res);- }-const events = await getEventMetrics(websiteId, {startDate,endDate,diff --git a/src/pages/api/websites/[id]/pageviews.ts b/src/pages/api/websites/[id]/pageviews.tsindex 8c10ffeba..0f034cc2a 100644--- a/src/pages/api/websites/[id]/pageviews.ts+++ b/src/pages/api/websites/[id]/pageviews.ts@@ -23,14 +23,14 @@ export interface WebsitePageviewRequestQuery {city?: string;}-import { TimezoneTest } from 'lib/yup';+import { TimezoneTest, UnitTypeTest } from 'lib/yup';import * as yup from 'yup';const schema = {GET: yup.object().shape({id: yup.string().uuid().required(),startAt: yup.number().required(),endAt: yup.number().required(),- unit: yup.string(),+ unit: UnitTypeTest,timezone: TimezoneTest,url: yup.string(),referrer: yup.string(),