Npm log levels used for the SAP Cloud SDK logger.
Denotes the OData version.
Default logger for the SAP Cloud SDK for unhandled exceptions.
Format for logging in Kibana.
Format for local logging.
Within all files generated by the SDK we use the unix style end of line delimiter.
We do not consider if the generator is executed on windows or unix systems.
It will always be \n
to have consistent clients between operating systems.
For request payloads, etc., it is convention to use the \r\n
new line.
Adds a key value pair to the given objects and returns a shallow copy. If the key is already present it will be overwritten.
key to be added
value to be added
object the key value pair is added to.
the object with the key value pair added
The key to associate with the given value.
The value to associate with the given key.
A copy of the input object with the new key-value pair if the value is neither null
nor undefined
.
Same as pipe but for asynchronous functions:
asyncPipe(func1, func2, ...funcN)(start)
executes all functions in sequence awaiting the result and piping the response through in other words await funcN(... await func2(await func1(start))...)
.
A list of asynchronous functions to be executed in sequence.
The return value of the last function in the list.
Convert a string to camelCase. This format used e. g. for properties on entity class instances.
The string to be transformed.
The transformed string.
Returns the OData version in capital letters so V2 or V4.
OData version in lower case: 'v2' or 'v4'
'V2' or 'V4'
Checks whether a URL is existing via a head request.
URL to be checked
promise - resolves if the URL exists
Strings in the tagged template. In the example above that would be ['Code with ', ' and more code;'].
Arguments in the tagged template. In the example above that would be the resolved value for arguments
;
A string formatted as code block.
Create a logger for the given message context, if available.
Usage:
To create a logger in your module, it is recommended to pass a module identifier that will be logged as messageContext
for all messages from this logger:
const logger = createLogger('my-module');
. Not setting any module identifier will retrieve the default logger.
Use this logger throughout your module. If the module is spread over multiple files, you can retrieve the logger instance by calling the createLogger
function with the respective module identifier.
There will always be only one instance of a logger per module identifier.
You can pass any custom data that you want to be logged in addition by passing an object instead. You can change the default logging level (INFO
) using the level
key in the object.
In those cases, provide the messageContext
as a key in the object:
const logger = createLogger({
messageContext: 'my-module',
myCustomKey: 'my-custom-data',
level: 'debug'
});
You will find these information under the custom_fields key in your Cloud Foundry logs.
To retrieve a logger after its creation use getLogger. If you want to change the log level of a logger use setLogLevel.
Either a key for the message context of all messages produced by the logger or an object with additional keys to set in the message.
A newly created or an already existing logger for the given context.
Disable logging of exceptions. Enabled by default.
Strings in the tagged template. In the example above that would be ['Docs with ', ' and more content;'].
Arguments in the tagged template. In the example above that would be the resolved value for arguments
;
A string formatted as documentation block.
Enable logging of exceptions. Enabled by default.
Encode a string to a base64 encoded string.
String to encode.
Base64 encoded string.
hidden
hidden
hidden
Error message.
Original error, causing the new error.
A new error with the given cause.
Create a shallow copy of the given object, that does not contain the given keys. Non existing keys in the source object are ignored.
properties to be selected
object from which the values are taken
an object with the selected keys and corresponding values.
Filter an array by removing duplicates and keeping the left most occurrence. By default this compares by identity.
Array to remove duplicates from.
Optional comparator function, indicating whether two items are equal and therefore handled as duplicates. Defaults to identity.
A filtered array containing no duplicates.
Filter an array by removing duplicates and keeping the right most occurrence. By default this compares by identity.
Array to remove duplicates from.
Optional comparator function, indicating whether two items are equal and therefore handled as duplicates. Defaults to identity.
A filtered array containing no duplicates.
Await all promises and resolve if non of them failed. Reject if at least one of them was rejected, but only once all of them are finished. Throws an error consisting of a list of reasons.
Promises to settle.
Message to use as introductory text of the error if an error occurs.
Get the first item from an array. Returns undefined
, if the array is empty.
Array to get the first item of.
Fist item of the array or undefined
, if the array was empty.
Flatten a two dimensional array into a one dimensional array
The array to be flattened.
A one dimensional array.
Flattens a array: [1,[2,[3,4]],5] will become [1,2,3,4,5]. Non primitive values are copied by reference.
Array to be flattened
The flattened array.
Convert a JSON object to a string using formatting in line with the prettier with indentation and new line at the end.
Object to be stringified
The JSON object as string.
Get logger for a given message context, if available.
A key for the message context of all messages produced by the logger
The logger for the given messageContext if it was created before
Identity function
Any object
the given argument without doing something
Type guard to check whether an error is of type ErrorWithCause.
An error.
Whether the given error is of type ErrorWithCause.
Checks whether a value is either null
or undefined
.
Value to check.
true
for null
or undefined
, false
otherwise.
Convert a string to kebab case. This format is used e. g. for file names.
The string to be transformed.
The transformed string.
Get the last item from an array. Returns undefined
, if the array is empty.
Array to get the last item of.
Last item of the array or undefined
, if the array was empty.
Create an object by merging the right
object into a shallow copy of the left
object ignoring casing, but keeping the right casing. Keys present both objects will be present in the merged object.
Object to merge.
Object to merge. The casing of the keys of this object takes precedence.
right
object are replaced. Note that the casing used by right
will be used.Create an object by merging the right
object into a shallow copy of the left
object ignoring casing, but keeping the right
casing. Only keys present in the left
object will be present in the merged object.
Object to merge into. They keys of this object will be present in the returned object.
Object to merge. Only keys in left
will be considered for merging.
left
object, where entries present in the right
object are replaced. Note that the casing used by right
will be used.The object to merge into.
The object which to merge into a
.
A copy of the merge(a, b)
or a
if b
is undefined
or null
.
Mute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
Split an array into two based on a condition.
Array to partition.
Function to determine to where to put each item.
A two dimensional array containing two arrays, where the first one includes all items where the given condition was met and the second one includes all items where it was not met.
Convert a string to pascal case. This format is used e. g. for types.
The string to be transformed.
The transformed string.
Create a shallow copy of the given object, that contains the given keys. Non existing keys in the source object are ignored.
properties to be selected
object from which the values are taken
an object with the selected keys and corresponding values.
Create a shallow copy of the given object, that contains the given keys, independent of casing. Non existing keys in the source object are ignored.
Object to pick the given key from.
Keys of the pair to be picked.
Create a shallow copy of the given object, that contains all entries with non-nullish values.
An object to pick from.
Returns the value of an object based on the given key, independent of casing.
Object to be searched for the given key.
Key of the value to pick.
The value of for the given key or undefined
, if not available.
Checks if a chain of properties exists on the given object.
The object to be checked.
Chained properties.
true
if the property chain leads to a truthy value, false
otherwise.
Read a JSON file from the file system.
The path to the JSON file.
An object parsed from the JSON file.
Remove file extension from a string, e.g. remove 'test.jpg' would return 'test'
File name to remove the file extension from.
File name without extension.
Takes an object and returns a new object whose keys are renamed according to the provided key mapping. Any keys in the input object not present in the key mapping will be present in the output object as-is. If a key in the key mapping is not present in the input object, the output object will contain the key with value "undefined".
An object mapping keys of the input object to keys of the output object.
The input object.
An object with renamed keys.
Change the global log level of the container which will set default level for all active loggers.
E. g., to set the global log level call setGlobalLogLevel('debug')
.
The log level to set the global log level to.
Change the log level of a logger based on its message context.
E. g., to set the log level for the destination accessor module of the SDK to debug, simply call setLogLevel('debug', 'destination-accessor')
.
level to set the logger to. Use an empty string '' as level to unset context level.
Message context of the logger to change the log level for or the logger itself
Split the given array in chunks.
Array to be split into chunks.
Size of the chunks.
Two dimensional array with arrays of length chunkSize. The last subarray could be shorter.
Convert a string to a human readable format, e.g. it transforms to_BusinessPartner
to To Business Partner
.
The string to be transformed.
The transformed string.
Create an object based on the given key and value if neither key nor value are nullish.
Name of the header.
Value of the header.
Remove whitespace from the left side of a string.
String to trim.
String without outer whitespace.
Remove whitespace from the left side of a string.
String to trim.
String without whitespace on the left side.
Remove whitespace from the right side of a string.
String to trim.
String without whitespace on the right side.
Remove all duplicates from an array.
Array that might contain duplicates.
Array of unique items.
Unmute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
Convert a string to the uppercase snake case. This format is used e. g. for static properties on entity classes.
The string to be transformed.
The input string in the case used by static methods on entity-classes.
We want to provide methods which accept a variable single number of elements and arrays. The overloaded signature to achieve this is:
function doSomething(array: T[])
function doSomething(...varArgs: T[])
function doSomething(first: undefined | T | T[], ...rest: T[]) {
//implementation
}
This wrapper methods makes it easy build an array from the input.
Either an array, the first element of the var args or undefined
, if no argument was given.
Second to last element, if var args were used, empty array, if the first argument is an array.
Array from the input or empty array if no input was given.
Merge two arrays by alternately adding inserting values from both arrays, starting from the left.
For example: zip([1, 2], [3, 4, 5, 6])
results in [1, 3, 2, 4, 5, 6]
Array to start alternately merging from.
Second array to merge.
Zipped array.
Copyright Ⓒ 2023 SAP SE or an SAP affiliate company. All rights reserved.
Version 2 Release
The SAP Cloud SDK has released version 2. Check the upgrade guide for more details. Make sure you understand our release policy.
@sap-cloud-sdk/util
This package contains general utility functions that we reuse multiple times in the SDK. While primarily designed for internal usage, they might also be beneficial for consumers of the SDK.
Installation
Usage
The util package collects useful tools that help build your SAP Business Technology Platform application. For example, the following code snippet changes the log level of the destination accessor of the SDK.
Support
The recommended way to get in touch with us is to create an issue in our github repository. Select the issue category
Bug
,Feature
orQuestion
depending on the nature of your request. We try to provide fixes, features and answers as soon as possible.Contribute
If you would like to contribute to the SAP Cloud SDK, please make yourself familiar with our contributing guidelines and follow the given instructions.
Links
License
The SAP Cloud SDK is released under the Apache License Version 2.0.