@@ -70,6 +70,14 @@ export class Controller {
70
70
throw new ServerError ( 501 , "Fetching contacts is not implemented" ) ;
71
71
}
72
72
73
+ if ( this . adapter . getToken && req . providerConfig ) {
74
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
75
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
76
+ providerConfig . apiKey = apiKey ;
77
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
78
+ res . header ( "X-Provider-Key" , apiKey ) ;
79
+ }
80
+
73
81
infoLogger ( "getContacts" , `Fetching contacts…` , providerConfig . apiKey ) ;
74
82
75
83
const fetchedContacts : Contact [ ] = await this . adapter . getContacts (
@@ -121,11 +129,6 @@ export class Controller {
121
129
res . header ( "X-Fetching-State" , "pending" ) ;
122
130
}
123
131
124
- if ( this . adapter . getToken && req . providerConfig ) {
125
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
126
- res . header ( "X-Provider-Key" , apiKey ) ;
127
- }
128
-
129
132
infoLogger ( "getContacts" , "END" , providerConfig . apiKey ) ;
130
133
res . status ( 200 ) . send ( responseContacts ) ;
131
134
} catch ( error : any ) {
@@ -153,21 +156,31 @@ export class Controller {
153
156
res : Response ,
154
157
next : NextFunction
155
158
) : Promise < void > {
156
- const { providerConfig : { apiKey = "" , locale = "" } = { } } = req ;
159
+ const { providerConfig } = req ;
160
+
161
+ if ( ! providerConfig ) {
162
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
163
+ }
164
+
157
165
try {
158
- infoLogger ( "createContact" , "START" , apiKey ) ;
166
+ infoLogger ( "createContact" , "START" , providerConfig . apiKey ) ;
159
167
160
168
if ( ! this . adapter . createContact ) {
161
169
throw new ServerError ( 501 , "Creating contacts is not implemented" ) ;
162
170
}
163
171
164
- if ( ! req . providerConfig ) {
165
- throw new ServerError ( 400 , "Missing config parameters" ) ;
172
+ if ( this . adapter . getToken && req . providerConfig ) {
173
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
174
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
175
+ providerConfig . apiKey = apiKey ;
176
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
177
+ res . header ( "X-Provider-Key" , apiKey ) ;
166
178
}
167
- infoLogger ( "createContact" , "Creating contact" , apiKey ) ;
179
+
180
+ infoLogger ( "createContact" , "Creating contact" , providerConfig . apiKey ) ;
168
181
169
182
const contact : Contact = await this . adapter . createContact (
170
- req . providerConfig ,
183
+ providerConfig ,
171
184
req . body
172
185
) ;
173
186
@@ -177,7 +190,7 @@ export class Controller {
177
190
errorLogger (
178
191
"createContact" ,
179
192
"Invalid contact provided by adapter" ,
180
- apiKey ,
193
+ providerConfig . apiKey ,
181
194
this . ajv . errorsText ( )
182
195
) ;
183
196
throw new ServerError ( 400 , "Invalid contact provided by adapter" ) ;
@@ -186,25 +199,26 @@ export class Controller {
186
199
infoLogger (
187
200
"createContact" ,
188
201
`Contact with id ${ contact . id } created` ,
189
- apiKey
202
+ providerConfig . apiKey
190
203
) ;
191
204
192
- const sanitizedContact : Contact = sanitizeContact ( contact , locale ) ;
193
-
194
- if ( this . adapter . getToken && req . providerConfig ) {
195
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
196
- res . header ( "X-Provider-Key" , apiKey ) ;
197
- }
205
+ const sanitizedContact : Contact = sanitizeContact (
206
+ contact ,
207
+ providerConfig . locale
208
+ ) ;
198
209
res . status ( 200 ) . send ( sanitizedContact ) ;
199
210
200
211
if ( this . contactCache ) {
201
- const contacts = await this . contactCache . get ( apiKey ) ;
212
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
202
213
if ( Array . isArray ( contacts ) ) {
203
- await this . contactCache . set ( apiKey , [ ...contacts , sanitizedContact ] ) ;
214
+ await this . contactCache . set ( providerConfig . apiKey , [
215
+ ...contacts ,
216
+ sanitizedContact ,
217
+ ] ) ;
204
218
}
205
219
}
206
220
207
- infoLogger ( "createContact" , "END" , apiKey ) ;
221
+ infoLogger ( "createContact" , "END" , providerConfig . apiKey ) ;
208
222
} catch ( error ) {
209
223
// prevent logging of refresh errors
210
224
if (
@@ -218,10 +232,10 @@ export class Controller {
218
232
errorLogger (
219
233
"createContact" ,
220
234
"Could not create contact:" ,
221
- apiKey ,
235
+ providerConfig . apiKey ,
222
236
error || "Unknown"
223
237
) ;
224
- errorLogger ( "createContact" , "Entity" , apiKey , req . body ) ;
238
+ errorLogger ( "createContact" , "Entity" , providerConfig . apiKey , req . body ) ;
225
239
next ( error ) ;
226
240
}
227
241
}
@@ -231,20 +245,28 @@ export class Controller {
231
245
res : Response ,
232
246
next : NextFunction
233
247
) : Promise < void > {
234
- const { providerConfig : { apiKey = "" , locale = "" } = { } } = req ;
248
+ const { providerConfig } = req ;
249
+ if ( ! providerConfig ) {
250
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
251
+ }
252
+
235
253
try {
236
254
if ( ! this . adapter . updateContact ) {
237
255
throw new ServerError ( 501 , "Updating contacts is not implemented" ) ;
238
256
}
239
257
240
- if ( ! req . providerConfig ) {
241
- throw new ServerError ( 400 , "Missing config parameters" ) ;
258
+ if ( this . adapter . getToken && req . providerConfig ) {
259
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
260
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
261
+ providerConfig . apiKey = apiKey ;
262
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
263
+ res . header ( "X-Provider-Key" , apiKey ) ;
242
264
}
243
265
244
- infoLogger ( "updateContact" , "Updating contact" , apiKey ) ;
266
+ infoLogger ( "updateContact" , "Updating contact" , providerConfig . apiKey ) ;
245
267
246
268
const contact : Contact = await this . adapter . updateContact (
247
- req . providerConfig ,
269
+ providerConfig ,
248
270
req . params . id ,
249
271
req . body
250
272
) ;
@@ -254,7 +276,7 @@ export class Controller {
254
276
errorLogger (
255
277
"updateContact" ,
256
278
"Invalid contact provided by adapter" ,
257
- apiKey ,
279
+ providerConfig . apiKey ,
258
280
this . ajv . errorsText ( )
259
281
) ;
260
282
throw new ServerError ( 400 , "Invalid contact provided by adapter" ) ;
@@ -263,28 +285,26 @@ export class Controller {
263
285
infoLogger (
264
286
"updateContact" ,
265
287
`Contact with id ${ contact . id } updated` ,
266
- apiKey
288
+ providerConfig . apiKey
267
289
) ;
268
290
269
- const sanitizedContact : Contact = sanitizeContact ( contact , locale ) ;
270
-
271
- if ( this . adapter . getToken && req . providerConfig ) {
272
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
273
- res . header ( "X-Provider-Key" , apiKey ) ;
274
- }
291
+ const sanitizedContact : Contact = sanitizeContact (
292
+ contact ,
293
+ providerConfig . locale
294
+ ) ;
275
295
res . status ( 200 ) . send ( sanitizedContact ) ;
276
296
277
297
if ( this . contactCache ) {
278
- const contacts = await this . contactCache . get ( apiKey ) ;
298
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
279
299
if ( Array . isArray ( contacts ) ) {
280
300
const updatedCache : Contact [ ] = contacts . map ( ( entry ) =>
281
301
entry . id === sanitizedContact . id ? sanitizedContact : entry
282
302
) ;
283
- await this . contactCache . set ( apiKey , updatedCache ) ;
303
+ await this . contactCache . set ( providerConfig . apiKey , updatedCache ) ;
284
304
}
285
305
}
286
306
287
- infoLogger ( "updateContact" , "END" , apiKey ) ;
307
+ infoLogger ( "updateContact" , "END" , providerConfig . apiKey ) ;
288
308
} catch ( error ) {
289
309
// prevent logging of refresh errors
290
310
if (
@@ -298,10 +318,10 @@ export class Controller {
298
318
errorLogger (
299
319
"updateContact" ,
300
320
"Could not update contact:" ,
301
- apiKey ,
321
+ providerConfig . apiKey ,
302
322
error || "Unknown"
303
323
) ;
304
- errorLogger ( "updateContact" , "Entity" , apiKey , req . body ) ;
324
+ errorLogger ( "updateContact" , "Entity" , providerConfig . apiKey , req . body ) ;
305
325
next ( error ) ;
306
326
}
307
327
}
@@ -311,46 +331,50 @@ export class Controller {
311
331
res : Response ,
312
332
next : NextFunction
313
333
) : Promise < void > {
314
- const { providerConfig : { apiKey = "" } = { } } = req ;
334
+ const { providerConfig } = req ;
335
+
336
+ if ( ! providerConfig ) {
337
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
338
+ }
339
+
315
340
try {
316
- infoLogger ( "deleteContact" , "START" , apiKey ) ;
341
+ infoLogger ( "deleteContact" , "START" , providerConfig . apiKey ) ;
317
342
318
343
if ( ! this . adapter . deleteContact ) {
319
344
throw new ServerError ( 501 , "Deleting contacts is not implemented" ) ;
320
345
}
321
346
322
- if ( ! req . providerConfig ) {
323
- throw new ServerError ( 400 , "Missing config parameters" ) ;
324
- }
325
-
326
- infoLogger ( "deleteContact" , "Deleting contact" , apiKey ) ;
327
-
328
- const contactId = req . params . id ;
329
- await this . adapter . deleteContact ( req . providerConfig , contactId ) ;
330
-
331
347
if ( this . adapter . getToken && req . providerConfig ) {
348
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
332
349
const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
350
+ providerConfig . apiKey = apiKey ;
351
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
333
352
res . header ( "X-Provider-Key" , apiKey ) ;
334
353
}
354
+
355
+ infoLogger ( "deleteContact" , "Deleting contact" , providerConfig . apiKey ) ;
356
+
357
+ const contactId = req . params . id ;
358
+ await this . adapter . deleteContact ( providerConfig , contactId ) ;
335
359
res . status ( 200 ) . send ( ) ;
336
360
337
361
infoLogger (
338
362
"deleteContact" ,
339
363
`Contact with id ${ contactId } deleted` ,
340
- apiKey
364
+ providerConfig . apiKey
341
365
) ;
342
366
343
367
if ( this . contactCache ) {
344
- const contacts = await this . contactCache . get ( apiKey ) ;
368
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
345
369
if ( Array . isArray ( contacts ) ) {
346
370
const updatedCache : Contact [ ] = contacts . filter (
347
371
( entry ) => entry . id !== contactId
348
372
) ;
349
- await this . contactCache . set ( apiKey , updatedCache ) ;
373
+ await this . contactCache . set ( providerConfig . apiKey , updatedCache ) ;
350
374
}
351
375
}
352
376
353
- infoLogger ( "deleteContact" , "END" , apiKey ) ;
377
+ infoLogger ( "deleteContact" , "END" , providerConfig . apiKey ) ;
354
378
} catch ( error ) {
355
379
// prevent logging of refresh errors
356
380
if (
@@ -364,7 +388,7 @@ export class Controller {
364
388
errorLogger (
365
389
"deleteContact" ,
366
390
"Could not delete contact:" ,
367
- apiKey ,
391
+ providerConfig . apiKey ,
368
392
error || "Unknown"
369
393
) ;
370
394
next ( error ) ;
0 commit comments