mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
change api
This commit is contained in:
@@ -58,22 +58,19 @@ export default function App() {
|
||||
window.location.reload()
|
||||
}
|
||||
|
||||
const _onGenerate = () => {
|
||||
const _onGenerate = async () => {
|
||||
setPdfIsLoading(true)
|
||||
setPdfError(null)
|
||||
|
||||
generatePdf(options)
|
||||
.then((data) => {
|
||||
const { id } = data
|
||||
|
||||
setPdfIsLoading(false)
|
||||
setPdfUrl(`/api/pdf/${id}`)
|
||||
})
|
||||
.catch((error) => {
|
||||
setPdfIsLoading(false)
|
||||
setPdfError(error.message)
|
||||
setPdfUrl(null)
|
||||
})
|
||||
try {
|
||||
const url = await generatePdf(options)
|
||||
setPdfIsLoading(false)
|
||||
setPdfUrl(url)
|
||||
} catch (error) {
|
||||
setPdfIsLoading(false)
|
||||
setPdfError(error.message)
|
||||
setPdfUrl(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -7,7 +7,7 @@ function checkStatus(res: Response) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function generatePdf(state: IDocProps) {
|
||||
export async function generatePdf(state: IDocProps): Promise<string> {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -16,19 +16,20 @@ export async function generatePdf(state: IDocProps) {
|
||||
body: JSON.stringify(state),
|
||||
}
|
||||
|
||||
const response = await fetch('/api/pdf/generate/brief', options)
|
||||
const response = await fetch('/api/document?template=brief', options)
|
||||
checkStatus(response)
|
||||
return response.json()
|
||||
const { id } = await response.json()
|
||||
return `/api/document/${id}`
|
||||
}
|
||||
|
||||
export async function getLatest(): Promise<ILatest[]> {
|
||||
const response = await fetch('/api/pdf/latest')
|
||||
const response = await fetch('/api/document/latest')
|
||||
checkStatus(response)
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export async function removeLatest(item: { id: string }) {
|
||||
const response = await fetch(`/api/pdf/${item.id}`, { method: 'DELETE' })
|
||||
const response = await fetch(`/api/document/${item.id}`, { method: 'DELETE' })
|
||||
checkStatus(response)
|
||||
return response
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
import { brief as briefTemplate } from '../../../../lib/templates'
|
||||
import renderer from '../../../../lib/renderer'
|
||||
import * as store from '../../../../lib/store'
|
||||
import { IDocProps } from '../../../../interfaces/IDocProps'
|
||||
import { brief as briefTemplate } from '../../../lib/templates'
|
||||
import renderer from '../../../lib/renderer'
|
||||
import * as store from '../../../lib/store'
|
||||
import { IDocProps } from '../../../interfaces/IDocProps'
|
||||
|
||||
const TEMPLATES: { [key: string]: (options: IDocProps) => string } = {
|
||||
brief: briefTemplate,
|
||||
@@ -24,7 +24,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const template = TEMPLATES[templateName]
|
||||
|
||||
if (!template) {
|
||||
res.status(404).json({ statusCode: 404, message: 'Template not availabe' })
|
||||
res.status(404).json({ statusCode: 404, message: 'Template not specified availabe' })
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user