Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor analytics API endpoints to use zod for query validation

Kiran Kabaa39023778
diff --git a/apps/web/lib/openapi/analytics/browser.ts b/apps/web/lib/openapi/analytics/browser.ts
index 39ebfe73ef..b815d61bda 100644
--- a/apps/web/lib/openapi/analytics/browser.ts
+++ b/apps/web/lib/openapi/analytics/browser.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getBrowserAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top browsers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/city.ts b/apps/web/lib/openapi/analytics/city.ts
index 4d9f9b1dd7..4096341899 100644
--- a/apps/web/lib/openapi/analytics/city.ts
+++ b/apps/web/lib/openapi/analytics/city.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { COUNTRY_CODES } from "@dub/utils";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -10,7 +10,13 @@ export const getCityAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks.ts b/apps/web/lib/openapi/analytics/clicks.ts
index f930fa6f91..648bb3e604 100644
--- a/apps/web/lib/openapi/analytics/clicks.ts
+++ b/apps/web/lib/openapi/analytics/clicks.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getClicksAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/country.ts b/apps/web/lib/openapi/analytics/country.ts
index 3a12836e13..590c86f9e1 100644
--- a/apps/web/lib/openapi/analytics/country.ts
+++ b/apps/web/lib/openapi/analytics/country.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { COUNTRY_CODES } from "@dub/utils";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -10,7 +10,13 @@ export const getCountryAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/device.ts b/apps/web/lib/openapi/analytics/device.ts
index af75ae6ef9..9bfd9202a2 100644
--- a/apps/web/lib/openapi/analytics/device.ts
+++ b/apps/web/lib/openapi/analytics/device.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getDeviceAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top devices by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/index.ts b/apps/web/lib/openapi/analytics/index.ts
index fa1b75d8eb..606f603995 100644
--- a/apps/web/lib/openapi/analytics/index.ts
+++ b/apps/web/lib/openapi/analytics/index.ts
@@ -42,4 +42,4 @@ export const analyticsPaths: ZodOpenApiPathsObject = {
"/analytics/top_urls": {
get: getTopURLs,
},
-};
+};
\ No newline at end of file
diff --git a/apps/web/lib/openapi/analytics/os.ts b/apps/web/lib/openapi/analytics/os.ts
index f2ce64867a..809b732107 100644
--- a/apps/web/lib/openapi/analytics/os.ts
+++ b/apps/web/lib/openapi/analytics/os.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getOSAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top OS by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/referer.ts b/apps/web/lib/openapi/analytics/referer.ts
index b17e5945bb..808f098f8a 100644
--- a/apps/web/lib/openapi/analytics/referer.ts
+++ b/apps/web/lib/openapi/analytics/referer.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getRefererAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the top referers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/timeseries.ts b/apps/web/lib/openapi/analytics/timeseries.ts
index 18f0b7af5d..1dc93a9928 100644
--- a/apps/web/lib/openapi/analytics/timeseries.ts
+++ b/apps/web/lib/openapi/analytics/timeseries.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getTimeseriesAnalytics: ZodOpenApiOperationObject = {
description:
"Retrieve the number of clicks for a link, a domain, or the authenticated workspace over a period of time.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/top-links.ts b/apps/web/lib/openapi/analytics/top-links.ts
index 5f0aa2e558..e46c9adce4 100644
--- a/apps/web/lib/openapi/analytics/top-links.ts
+++ b/apps/web/lib/openapi/analytics/top-links.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getTopLinks: ZodOpenApiOperationObject = {
description:
"Retrieve the top links by number of clicks for a domain or the authenticated workspace.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/top-urls.ts b/apps/web/lib/openapi/analytics/top-urls.ts
index 006c5c8370..b0eacf15b5 100644
--- a/apps/web/lib/openapi/analytics/top-urls.ts
+++ b/apps/web/lib/openapi/analytics/top-urls.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z, { workspaceIdSchema } from "@/lib/zod";
+import z from "@/lib/zod";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -9,7 +9,13 @@ export const getTopURLs: ZodOpenApiOperationObject = {
description:
"Retrieve the top URLs by number of clicks for a given short link.",
requestParams: {
- query: workspaceIdSchema.merge(getAnalyticsQuerySchema),
+ query: z
+ .object({
+ workspaceId: z
+ .string()
+ .describe("The ID of the workspace the link belongs to."),
+ })
+ .merge(getAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/zod/index.ts b/apps/web/lib/zod/index.ts
index e1c59a9f66..66105b0c43 100644
--- a/apps/web/lib/zod/index.ts
+++ b/apps/web/lib/zod/index.ts
@@ -18,4 +18,4 @@ export const workspaceIdSchema = z.object({
.string()
.min(1, "Workspace ID is required.")
.describe("The ID of the workspace the link belongs to."),
-});
\ No newline at end of file
+});