Laravel's Pipeline in JS form
npm install @zacksmash/pipeline.jsImport the Pipeline class and create a new instance of it.
import Pipeline from '@zacksmash/pipeline.js'
const pipeline = new Pipeline()
pipeline.send('foo')
.through([
(value, next) => next(value + 'bar'),
(value, next) => next(value + 'baz')
])
.then((value) => {
console.log(value) // foobarbaz
})The send() method is used to send a value through the pipeline.
let value = 'foo'
pipeline.send(value)The through() method is used to define the pipeline's steps. This can be an anonoymous function or lambda. You may also use a JS Class, with a handle() method (this can be overridden with the via() method). You may choose to use a static method to handle the value, or an instance method.
// Lambda, Anonoymous function
const pipeTwo = (value, next) => next(value + 'baz')
// Class
class PipeThree {
handle(value, next) {
return next(value + 'qux')
}
}
// Static method
class PipeFour {
static handle(value, next) {
return next(value + 'quux')
}
}
pipeline.send('foo')
.through([
pipeOne,
pipeTwo,
new PipeThree,
PipeFour
])The pipe() method is used to define a single step in the pipeline.
pipeline.send('foo')
.pipe((value, next) => next(value + 'bar'))
.pipe((value, next) => next(value + 'baz'))
.then((value) => {
console.log(value) // foobarbaz
})The via() method is used to define the method to call on a class. This is useful if you want to use a class, but don't want to use the handle() method. For example, if you want to use a class with a process() method:
class PipeFive {
process(value, next) {
return next(value + 'quuz')
}
}
pipeline.send('foo')
.through([
new PipeFive
])
.via('process')The then() method is used to define the callback to run after the pipeline has finished.
pipeline.send('foo')
.through([
(value, next) => next(value + 'bar'),
(value, next) => next(value + 'baz')
])
.then((value) => {
console.log(value) // foobarbaz
})The thenReturn() method is used to return the value after the pipeline has finished.
const result = pipeline.send('foo')
.through([
(value, next) => next(value + 'bar'),
(value, next) => next(value + 'baz')
])
.thenReturn()
console.log(result) // foobarbazMIT