generated from azures04/Base-REST-API
Added product route
Added a single product route to get metadata and products list
This commit is contained in:
parent
4295d6bca3
commit
4e70d59c63
15
routes/products/index.js
Normal file
15
routes/products/index.js
Normal file
@ -0,0 +1,15 @@
|
||||
const express = require("express")
|
||||
const router = express.Router({ mergeParams: false })
|
||||
const productsFileService = require("../../services/productsFileService")
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const products = await productsFileService.getProducts()
|
||||
return res.status(200).json(products)
|
||||
})
|
||||
|
||||
router.get("/product/:productName", async (req, res) => {
|
||||
const product = await productsFileService.getProduct(req.params.productName)
|
||||
return res.status(200).json(product)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -85,7 +85,7 @@ async function canAccess(productName, productPlatform) {
|
||||
|
||||
try {
|
||||
const brikConfig = JSON.parse(await fs.promises.readFile(fixedPath))
|
||||
if (!brikConfig.canAccess) {
|
||||
if (!brikConfig.public) {
|
||||
throw new DefaultError(403, "Forbidden", "Locked resource.", "InsuffisentPrivilegeException")
|
||||
}
|
||||
return { code: 200 }
|
||||
@ -98,9 +98,37 @@ async function canAccess(productName, productPlatform) {
|
||||
|
||||
}
|
||||
|
||||
async function getProduct(productName) {
|
||||
const fixedPath = path.join(productsDataPath, productName, ".brikpkg")
|
||||
const fileState = await fs.promises.exists(fixedPath, fs.constants.F_OK)
|
||||
if (!fileState) {
|
||||
throw new DefaultError(404, "Not found", "MissingDir", "NotFoundException")
|
||||
}
|
||||
|
||||
try {
|
||||
const brikPackage = JSON.parse(await fs.promises.readFile(fixedPath))
|
||||
if (!brikPackage.public) {
|
||||
throw new DefaultError(403, "Forbidden", "Locked resource.", "InsuffisentPrivilegeException")
|
||||
}
|
||||
return brikPackage
|
||||
} catch (error) {
|
||||
if (!error instanceof DefaultError) {
|
||||
throw new DefaultError(500, "Please contact the maintener", "CONFIGURATION", "0x00")
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function getProducts() {
|
||||
const files = await fs.promises.readdir(path.join(productsDataPath), { recursive: false })
|
||||
return files
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFile,
|
||||
canAccess,
|
||||
getProduct,
|
||||
getFileSha1,
|
||||
getProducts,
|
||||
getProductFiles
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user