gist.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { pipe } from "fp-ts/function"
  2. import * as TE from "fp-ts/TaskEither"
  3. import * as TO from "fp-ts/TaskOption"
  4. import * as O from "fp-ts/Option"
  5. import axios from "axios"
  6. import { HoppRESTRequest, HoppCollection } from "@hoppscotch/data"
  7. import { step } from "../steps"
  8. import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
  9. // TODO: Add validation to output
  10. const fetchGist = (
  11. url: string
  12. ): TO.TaskOption<HoppCollection<HoppRESTRequest>> =>
  13. pipe(
  14. TO.tryCatch(() =>
  15. axios.get(`https://api.github.com/gists/${url.split("/").pop()}`, {
  16. headers: {
  17. Accept: "application/vnd.github.v3+json",
  18. },
  19. })
  20. ),
  21. TO.chain((res) =>
  22. pipe(
  23. O.tryCatch(() =>
  24. JSON.parse((Object.values(res.data.files)[0] as any).content)
  25. ),
  26. TO.fromOption
  27. )
  28. )
  29. )
  30. export default defineImporter({
  31. name: "import.from_gist",
  32. icon: "github",
  33. steps: [
  34. step({
  35. stepName: "URL_IMPORT",
  36. metadata: {
  37. caption: "import.from_gist_description",
  38. placeholder: "import.gist_url",
  39. },
  40. }),
  41. ] as const,
  42. importer: ([content]) =>
  43. pipe(
  44. fetchGist(content),
  45. TE.fromTaskOption(() => IMPORTER_INVALID_FILE_FORMAT)
  46. ),
  47. })