Skip to content

Commit 820e6a8

Browse files
committed
feat: Add url validation on feed
1 parent 067b2f3 commit 820e6a8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/__tests__/__snapshots__/api.tests.ts.snap

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,35 @@ exports[`
334334
}
335335
`;
336336

337+
exports[`
338+
feed(url: "not-a-url") { title }
339+
1`] = `
340+
{
341+
"data": {
342+
"feed": null,
343+
},
344+
"errors": [
345+
{
346+
"extensions": {
347+
"code": "invalid-url",
348+
"message": "Invalid url",
349+
"type": "InvalidUrlError",
350+
},
351+
"locations": [
352+
{
353+
"column": 3,
354+
"line": 2,
355+
},
356+
],
357+
"message": "Invalid url",
358+
"path": [
359+
"feed",
360+
],
361+
},
362+
],
363+
}
364+
`;
365+
337366
exports[`
338367
findFeed(url: "https://non--------existing-domain.com") { link }
339368
1`] = `

src/__tests__/api.tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ testGraphqlApi`
5151
feed(url: "https://non--------existing-domain.com") { title }
5252
`;
5353

54+
testGraphqlApi`
55+
feed(url: "not-a-url") { title }
56+
`;
57+
5458
testGraphqlApi`
5559
feed(url: "${url}", startTime: "2020-01-01", endTime: "2020-10-31") {
5660
items { title }

src/handlers/feed.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import isUrl from "is-url";
12
import {
23
BaseError,
34
EmptyParserOutputError,
5+
InvalidUrlError,
46
NotAFeedError,
57
ParserError,
68
} from "../errors.js";
@@ -53,6 +55,9 @@ export async function parseFromQuery({
5355
endTime?: string;
5456
startTime?: string;
5557
}): Promise<ParserResponse> {
58+
if (!isUrl(url)) {
59+
throw new InvalidUrlError(url);
60+
}
5661
const response = await request(url);
5762

5863
const contentType =

0 commit comments

Comments
 (0)