import URL from "core-js-pure/features/url" ; public export interface ImageProcessingPipelineBuilderResizeOptions { scalingMode ? : "lfit" | "mfit" | "fill" | "pad" | "fixed" ; targetWidth ? : number ; targetHeight ? : number ; longerSide ? : number ; shorterSide ? : number ; color ? : string ; } type Operation = FormatOperation | QualityOperation | ResizeOperation ; interface FormatOperation { op : "format" ; format : "jpg" | "png" | "webp" ; } interface QualityOperation { op : "quality" ; absoluteQuality : number ; } interface ResizeOperation extends ImageProcessingPipelineBuilderResizeOptions { op : "resize" ; } public export class ImageProcessingPipelineBuilder { private _ops : Operation [ ] ; constructor ( ) { this . _ops = [ ] ; } param format format ( format ) { this . _ops . push ( { op : "format" , format } ) ; return this ; } param absoluteQuality quality ( absoluteQuality ) { this . _ops . push ( { op : "quality" , absoluteQuality } ) ; return this ; } param options resize ( options ) { this . _ops . push ( { op : "resize" , ... options } ) ; return this ; } /**
     * Returns the query parameter name to be used.
     */ getName ( ) { return "pipeline" ; } /**
     * Returns the built pipeline string to be used as query parameter value.
     */ getValue ( ) { const parts = [ "image" ] ; for ( const op of this . _ops ) { switch ( op . op ) { case "format" : parts . push ( renderFormat ( op ) ) ; break ; case "quality" : parts . push ( renderQuality ( op ) ) ; break ; case "resize" : parts . push ( renderResize ( op ) ) ; break ; default : break ; } } return parts . join ( "/" ) ; } example param urlStr setToURLString ( urlStr ) { const u = new URL ( urlStr ) ; u . searchParams . delete ( "pipeline" ) ; u . searchParams . append ( this . getName ( ) , this . getValue ( ) ) ; return u . toString ( ) ; } } function renderFormat ( op ) { return `format,${ op . format }` ; } function renderQuality ( op ) { return `quality,Q_${ op . absoluteQuality }` ; } function renderResize ( op ) { const parts = [ "resize" ] ; if ( op . scalingMode != null ) { parts . push ( `m_${ op . scalingMode }` ) ; } if ( op . longerSide != null ) { parts . push ( `l_${ op . longerSide }` ) ; } if ( op . shorterSide != null ) { parts . push ( `s_${ op . shorterSide }` ) ; } if ( op . targetWidth != null ) { parts . push ( `w_${ op . targetWidth }` ) ; } if ( op . targetHeight != null ) { parts . push ( `h_${ op . targetHeight }` ) ; } if ( op . color != null ) { parts . push ( `color_${ op . color }` ) ; } return parts . join ( "," ) ; }