Skip to content

Commit

Permalink
feat: new endpoint for retrieving information on all folders
Browse files Browse the repository at this point in the history
This commit introduces a new GET `/:siteName/folders/all` endpoint
which:
1) gets the names of all collections/folders within the repo
2) gets the collection.yml directory config file for each collection
  • Loading branch information
SingpassAuth committed Mar 3, 2021
1 parent ddf0c8c commit 5980779
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ auth.post('/v1/sites/:siteName/homepage', verifyJwt)

// Folder pages
auth.get('/v1/sites/:siteName/folders', verifyJwt)
auth.get('/v1/sites/:siteName/folders/all', verifyJwt)

// Collection pages
auth.get('/v1/sites/:siteName/collections/:collectionName', verifyJwt)
Expand Down
41 changes: 38 additions & 3 deletions routes/folders.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const express = require('express');
const router = express.Router();
const Bluebird = require('bluebird')

// Import middleware
const { attachReadRouteHandlerWrapper } = require('../middleware/routeHandler')

// Import classes
const { Directory, FolderType } = require('../classes/Directory.js');
// Import classes
const { File, CollectionPageType } = require('../classes/File.js');
const { Directory, RootType, FolderType } = require('../classes/Directory.js');

const ISOMER_TEMPLATE_DIRS = ['_data', '_includes', '_site', '_layouts']

// List pages and directories in folder
async function listFolderContent (req, res, next) {
Expand All @@ -20,6 +24,37 @@ async function listFolderContent (req, res, next) {
res.status(200).json({ folderPages })
}

router.get('/:siteName/folders', attachReadRouteHandlerWrapper(listFolderContent))
// List pages and directories from all folders
async function listAllFolderContent (req, res, next) {
const { accessToken } = req
const { siteName } = req.params

const IsomerDirectory = new Directory(accessToken, siteName)
const folderType = new RootType()
IsomerDirectory.setDirType(folderType)
const repoRootContent = await IsomerDirectory.list()

const allFolders = repoRootContent.reduce((acc, curr) => {
if (
curr.type === 'dir'
&& !ISOMER_TEMPLATE_DIRS.includes(curr.name)
&& curr.name.slice(0, 1) === '_'
) acc.push(curr.path)
return acc
}, [])

const allFolderContent = await Bluebird.map(allFolders, async (folder) => {
const IsomerFile = new File(accessToken, siteName)
const collectionPageType = new CollectionPageType(folder.slice(1))
IsomerFile.setFileType(collectionPageType)
const { sha, content } = await IsomerFile.read('collection.yml')
return { name: folder, sha, content }
})

res.status(200).json({ allFolderContent })
}

router.get('/:siteName/folders', attachRouteHandlerWrapper(listFolderContent))
router.get('/:siteName/folders/all', attachRouteHandlerWrapper(listAllFolderContent))

module.exports = router;

0 comments on commit 5980779

Please sign in to comment.