From 5175a86145f2a8a94a473909cacf940c7c98731d Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 23 Dec 2021 14:43:46 +0530 Subject: [PATCH] feat: steps system infra --- .../helpers/import-export/import/hopp.ts | 35 ++++++++-------- .../helpers/import-export/import/index.ts | 38 +++++++++++------- .../helpers/import-export/steps.ts | 40 +++++++++++++++++++ 3 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 packages/hoppscotch-app/helpers/import-export/steps.ts diff --git a/packages/hoppscotch-app/helpers/import-export/import/hopp.ts b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts index 66be98062..5c64275e5 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/hopp.ts @@ -1,23 +1,22 @@ -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" +import { step } from "../steps" +import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "." +import { translateToNewRESTCollection } from "~/newstore/collections" -const importer: HoppImporter[]> = (content) => - pipe( - E.tryCatch( - () => - JSON.parse(content).map((coll: any) => - translateToNewRESTCollection(coll) - ), - () => IMPORTER_INVALID_FILE_FORMAT +export default defineImporter({ + name: "Hoppscotch REST Collection", + steps: [step("FILE_OR_URL_IMPORT", "Select a file or URL")] as const, + importer: ([content]) => + pipe( + E.tryCatch( + () => + JSON.parse(content).map((coll: any) => + translateToNewRESTCollection(coll) + ), + () => IMPORTER_INVALID_FILE_FORMAT + ), + TE.fromEither ), - 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 index 239d06052..359d73876 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/index.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/index.ts @@ -1,6 +1,6 @@ import * as TE from "fp-ts/TaskEither" -import { HoppRESTRequest } from "@hoppscotch/data" -import { Collection } from "~/newstore/collections" +import { StepsOutputList } from "../steps" +import HoppRESTCollImporter from "../import/hopp" /** * The error state to be used when the file formats do not match @@ -10,14 +10,14 @@ export const IMPORTER_INVALID_FILE_FORMAT = export type HoppImporterError = typeof IMPORTER_INVALID_FILE_FORMAT -export type HoppImporter = ( - content: string +type HoppImporter = ( + stepValues: StepsOutputList ) => TE.TaskEither /** * Definition for importers */ -export type HoppImporterDefintion = { +type HoppImporterDefintion = { /** * Name of the importer, shown on the Select Importer dropdown */ @@ -26,14 +26,24 @@ export type HoppImporterDefintion = { /** * The importer function, It is a Promise because its supposed to be loaded in lazily (dynamic imports ?) */ - importer: () => Promise> + importer: HoppImporter + + /** + * The steps to fetch information required to run an importer + */ + steps: Y } -export const RESTCollectionImporters: HoppImporterDefintion< - Collection[] ->[] = [ - { - name: "Hoppscotch REST Collection", - importer: () => import("./hopp").then((m) => m.default), - }, -] +/** + * Defines a Hoppscotch importer + */ +export const defineImporter = (input: { + name: string + importer: HoppImporter + steps: StepType +}) => + >{ + ...input, + } + +export const RESTCollectionImporters = [HoppRESTCollImporter] as const diff --git a/packages/hoppscotch-app/helpers/import-export/steps.ts b/packages/hoppscotch-app/helpers/import-export/steps.ts new file mode 100644 index 000000000..cadcecb30 --- /dev/null +++ b/packages/hoppscotch-app/helpers/import-export/steps.ts @@ -0,0 +1,40 @@ +/** + * Defines which type of content a Step returns. + * Add an entry here when you define a step + */ +export type StepReturnTypes = { + FILE_OR_URL_IMPORT: string // String content of the file/url + TARGET_MY_COLLECTION: number // folderPath +} + +/** + * Defines what the data structure of a step + */ +export type Step = { + name: T + caption?: string +} + +/** + * The return output value of an individual step + */ +export type StepReturnType = T extends Step + ? StepReturnTypes[U] + : never + +/** + * Defines the value of the output list generated by a step + */ +export type StepsOutputList = { + [K in keyof T]: StepReturnType +} + +/** Use this function to define a step */ +export const step = ( + stepName: T, + caption?: string +) => + >{ + name: stepName, + caption, + }