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.

pluralize endpoints

Steven Tey778810a84796
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 11a3af90e6..03ebee9f7c 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -89,12 +89,17 @@ export const intervalData: Record<
export const VALID_ANALYTICS_ENDPOINTS = [
"count",
"timeseries",
- "country",
- "city",
- "device",
- "browser",
+ "country", // backwards compatibility
+ "countries",
+ "city", // backwards compatibility
+ "cities",
+ "device", // backwards compatibility
+ "devices",
+ "browser", // backwards compatibility
+ "browsers",
"os",
- "referer",
+ "referer", // backwards compatibility
+ "referers",
"top_links",
"top_urls",
] as const;
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index c1e972c6d0..62453bf5f4 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -4,14 +4,14 @@ export type IntervalProps = (typeof intervals)[number];
export type AnalyticsEndpoints =
| "count"
| "timeseries"
- | "country"
- | "city"
- | "device"
- | "browser"
+ | "countries"
+ | "cities"
+ | "devices"
+ | "browsers"
| "os"
- | "referer"
+ | "referers"
| "top_links"
| "top_urls";
-export type LocationTabs = "country" | "city";
+export type LocationTabs = "countries" | "cities";
export type TopLinksTabs = "link" | "url";
-export type DeviceTabs = "device" | "browser" | "os";
+export type DeviceTabs = "devices" | "browsers" | "os";
diff --git a/apps/web/lib/analytics/utils.ts b/apps/web/lib/analytics/utils.ts
index 69ad304f35..98be0cd9db 100644
--- a/apps/web/lib/analytics/utils.ts
+++ b/apps/web/lib/analytics/utils.ts
@@ -1,6 +1,33 @@
import { getDaysDifference } from "@dub/utils";
import { DubApiError } from "../api/errors";
+export const formatAnalyticsEndpoint = (
+ endpoint: string,
+ type: "plural" | "singular",
+) => {
+ const plural = {
+ country: "countries",
+ city: "cities",
+ device: "devices",
+ browser: "browsers",
+ referer: "referers",
+ };
+
+ const singular = {
+ countries: "country",
+ cities: "city",
+ devices: "device",
+ browsers: "browser",
+ referers: "referer",
+ };
+
+ if (type === "plural") {
+ return plural[endpoint] || endpoint;
+ } else if (type === "singular") {
+ return singular[endpoint] || endpoint;
+ }
+};
+
export const validDateRangeForPlan = ({
plan,
interval,
diff --git a/apps/web/lib/openapi/analytics/clicks/browser.ts b/apps/web/lib/openapi/analytics/clicks/browsers.ts
similarity index 83%
rename from apps/web/lib/openapi/analytics/clicks/browser.ts
rename to apps/web/lib/openapi/analytics/clicks/browsers.ts
index a663fffc71..347698445e 100644
--- a/apps/web/lib/openapi/analytics/clicks/browser.ts
+++ b/apps/web/lib/openapi/analytics/clicks/browsers.ts
@@ -7,8 +7,8 @@ import {
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
-export const getBrowserByClicks: ZodOpenApiOperationObject = {
- operationId: "getBrowserByClicks",
+export const getBrowsersByClicks: ZodOpenApiOperationObject = {
+ operationId: "getBrowsersByClicks",
"x-speakeasy-name-override": "browsers",
summary: "Retrieve top browsers by clicks",
description:
@@ -21,7 +21,7 @@ export const getBrowserByClicks: ZodOpenApiOperationObject = {
description: "The top browsers by number of clicks",
content: {
"application/json": {
- schema: z.array(getClickAnalyticsResponse["browser"]),
+ schema: z.array(getClickAnalyticsResponse["browsers"]),
},
},
},
diff --git a/apps/web/lib/openapi/analytics/clicks/city.ts b/apps/web/lib/openapi/analytics/clicks/cities.ts
similarity index 84%
rename from apps/web/lib/openapi/analytics/clicks/city.ts
rename to apps/web/lib/openapi/analytics/clicks/cities.ts
index 4d8342bb27..a87be04903 100644
--- a/apps/web/lib/openapi/analytics/clicks/city.ts
+++ b/apps/web/lib/openapi/analytics/clicks/cities.ts
@@ -7,8 +7,8 @@ import {
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
-export const getCityByClicks: ZodOpenApiOperationObject = {
- operationId: "getCityByClicks",
+export const getCitiesByClicks: ZodOpenApiOperationObject = {
+ operationId: "getCitiesByClicks",
"x-speakeasy-name-override": "cities",
summary: "Retrieve top cities by clicks",
description:
@@ -21,7 +21,7 @@ export const getCityByClicks: ZodOpenApiOperationObject = {
description: "The top cities by number of clicks",
content: {
"application/json": {
- schema: z.array(getClickAnalyticsResponse["city"]),
+ schema: z.array(getClickAnalyticsResponse["cities"]),
},
},
},
diff --git a/apps/web/lib/openapi/analytics/clicks/country.ts b/apps/web/lib/openapi/analytics/clicks/countries.ts
similarity index 87%
rename from apps/web/lib/openapi/analytics/clicks/country.ts
rename to apps/web/lib/openapi/analytics/clicks/countries.ts
index 6f935bd426..39fc0c5a04 100644
--- a/apps/web/lib/openapi/analytics/clicks/country.ts
+++ b/apps/web/lib/openapi/analytics/clicks/countries.ts
@@ -7,7 +7,7 @@ import {
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
-export const getCountryByClicks: ZodOpenApiOperationObject = {
+export const getCountriesByClicks: ZodOpenApiOperationObject = {
operationId: "getCountryByClicks",
"x-speakeasy-name-override": "countries",
summary: "Retrieve top countries by clicks",
@@ -21,7 +21,7 @@ export const getCountryByClicks: ZodOpenApiOperationObject = {
description: "The top countries by number of clicks",
content: {
"application/json": {
- schema: z.array(getClickAnalyticsResponse["country"]),
+ schema: z.array(getClickAnalyticsResponse["countries"]),
},
},
},
diff --git a/apps/web/lib/openapi/analytics/clicks/device.ts b/apps/web/lib/openapi/analytics/clicks/devices.ts
similarity index 83%
rename from apps/web/lib/openapi/analytics/clicks/device.ts
rename to apps/web/lib/openapi/analytics/clicks/devices.ts
index 600a7948f3..d87c9e51c5 100644
--- a/apps/web/lib/openapi/analytics/clicks/device.ts
+++ b/apps/web/lib/openapi/analytics/clicks/devices.ts
@@ -7,8 +7,8 @@ import {
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
-export const getDeviceByClicks: ZodOpenApiOperationObject = {
- operationId: "getDeviceByClicks",
+export const getDevicesByClicks: ZodOpenApiOperationObject = {
+ operationId: "getDevicesByClicks",
"x-speakeasy-name-override": "devices",
summary: "Retrieve top devices by clicks",
description:
@@ -21,7 +21,7 @@ export const getDeviceByClicks: ZodOpenApiOperationObject = {
description: "The top devices by number of clicks",
content: {
"application/json": {
- schema: z.array(getClickAnalyticsResponse["device"]),
+ schema: z.array(getClickAnalyticsResponse["devices"]),
},
},
},
diff --git a/apps/web/lib/openapi/analytics/clicks/index.ts b/apps/web/lib/openapi/analytics/clicks/index.ts
index 206c848ead..3b6dea7f4e 100644
--- a/apps/web/lib/openapi/analytics/clicks/index.ts
+++ b/apps/web/lib/openapi/analytics/clicks/index.ts
@@ -1,11 +1,11 @@
import { ZodOpenApiPathsObject } from "zod-openapi";
-import { getBrowserByClicks } from "./browser";
-import { getCityByClicks } from "./city";
+import { getBrowsersByClicks } from "./browsers";
+import { getCitiesByClicks } from "./cities";
import { getClicksCount } from "./count";
-import { getCountryByClicks } from "./country";
-import { getDeviceByClicks } from "./device";
+import { getCountriesByClicks } from "./countries";
+import { getDevicesByClicks } from "./devices";
import { getOSByClicks } from "./os";
-import { getRefererByClicks } from "./referer";
+import { getReferersByClicks } from "./referers";
import { getTimeseriesByClicks } from "./timeseries";
import { getTopLinksByClicks } from "./top-links";
import { getTopURLsByClicks } from "./top-urls";
@@ -17,23 +17,23 @@ export const clickAnalyticsPaths: ZodOpenApiPathsObject = {
"/analytics/clicks/timeseries": {
get: getTimeseriesByClicks,
},
- "/analytics/clicks/country": {
- get: getCountryByClicks,
+ "/analytics/clicks/countries": {
+ get: getCountriesByClicks,
},
- "/analytics/clicks/city": {
- get: getCityByClicks,
+ "/analytics/clicks/cities": {
+ get: getCitiesByClicks,
},
- "/analytics/clicks/device": {
- get: getDeviceByClicks,
+ "/analytics/clicks/devices": {
+ get: getDevicesByClicks,
},
- "/analytics/clicks/browser": {
- get: getBrowserByClicks,
+ "/analytics/clicks/browsers": {
+ get: getBrowsersByClicks,
},
"/analytics/clicks/os": {
get: getOSByClicks,
},
- "/analytics/clicks/referer": {
- get: getRefererByClicks,
+ "/analytics/clicks/referers": {
+ get: getReferersByClicks,
},
"/analytics/clicks/top_links": {
get: getTopLinksByClicks,
diff --git a/apps/web/lib/openapi/analytics/clicks/referer.ts b/apps/web/lib/openapi/analytics/clicks/referers.ts
similarity index 83%
rename from apps/web/lib/openapi/analytics/clicks/referer.ts
rename to apps/web/lib/openapi/analytics/clicks/referers.ts
index cf13da4bdc..1b8dfd0a98 100644
--- a/apps/web/lib/openapi/analytics/clicks/referer.ts
+++ b/apps/web/lib/openapi/analytics/clicks/referers.ts
@@ -7,8 +7,8 @@ import {
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
-export const getRefererByClicks: ZodOpenApiOperationObject = {
- operationId: "getRefererByClicks",
+export const getReferersByClicks: ZodOpenApiOperationObject = {
+ operationId: "getReferersByClicks",
"x-speakeasy-name-override": "referers",
summary: "Retrieve top referers by clicks",
description:
@@ -21,7 +21,7 @@ export const getRefererByClicks: ZodOpenApiOperationObject = {
description: "The top referers by number of clicks",
content: {
"application/json": {
- schema: z.array(getClickAnalyticsResponse["referer"]),
+ schema: z.array(getClickAnalyticsResponse["referers"]),
},
},
},
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index f40483d6b7..ce1c9873c4 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -1,3 +1,4 @@
+import { formatAnalyticsEndpoint } from "@/lib/analytics";
import {
intervals,
VALID_ANALYTICS_ENDPOINTS,
@@ -16,6 +17,7 @@ export const analyticsEndpointSchema = z.object({
};
},
})
+ .transform((v) => formatAnalyticsEndpoint(v, "plural"))
.optional()
.describe(
"The field to group the analytics by. If undefined, returns the total click count.",
@@ -141,48 +143,41 @@ export const getClickAnalyticsResponse = {
start: z.string().describe("The starting timestamp of the interval"),
clicks: z.number().describe("The number of clicks in the interval"),
}),
- country: z.object({
+ countries: 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({
+ cities: 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({
+ devices: 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({
+ browsers: 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({
+ referers: 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"),
}),
-
top_links: z.object({
link: z.string().describe("The unique ID of the short link"),
clicks: z.number().describe("The number of clicks from this link"),
}),
-
top_urls: z.object({
url: z.string().describe("The destination URL"),
clicks: z.number().describe("The number of clicks from this URL"),
diff --git a/apps/web/ui/analytics/device-icon.tsx b/apps/web/ui/analytics/device-icon.tsx
index 63427ad133..5cdaa9e411 100644
--- a/apps/web/ui/analytics/device-icon.tsx
+++ b/apps/web/ui/analytics/device-icon.tsx
@@ -11,7 +11,7 @@ export default function DeviceIcon({
tab: DeviceTabs;
className: string;
}) {
- if (tab === "device") {
+ if (tab === "devices") {
return (
<BlurImage
src={
@@ -26,7 +26,7 @@ export default function DeviceIcon({
className={className}
/>
);
- } else if (tab === "browser") {
+ } else if (tab === "browsers") {
if (display === "Chrome") {
return <Chrome className={className} />;
} else if (display === "Safari" || display === "Mobile Safari") {
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index 374894edb6..68d3f82b5d 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -1,15 +1,19 @@
-import { DeviceTabs } from "@/lib/analytics";
+import { DeviceTabs, formatAnalyticsEndpoint } from "@/lib/analytics";
import { LoadingSpinner, Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { fetcher } from "@dub/utils";
import { Maximize } from "lucide-react";
-import { useContext, useState } from "react";
+import { useContext, useMemo, useState } from "react";
import useSWR from "swr";
import { AnalyticsContext } from ".";
import BarList from "./bar-list";
import DeviceIcon from "./device-icon";
export default function Devices() {
- const [tab, setTab] = useState<DeviceTabs>("device");
+ const [tab, setTab] = useState<DeviceTabs>("devices");
+ const singularTabName = useMemo(
+ () => formatAnalyticsEndpoint(tab, "singular"),
+ [tab],
+ );
const { baseApiPath, queryString } = useContext(AnalyticsContext);
@@ -24,14 +28,20 @@ export default function Devices() {
const barList = (limit?: number) => (
<BarList
- tab={tab}
+ tab={singularTabName}
data={
data?.map((d) => ({
- icon: <DeviceIcon display={d[tab]} tab={tab} className="h-4 w-4" />,
- title: d[tab],
+ icon: (
+ <DeviceIcon
+ display={d[singularTabName]}
+ tab={tab}
+ className="h-4 w-4"
+ />
+ ),
+ title: d[singularTabName],
href: queryParams({
set: {
- [tab]: d[tab],
+ [singularTabName]: d[singularTabName],
},
getNewPath: true,
}) as string,
@@ -61,7 +71,7 @@ export default function Devices() {
<div className="mb-3 flex justify-between">
<h1 className="text-lg font-semibold">Devices</h1>
<TabSelect
- options={["device", "browser", "os"]}
+ options={["devices", "browsers", "os"]}
selected={tab}
// @ts-ignore
selectAction={setTab}
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index c2e2607e17..ed932f1983 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -1,14 +1,18 @@
-import { LocationTabs } from "@/lib/analytics";
+import { LocationTabs, formatAnalyticsEndpoint } from "@/lib/analytics";
import { LoadingSpinner, Modal, TabSelect, useRouterStuff } from "@dub/ui";
import { COUNTRIES, fetcher } from "@dub/utils";
import { Maximize } from "lucide-react";
-import { useContext, useState } from "react";
+import { useContext, useMemo, useState } from "react";
import useSWR from "swr";
import { AnalyticsContext } from ".";
import BarList from "./bar-list";
export default function Locations() {
- const [tab, setTab] = useState<LocationTabs>("country");
+ const [tab, setTab] = useState<LocationTabs>("countries");
+ const singularTabName = useMemo(
+ () => formatAnalyticsEndpoint(tab, "singular"),
+ [tab],
+ );
const { baseApiPath, queryString } = useContext(AnalyticsContext);
@@ -22,7 +26,7 @@ export default function Locations() {
const barList = (limit?: number) => (
<BarList
- tab={tab}
+ tab={singularTabName}
data={
data?.map((d) => ({
icon: (
@@ -32,10 +36,10 @@ export default function Locations() {
className="h-3 w-5"
/>
),
- title: tab === "country" ? COUNTRIES[d.country] : d.city,
+ title: tab === "countries" ? COUNTRIES[d.country] : d.city,
href: queryParams({
set: {
- [tab]: d[tab],
+ [singularTabName]: d[singularTabName],
},
getNewPath: true,
}) as string,
@@ -65,7 +69,7 @@ export default function Locations() {
<div className="mb-3 flex justify-between">
<h1 className="text-lg font-semibold">Locations</h1>
<TabSelect
- options={["country", "city"]}
+ options={["countries", "cities"]}
selected={tab}
// @ts-ignore
selectAction={setTab}
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 53a4ef7ad9..36e22012e6 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -10,7 +10,7 @@ export default function Referer() {
const { baseApiPath, queryString } = useContext(AnalyticsContext);
const { data } = useSWR<{ referer: string; clicks: number }[]>(
- `${baseApiPath}/referer?${queryString}`,
+ `${baseApiPath}/referers?${queryString}`,
fetcher,
);
@@ -59,13 +59,13 @@ export default function Referer() {
className="max-w-lg"
>
<div className="border-b border-gray-200 px-6 py-4">
- <h1 className="text-lg font-semibold">Referrers</h1>
+ <h1 className="text-lg font-semibold">Referers</h1>
</div>
{barList()}
</Modal>
<div className="scrollbar-hide relative z-0 h-[400px] border border-gray-200 bg-white px-7 py-5 sm:rounded-lg sm:border-gray-100 sm:shadow-lg">
<div className="mb-3 flex justify-between">
- <h1 className="text-lg font-semibold">Referrers</h1>
+ <h1 className="text-lg font-semibold">Referers</h1>
</div>
{data ? (
data.length > 0 ? (