index.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @typedef {(error?: Error | null | undefined, ...output: Array<any>) => void} Callback
  3. * Callback.
  4. *
  5. * @typedef {(...input: Array<any>) => any} Middleware
  6. * Ware.
  7. *
  8. * @typedef Pipeline
  9. * Pipeline.
  10. * @property {Run} run
  11. * Run the pipeline.
  12. * @property {Use} use
  13. * Add middleware.
  14. *
  15. * @typedef {(...input: Array<any>) => void} Run
  16. * Call all middleware.
  17. *
  18. * Calls `done` on completion with either an error or the output of the
  19. * last middleware.
  20. *
  21. * > 👉 **Note**: as the length of input defines whether async functions get a
  22. * > `next` function,
  23. * > it’s recommended to keep `input` at one value normally.
  24. *
  25. * @typedef {(fn: Middleware) => Pipeline} Use
  26. * Add middleware.
  27. */
  28. /**
  29. * Create new middleware.
  30. *
  31. * @returns {Pipeline}
  32. * Pipeline.
  33. */
  34. export function trough(): Pipeline;
  35. /**
  36. * Wrap `middleware` into a uniform interface.
  37. *
  38. * You can pass all input to the resulting function.
  39. * `callback` is then called with the output of `middleware`.
  40. *
  41. * If `middleware` accepts more arguments than the later given in input,
  42. * an extra `done` function is passed to it after that input,
  43. * which must be called by `middleware`.
  44. *
  45. * The first value in `input` is the main input value.
  46. * All other input values are the rest input values.
  47. * The values given to `callback` are the input values,
  48. * merged with every non-nullish output value.
  49. *
  50. * * if `middleware` throws an error,
  51. * returns a promise that is rejected,
  52. * or calls the given `done` function with an error,
  53. * `callback` is called with that error
  54. * * if `middleware` returns a value or returns a promise that is resolved,
  55. * that value is the main output value
  56. * * if `middleware` calls `done`,
  57. * all non-nullish values except for the first one (the error) overwrite the
  58. * output values
  59. *
  60. * @param {Middleware} middleware
  61. * Function to wrap.
  62. * @param {Callback} callback
  63. * Callback called with the output of `middleware`.
  64. * @returns {Run}
  65. * Wrapped middleware.
  66. */
  67. export function wrap(middleware: Middleware, callback: Callback): Run;
  68. /**
  69. * Callback.
  70. */
  71. export type Callback = (error?: Error | null | undefined, ...output: Array<any>) => void;
  72. /**
  73. * Ware.
  74. */
  75. export type Middleware = (...input: Array<any>) => any;
  76. /**
  77. * Pipeline.
  78. */
  79. export type Pipeline = {
  80. /**
  81. * Run the pipeline.
  82. */
  83. run: Run;
  84. /**
  85. * Add middleware.
  86. */
  87. use: Use;
  88. };
  89. /**
  90. * Call all middleware.
  91. *
  92. * Calls `done` on completion with either an error or the output of the
  93. * last middleware.
  94. *
  95. * > 👉 **Note**: as the length of input defines whether async functions get a
  96. * > `next` function,
  97. * > it’s recommended to keep `input` at one value normally.
  98. */
  99. export type Run = (...input: Array<any>) => void;
  100. /**
  101. * Add middleware.
  102. */
  103. export type Use = (fn: Middleware) => Pipeline;
  104. //# sourceMappingURL=index.d.ts.map