1- import { isEmpty , isNil , view , set , type } from 'ramda' ;
2- import { RouteOptions } from './types' ;
1+ import { isEmpty , isNil , set , view , type } from 'ramda' ;
2+ import { PluginOptions , Response , RouteOptions } from './types' ;
33
44const isPluginActive = ( request ) : boolean => {
55 return (
@@ -14,67 +14,90 @@ const isResponseAbsent = (request): boolean => {
1414 ) ;
1515} ;
1616
17- const getRouteOptions = ( request , getSignedUrl ) : RouteOptions => {
17+ const validateRouteOptions = ( options : RouteOptions | RouteOptions [ ] ) : void => {
18+ if ( Array . isArray ( options ) ) {
19+ options . map ( ( option ) => validateRouteOptions ( option ) ) ;
20+ } else if ( ! options . lenses ) {
21+ throw new Error ( 'hapi-signed-url: requires lenses in route options' ) ;
22+ }
23+ } ;
24+
25+ const validatePluginOptions = ( options : PluginOptions ) : void => {
26+ if ( type ( options . getSignedUrl ) !== 'Function' ) {
27+ throw new Error ( 'hapi-signed-url: requires getSignedUrl function while registering' ) ;
28+ }
29+ } ;
30+
31+ const getRouteOptions = ( request ) : RouteOptions [ ] => {
1832 const options = request . route . settings . plugins . signedUrl ;
19- const source = options . pathToSource
20- ? view ( options . pathToSource , request . response . source )
21- : request . response . source ;
22- return {
23- ...options ,
24- source : source ,
25- getSignedUrl : getSignedUrl ,
26- } ;
33+ return Array . isArray ( options ) ? options : [ options ] ;
2734} ;
2835
29- const updateSignedUrl = async ( options : RouteOptions ) => {
30- const { source, lenses, getSignedUrl } = options ;
31- const toUpdateLinks : string [ ] = lenses . map ( ( lens ) => view ( lens , source ) ) ;
32- const promises = toUpdateLinks . map ( async ( link : string ) => await getSignedUrl ( link ) ) ;
36+ const updateSignedUrl = async (
37+ source : object ,
38+ routeOptions : RouteOptions ,
39+ pluginOptions : PluginOptions ,
40+ ) : Promise < object > => {
41+ const { lenses } = routeOptions ;
42+ const toUpdateLinks = lenses . map ( ( lens ) => view ( lens , source ) ) ;
43+ const promises = toUpdateLinks . map (
44+ async ( link ) => await pluginOptions . getSignedUrl ( link ) ,
45+ ) ;
3346 const updatedLinks = await Promise . all ( promises ) ;
3447 const updatedSource = updatedLinks . reduce ( ( source , link , index ) => {
3548 return view ( lenses [ index ] , source ) ? set ( lenses [ index ] , link , source ) : source ;
3649 } , source ) ;
37- return updatedSource as object ;
50+ return updatedSource ;
3851} ;
3952
40- const processSource = async ( options : RouteOptions ) => {
53+ const processSource = async (
54+ source : object | object [ ] ,
55+ routeOptions : RouteOptions ,
56+ pluginOptions : PluginOptions ,
57+ ) : Promise < object | object [ ] > => {
4158 // single object
42- if ( type ( options . source ) !== 'Array' ) {
43- return updateSignedUrl ( options ) ;
59+ if ( ! Array . isArray ( source ) ) {
60+ return updateSignedUrl ( source , routeOptions , pluginOptions ) ;
4461 }
4562
4663 // if source is array
47- const promises = ( options . source as any [ ] ) . map ( async ( src ) => {
48- return updateSignedUrl ( {
49- ...options ,
50- source : src ,
51- } ) ;
64+ const promises = source . map ( async ( src ) => {
65+ return updateSignedUrl ( src , routeOptions , pluginOptions ) ;
5266 } ) ;
5367 return Promise . all ( promises ) ;
5468} ;
5569
56- const signUrl = ( getSignedUrl ) => async ( request , h ) => {
70+ const signUrl = ( options : PluginOptions ) => async ( request , h ) => {
5771 if ( ! isPluginActive ( request ) ) {
5872 return h . continue ;
5973 }
6074 if ( isResponseAbsent ( request ) ) {
6175 return h . continue ;
6276 }
6377
64- const routeOptions = getRouteOptions ( request , getSignedUrl ) ;
65- const updated = await processSource ( routeOptions ) ;
66- const updatedSource = routeOptions . pathToSource
67- ? set ( routeOptions . pathToSource , updated , request . response . source )
68- : updated ;
78+ const routeOptions = getRouteOptions ( request ) ;
79+ validateRouteOptions ( routeOptions ) ;
80+ let toUpdateResponse = request . response . source as Response ;
81+
82+ for ( const routeOption of routeOptions ) {
83+ const source = routeOption . pathToSource
84+ ? view ( routeOption . pathToSource , toUpdateResponse )
85+ : toUpdateResponse ;
86+ const processed = await processSource ( source , routeOption , options ) ;
87+ toUpdateResponse = routeOption . pathToSource
88+ ? set ( routeOption . pathToSource , processed , toUpdateResponse )
89+ : processed ;
90+ }
6991
70- request . response . source = updatedSource ;
92+ request . response . source = toUpdateResponse ;
7193 return h . continue ;
7294} ;
7395
7496export const signedUrl = {
7597 name : 'signedUrl' ,
7698 version : '1.0.0' ,
77- register : async function ( server , options ) {
78- server . ext ( 'onPreResponse' , signUrl ( options . getSignedUrl ) ) ;
99+ register : ( server , options : PluginOptions ) => {
100+ validatePluginOptions ( options ) ;
101+ server . ext ( 'onPreResponse' , signUrl ( options ) ) ;
79102 } ,
80103} ;
0 commit comments