steps.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Defines which type of content a Step returns.
  3. * Add an entry here when you define a step
  4. */
  5. export type StepDefinition = {
  6. FILE_IMPORT: {
  7. returnType: string
  8. metadata: {
  9. caption: string
  10. acceptedFileTypes: string
  11. }
  12. } // String content of the file
  13. TARGET_MY_COLLECTION: {
  14. returnType: number
  15. metadata: {
  16. caption: string
  17. }
  18. } // folderPath
  19. URL_IMPORT: {
  20. returnType: string
  21. metadata: {
  22. caption: string
  23. placeholder: string
  24. }
  25. } // String content of the url
  26. }
  27. export type StepReturnValue = StepDefinition[keyof StepDefinition]["returnType"]
  28. /**
  29. * Defines what the data structure of a step
  30. */
  31. export type Step<T extends keyof StepDefinition> =
  32. StepDefinition[T]["metadata"] extends never
  33. ? {
  34. name: T
  35. caption?: string
  36. metadata: undefined
  37. }
  38. : {
  39. name: T
  40. caption?: string
  41. metadata: StepDefinition[T]["metadata"]
  42. }
  43. /**
  44. * The return output value of an individual step
  45. */
  46. export type StepReturnType<T> = T extends Step<infer U>
  47. ? StepDefinition[U]["returnType"]
  48. : never
  49. export type StepMetadata<T> = T extends Step<infer U>
  50. ? StepDefinition[U]["metadata"]
  51. : never
  52. /**
  53. * Defines the value of the output list generated by a step
  54. */
  55. export type StepsOutputList<T> = {
  56. [K in keyof T]: StepReturnType<T[K]>
  57. }
  58. type StepFuncInput<T extends keyof StepDefinition> =
  59. StepDefinition[T]["metadata"] extends never
  60. ? {
  61. stepName: T
  62. caption?: string
  63. }
  64. : {
  65. stepName: T
  66. caption?: string
  67. metadata: StepDefinition[T]["metadata"]
  68. }
  69. /** Use this function to define a step */
  70. export const step = <T extends keyof StepDefinition>(input: StepFuncInput<T>) =>
  71. <Step<T>>{
  72. name: input.stepName,
  73. metadata: (input as any).metadata ?? undefined,
  74. caption: input.caption,
  75. }