mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-04 19:42:46 +08:00
[@sap/xsenv] Add types for @sap/xsenv module (#18605)
* Add types for @sap/xsenv module (available on SAP Service Marketplace or https://npm.sap.com) * Fix linter errors * Switch to path mapping instead of declare module * Rework links to documentation * Fix linter message
This commit is contained in:
54
types/sap__xsenv/index.d.ts
vendored
Normal file
54
types/sap__xsenv/index.d.ts
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
// Type definitions for @sap/xsenv 1.2
|
||||
// Project: https://help.sap.com/hana_platform
|
||||
// Definitions by: [Michael Müller] <https://github.com/mad-mike>
|
||||
// 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 <code>VCAP_SERVICES</code>.
|
||||
*
|
||||
* @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;
|
||||
52
types/sap__xsenv/sap__xsenv-tests.ts
Normal file
52
types/sap__xsenv/sap__xsenv-tests.ts
Normal file
@@ -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();
|
||||
}
|
||||
27
types/sap__xsenv/tsconfig.json
Normal file
27
types/sap__xsenv/tsconfig.json
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
1
types/sap__xsenv/tslint.json
Normal file
1
types/sap__xsenv/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user