hopp.ts 912 B

12345678910111213141516171819202122232425262728293031323334
  1. import { pipe } from "fp-ts/function"
  2. import * as TE from "fp-ts/TaskEither"
  3. import * as E from "fp-ts/Either"
  4. import { translateToNewRESTCollection } from "@hoppscotch/data"
  5. import { step } from "../steps"
  6. import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
  7. export default defineImporter({
  8. name: "import.from_json",
  9. icon: "folder-plus",
  10. steps: [
  11. step({
  12. stepName: "FILE_IMPORT",
  13. metadata: {
  14. caption: "import.from_json_description",
  15. acceptedFileTypes: "application/json",
  16. },
  17. }),
  18. ] as const,
  19. importer: ([content]) =>
  20. pipe(
  21. E.tryCatch(
  22. () => {
  23. const x = JSON.parse(content)
  24. return Array.isArray(x)
  25. ? x.map((coll: any) => translateToNewRESTCollection(coll))
  26. : [translateToNewRESTCollection(x)]
  27. },
  28. () => IMPORTER_INVALID_FILE_FORMAT
  29. ),
  30. TE.fromEither
  31. ),
  32. })