From 695b9d337cb3494304c57fb8756c923d6da2d352 Mon Sep 17 00:00:00 2001 From: Ayo Date: Thu, 11 May 2023 16:50:17 +0200 Subject: [PATCH] feat: simple url validation --- src/components/article.astro | 11 +++++++---- src/pages/index.astro | 6 +++--- src/utils/get-params.ts | 4 +--- src/utils/index.ts | 2 ++ src/utils/is-url.ts | 4 ++++ 5 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 src/utils/index.ts create mode 100644 src/utils/is-url.ts diff --git a/src/components/article.astro b/src/components/article.astro index ff809e1..a0c8315 100644 --- a/src/components/article.astro +++ b/src/components/article.astro @@ -8,7 +8,10 @@ export interface Props { } --- -

{article.title}

-

by {article.author}

- - +{ +article ? ( +

{article.title}

+

by {article.author}

+ + ) : '' +} diff --git a/src/pages/index.astro b/src/pages/index.astro index 65c634e..a005dae 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,13 +1,13 @@ --- import Form, { FormGroup } from "@astro-reactive/form"; import { Validators } from "@astro-reactive/validator"; -import getParams from "../utils/get-params"; import { extract } from "@extractus/article-extractor"; import Article from "../components/article.astro"; +import { getParams, isURL } from "../utils"; const params = getParams(Astro.url); -const url = params?.url; -const article = await extract(url); +const url: string = params?.url; +const article = isURL(url) && (await extract(url)); const form = new FormGroup([ { diff --git a/src/utils/get-params.ts b/src/utils/get-params.ts index 5442d84..ef68b75 100644 --- a/src/utils/get-params.ts +++ b/src/utils/get-params.ts @@ -1,6 +1,4 @@ -export default function getParams( - url: URL -): { [key: string]: string } | undefined { +export function getParams(url: URL): { [key: string]: string } | undefined { if (url.search === "") return; return Object.fromEntries( diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..a213a7d --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,2 @@ +export * from "./get-params"; +export * from "./is-url"; diff --git a/src/utils/is-url.ts b/src/utils/is-url.ts new file mode 100644 index 0000000..56c62e6 --- /dev/null +++ b/src/utils/is-url.ts @@ -0,0 +1,4 @@ +export function isURL(str: string): boolean { + // TODO: improve pls + return str.includes("http://") || str.includes("https://"); +}