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.

make API super flexible too

Steven Teye2168d59a765
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index c85494768d..d98ddd43a4 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -9,6 +9,7 @@ import {
getUrlFromString,
isDubDomain,
isValidUrl,
+ parseDateTime,
} from "@dub/utils";
import { combineTagIds, keyChecks, processKey } from "./utils";
@@ -51,8 +52,8 @@ export async function processLink({
code: "bad_request",
};
}
- const processedUrl = getUrlFromString(url);
- if (!isValidUrl(processedUrl)) {
+ url = getUrlFromString(url);
+ if (!isValidUrl(url)) {
return {
link: payload,
error: "Invalid destination url.",
@@ -202,22 +203,15 @@ export async function processLink({
// expire date checks
if (expiresAt) {
- const date = new Date(expiresAt);
- if (isNaN(date.getTime())) {
+ const datetime = parseDateTime(expiresAt);
+ if (!datetime) {
return {
link: payload,
- error: "Invalid expiry date. Expiry date must be in ISO-8601 format.",
- code: "unprocessable_entity",
- };
- }
- // check if expiresAt is in the future
- if (new Date(expiresAt) < new Date()) {
- return {
- link: payload,
- error: "Expiry date must be in the future.",
+ error: "Invalid expiration date.",
code: "unprocessable_entity",
};
}
+ expiresAt = datetime;
if (expiredUrl && !isValidUrl(getUrlFromString(expiredUrl))) {
return {
link: payload,
@@ -237,7 +231,8 @@ export async function processLink({
...payload,
domain,
key,
- url: processedUrl,
+ url,
+ expiresAt,
// make sure projectId is set to the current workspace
projectId: workspace?.id || null,
// if userId is passed, set it (we don't change the userId if it's already set, e.g. when editing a link)
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 2f1228c5d8..250ed3440f 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -128,9 +128,7 @@ export const createLinkBodySchema = z.object({
expiresAt: z
.string()
.nullish()
- .describe(
- "The date and time when the short link will expire in ISO-8601 format. Must be in the future.",
- ),
+ .describe("The date and time when the short link will expire at."),
expiredUrl: z
.string()
.nullish()
@@ -234,9 +232,10 @@ export const LinkSchema = z
.describe("Whether the short link is archived."),
expiresAt: z
.string()
+ .datetime()
.nullable()
.describe(
- "The date and time when the short link will expire in ISO-8601 format. Must be in the future.",
+ "The date and time when the short link will expire in ISO-8601 format.",
),
expiredUrl: z
.string()
diff --git a/apps/web/ui/modals/add-edit-link-modal/expiration-section.tsx b/apps/web/ui/modals/add-edit-link-modal/expiration-section.tsx
index dfae22353c..d96781e049 100644
--- a/apps/web/ui/modals/add-edit-link-modal/expiration-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/expiration-section.tsx
@@ -97,6 +97,7 @@ export default function ExpirationSection({
ref={inputRef}
type="text"
placeholder='E.g. "tomorrow at 5pm" or "in 2 hours"'
+ defaultValue={expiresAt ? formatDateTime(expiresAt) : ""}
onBlur={(e) => {
if (e.target.value.length > 0) {
const parsedDateTime = parseDateTime(e.target.value);
diff --git a/packages/utils/src/functions/datetime.ts b/packages/utils/src/functions/datetime.ts
index 46880b4963..53316bd682 100644
--- a/packages/utils/src/functions/datetime.ts
+++ b/packages/utils/src/functions/datetime.ts
@@ -11,12 +11,13 @@ export const getDateTimeLocal = (timestamp?: Date): string => {
};
// Function to parse a date string into a Date object
-export const parseDateTime = (text: string) => {
- return chrono.parseDate(text);
+export const parseDateTime = (str: Date | string) => {
+ if (str instanceof Date) return str;
+ return chrono.parseDate(str);
};
-export const formatDate = (dateString: string) => {
- return new Date(dateString).toLocaleDateString("en-US", {
+export const formatDate = (datetime: Date | string) => {
+ return new Date(datetime).toLocaleDateString("en-US", {
day: "numeric",
month: "long",
year: "numeric",