Line data Source code
1 : import { isObject } from 'es-toolkit/compat'
2 :
3 : import { parseJsonWithNonFinite } from '@/utils/jsonUtil'
4 :
5 1035 : export function getDataFromJSON(
6 : file: File
7 : ): Promise<Record<string, object> | undefined> {
8 1035 : return new Promise<Record<string, object> | undefined>((resolve) => {
9 1035 : const reader = new FileReader()
10 1035 : reader.onload = async () => {
11 1035 : try {
12 1035 : if (typeof reader.result !== 'string') {
13 0 : resolve(undefined)
14 0 : return
15 : }
16 1035 : const jsonContent = parseJsonWithNonFinite<Record<string, unknown>>(
17 : reader.result
18 : )
19 1035 : if (jsonContent.templates) {
20 0 : resolve({ templates: jsonContent.templates })
21 0 : return
22 : }
23 1035 : if (isApiJson(jsonContent)) {
24 5 : resolve({ prompt: jsonContent })
25 5 : return
26 : }
27 1030 : resolve({ workflow: jsonContent })
28 : } catch {
29 0 : resolve(undefined)
30 : }
31 : }
32 1035 : reader.onerror = () => resolve(undefined)
33 1035 : reader.onabort = () => resolve(undefined)
34 1035 : reader.readAsText(file)
35 : })
36 : }
37 :
38 1035 : function isApiJson(data: unknown) {
39 1035 : return isObject(data) && Object.values(data).every((v) => v.class_type)
40 : }
|