Skip to content

Commit

Permalink
extract lib
Browse files Browse the repository at this point in the history
  • Loading branch information
curly210102 committed Nov 4, 2021
1 parent b78516a commit c974917
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 10 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { resolve, join } from "path";
import { readFileSync } from "fs";
export const getTemplate = (slug) => {
const templateDirectory = resolve(process.cwd(), "email");
const emailTemplate = readFileSync(
join(templateDirectory, slug, "template.mjml"),
"utf8"
);
return emailTemplate;
};
10 changes: 2 additions & 8 deletions pages/api/contact.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const { join, resolve } = require("path");
const { readFileSync } = require("fs");
import type { NextApiRequest, NextApiResponse } from "next";
import { getTemplate } from "../../lib/api";

export default async (req: NextApiRequest, res: NextApiResponse) => {
const { slug } = req.query;
const templateDirectory = resolve(process.cwd(), "email");
const emailTemplate = readFileSync(
join(templateDirectory, slug, "template.mjml"),
"utf8"
);

res.status(200).json({
content: emailTemplate,
content: getTemplate(slug),
});
};

0 comments on commit c974917

Please sign in to comment.