This commit introduces Swagger API documentation for all endpoints in the application. - Installs and . - Configures Swagger in to generate and serve API documentation at . - Adds JSDoc-style Swagger annotations to all routes in and the directory (, , , , , ). - Defines a cookie-based security scheme for authenticated routes. This allows for interactive API documentation and testing via the endpoint.
221 lines
5.5 KiB
JavaScript
221 lines
5.5 KiB
JavaScript
const axios = require('axios');
|
|
var express = require('express')
|
|
var router = express.Router()
|
|
const DB = require("./../mongoDB.js");
|
|
|
|
const fetchAPI = async (path) => {
|
|
baseUrl = "https://api.scripture.api.bible/v1/"
|
|
let detailHtml = await axios.get(baseUrl + path, {headers:{'api-key':'8b43472a173a39e04cd868fd4848ed75'}}).catch(console.error);
|
|
return detailHtml?.data;
|
|
}
|
|
|
|
const defaultBibleId = "592420522e16049f-01";
|
|
|
|
//getMedia('y42zyf3').then(console.log)
|
|
DB.getDB.then((DB) => {
|
|
/**
|
|
* @swagger
|
|
* tags:
|
|
* name: Bible
|
|
* description: Bible API
|
|
*/
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible:
|
|
* get:
|
|
* summary: Get a list of available Bibles
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("", async (req, res) => {
|
|
const bibles = await fetchAPI('bibles');
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/books:
|
|
* get:
|
|
* summary: Get the books of a Bible
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/books", async (req, res) => {
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId +"/books");
|
|
return res.json(bibles);
|
|
});
|
|
|
|
router.get("/books", async (req, res) => {
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId +"/books");
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/books/{bookId}:
|
|
* get:
|
|
* summary: Get details for a specific book
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: bookId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/books/:bookId", async (req, res) => {
|
|
const bookId = req.params.bookId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId +"/books/" + bookId);
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/books/{bookId}/chapters:
|
|
* get:
|
|
* summary: Get the chapters of a book
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: bookId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/books/:bookId/chapters", async (req, res) => {
|
|
const bookId = req.params.bookId;
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId +"/books/" + bookId + "/chapters");
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/chapters/{chapterId}:
|
|
* get:
|
|
* summary: Get the content of a chapter
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: chapterId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/chapters/:chapterId", async (req, res) => {
|
|
const chapterId = req.params.chapterId;
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId);
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/chapters/{chapterId}/verses:
|
|
* get:
|
|
* summary: Get the verses of a chapter
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: chapterId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/chapters/:chapterId/verses", async (req, res) => {
|
|
const chapterId = req.params.chapterId;
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId + "/verses");
|
|
return res.json(bibles);
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /bible/search:
|
|
* get:
|
|
* summary: Search the Bible
|
|
* tags: [Bible]
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: query
|
|
* name: query
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: query
|
|
* name: limit
|
|
* schema:
|
|
* type: integer
|
|
* default: 10
|
|
* - in: query
|
|
* name: bibleId
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.get("/search", async (req, res) => {
|
|
const query = req.query.query;
|
|
const limit = req.query.limit || 10;
|
|
const bibleId = req.query.bibleId || defaultBibleId;
|
|
const bibles = await fetchAPI('bibles/' + bibleId + "/search?query=" + query + "&limit=" + limit);
|
|
return res.json(bibles);
|
|
});
|
|
|
|
});
|
|
|
|
module.exports = router |