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