Global

Members

(constant) DateFormat

Enumeration object of date formats. Values are:

  • ISO_8601
  • UNIX_MILLISECONDS
  • UNIX_SECONDS
Source:

(constant) ResponseStatus

Enumeration object of service response statuses. Values are:

  • SUCCESS
  • FAILURE
  • PARTIAL
Source:

Methods

extractDtoFromEntity(entity, arg1)

Extracts a data transfer object (DTO) from the given entity. The entity normally is an instance of a class with data properties whose names begin with the underscore character (_). By default, this will descend recursively through the given object and drop the leading underscore prefix from its keys and convert any convertible values, mainly Dates to ISO-8601 strings and Enumeration instances to just their names. All methods found are excluded from the returned DTO. Each formatter is customizable.

Parameters:
Name Type Description
entity *

The entity from which to extract a data transfer object (DTO).

arg1 object

The argument to be deconstructed.

Properties
Name Type Attributes Default Description
keyReplacementRegEx string | RegExp <optional>
/^_/

The key replacement regular expression given to the replace method of String; if falsey, no key replacement is performed.

keyReplacement string | function <optional>
''

The key replacement given to the replace method of String.

dateFormatter function <optional>

The formatter of Dates; defaults to toISOString() on Date.

enumerationFormatter function <optional>

The formatter of Enumeration instances; defaults to returning the name.

Source:

formatDate(arg0opt, dateopt, formatopt) → {string|number}

Formats the given date using the given DateFormat enum.

Parameters:
Name Type Attributes Default Description
arg0 object <optional>

The argument to be deconstructed.

date Date <optional>

The Date to be formatted; if not a Date, the value is returned unchanged.

format DateFormat <optional>
DateFormat.ISO_8601

The DateFormat enum value to use to format date.

Source:
Returns:

The formatted date.

Type
string | number

formatEnumeration(arg0opt) → {string}

Formats the given (enumeration)[https://www.npmjs.com/package/@northscaler/enum-support].

Parameters:
Name Type Attributes Description
arg0 object <optional>

The argument to be deconstructed.

Properties
Name Type Attributes Default Description
enumeration object

The Enumeration to be formatted.

useName boolean <optional>
true

If truey, the the name of the Enumeration will be returned, else its ordinal.

Source:
Returns:

If useName is truey, the name of the Enumeration, else its ordinal.

Type
string

formatError(arg0opt, erroropt, includeStackopt, includeCauseopt) → {FormattedError}

Formats the given error as a plain object.

Parameters:
Name Type Attributes Default Description
arg0 object <optional>

The argument to be deconstructed.

error object <optional>

The Error (or Error-like object) to be formatted.

includeStack boolean <optional>
true

Whether to include the stack property.

includeCause boolean <optional>
true

Whether to include the cause property.

Source:
Returns:

The formatted error as a literal object.

Type
FormattedError

serviceMethod(arg0opt)

Decorator used to execute the decorated method in a try/catch block. Use on service class methods only. If the method returns normally, the return value is wrapped in a service response object with the following shape:

{
  data: <method return value>,
  meta: {
    status: ResponseStatus.SUCCESS,
    elapsedMillis: n
  }
}

If the method throws, the Error is caught, and formatted as a service response object with the following shape:

{
  error: {
    name: ...,
    message: ...,
    code: ...,
    info: ...,
    cause: ...,
    stack: ...,
  },
  meta: {
    status: ResponseStatus.FAILURE,
    elapsedMillis: n
  }
}
Parameters:
Name Type Attributes Description
arg0 object <optional>

The argument to be deconstructed.

Properties
Name Type Attributes Default Description
formatters Array.<Formatter> <optional>

Formatters to be used when formatting values in returned objects or thrown Errors.

includeErrorStacks boolean <optional>
true

Whether to include the Error stack property if one is thrown or there are Errors in the return value. Ignored if formatters is given.

includeErrorCauses boolean <optional>
true

Whether to include the Error cause property if one is thrown or there are Errors in the return value. Ignored if formatters is given.

dateFormat DateFormat <optional>
DateFormat.ISO_8601

The date format to use. Ignored if formatters is given.

Source:

Type Definitions

FormattedError

Type:
  • object
Properties:
Name Type Attributes Description
name string <optional>
message string <optional>
code string <optional>
info * <optional>
stack string <optional>
cause object <optional>
Source:

Formatter

Properties:
Name Type Description
type *

The type of object to format if the value is instanceof type.

formatter function

The function that takes a single argument that is the instance of the type and returns that value formatted.

Source: