Skip to content

Commit

Permalink
Full chapter fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowstudent777 committed Jan 26, 2025
1 parent 66ef799 commit 102b8d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "npm run clean && npx tsc && npm run copyfiles",
"clean": "npx rimraf --glob dist/",
"copyfiles": "mkdir \"dist\\api\\v1\\core\\db\" && cp ./src/api/v1/core/db/ ./dist/api/v1/core -r",
"dev": "npm run copyfiles && tsc-watch --onSuccess \"node ./dist/index.js\"",
"dev": "npm run clean && npm run copyfiles && tsc-watch --onSuccess \"node ./dist/index.js\"",
"start": "node ./dist/index.js",
"test": "vitest",
"test:coverage": "vitest run --coverage"
Expand Down Expand Up @@ -48,4 +48,4 @@
"typescript": "^4.9.5",
"vitest": "^1.6.0"
}
}
}
2 changes: 1 addition & 1 deletion src/api/v1/core
Submodule core updated from 01bff5 to 7ac540
51 changes: 30 additions & 21 deletions src/api/v1/verse/verse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express, { Request, Response, Router } from 'express'
import axios from 'axios';
import * as cheerio from 'cheerio';
import { getVerse } from '../core/functions/verse';
import express, { Request, Response, Router } from "express";
import axios from "axios";
import * as cheerio from "cheerio";
import { getVerse } from "../core/functions/verse";

// Router
const router: Router = express.Router();
Expand Down Expand Up @@ -52,25 +52,34 @@ const router: Router = express.Router();
* example: OK
*/
router.get("/", async (req: Request, res: Response) => {
let book = req.query.book as string;
const chapter = req.query.chapter ??= "1";
const verses = req.query.verses ??= "1" as string;
let version = req.query.version ??= "KJV" as string;
let book = req.query.book as string;
const chapter = (req.query.chapter ??= "1");
const verses = (req.query.verses ??= "-1" as string);
let version = (req.query.version ??= "KJV" as string);

function apiError(code: number, message: string) {
res.status(code).send({
"code": code,
"message": message
});
}
if (!book) return apiError(400, "Missing field 'book'");
if (isNaN(parseInt(chapter.toString()))) return apiError(400, "Chapter must be a number");
if (isNaN(parseInt(verses.toString()))) return apiError(400, "Verses must be a number");
function apiError(code: number, message: string) {
res.status(code).send({
code: code,
message: message,
});
}
if (!book) return apiError(400, "Missing field 'book'");
if (isNaN(parseInt(chapter.toString())))
return apiError(400, "Chapter must be a number");
if (isNaN(parseInt(verses.toString())))
return apiError(400, "Verses must be a number");
if (parseInt(chapter.toString()) <= 0)
return apiError(400, "Chapter must be greater than 0");

const data = await getVerse(book, chapter.toString(), verses.toString(), version.toString());
const data = await getVerse(
book,
chapter.toString(),
verses.toString(),
version.toString()
);

if (data?.code) return apiError(data.code, data.message);
else return res.status(200).send(data);
})
if (data?.code) return apiError(data.code, data.message);
else return res.status(200).send(data);
});

module.exports = router;

0 comments on commit 102b8d4

Please sign in to comment.