SAP-BTP-Spielwiese/app1/node_modules/@sap/xsenv/lib/cfservice.js
Markus Rettig 775ac7b58c completed step 3 from the tutorial
you must login with an BTP account in order to see the app
2024-02-08 16:13:36 +01:00

41 lines
1.3 KiB
JavaScript

'use strict';
var filter = require('./filter');
var VError = require('verror');
exports.readCFServices = readCFServices;
exports.filterCFServices = filterCFServices;
function readCFServices() {
if (!process.env.VCAP_SERVICES) {
return;
}
try {
var services = JSON.parse(process.env.VCAP_SERVICES);
} catch (err) {
throw new VError(err, 'Environment variable VCAP_SERVICES is not a valid JSON string.');
}
var result = {};
for (var s in services) {
for (var si in services[s]) {
var svc = services[s][si];
result[svc.name] = svc; // name is the service instance id
}
}
return result;
}
/**
* Returns an array of Cloud Foundry services matching the given filter.
*
* @param filterQuery {(string|Object|function)}
* - if string, returns the service with the same service instance name (name property)
* - if Object, should have some of these properties [name, label, tag, plan] and returns all services
* where all of the given properties match. Given tag matches if it is present in the tags array.
* - if function, should take a service object as argument and return true only if it matches the filter
* @returns Arrays of matching service objects, empty if no matches
*/
function filterCFServices(filterQuery) {
return filter.apply(readCFServices(), filterQuery);
}