11import express , { Router } from 'express' ;
22import { getSsmValue } from '../utils/ssm' ;
3+ import fetch from 'node-fetch' ;
34
45interface AuxiaApiRequestPayloadContextualAttributes {
56 key : string ;
@@ -52,17 +53,7 @@ interface AuxiaProxyResponseData {
5253 shouldShowSignInGate : boolean ;
5354}
5455
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-
56+ const buildAuxiaAPIRequestPayload = ( projectId : string , userId : string ) : AuxiaAPIRequestPayload => {
6657 // For the moment we are hard coding the data provided in contextualAttributes and surfaces.
6758 return {
6859 projectId : projectId ,
@@ -88,22 +79,19 @@ const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> =>
8879 } ;
8980} ;
9081
91- const fetchAuxiaData = async ( ) : Promise < AuxiaAPIAnswerData > => {
82+ const fetchAuxiaData = async (
83+ apiKey : string ,
84+ projectId : string ,
85+ userId : string ,
86+ ) : Promise < AuxiaAPIAnswerData > => {
9287 const url = 'https://apis.auxia.io/v1/GetTreatments' ;
9388
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-
10189 const headers = {
10290 'Content-Type' : 'application/json' ,
10391 'x-api-key' : apiKey ,
10492 } ;
10593
106- const payload = await buildAuxiaAPIRequestPayload ( ) ;
94+ const payload = buildAuxiaAPIRequestPayload ( projectId , userId ) ;
10795
10896 const params = {
10997 method : 'POST' ,
@@ -130,9 +118,37 @@ const buildAuxiaProxyResponseData = (auxiaData: AuxiaAPIAnswerData): AuxiaProxyR
130118 return { shouldShowSignInGate } ;
131119} ;
132120
133- export const buildAuxiaProxyRouter = ( ) : Router => {
134- const router = Router ( ) ;
121+ interface AuxiaRouterConfig {
122+ apiKey : string ;
123+ projectId : string ;
124+ userId : string ;
125+ }
135126
127+ export const getAuxiaRouterConfig = async ( ) : Promise < AuxiaRouterConfig > => {
128+ const apiKey = await getSsmValue ( 'PROD' , 'auxia-api-key' ) ;
129+ if ( apiKey === undefined ) {
130+ throw new Error ( 'auxia-api-key is undefined' ) ;
131+ }
132+
133+ const projectId = await getSsmValue ( 'PROD' , 'auxia-projectId' ) ;
134+ if ( projectId === undefined ) {
135+ throw new Error ( 'auxia-projectId is undefined' ) ;
136+ }
137+
138+ const userId = await getSsmValue ( 'PROD' , 'auxia-userId' ) ;
139+ if ( userId === undefined ) {
140+ throw new Error ( 'auxia-userId is undefined' ) ;
141+ }
142+
143+ return {
144+ apiKey,
145+ projectId,
146+ userId,
147+ } ;
148+ } ;
149+
150+ export const buildAuxiaProxyRouter = ( config : AuxiaRouterConfig ) : Router => {
151+ const router = Router ( ) ;
136152 router . post (
137153 '/auxia' ,
138154
@@ -142,7 +158,11 @@ export const buildAuxiaProxyRouter = (): Router => {
142158
143159 async ( req : express . Request , res : express . Response , next : express . NextFunction ) => {
144160 try {
145- const auxiaData = await fetchAuxiaData ( ) ;
161+ const auxiaData = await fetchAuxiaData (
162+ config . apiKey ,
163+ config . projectId ,
164+ config . userId ,
165+ ) ;
146166 const response = buildAuxiaProxyResponseData ( auxiaData ) ;
147167
148168 res . send ( response ) ;
0 commit comments