44 * This software may be modified and distributed under the terms
55 * of the MIT license. See the LICENSE file for details.
66 */
7- import { Console , Effect , pipe } from 'effect' ;
7+ import { Effect , pipe } from 'effect' ;
88import { MockApi } from '../spec.js' ;
99import {
1010 HttpApiBuilder ,
1111 HttpApiError ,
12- HttpBody ,
1312 HttpServerRequest ,
1413 HttpServerResponse ,
1514} from '@effect/platform' ;
@@ -18,15 +17,13 @@ import { validator } from '../helpers/match.js';
1817import { returnSuccessResponseRedirect } from '../responses/return-success-redirect.js' ;
1918
2019const CapabilitiesHandlerMock = HttpApiBuilder . group ( MockApi , 'Capabilities' , ( handlers ) =>
21- handlers . handle ( 'capabilities' , ( { urlParams , payload } ) =>
20+ handlers . handle ( 'capabilities' , ( { payload } ) =>
2221 Effect . gen ( function * ( ) {
23- /**
24- * We expect an acr_value query parameter to be present in the request.
25- * If it is not present, we return a 404 Not Found error.
26- */
27- const acr_value = urlParams ?. acr_values ?? '' ;
28- console . log ( 'acr_value' , acr_value ) ;
22+ const req = yield * HttpServerRequest . HttpServerRequest ;
23+ console . log ( 'request cookies' , req . cookies ) ;
24+ const acr_value = req . cookies . acr_values ?? '' ;
2925
26+ console . log ( 'acr_value' , acr_value ) ;
3027 if ( ! acr_value ) {
3128 return yield * Effect . fail ( new HttpApiError . NotFound ( ) ) ;
3229 }
@@ -36,15 +33,13 @@ const CapabilitiesHandlerMock = HttpApiBuilder.group(MockApi, 'Capabilities', (h
3633 * If the cookie is not present, we return a 404 Not Found error.
3734 */
3835
39- const req = yield * HttpServerRequest . HttpServerRequest ;
40-
4136 const stepIndexCookie = req . cookies [ 'stepIndex' ] ;
42- console . log ( req . cookies ) ;
4337
4438 /**
4539 * If we are here with no step index that means we can't continue through a flow.
4640 * We should error
4741 */
42+ console . log ( 'step index cookie' , stepIndexCookie ) ;
4843 if ( ! stepIndexCookie ) {
4944 console . log ( 'no step index' ) ;
5045 return yield * Effect . fail ( new HttpApiError . NotFound ( ) ) ;
@@ -76,59 +71,49 @@ const CapabilitiesHandlerMock = HttpApiBuilder.group(MockApi, 'Capabilities', (h
7671 */
7772 const steps = responseMap [ acr_value ] ;
7873
79- /**
80- * This may not be the best way to write this.
81- * An alternative option would be for us to include the success response we want to return,
82- * in the response map.
83- *
84- * then we can check if we are at the last step. if we are we write the cookie
85- * and then we return the success response (last item in array)
86- *
87- * for now, this returns a default success response and writes cookies.
88- */
89- if ( stepIndex + 1 >= steps . length ) {
74+ if ( stepIndex + 1 === steps . length - 1 ) {
9075 /**
9176 * we need to return a success because we have not failed yet,
9277 * and we have no more steps to process.
9378 */
94- const body = yield * HttpBody . json ( returnSuccessResponseRedirect ) . pipe (
95- Effect . tap ( Console . log ( `here stepIndex: ${ stepIndex } ` ) ) ,
96- /**
97- * Decide on a better way to handle this error possibiltiy
98- */
99- Effect . catchTag ( 'HttpBodyError' , ( ) =>
100- Effect . fail (
101- new HttpApiError . HttpApiDecodeError ( {
102- message : 'Failed to encode body' ,
103- issues : [ ] ,
104- } ) ,
79+ const body = responseMap [ stepIndex ] ;
80+
81+ return yield * pipe (
82+ HttpServerResponse . json ( body ) ,
83+ Effect . flatMap ( ( res ) => HttpServerResponse . setCookie ( res , 'ST' , 'MockApiCookie123' ) ) ,
84+ Effect . flatMap ( ( res ) =>
85+ HttpServerResponse . setCookie (
86+ res ,
87+ 'interactionId' ,
88+ returnSuccessResponseRedirect . interactionId ,
89+ {
90+ httpOnly : true ,
91+ secure : true ,
92+ sameSite : 'strict' ,
93+ } ,
10594 ) ,
10695 ) ,
107- ) ;
108- return pipe (
109- HttpServerResponse . json ( body ) ,
110- HttpServerResponse . setCookie ( 'ST' , 'MockApiCookie123' ) ,
111- HttpServerResponse . setCookie (
112- 'interactionId' ,
113- returnSuccessResponseRedirect . interactionId ,
114- {
115- httpOnly : true ,
116- secure : true ,
117- sameSite : 'strict' ,
118- } ,
96+ Effect . flatMap ( ( res ) =>
97+ HttpServerResponse . setCookie (
98+ res ,
99+ 'interactionToken' ,
100+ returnSuccessResponseRedirect . interactionToken ,
101+ {
102+ httpOnly : true ,
103+ secure : true ,
104+ sameSite : 'strict' ,
105+ } ,
106+ ) ,
119107 ) ,
120- HttpServerResponse . setCookie (
121- 'interactionToken' ,
122- returnSuccessResponseRedirect . interactionToken ,
123- {
124- httpOnly : true ,
125- secure : true ,
126- sameSite : 'strict' ,
127- } ,
108+ Effect . flatMap ( ( res ) => HttpServerResponse . removeCookie ( res , 'stepIndex' ) ) ,
109+ Effect . flatMap ( ( res ) => HttpServerResponse . setStatus ( res , 200 ) ) ,
110+ Effect . flatMap ( ( res ) =>
111+ HttpServerResponse . setHeader ( res , 'Content-Type' , 'application/json' ) ,
112+ ) ,
113+ Effect . catchTag ( 'CookieError' , ( ) => Effect . fail ( new HttpApiError . InternalServerError ( ) ) ) ,
114+ Effect . catchTag ( 'HttpBodyError' , ( ) =>
115+ Effect . fail ( new HttpApiError . InternalServerError ( ) ) ,
128116 ) ,
129- HttpServerResponse . removeCookie ( 'stepIndex' ) ,
130- HttpServerResponse . setStatus ( 200 ) ,
131- HttpServerResponse . setHeader ( 'Content-Type' , 'application/json' ) ,
132117 ) ;
133118 }
134119
0 commit comments