@@ -21,6 +21,9 @@ function _setup (apis, keys, opts) {
2121 if ( endpoint ) {
2222 output [ endpoint . key ] = endpoint
2323 }
24+ if ( opts . checkAPIs ) {
25+ _checkAPI ( endpoint , opts . log || defaultLog )
26+ }
2427 } )
2528 return output
2629 } )
@@ -33,6 +36,27 @@ function _setup (apis, keys, opts) {
3336 return output
3437}
3538
39+ // check API key and kill if api is required
40+ function _checkAPI ( endpoint , log ) {
41+ const config = endpoint . config
42+ const apiName = endpoint . key
43+ const uri = `${ config . proxyBasePath } /_checkAPIkey` // get the proxyBasePath eg. api/publications
44+ endpoint . client . getAsync ( { uri : uri } )
45+ . then ( res => {
46+ if ( res . statusCode === 401 ) {
47+ log . error ( 'Bad API key for ' + apiName )
48+ if ( config . required ) {
49+ log . error ( 'Required API misconfigured, EXITING' )
50+ process . exit ( 1 )
51+ }
52+ } else if ( res . statusCode === 404 ) {
53+ log . error ( 'Check API functionality not implemented on ' + apiName )
54+ } else if ( res . statusCode === 500 ) {
55+ log . error ( 'Got 500 response on checkAPI call, most likely a bad API key for ' + apiName )
56+ }
57+ } )
58+ }
59+
3660// unpack nodeApi:s and pair with keys, returns BasicAPI objects
3761function _createClients ( nodeApi , apiKey ) {
3862 return Object . keys ( nodeApi ) . map ( ( key ) => {
0 commit comments