diff --git a/src/author.ts b/src/author.ts index f6d640f..adda90c 100644 --- a/src/author.ts +++ b/src/author.ts @@ -42,7 +42,7 @@ export class Author { this.ipAddress = options.ipAddress ?? ""; this.name = options.name ?? ""; this.role = options.role ?? ""; - this.url = options.url ? new URL(options.url) : null; + this.url = options.url && URL.canParse(options.url) ? new URL(options.url) : null; this.userAgent = options.userAgent ?? ""; } diff --git a/src/blog.ts b/src/blog.ts index fa8b4e5..728a1f8 100644 --- a/src/blog.ts +++ b/src/blog.ts @@ -25,7 +25,7 @@ export class Blog { constructor(options: Partial = {}) { this.charset = options.charset ?? ""; this.languages = options.languages ?? []; - this.url = options.url ? new URL(options.url) : null; + this.url = options.url && URL.canParse(options.url) ? new URL(options.url) : null; } /** diff --git a/src/client.ts b/src/client.ts index f89234b..cbe1341 100644 --- a/src/client.ts +++ b/src/client.ts @@ -49,11 +49,11 @@ export class Client { * @param options An object providing values to initialize this instance. */ constructor(apiKey: string, blog: Blog, options: Partial = {}) { - const {baseUrl = "https://rest.akismet.com"} = options; + const {baseUrl = ""} = options; const url = baseUrl instanceof URL ? baseUrl.href : baseUrl; this.apiKey = apiKey; - this.baseUrl = new URL(url.endsWith("/") ? url : `${url}/`); + this.baseUrl = new URL(URL.canParse(url) ? (url.endsWith("/") ? url : `${url}/`) : "https://rest.akismet.com/"); this.blog = blog; this.isTest = options.isTest ?? false; this.userAgent = options.userAgent ?? `${navigator.userAgent} | Akismet/${Client.#version}`; diff --git a/src/comment.ts b/src/comment.ts index 7f66fd0..c944c11 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -59,10 +59,10 @@ export class Comment { this.content = options.content ?? ""; this.context = options.context ?? []; this.date = options.date ?? null; - this.permalink = options.permalink ? new URL(options.permalink) : null; + this.permalink = options.permalink && URL.canParse(options.permalink) ? new URL(options.permalink) : null; this.postModified = options.postModified ?? null; this.recheckReason = options.recheckReason ?? ""; - this.referrer = options.referrer ? new URL(options.referrer) : null; + this.referrer = options.referrer && URL.canParse(options.referrer) ? new URL(options.referrer) : null; this.type = options.type ?? ""; }