Skip to content

Commit

Permalink
refactor: add hbs handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Yizack committed Nov 24, 2024
1 parent 3a0efd6 commit e54bc3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
31 changes: 15 additions & 16 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @module api
* @requires appManager
* @requires hbsHandler
* @requires Card
* @requires gistsList
*/
import { hbsRender } from "./../src/appManager.js";
import { defineHbsHandler } from "../src/hbsHandler.js";
import { getCard } from "./../src/card.js";
import { getGists } from "./../src/gistsList.js";

Expand All @@ -14,18 +14,17 @@ import { getGists } from "./../src/gistsList.js";
* @name /api
* @function
* @async
* @param {Object} req Request object
* @param {Object} req.query Query object
* @param {string} req.query.user Github username
* @param {string} req.query.theme Theme name
* @param {number} req.query.n Number of gists to display
* @param {string} req.query.title Title of the card
* @param {Object} res Response object
* @param {Object} event.req Request object
* @param {Object} event.req.query Query object
* @param {string} event.req.query.user Github username
* @param {string} event.req.query.theme Theme name
* @param {number} event.req.query.n Number of gists to display
* @param {string} event.req.query.title Title of the card
* @param {Object} event.res Response object
*/
export default async (req, res) => {
const card = getCard(req.query, await getGists(req.query.user)); // get card
res.setHeader("Cache-Control", "max-age=0, s-maxage=14400");
res.setHeader("Content-Type", "image/svg+xml"); // set content type to svg
const svg = hbsRender("card", card); // render card template
return res.status(200).send(svg);
};
export default defineHbsHandler (async (event) => {
const query = event.req.query;
const gists = await getGists(query.user);
const data = getCard(query, gists); // get card
return data;
}, { template: "card" });
31 changes: 15 additions & 16 deletions api/pin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @memberof api
* @requires appManager
* @requires hbsHandler
* @requires Pin
* @requires gist
*/
import { hbsRender } from "./../src/appManager.js";
import { defineHbsHandler } from "../src/hbsHandler.js";
import { getPin } from "./../src/pin.js";
import { getGist } from "./../src/gist.js";

Expand All @@ -14,18 +14,17 @@ import { getGist } from "./../src/gist.js";
* @name /api/pin
* @function
* @async
* @param {Object} req Request object
* @param {Object} req.query Query object
* @param {string} req.query.user Github username
* @param {string} req.query.id Gist id
* @param {string} req.query.theme Theme name
* @param {boolean} req.query.owner Show gist owner
* @param {Object} res Response object
* @param {Object} event.req Request object
* @param {Object} event.req.query Query object
* @param {string} event.req.query.user Github username
* @param {string} event.req.query.id Gist id
* @param {string} event.req.query.theme Theme name
* @param {boolean} event.req.query.owner Show gist owner
* @param {Object} event.res Response object
*/
export default async (req, res) => {
const pin = await getPin(req.query, await getGist(req.query.id)); // get card
res.setHeader("Cache-Control", "max-age=0, s-maxage=14400");
res.setHeader("Content-Type", "image/svg+xml"); // set content type to svg
const svg = hbsRender("pin", pin); // render pin template
return res.status(200).send(svg);
};
export default defineHbsHandler (async (event) => {
const query = event.req.query;
const gist = await getGist(query.id);
const data = getPin(query, gist); // get card
return data;
}, { template: "pin" });
22 changes: 12 additions & 10 deletions src/appManager.js → src/hbsHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @module appManager
* @module hbsHandler
* @requires hbs
* @requires fs
* @requires path
* @requires url
*/
Expand All @@ -16,13 +17,14 @@ hbs.handlebars.registerHelper("subtract", (lVal, rVal) => lVal - rVal); // regis

const viewsDir = path.join(__dirname, "../templates");

export const hbsRender = (name, data) => {
try {
const templateSource = fs.readFileSync(path.join(viewsDir, `${name}.hbs`)).toString();
const template = hbs.handlebars.compile(templateSource);
return template(data);
}
catch (error) {
throw new Error("Error rendering template: " + error);
}
export const defineHbsHandler = (handle, options) => {
return async (req, res) => {
const data = await handle({ req, res });
const templateSource = fs.readFileSync(path.join(viewsDir, `${options.template}.hbs`)).toString();
const render = hbs.handlebars.compile(templateSource);
const svg = render(data);
res.setHeader("Cache-Control", "max-age=0, s-maxage=14400");
res.setHeader("Content-Type", "image/svg+xml"); // set content type to svg
return res.status(200).send(svg);
};
};

0 comments on commit e54bc3a

Please sign in to comment.