gist.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. id: "gist",
  32. name: "import.from_gist",
  33. icon: "github",
  34. applicableTo: ["my-collections", "team-collections"],
  35. steps: [
  36. step({
  37. stepName: "URL_IMPORT",
  38. metadata: {
  39. caption: "import.from_gist_description",
  40. placeholder: "import.gist_url",
  41. },
  42. }),
  43. ] as const,
  44. importer: ([content]) =>
  45. pipe(
  46. fetchGist(content),
  47. TE.fromTaskOption(() => IMPORTER_INVALID_FILE_FORMAT)
  48. ),
  49. })