mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
start over with nextjs
This commit is contained in:
24
pages/_app.tsx
Normal file
24
pages/_app.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
// import App from "next/app";
|
||||
import type { AppProps /*, AppContext */ } from 'next/app'
|
||||
|
||||
import '../styles/index.css'
|
||||
import '../styles/App.css'
|
||||
import '../styles/LetterOptions.css'
|
||||
|
||||
function MyApp({ Component, pageProps }: AppProps) {
|
||||
return <Component {...pageProps} />
|
||||
}
|
||||
|
||||
// Only uncomment this method if you have blocking data requirements for
|
||||
// every single page in your application. This disables the ability to
|
||||
// perform automatic static optimization, causing every page in your app to
|
||||
// be server-side rendered.
|
||||
//
|
||||
// MyApp.getInitialProps = async (appContext: AppContext) => {
|
||||
// // calls page's `getInitialProps` and fills `appProps.pageProps`
|
||||
// const appProps = await App.getInitialProps(appContext);
|
||||
|
||||
// return { ...appProps }
|
||||
// }
|
||||
|
||||
export default MyApp
|
||||
45
pages/api/pdf/[id].ts
Normal file
45
pages/api/pdf/[id].ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import {promises} from 'fs'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
import {remove as storeRemove} from '../../../lib/store'
|
||||
import { getPdfPath } from '../../../lib/utils'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const { method } = req;
|
||||
|
||||
switch (req.method) {
|
||||
case 'GET':
|
||||
try {
|
||||
const {
|
||||
query: { id: idArg },
|
||||
} = req
|
||||
|
||||
const fileContent = await promises.readFile(getPdfPath(idArg))
|
||||
res.setHeader('Content-Type', 'application/pdf')
|
||||
res.status(200).send(fileContent);
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
|
||||
}
|
||||
break;
|
||||
case 'DELETE':
|
||||
try {
|
||||
const {
|
||||
query: { id: idArg },
|
||||
} = req
|
||||
|
||||
storeRemove(idArg)
|
||||
|
||||
res.status(202).end()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res.setHeader('Allow', ['GET', 'DELETE'])
|
||||
res.status(405).end(`Method ${method} Not Allowed`)
|
||||
}
|
||||
}
|
||||
|
||||
export default handler;
|
||||
45
pages/api/pdf/generate/[template].ts
Normal file
45
pages/api/pdf/generate/[template].ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
import { brief as briefTemplate } from '../../../../lib/templates'
|
||||
import renderer from '../../../../lib/renderer'
|
||||
import * as store from '../../../../lib/store'
|
||||
|
||||
const TEMPLATES : { [key: string]: (options: object) => string; } = {
|
||||
brief: briefTemplate
|
||||
};
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
if (req.method !== 'POST') {
|
||||
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const {
|
||||
query: { template: templateArg },
|
||||
} = req
|
||||
const options = req.body;
|
||||
const templateName = Array.isArray(templateArg) ? templateArg[0] : templateArg;
|
||||
const template = TEMPLATES[templateName];
|
||||
|
||||
if (!template) {
|
||||
res.status(404).json({ statusCode: 404, message: 'Template not availabe' })
|
||||
return;
|
||||
}
|
||||
|
||||
const texDoc = template(options)
|
||||
const id = await renderer(texDoc)
|
||||
const storeData = Object.assign({}, options, {
|
||||
id,
|
||||
created: new Date().toISOString()
|
||||
});
|
||||
await store.add(storeData)
|
||||
res.status(200).json({ id: id });
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error:', err, 'for', req.url);
|
||||
res.status(500).json({ error: err.toString() });
|
||||
}
|
||||
}
|
||||
|
||||
export default handler
|
||||
21
pages/api/pdf/latest.ts
Normal file
21
pages/api/pdf/latest.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
import { list as storeList } from '../../../lib/store'
|
||||
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
if (req.method !== 'GET') {
|
||||
res.status(405).json({ statusCode: 405, message: 'Method Not Allowed' })
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const latest = await storeList();
|
||||
res.status(200).json(latest);
|
||||
} catch (err) {
|
||||
console.error('Error:', err, 'for', req.url);
|
||||
res.status(500).json({ error: err.toString() });
|
||||
}
|
||||
}
|
||||
|
||||
export default handler
|
||||
10
pages/index.tsx
Normal file
10
pages/index.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import Layout from '../components/Layout'
|
||||
import App from '../components/App'
|
||||
|
||||
const IndexPage = () => (
|
||||
<Layout title="Home | Next.js + TypeScript Example">
|
||||
<App/>
|
||||
</Layout>
|
||||
)
|
||||
|
||||
export default IndexPage
|
||||
Reference in New Issue
Block a user