diff --git a/types/sap__xsenv/index.d.ts b/types/sap__xsenv/index.d.ts new file mode 100644 index 0000000000..3858f77feb --- /dev/null +++ b/types/sap__xsenv/index.d.ts @@ -0,0 +1,54 @@ +// Type definitions for @sap/xsenv 1.2 +// Project: https://help.sap.com/hana_platform +// Definitions by: [Michael Müller] +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// For more information see the Developer Guide for SAP HANA XS Advanced Model run time. +// The module is available on the https://npm.sap.com registry. + +export function loadEnv(jsonFile?: string): void; +export function readCFServices(): any; + +export type ServiceFilter = string | { name?: string; label?: string; tag?: string; plan?: string } | ((service: any) => boolean); + +/** + * Reads service configuration from CloudFoundry environment variable VCAP_SERVICES. + * + * @param filter Filter used to find a bound Cloud Foundry service, see filterCFServices + * @return credentials property of found service + * @throws Error in case no or multiple matching services are found + */ +export function cfServiceCredentials(filter: ServiceFilter): any; +/** + * Returns an array of Cloud Foundry services matching the given filter. + * + * @param filter {(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 + */ +export function filterCFServices(filter: ServiceFilter): any; + +/** + * Looks up and returns bound Cloud Foundry services. + * + * If a service is not found in VCAP_SERVICES, returns default service configuration loaded from a JSON file. + * + * @param query {object} describes requested Cloud Foundry services, each property value is a filter + * as described in filterCFServices. + * @param servicesFile {string} path to JSON file to load default service configuration (default is default-services.json). + * If null, do not load default service configuration. + * + * @returns {object} with the same properties as in query argument where the value of each + * property is the respective service credentials object. + * @throws Error, if for some of the requested services no or multiple instances are found; Error, if query parameter is not provided + */ +export function getServices(query: any, servicesFile?: string): any; + +/** + * @deprecated use loadCertificates instead + */ +export function loadCaCert(): void; + +export function loadCertificates(certPath?: string): any; diff --git a/types/sap__xsenv/sap__xsenv-tests.ts b/types/sap__xsenv/sap__xsenv-tests.ts new file mode 100644 index 0000000000..4e7b6a5134 --- /dev/null +++ b/types/sap__xsenv/sap__xsenv-tests.ts @@ -0,0 +1,52 @@ +import xsenv = require('@sap/xsenv'); + +// code examples from the README.MD + +function test1() { + const services = xsenv.readCFServices(); + services.aservice.credentials; // prints { host: '...', port: '...', user: '...', passwrod: '...', ... } +} + +function test2() { + const services = xsenv.readCFServices(); + const svc = services['process.env.SERVICE_NAME']; +} + +function test3() { + const svc = xsenv.cfServiceCredentials({ tag: 'hdb' }); + // console.log(svc); // prints { host: '...', port: '...', user: '...', passwrod: '...', ... } +} + +function test4() { + const services = xsenv.getServices({ + hana: { tag: 'hdb' }, + scheduler: { label: 'jobs' } + }); + + const hanaCredentials = services.hana; + const schedulerCredentials = services.scheduler; +} + +function test5() { + xsenv.cfServiceCredentials('hana'); + xsenv.cfServiceCredentials({ tag: 'relational' }); + xsenv.cfServiceCredentials({ label: 'hana', plan: 'shared' }); + xsenv.cfServiceCredentials((service: any) => { + return /shared/.test(service.plan) && /hdi/.test(service.label); + }); +} + +function test6() { + xsenv.loadEnv(); + // console.log(process.env.PORT); // prints 3000 + xsenv.cfServiceCredentials('hana-R90'); // prints { host: 'myhana, port: '30015', user: 'SYSTEM', password: 'secret' } +} + +function test7() { + xsenv.loadEnv('myenv.json'); +} + +function test8() { + xsenv.loadCertificates(); + xsenv.loadCaCert(); +} diff --git a/types/sap__xsenv/tsconfig.json b/types/sap__xsenv/tsconfig.json new file mode 100644 index 0000000000..f338a2e5d8 --- /dev/null +++ b/types/sap__xsenv/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "paths": { + "@sap/xsenv": [ + "sap__xsenv" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sap__xsenv-tests.ts" + ] +} \ No newline at end of file diff --git a/types/sap__xsenv/tslint.json b/types/sap__xsenv/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sap__xsenv/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }