(props: {
open: boolean;
setOpen: (open: boolean) => void;
loadActions: () => Promise<void>;
api_id: string | undefined;
updateSelectedApiTab: (apiId: string | undefined) => void;
})
| 28 | } |
| 29 | |
| 30 | export default function UploadModal(props: { |
| 31 | open: boolean; |
| 32 | setOpen: (open: boolean) => void; |
| 33 | loadActions: () => Promise<void>; |
| 34 | api_id: string | undefined; |
| 35 | updateSelectedApiTab: (apiId: string | undefined) => void; |
| 36 | }) { |
| 37 | const { profile } = useProfile(); |
| 38 | const [isLoading, setIsLoading] = React.useState<boolean>(false); |
| 39 | const [error, setError] = React.useState<{ |
| 40 | message: string; |
| 41 | error: Record<string, any>; |
| 42 | } | null>(null); |
| 43 | |
| 44 | useEffect(() => { |
| 45 | if (!profile) return; |
| 46 | uppy = new Uppy({ |
| 47 | autoProceed: true, |
| 48 | allowMultipleUploadBatches: false, |
| 49 | restrictions: { |
| 50 | maxNumberOfFiles: 1, |
| 51 | maxFileSize: 100_000_000, |
| 52 | allowedFileTypes: ["application/json", ".yaml", ".txt"], |
| 53 | }, |
| 54 | }); |
| 55 | uppy.on("file-added", async (file: UppyFile) => { |
| 56 | if (isLoading) return; |
| 57 | setError(null); |
| 58 | setIsLoading(true); |
| 59 | const text = await file.data.text(); |
| 60 | let json: Record<string, any>; |
| 61 | try { |
| 62 | // First, try JSON |
| 63 | json = JSON.parse(text); |
| 64 | } catch (e) { |
| 65 | try { |
| 66 | // Try yaml |
| 67 | json = parse(text); |
| 68 | } catch (e) { |
| 69 | setError({ |
| 70 | message: |
| 71 | "Uploaded file contents has invalid format: must be JSON or YAML", |
| 72 | error: {}, |
| 73 | }); |
| 74 | setIsLoading(false); |
| 75 | uppy.removeFile(file.id); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | const api_id = props.api_id; |
| 80 | const res = await fetch("/api/swagger-to-actions", { |
| 81 | method: "POST", |
| 82 | headers: { |
| 83 | "Content-Type": "application/json", |
| 84 | }, |
| 85 | body: JSON.stringify({ |
| 86 | swagger: json, |
| 87 | api_id, |
nothing calls this directly
no test coverage detected