feat: steps system metadata

This commit is contained in:
Andrew Bastin
2021-12-23 15:25:15 +05:30
parent 5175a86145
commit 8ac38d7517
2 changed files with 52 additions and 15 deletions

View File

@@ -7,7 +7,14 @@ import { translateToNewRESTCollection } from "~/newstore/collections"
export default defineImporter({ export default defineImporter({
name: "Hoppscotch REST Collection", name: "Hoppscotch REST Collection",
steps: [step("FILE_OR_URL_IMPORT", "Select a file or URL")] as const, steps: [
step({
stepName: "FILE_OR_URL_IMPORT",
metadata: {
acceptedFileTypes: ["application/json"],
},
}),
] as const,
importer: ([content]) => importer: ([content]) =>
pipe( pipe(
E.tryCatch( E.tryCatch(

View File

@@ -2,24 +2,44 @@
* Defines which type of content a Step returns. * Defines which type of content a Step returns.
* Add an entry here when you define a step * Add an entry here when you define a step
*/ */
export type StepReturnTypes = { export type StepDefinition = {
FILE_OR_URL_IMPORT: string // String content of the file/url FILE_OR_URL_IMPORT: {
TARGET_MY_COLLECTION: number // folderPath returnType: string
metadata: {
acceptedFileTypes: string[]
}
} // String content of the file/url
TARGET_MY_COLLECTION: {
returnType: number
metadata: never
} // folderPath
} }
/** /**
* Defines what the data structure of a step * Defines what the data structure of a step
*/ */
export type Step<T extends keyof StepReturnTypes> = { export type Step<T extends keyof StepDefinition> =
name: T StepDefinition[T]["metadata"] extends never
caption?: string ? {
} name: T
caption?: string
metadata: undefined
}
: {
name: T
caption?: string
metadata: StepDefinition[T]["metadata"]
}
/** /**
* The return output value of an individual step * The return output value of an individual step
*/ */
export type StepReturnType<T> = T extends Step<infer U> export type StepReturnType<T> = T extends Step<infer U>
? StepReturnTypes[U] ? StepDefinition[U]["returnType"]
: never
export type StepMetadata<T> = T extends Step<infer U>
? StepDefinition[U]["metadata"]
: never : never
/** /**
@@ -29,12 +49,22 @@ export type StepsOutputList<T> = {
[K in keyof T]: StepReturnType<T[K]> [K in keyof T]: StepReturnType<T[K]>
} }
type StepFuncInput<T extends keyof StepDefinition> =
StepDefinition[T]["metadata"] extends never
? {
stepName: T
caption?: string
}
: {
stepName: T
caption?: string
metadata: StepDefinition[T]["metadata"]
}
/** Use this function to define a step */ /** Use this function to define a step */
export const step = <T extends keyof StepReturnTypes>( export const step = <T extends keyof StepDefinition>(input: StepFuncInput<T>) =>
stepName: T,
caption?: string
) =>
<Step<T>>{ <Step<T>>{
name: stepName, name: input.stepName,
caption, metadata: (input as any).metadata ?? undefined,
caption: input.caption,
} }