Skip to content

Commit

Permalink
Duplicate of #117 (#119)
Browse files Browse the repository at this point in the history
* feat: add RootType Directory

This commit adds a RootType to the Directory class so that we can
retrieve directories from the root of a repo.

* feat: new endpoint for retrieving information on all folders

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

* fix: remove _ before sending folder name

* fix: use read route handler wrapper

Co-authored-by: jiehao <[email protected]>
  • Loading branch information
kwajiehao and SingpassAuth authored Mar 3, 2021
1 parent 16ae433 commit f725104
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
17 changes: 16 additions & 1 deletion classes/Directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class Directory {
}
}

class RootType {
constructor() {
this.folderName = ''
}

getFolderName() {
return this.folderName
}
}

class FolderType {
constructor(folderPath) {
this.folderName = folderPath
Expand All @@ -118,4 +128,9 @@ class ResourceRoomType {
}
}

module.exports = { Directory, FolderType, ResourceRoomType }
module.exports = {
Directory,
RootType,
FolderType,
ResourceRoomType,
}
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
39 changes: 37 additions & 2 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 })
}

// 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.slice(1), sha, content }
})

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

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

module.exports = router;

0 comments on commit f725104

Please sign in to comment.