The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add tests for analytics endpoints
Kiran Kbb4e990b623d
diff --git a/apps/web/lib/openapi/analytics/browser.ts b/apps/web/lib/openapi/analytics/browser.tsindex 16762cb95b..a53bd3f597 100644--- a/apps/web/lib/openapi/analytics/browser.ts+++ b/apps/web/lib/openapi/analytics/browser.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,14 +21,7 @@ export const getBrowserAnalytics: ZodOpenApiOperationObject = {description: "The top browsers by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- browser: z.string().describe("The name of the browser"),- clicks: z- .number()- .describe("The number of clicks from this browser"),- }),- ),+ schema: z.array(analyticsResponseSchema["browser"]),},},},diff --git a/apps/web/lib/openapi/analytics/city.ts b/apps/web/lib/openapi/analytics/city.tsindex 3575c6272f..7f9091f2d5 100644--- a/apps/web/lib/openapi/analytics/city.ts+++ b/apps/web/lib/openapi/analytics/city.ts@@ -1,7 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";-import { COUNTRY_CODES } from "@dub/utils";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -19,19 +21,7 @@ export const getCityAnalytics: ZodOpenApiOperationObject = {description: "The top cities by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- city: z.string().describe("The name of the city"),- country: z- .enum(COUNTRY_CODES)- .describe(- "The 2-letter country code of the city: https://d.to/geo",- ),- clicks: z- .number()- .describe("The number of clicks from this city"),- }),- ),+ schema: z.array(analyticsResponseSchema["city"]),},},},diff --git a/apps/web/lib/openapi/analytics/country.ts b/apps/web/lib/openapi/analytics/country.tsindex d466b46cb9..098206f0ff 100644--- a/apps/web/lib/openapi/analytics/country.ts+++ b/apps/web/lib/openapi/analytics/country.ts@@ -1,7 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";-import { COUNTRY_CODES } from "@dub/utils";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -19,16 +21,7 @@ export const getCountryAnalytics: ZodOpenApiOperationObject = {description: "The top countries by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- country: z- .enum(COUNTRY_CODES)- .describe("The 2-letter country code: https://d.to/geo"),- clicks: z- .number()- .describe("The number of clicks from this country"),- }),- ),+ schema: z.array(analyticsResponseSchema["country"]),},},},diff --git a/apps/web/lib/openapi/analytics/device.ts b/apps/web/lib/openapi/analytics/device.tsindex 185a788fa8..7fec3d1bc7 100644--- a/apps/web/lib/openapi/analytics/device.ts+++ b/apps/web/lib/openapi/analytics/device.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,14 +21,7 @@ export const getDeviceAnalytics: ZodOpenApiOperationObject = {description: "The top devices by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- device: z.string().describe("The name of the device"),- clicks: z- .number()- .describe("The number of clicks from this device"),- }),- ),+ schema: z.array(analyticsResponseSchema["device"]),},},},diff --git a/apps/web/lib/openapi/analytics/os.ts b/apps/web/lib/openapi/analytics/os.tsindex 25b8f3e225..260ac870a2 100644--- a/apps/web/lib/openapi/analytics/os.ts+++ b/apps/web/lib/openapi/analytics/os.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,12 +21,7 @@ export const getOSAnalytics: ZodOpenApiOperationObject = {description: "The top OS by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- os: z.string().describe("The name of the OS"),- clicks: z.number().describe("The number of clicks from this OS"),- }),- ),+ schema: z.array(analyticsResponseSchema["os"]),},},},diff --git a/apps/web/lib/openapi/analytics/referer.ts b/apps/web/lib/openapi/analytics/referer.tsindex 7cbde332f9..ba3e001168 100644--- a/apps/web/lib/openapi/analytics/referer.ts+++ b/apps/web/lib/openapi/analytics/referer.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,18 +21,7 @@ export const getRefererAnalytics: ZodOpenApiOperationObject = {description: "The top referers by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- referer: z- .string()- .describe(- "The name of the referer. If unknown, this will be `(direct)`",- ),- clicks: z- .number()- .describe("The number of clicks from this referer"),- }),- ),+ schema: z.array(analyticsResponseSchema["referer"]),},},},diff --git a/apps/web/lib/openapi/analytics/timeseries.ts b/apps/web/lib/openapi/analytics/timeseries.tsindex 18c17fe500..28790ce9dd 100644--- a/apps/web/lib/openapi/analytics/timeseries.ts+++ b/apps/web/lib/openapi/analytics/timeseries.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,16 +21,7 @@ export const getTimeseriesAnalytics: ZodOpenApiOperationObject = {description: "The number of clicks over a period of time",content: {"application/json": {- schema: z.array(- z.object({- start: z- .string()- .describe("The starting timestamp of the interval"),- clicks: z- .number()- .describe("The number of clicks in the interval"),- }),- ),+ schema: z.array(analyticsResponseSchema["timeseries"]),},},},diff --git a/apps/web/lib/openapi/analytics/top-links.ts b/apps/web/lib/openapi/analytics/top-links.tsindex 586a1abc1d..00fb4950b8 100644--- a/apps/web/lib/openapi/analytics/top-links.ts+++ b/apps/web/lib/openapi/analytics/top-links.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,14 +21,7 @@ export const getTopLinks: ZodOpenApiOperationObject = {description: "The top links by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- link: z.string().describe("The unique ID of the short link"),- clicks: z- .number()- .describe("The number of clicks from this link"),- }),- ),+ schema: z.array(analyticsResponseSchema["topLinks"]),},},},diff --git a/apps/web/lib/openapi/analytics/top-urls.ts b/apps/web/lib/openapi/analytics/top-urls.tsindex 979c43f368..0b1923e41c 100644--- a/apps/web/lib/openapi/analytics/top-urls.ts+++ b/apps/web/lib/openapi/analytics/top-urls.ts@@ -1,6 +1,9 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";-import { getAnalyticsQuerySchema } from "@/lib/zod/schemas";+import {+ analyticsResponseSchema,+ getAnalyticsQuerySchema,+} from "@/lib/zod/schemas";import { ZodOpenApiOperationObject } from "zod-openapi";import { workspaceParamsSchema } from "../request";@@ -18,12 +21,7 @@ export const getTopURLs: ZodOpenApiOperationObject = {description: "The top URLs by number of clicks",content: {"application/json": {- schema: z.array(- z.object({- url: z.string().describe("The destination URL"),- clicks: z.number().describe("The number of clicks from this URL"),- }),- ),+ schema: z.array(analyticsResponseSchema["topUrls"]),},},},diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.tsindex 26554fa91e..fbf7f0b8cd 100644--- a/apps/web/lib/zod/schemas/analytics.ts+++ b/apps/web/lib/zod/schemas/analytics.ts@@ -68,3 +68,58 @@ export const analyticsEndpointSchema = z.object({},}),});++// Analytics response schemas+export const analyticsResponseSchema = {+ timeseries: z.object({+ start: z.string().describe("The starting timestamp of the interval"),+ clicks: z.number().describe("The number of clicks in the interval"),+ }),++ country: z.object({+ country: z+ .enum(COUNTRY_CODES)+ .describe("The 2-letter country code: https://d.to/geo"),+ clicks: z.number().describe("The number of clicks from this country"),+ }),++ city: z.object({+ city: z.string().describe("The name of the city"),+ country: z+ .enum(COUNTRY_CODES)+ .describe("The 2-letter country code of the city: https://d.to/geo"),+ clicks: z.number().describe("The number of clicks from this city"),+ }),++ device: z.object({+ device: z.string().describe("The name of the device"),+ clicks: z.number().describe("The number of clicks from this device"),+ }),++ browser: z.object({+ browser: z.string().describe("The name of the browser"),+ clicks: z.number().describe("The number of clicks from this browser"),+ }),++ os: z.object({+ os: z.string().describe("The name of the OS"),+ clicks: z.number().describe("The number of clicks from this OS"),+ }),++ referer: z.object({+ referer: z+ .string()+ .describe("The name of the referer. If unknown, this will be `(direct)`"),+ clicks: z.number().describe("The number of clicks from this referer"),+ }),++ topLinks: z.object({+ link: z.string().describe("The unique ID of the short link"),+ clicks: z.number().describe("The number of clicks from this link"),+ }),++ topUrls: z.object({+ url: z.string().describe("The destination URL"),+ clicks: z.number().describe("The number of clicks from this URL"),+ }),+} as const;diff --git a/apps/web/tests/analytics/browser.test.ts b/apps/web/tests/analytics/browser.test.tsnew file mode 100644index 0000000000..025a731f42--- /dev/null+++ b/apps/web/tests/analytics/browser.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/browser", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/browser",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["browser"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/city.test.ts b/apps/web/tests/analytics/city.test.tsnew file mode 100644index 0000000000..7e14be193a--- /dev/null+++ b/apps/web/tests/analytics/city.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/city", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/city",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["city"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/clicks.test.ts b/apps/web/tests/analytics/clicks.test.tsnew file mode 100644index 0000000000..4cf1217a13--- /dev/null+++ b/apps/web/tests/analytics/clicks.test.ts@@ -0,0 +1,17 @@+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/clicks", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data: clicks } = await http.get<number>({+ path: "/analytics/clicks",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(clicks).toEqual(expect.any(Number));+ expect(clicks).toBeGreaterThanOrEqual(0);+});diff --git a/apps/web/tests/analytics/country.test.ts b/apps/web/tests/analytics/country.test.tsnew file mode 100644index 0000000000..ed167f1e0c--- /dev/null+++ b/apps/web/tests/analytics/country.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/country", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/country",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["country"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/device.test.ts b/apps/web/tests/analytics/device.test.tsnew file mode 100644index 0000000000..8419640cd9--- /dev/null+++ b/apps/web/tests/analytics/device.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/device", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/device",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["device"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/os.test.ts b/apps/web/tests/analytics/os.test.tsnew file mode 100644index 0000000000..2d3c2bd9fa--- /dev/null+++ b/apps/web/tests/analytics/os.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/os", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/os",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["os"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/referer.test.ts b/apps/web/tests/analytics/referer.test.tsnew file mode 100644index 0000000000..d57e4416fe--- /dev/null+++ b/apps/web/tests/analytics/referer.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/referer", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/referer",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["referer"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/timeseries.test.ts b/apps/web/tests/analytics/timeseries.test.tsnew file mode 100644index 0000000000..f9d391677d--- /dev/null+++ b/apps/web/tests/analytics/timeseries.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/timeseries", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/timeseries",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["timeseries"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/top-links.test.ts b/apps/web/tests/analytics/top-links.test.tsnew file mode 100644index 0000000000..5c18148d6b--- /dev/null+++ b/apps/web/tests/analytics/top-links.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/top_links", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/top_links",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["topLinks"].strict()).safeParse(data),+ ).toBeTruthy();+});diff --git a/apps/web/tests/analytics/top-urls.test.ts b/apps/web/tests/analytics/top-urls.test.tsnew file mode 100644index 0000000000..5b992e72c9--- /dev/null+++ b/apps/web/tests/analytics/top-urls.test.ts@@ -0,0 +1,21 @@+import z from "@/lib/zod";+import { analyticsResponseSchema } from "@/lib/zod/schemas";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++test("GET /analytics/top_urls", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;++ const { status, data } = await http.get<any[]>({+ path: "/analytics/top_urls",+ query: { workspaceId },+ });++ expect(status).toEqual(200);+ expect(data.length).toBeGreaterThanOrEqual(0);+ expect(+ z.array(analyticsResponseSchema["topUrls"].strict()).safeParse(data),+ ).toBeTruthy();+});