0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 16:22:44 +00:00

renaming "get" functions

rename `getStaticTheme` to `getStaticThemeCSS`
rename `getBrewThemeWithCSS` to `getBrewThemeCSS`
rename `getBrewThemeParent` to `getBrewThemeParentCSS`

to avoid confusion with other "get" endpoints like `getBrew`, and unify naming for endpoint functions that return CSS.

Simplify `isStaticTheme` function (getting the parent theme is handled elsewhere)
This commit is contained in:
Trevor Buckner
2024-07-10 14:15:03 -04:00
parent 656edb07ea
commit a247e50c9f
3 changed files with 29 additions and 31 deletions

View File

@@ -9,7 +9,7 @@ const yaml = require('js-yaml');
const app = express();
const config = require('./config.js');
const { homebrewApi, getBrew, getBrewThemeWithCSS, getStaticTheme, getBrewThemeParent } = require('./homebrew.api.js');
const { homebrewApi, getBrew, getBrewThemeCSS, getStaticThemeCSS, getBrewThemeParentCSS } = require('./homebrew.api.js');
const GoogleActions = require('./googleActions.js');
const serveCompressedStaticAssets = require('./static-assets.mv.js');
const sanitizeFilename = require('sanitize-filename');
@@ -79,9 +79,9 @@ app.get('/robots.txt', (req, res)=>{
// Theme
app.get('/css/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeWithCSS));
app.get('/css/:engine/:id/', asyncHandler(getStaticTheme));
app.get('/cssParent/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeParent));
app.get('/css/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeCSS));
app.get('/css/:engine/:id/', asyncHandler(getStaticThemeCSS));
app.get('/cssParent/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeParentCSS));
//Home page

View File

@@ -13,17 +13,10 @@ var url = require('url');
const { DEFAULT_BREW, DEFAULT_BREW_LOAD } = require('./brewDefaults.js');
const themes = require('../themes/themes.json');
const Themes = require('../themes/themes.json');
const isStaticTheme = (engine, themeName)=>{
if(!themes.hasOwnProperty(engine)) {
return undefined;
}
if(themes[engine].hasOwnProperty(themeName)) {
return themes[engine][themeName].baseTheme;
} else {
return undefined;
}
const isStaticTheme = (renderer, themeName)=>{
return Themes[renderer]?.[themeName] !== undefined;
};
// const getTopBrews = (cb) => {
@@ -281,35 +274,40 @@ const api = {
res.status(200).send(saved);
},
getBrewThemeWithCSS : async (req, res)=>{
getBrewThemeCSS : async (req, res)=>{
const brew = req.brew;
console.log(`getBrewThemeCSS for ${brew.shareId}`)
splitTextStyleAndMetadata(brew);
res.setHeader('Content-Type', 'text/css');
const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
const themePath = Themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
// Drop Parent theme if it has already been loaded.
// This assumes the continued use of the V3/5ePHB and V3/Blank themes for the app.
console.log(`and parentThemeImport for ${brew.theme}`)
const parentThemeImport = ((req.brew.theme != '5ePHB') && (req.brew.theme != 'Blank')) ? `@import url(\"${themePath}\");\n\n`:'';
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}${req.brew.style}`);
},
getBrewThemeParent : async (req, res)=>{
getBrewThemeParentCSS : async (req, res)=>{
const brew = req.brew;
console.log(`getBrewThemeParentCSS for ${brew.shareId}`)
splitTextStyleAndMetadata(brew);
res.setHeader('Content-Type', 'text/css');
const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
console.log(`getBrewThemeParentCSS parent is ${brew.theme}`)
const themePath = Themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
const parentThemeImport = `@import url(\"${themePath}\");\n\n`;
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}`);
},
getStaticTheme : async(req, res)=>{
const themeParent = isStaticTheme(req.params.engine, req.params.id);
if(themeParent === undefined){
res.status(404).send(`Invalid Theme - Engine: ${req.params.engine}, Name: ${req.params.id}`);
} else {
getStaticThemeCSS : async(req, res)=>{
if (!isStaticTheme(req.params.engine, req.params.id))
res.status(404).send(`Invalid Theme - Renderer: ${req.params.engine}, Name: ${req.params.id}`);
else {
const themeParent = Themes[req.params.engine][req.params.id].baseTheme;
console.log(`getStaticThemeCSS for ${themeParent}`)
res.setHeader('Content-Type', 'text/css');
res.setHeader('Cache-Control', 'public, max-age: 43200, must-revalidate');
const parentTheme = themeParent ? `@import url(\"/css/${req.params.engine}/${themeParent}\");\n/* Static Theme ${themes[req.params.engine][themeParent].name} */\n` : '';
return res.status(200).send(`${parentTheme}@import url(\"/themes/${req.params.engine}/${req.params.id}/style.css\");\n/* Static Theme ${themes[req.params.engine][req.params.id].name} */\n`);
const parentTheme = themeParent ? `@import url(\"/css/${req.params.engine}/${themeParent}\");\n/* Static Theme ${Themes[req.params.engine][themeParent].name} */\n` : '';
return res.status(200).send(`${parentTheme}@import url(\"/themes/${req.params.engine}/${req.params.id}/style.css\");\n/* Static Theme ${Themes[req.params.engine][req.params.id].name} */\n`);
}
},
updateBrew : async (req, res)=>{

View File

@@ -594,7 +594,7 @@ brew`);
const next = jest.fn();
await fn(req, null, next);
api.getBrewThemeWithCSS(req, res);
api.getBrewThemeCSS(req, res);
const sent = res.send.mock.calls[0][0];
expect(sent).toBe(`@import url("/css/V3/5eDMG");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
expect(res.status).toHaveBeenCalledWith(200);
@@ -610,14 +610,14 @@ brew`);
const next = jest.fn();
await fn(req, null, next);
api.getBrewThemeWithCSS(req, res);
api.getBrewThemeCSS(req, res);
const sent = res.send.mock.calls[0][0];
expect(sent).toBe(`@import url("/css/IamATheme");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
expect(res.status).toHaveBeenCalledWith(200);
});
});
describe('getStaticTheme', ()=>{
describe('getStaticThemeCSS', ()=>{
it('should return an import of the theme without including a parent.', async ()=>{
const req = {
params : {
@@ -625,7 +625,7 @@ brew`);
id : '5ePHB'
}
};
api.getStaticTheme(req, res);
api.getStaticThemeCSS(req, res);
const sent = res.send.mock.calls[0][0];
expect(sent).toBe('@import url("/themes/V3/5ePHB/style.css");\n/* Static Theme 5e PHB */\n');
expect(res.status).toHaveBeenCalledWith(200);
@@ -637,7 +637,7 @@ brew`);
id : '5eDMG'
}
};
api.getStaticTheme(req, res);
api.getStaticThemeCSS(req, res);
const sent = res.send.mock.calls[0][0];
expect(sent).toBe('@import url("/css/V3/5ePHB");\n/* Static Theme 5e PHB */\n@import url("/themes/V3/5eDMG/style.css");\n/* Static Theme 5e DMG */\n');
expect(res.status).toHaveBeenCalledWith(200);
@@ -649,7 +649,7 @@ brew`);
id : '5eDMGGGG'
}
};
api.getStaticTheme(req, res);
api.getStaticThemeCSS(req, res);
const sent = res.send.mock.calls[0][0];
expect(sent).toBe('Invalid Theme - Engine: V3, Name: 5eDMGGGG');
expect(res.status).toHaveBeenCalledWith(404);