Skip to content

Commit 5cdafaa

Browse files
committed
feat: config schema validation
1 parent 39eded8 commit 5cdafaa

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
API_PORT=5000
22
API_HOST=localhost
3+
MAX_PAGES_TO_CRAWL=45
34
NODE_ENV=development

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"express-fileupload": "^1.4.3",
1616
"glob": "^10.3.10",
1717
"inquirer": "^9.2.12",
18+
"joi": "^17.11.0",
1819
"playwright": "*",
1920
"prettier": "^3.1.0"
2021
},

src/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { Page } from "playwright";
2+
import Joi from "joi";
3+
import { configDotenv } from "dotenv";
4+
5+
configDotenv();
26

37
export type Config = {
48
/**
@@ -39,3 +43,17 @@ export type Config = {
3943
/** Optional timeout for waiting for a selector to appear */
4044
waitForSelectorTimeout?: number;
4145
};
46+
47+
export const ConfigSchema = Joi.object({
48+
url: Joi.string().uri().required(),
49+
match: Joi.alternatives(Joi.string().required(), Joi.array().items(Joi.string().required())).required(),
50+
selector: Joi.string().default(''),
51+
maxPagesToCrawl: Joi.number().integer().default(process.env.MAX_PAGES_TO_CRAWL || 50),
52+
outputFileName: Joi.string().default('output.json'),
53+
cookie: Joi.object({
54+
name: Joi.string().required(),
55+
value: Joi.string().required(),
56+
}).optional(),
57+
onVisitPage: Joi.function().optional(),
58+
waitForSelectorTimeout: Joi.number().optional(),
59+
});

src/server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import cors from 'cors';
33
import { readFile } from 'fs/promises';
44
import { crawl, write } from "./core.js";
5-
import { Config } from './config.js';
5+
import { Config, ConfigSchema } from './config.js';
66
import { configDotenv } from 'dotenv';
77

88
configDotenv();
@@ -18,9 +18,10 @@ app.use(express.json());
1818
app.post('/crawl', async (req, res) => {
1919
const config: Config = req.body;
2020
try {
21-
await crawl(config);
22-
await write(config);
23-
const outputFileContent = await readFile(config.outputFileName, 'utf-8');
21+
const validatedConfig = ConfigSchema.validate(config).value;
22+
await crawl(validatedConfig);
23+
await write(validatedConfig);
24+
const outputFileContent = await readFile(validatedConfig.outputFileName, 'utf-8');
2425
res.contentType('application/json');
2526
return res.send(outputFileContent);
2627
} catch (error) {

0 commit comments

Comments
 (0)