mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 15:35:35 +00:00
feat: add support for domain without extension (ie: localhost)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { VALID_URL_REGEX } from "constants/url";
|
|
||||||
import { boolean, number, object, string } from "yup";
|
import { boolean, number, object, string } from "yup";
|
||||||
|
import { isValidHttpUrl } from "../url";
|
||||||
|
|
||||||
const LinkBodySchema = object({
|
const LinkBodySchema = object({
|
||||||
name: string()
|
name: string()
|
||||||
@@ -9,7 +9,7 @@ const LinkBodySchema = object({
|
|||||||
url: string()
|
url: string()
|
||||||
.trim()
|
.trim()
|
||||||
.required("URl is required")
|
.required("URl is required")
|
||||||
.matches(VALID_URL_REGEX, "Invalid URL format"),
|
.test("test_url", "Invalid URL format", (value) => isValidHttpUrl(value)),
|
||||||
categoryId: number().required("CategoryId must be a number"),
|
categoryId: number().required("CategoryId must be a number"),
|
||||||
favorite: boolean().default(() => false),
|
favorite: boolean().default(() => false),
|
||||||
}).typeError("Missing request Body");
|
}).typeError("Missing request Body");
|
||||||
|
|||||||
@@ -47,3 +47,15 @@ export function findFaviconPath(text: string) {
|
|||||||
|
|
||||||
return favicon?.getAttribute("href") || undefined;
|
return favicon?.getAttribute("href") || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isValidHttpUrl(urlParam: string) {
|
||||||
|
let url;
|
||||||
|
|
||||||
|
try {
|
||||||
|
url = new URL(urlParam);
|
||||||
|
} catch (_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url.protocol === "http:" || url.protocol === "https:";
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { apiHandler } from "lib/api/handler";
|
import { apiHandler } from "lib/api/handler";
|
||||||
import getUserLink from "lib/link/getUserLink";
|
import getUserLink from "lib/link/getUserLink";
|
||||||
import { LinkBodySchema, LinkQuerySchema } from "lib/link/linkValidationSchema";
|
import { LinkBodySchema, LinkQuerySchema } from "lib/link/linkValidationSchema";
|
||||||
|
|
||||||
import prisma from "utils/prisma";
|
import prisma from "utils/prisma";
|
||||||
|
|
||||||
export default apiHandler({
|
export default apiHandler({
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import { useRouter } from "next/router";
|
|||||||
import { FormEvent, useMemo, useState } from "react";
|
import { FormEvent, useMemo, useState } from "react";
|
||||||
import styles from "styles/form.module.scss";
|
import styles from "styles/form.module.scss";
|
||||||
import { Category, Link } from "types";
|
import { Category, Link } from "types";
|
||||||
import { IsValidURL } from "utils/front";
|
|
||||||
import { withAuthentication } from "utils/session";
|
import { withAuthentication } from "utils/session";
|
||||||
import { makeRequest } from "lib/request";
|
import { makeRequest } from "lib/request";
|
||||||
|
import { isValidHttpUrl } from "lib/url";
|
||||||
|
|
||||||
export default function PageCreateLink({
|
export default function PageCreateLink({
|
||||||
categories,
|
categories,
|
||||||
@@ -39,7 +39,7 @@ export default function PageCreateLink({
|
|||||||
const canSubmit = useMemo<boolean>(
|
const canSubmit = useMemo<boolean>(
|
||||||
() =>
|
() =>
|
||||||
name !== "" &&
|
name !== "" &&
|
||||||
IsValidURL(url) &&
|
isValidHttpUrl(url) &&
|
||||||
favorite !== null &&
|
favorite !== null &&
|
||||||
categoryId !== null &&
|
categoryId !== null &&
|
||||||
!submitted,
|
!submitted,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { useRouter } from "next/router";
|
|||||||
import { FormEvent, useMemo, useState } from "react";
|
import { FormEvent, useMemo, useState } from "react";
|
||||||
import styles from "styles/form.module.scss";
|
import styles from "styles/form.module.scss";
|
||||||
import { Category, Link } from "types";
|
import { Category, Link } from "types";
|
||||||
import { IsValidURL } from "utils/front";
|
import { isValidHttpUrl } from "lib/url";
|
||||||
import { withAuthentication } from "utils/session";
|
import { withAuthentication } from "utils/session";
|
||||||
import { makeRequest } from "lib/request";
|
import { makeRequest } from "lib/request";
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ export default function PageEditLink({
|
|||||||
categoryId !== link.category.id;
|
categoryId !== link.category.id;
|
||||||
const isFormValid =
|
const isFormValid =
|
||||||
name !== "" &&
|
name !== "" &&
|
||||||
IsValidURL(url) &&
|
isValidHttpUrl(url) &&
|
||||||
favorite !== null &&
|
favorite !== null &&
|
||||||
categoryId !== null;
|
categoryId !== null;
|
||||||
return isFormEdited && isFormValid && !submitted;
|
return isFormEdited && isFormValid && !submitted;
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
import { VALID_URL_REGEX } from "constants/url";
|
|
||||||
|
|
||||||
export function IsValidURL(url: string): boolean {
|
|
||||||
const regex = new RegExp(VALID_URL_REGEX);
|
|
||||||
return !!url.match(regex);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user