@@ -52,17 +52,10 @@ interface AuxiaProxyResponseData {
5252 shouldShowSignInGate : boolean ;
5353}
5454
55- const buildAuxiaAPIRequestPayload = async ( ) : Promise < AuxiaAPIRequestPayload > => {
56- const projectId = await getSsmValue ( 'PROD' , 'auxia-projectId' ) ;
57- if ( projectId === undefined ) {
58- throw new Error ( 'auxia-projectId is undefined' ) ;
59- }
60-
61- const userId = await getSsmValue ( 'PROD' , 'auxia-userId' ) ;
62- if ( userId === undefined ) {
63- throw new Error ( 'auxia-userId is undefined' ) ;
64- }
65-
55+ const buildAuxiaAPIRequestPayload = async (
56+ projectId : string ,
57+ userId : string ,
58+ ) : Promise < AuxiaAPIRequestPayload > => {
6659 // For the moment we are hard coding the data provided in contextualAttributes and surfaces.
6760 return {
6861 projectId : projectId ,
@@ -88,22 +81,19 @@ const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> =>
8881 } ;
8982} ;
9083
91- const fetchAuxiaData = async ( ) : Promise < AuxiaAPIAnswerData > => {
84+ const fetchAuxiaData = async (
85+ apiKey : string ,
86+ projectId : string ,
87+ userId : string ,
88+ ) : Promise < AuxiaAPIAnswerData > => {
9289 const url = 'https://apis.auxia.io/v1/GetTreatments' ;
9390
94- // We are hardcoding PROD for the moment, because I haven't created a CODE key
95- const apiKey = await getSsmValue ( 'PROD' , 'auxia-api-key' ) ;
96-
97- if ( apiKey === undefined ) {
98- throw new Error ( 'auxia-api-key is undefined' ) ;
99- }
100-
10191 const headers = {
10292 'Content-Type' : 'application/json' ,
10393 'x-api-key' : apiKey ,
10494 } ;
10595
106- const payload = await buildAuxiaAPIRequestPayload ( ) ;
96+ const payload = await buildAuxiaAPIRequestPayload ( projectId , userId ) ;
10797
10898 const params = {
10999 method : 'POST' ,
@@ -130,9 +120,37 @@ const buildAuxiaProxyResponseData = (auxiaData: AuxiaAPIAnswerData): AuxiaProxyR
130120 return { shouldShowSignInGate } ;
131121} ;
132122
133- export const buildAuxiaProxyRouter = ( ) : Router => {
134- const router = Router ( ) ;
123+ interface AuxiaRouterConfig {
124+ apiKey : string ;
125+ projectId : string ;
126+ userId : string ;
127+ }
135128
129+ export const getAuxiaRouterConfig = async ( ) : Promise < AuxiaRouterConfig > => {
130+ const apiKey = await getSsmValue ( 'PROD' , 'auxia-api-key' ) ;
131+ if ( apiKey === undefined ) {
132+ throw new Error ( 'auxia-api-key is undefined' ) ;
133+ }
134+
135+ const projectId = await getSsmValue ( 'PROD' , 'auxia-projectId' ) ;
136+ if ( projectId === undefined ) {
137+ throw new Error ( 'auxia-projectId is undefined' ) ;
138+ }
139+
140+ const userId = await getSsmValue ( 'PROD' , 'auxia-userId' ) ;
141+ if ( userId === undefined ) {
142+ throw new Error ( 'auxia-userId is undefined' ) ;
143+ }
144+
145+ return {
146+ apiKey,
147+ projectId,
148+ userId,
149+ } ;
150+ } ;
151+
152+ export const buildAuxiaProxyRouter = ( config : AuxiaRouterConfig ) : Router => {
153+ const router = Router ( ) ;
136154 router . post (
137155 '/auxia' ,
138156
@@ -142,7 +160,11 @@ export const buildAuxiaProxyRouter = (): Router => {
142160
143161 async ( req : express . Request , res : express . Response , next : express . NextFunction ) => {
144162 try {
145- const auxiaData = await fetchAuxiaData ( ) ;
163+ const auxiaData = await fetchAuxiaData (
164+ config . apiKey ,
165+ config . projectId ,
166+ config . userId ,
167+ ) ;
146168 const response = buildAuxiaProxyResponseData ( auxiaData ) ;
147169
148170 res . send ( response ) ;
0 commit comments