feat: add ctx.api.convertFile method

This commit is contained in:
purocean
2021-12-20 18:56:59 +08:00
parent a2847f7d87
commit f524d70fca
+23 -1
View File
@@ -1,4 +1,4 @@
import type { Components, Doc, FileItem, PathItem } from '@fe/types'
import type { Components, Doc, ExportTypes, FileItem, PathItem } from '@fe/types'
import { isElectron } from '@fe/support/env'
export type ApiResult<T = any> = {
@@ -10,6 +10,10 @@ export type ApiResult<T = any> = {
async function fetchHttp (input: RequestInfo, init?: RequestInit) {
const response = await fetch(input, init)
if (!response.headers.get('content-type')?.includes('json')) {
return response
}
let result: any = null
try {
result = await response.json()
@@ -244,6 +248,24 @@ export async function deleteTmpFile (name: string): Promise<ApiResult<any>> {
)
}
/**
* Convert file
* @param source
* @param fromType
* @param toType
* @returns
*/
export async function convertFile (
source: string, fromType: 'html' | 'markdown',
toType: Exclude<ExportTypes, 'pdf'>
): Promise<Response> {
return fetchHttp(`/api/convert/export.${toType}`, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source, fromType, toType }),
})
}
/**
* Run code.
* @param language