From f524d70fca9bfee79510ea13cbd31b8ca11eb240 Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 20 Dec 2021 18:56:59 +0800 Subject: [PATCH] feat: add `ctx.api.convertFile` method --- src/renderer/support/api.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/renderer/support/api.ts b/src/renderer/support/api.ts index 50253e24..585eee4e 100644 --- a/src/renderer/support/api.ts +++ b/src/renderer/support/api.ts @@ -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 = { @@ -10,6 +10,10 @@ export type ApiResult = { 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> { ) } +/** + * Convert file + * @param source + * @param fromType + * @param toType + * @returns + */ +export async function convertFile ( + source: string, fromType: 'html' | 'markdown', + toType: Exclude +): Promise { + return fetchHttp(`/api/convert/export.${toType}`, { + method: 'post', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ source, fromType, toType }), + }) +} + /** * Run code. * @param language