From 681a957611fa02f337679ecfcb199f58ce5fdbc5 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 22 Dec 2021 17:36:49 +0530 Subject: [PATCH] refactor: import/export new architecture --- .../helpers/import-export/export/hopp.ts | 10 +++++ .../helpers/import-export/export/index.ts | 19 +++++++++ .../helpers/import-export/import/hopp.ts | 20 ++++++++++ .../helpers/import-export/import/index.ts | 39 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 packages/hoppscotch-app/helpers/import-export/export/hopp.ts create mode 100644 packages/hoppscotch-app/helpers/import-export/export/index.ts create mode 100644 packages/hoppscotch-app/helpers/import-export/import/hopp.ts create mode 100644 packages/hoppscotch-app/helpers/import-export/import/index.ts diff --git a/packages/hoppscotch-app/helpers/import-export/export/hopp.ts b/packages/hoppscotch-app/helpers/import-export/export/hopp.ts new file mode 100644 index 000000000..900079412 --- /dev/null +++ b/packages/hoppscotch-app/helpers/import-export/export/hopp.ts @@ -0,0 +1,10 @@ +import { HoppRESTRequest } from "@hoppscotch/data" +import { pipe } from "fp-ts/function" +import * as TE from "fp-ts/TaskEither" +import { HoppExporter } from "." +import { Collection } from "~/newstore/collections" + +const exporter: HoppExporter> = (content) => + pipe(content, JSON.stringify, TE.right) + +export default exporter diff --git a/packages/hoppscotch-app/helpers/import-export/export/index.ts b/packages/hoppscotch-app/helpers/import-export/export/index.ts new file mode 100644 index 000000000..058fa57b4 --- /dev/null +++ b/packages/hoppscotch-app/helpers/import-export/export/index.ts @@ -0,0 +1,19 @@ +import * as TE from "fp-ts/TaskEither" +import { HoppRESTRequest } from "@hoppscotch/data" +import { Collection } from "~/newstore/collections" + +export type HoppExporter = (content: T) => TE.TaskEither + +export type HoppExporterDefintion = { + name: string + exporter: () => Promise> +} + +export const RESTCollectionExporters: HoppExporterDefintion< + Collection +>[] = [ + { + name: "Hoppscotch REST Collection JSON", + exporter: () => import("./hopp").then((m) => m.default), + }, +] diff --git a/packages/hoppscotch-app/helpers/import-export/import/hopp.ts b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts new file mode 100644 index 000000000..bd7877c06 --- /dev/null +++ b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts @@ -0,0 +1,20 @@ +import { HoppRESTRequest } from "@hoppscotch/data" +import { pipe } from "fp-ts/function" +import * as TE from "fp-ts/TaskEither" +import * as E from "fp-ts/Either" +import { HoppImporter, IMPORTER_INVALID_FILE_FORMAT } from "." +import { + Collection, + translateToNewRESTCollection, +} from "~/newstore/collections" + +const importer: HoppImporter> = (content) => + pipe( + E.tryCatch( + () => translateToNewRESTCollection(content), + () => IMPORTER_INVALID_FILE_FORMAT + ), + TE.fromEither + ) + +export default importer diff --git a/packages/hoppscotch-app/helpers/import-export/import/index.ts b/packages/hoppscotch-app/helpers/import-export/import/index.ts new file mode 100644 index 000000000..090c23f03 --- /dev/null +++ b/packages/hoppscotch-app/helpers/import-export/import/index.ts @@ -0,0 +1,39 @@ +import * as TE from "fp-ts/TaskEither" +import { HoppRESTRequest } from "@hoppscotch/data" +import { Collection } from "~/newstore/collections" + +/** + * The error state to be used when the file formats do not match + */ +export const IMPORTER_INVALID_FILE_FORMAT = + "importer_invalid_file_format" as const + +export type HoppImporterError = typeof IMPORTER_INVALID_FILE_FORMAT + +export type HoppImporter = ( + content: string +) => TE.TaskEither + +/** + * Definition for importers + */ +export type HoppImporterDefintion = { + /** + * Name of the importer, shown on the Select Importer dropdown + */ + name: string + + /** + * The importer function, It is a Promise because its supposed to be loaded in lazily (dynamic imports ?) + */ + importer: () => Promise> +} + +export const RESTCollectionImporters: HoppImporterDefintion< + Collection +>[] = [ + { + name: "Hoppscotch REST Collection", + importer: () => import("./hopp").then((m) => m.default), + }, +]