From f3e733dea32e197261e8e728ad9a1d87dbf784ae Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 31 Jan 2025 23:46:23 -0800 Subject: [PATCH] Updated report schemas. Removed yup. --- package.json | 1 - src/app/api/reports/funnel/route.ts | 9 ++--- src/app/api/reports/goals/route.ts | 7 ++-- src/app/api/reports/insights/route.ts | 7 ++-- src/app/api/reports/journey/route.ts | 7 ++-- src/app/api/reports/retention/route.ts | 8 ++--- src/app/api/reports/revenue/route.ts | 8 ++--- src/app/api/reports/utm/route.ts | 9 ++--- src/lib/detect.ts | 2 +- src/lib/schema.ts | 47 ++++++++++++-------------- src/lib/types.ts | 22 ------------ src/lib/yup.ts | 18 ---------- yarn.lock | 38 --------------------- 13 files changed, 37 insertions(+), 146 deletions(-) delete mode 100644 src/lib/yup.ts diff --git a/package.json b/package.json index 1b81c514..afab156b 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,6 @@ "serialize-error": "^12.0.0", "thenby": "^1.3.4", "uuid": "^9.0.0", - "yup": "^0.32.11", "zod": "^3.24.1", "zustand": "^4.5.5" }, diff --git a/src/app/api/reports/funnel/route.ts b/src/app/api/reports/funnel/route.ts index 9a0cfd65..23a05014 100644 --- a/src/app/api/reports/funnel/route.ts +++ b/src/app/api/reports/funnel/route.ts @@ -3,10 +3,12 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getFunnel } from 'queries'; +import { reportParms } from 'lib/schema'; export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), + ...reportParms, + window: z.number().positive(), steps: z .array( z.object({ @@ -15,11 +17,6 @@ export async function POST(request: Request) { }), ) .min(2), - window: z.number().positive(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - }), }); const { auth, body, error } = await parseRequest(request, schema); diff --git a/src/app/api/reports/goals/route.ts b/src/app/api/reports/goals/route.ts index ee102bc6..cad774bd 100644 --- a/src/app/api/reports/goals/route.ts +++ b/src/app/api/reports/goals/route.ts @@ -3,14 +3,11 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getGoals } from 'queries/analytics/reports/getGoals'; +import { reportParms } from 'lib/schema'; export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - }), + ...reportParms, goals: z .array( z.object({ diff --git a/src/app/api/reports/insights/route.ts b/src/app/api/reports/insights/route.ts index f622de6a..a7ed1c15 100644 --- a/src/app/api/reports/insights/route.ts +++ b/src/app/api/reports/insights/route.ts @@ -3,6 +3,7 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getInsights } from 'queries'; +import { reportParms } from 'lib/schema'; function convertFilters(filters: any[]) { return filters.reduce((obj, filter) => { @@ -14,11 +15,7 @@ function convertFilters(filters: any[]) { export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.coerce.date(), - endDate: z.coerce.date(), - }), + ...reportParms, fields: z .array( z.object({ diff --git a/src/app/api/reports/journey/route.ts b/src/app/api/reports/journey/route.ts index 50b64952..f5121fdc 100644 --- a/src/app/api/reports/journey/route.ts +++ b/src/app/api/reports/journey/route.ts @@ -3,14 +3,11 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getJourney } from 'queries'; +import { reportParms } from 'lib/schema'; export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - }), + ...reportParms, steps: z.number().min(3).max(7), startStep: z.string(), endStep: z.string(), diff --git a/src/app/api/reports/retention/route.ts b/src/app/api/reports/retention/route.ts index 794ebbe4..8e854fa9 100644 --- a/src/app/api/reports/retention/route.ts +++ b/src/app/api/reports/retention/route.ts @@ -3,15 +3,11 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getRetention } from 'queries'; -import { timezoneParam } from 'lib/schema'; +import { reportParms, timezoneParam } from 'lib/schema'; export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - }), + ...reportParms, timezone: timezoneParam, }); diff --git a/src/app/api/reports/revenue/route.ts b/src/app/api/reports/revenue/route.ts index 0f8f7d55..d3c21b9c 100644 --- a/src/app/api/reports/revenue/route.ts +++ b/src/app/api/reports/revenue/route.ts @@ -2,17 +2,13 @@ import { z } from 'zod'; import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; -import { timezoneParam, unitParam } from 'lib/schema'; +import { reportParms, timezoneParam, unitParam } from 'lib/schema'; import { getRevenue } from 'queries/analytics/reports/getRevenue'; import { getRevenueValues } from 'queries/analytics/reports/getRevenueValues'; export async function GET(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - }), + ...reportParms, }); const { auth, query, error } = await parseRequest(request, schema); diff --git a/src/app/api/reports/utm/route.ts b/src/app/api/reports/utm/route.ts index 0af8b419..2412134d 100644 --- a/src/app/api/reports/utm/route.ts +++ b/src/app/api/reports/utm/route.ts @@ -3,16 +3,11 @@ import { canViewWebsite } from 'lib/auth'; import { unauthorized, json } from 'lib/response'; import { parseRequest } from 'lib/request'; import { getUTM } from 'queries'; -import { timezoneParam } from 'lib/schema'; +import { reportParms } from 'lib/schema'; export async function POST(request: Request) { const schema = z.object({ - websiteId: z.string().uuid(), - dateRange: z.object({ - startDate: z.date(), - endDate: z.date(), - timezone: timezoneParam, - }), + ...reportParms, }); const { auth, body, error } = await parseRequest(request, schema); diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 83504095..89e73a0b 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -148,7 +148,7 @@ export async function getClientInfo(request: Request, payload: Record; - POST?: yup.ObjectSchema; - PUT?: yup.ObjectSchema; - DELETE?: yup.ObjectSchema; -} - -export interface NextApiRequestQueryBody extends NextApiRequest { - auth?: Auth; - query: TQuery & { [key: string]: string | string[] }; - body: TBody; - headers: any; - yup: YupRequest; -} - -export interface NextApiRequestAuth extends NextApiRequest { - auth?: Auth; - headers: any; -} - export interface User { id: string; username: string; diff --git a/src/lib/yup.ts b/src/lib/yup.ts deleted file mode 100644 index d2652eda..00000000 --- a/src/lib/yup.ts +++ /dev/null @@ -1,18 +0,0 @@ -import * as yup from 'yup'; -import { isValidTimezone } from 'lib/date'; -import { UNIT_TYPES } from './constants'; - -export const TimezoneTest = yup - .string() - .default('UTC') - .test( - 'timezone', - () => `Invalid timezone`, - value => isValidTimezone(value), - ); - -export const UnitTypeTest = yup.string().test( - 'unit', - () => `Invalid unit`, - value => UNIT_TYPES.includes(value), -); diff --git a/yarn.lock b/yarn.lock index fb739910..91546c34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2968,11 +2968,6 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== -"@types/lodash@^4.14.175": - version "4.14.200" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.200.tgz#435b6035c7eba9cdf1e039af8212c9e9281e7149" - integrity sha512-YI/M/4HRImtNf3pJgbF+W6FrXovqj+T+/HpENLTooK9PnkacBsDpeP3IpHab40CClUfhNmdM2WTNP2sa2dni5Q== - "@types/minimatch@*": version "5.1.2" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-5.1.2.tgz#07508b45797cb81ec3f273011b054cd0755eddca" @@ -7610,11 +7605,6 @@ locate-path@^6.0.0: dependencies: p-locate "^5.0.0" -lodash-es@^4.17.21: - version "4.17.21" - resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee" - integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw== - lodash.camelcase@^4.3.0: version "4.3.0" resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6" @@ -8042,11 +8032,6 @@ ms@^2.1.1, ms@^2.1.3: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== -nanoclone@^0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/nanoclone/-/nanoclone-0.2.1.tgz#dd4090f8f1a110d26bb32c49ed2f5b9235209ed4" - integrity sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA== - nanoid@^3.3.6, nanoid@^3.3.7: version "3.3.8" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.8.tgz#b1be3030bee36aaff18bacb375e5cce521684baf" @@ -9164,11 +9149,6 @@ prop-types@^15.7.2, prop-types@^15.8.1: object-assign "^4.1.1" react-is "^16.13.1" -property-expr@^2.0.4: - version "2.0.6" - resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.6.tgz#f77bc00d5928a6c748414ad12882e83f24aec1e8" - integrity sha512-SVtmxhRE/CGkn3eZY1T6pC8Nln6Fr/lu1mKSgRud0eC73whjGfoAogbn78LkD8aFL0zz3bAFerKSnOl7NlErBA== - proxy-from-env@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.0.0.tgz#33c50398f70ea7eb96d21f7b817630a55791c7ee" @@ -10592,11 +10572,6 @@ topojson-client@^3.1.0: dependencies: commander "2" -toposort@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330" - integrity sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg== - tough-cookie@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-5.0.0.tgz#6b6518e2b5c070cf742d872ee0f4f92d69eac1af" @@ -11197,19 +11172,6 @@ yocto-queue@^0.1.0: resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== -yup@^0.32.11: - version "0.32.11" - resolved "https://registry.yarnpkg.com/yup/-/yup-0.32.11.tgz#d67fb83eefa4698607982e63f7ca4c5ed3cf18c5" - integrity sha512-Z2Fe1bn+eLstG8DRR6FTavGD+MeAwyfmouhHsIUgaADz8jvFKbO/fXc2trJKZg+5EBjh4gGm3iU/t3onKlXHIg== - dependencies: - "@babel/runtime" "^7.15.4" - "@types/lodash" "^4.14.175" - lodash "^4.17.21" - lodash-es "^4.17.21" - nanoclone "^0.2.1" - property-expr "^2.0.4" - toposort "^2.0.2" - zod@^3.24.1: version "3.24.1" resolved "https://registry.yarnpkg.com/zod/-/zod-3.24.1.tgz#27445c912738c8ad1e9de1bea0359fa44d9d35ee"