@btilford/ts-base
v1.1.2
Published
Boring things used in projects.
Downloads
6
Maintainers
Readme
ts-base
Boring project components.
Install
npm install --save @btilford/ts-base-node
npm install --save @btilford/ts-base-browser
Usage
Examples can be found here
Features
Logging
Extra features and configurability for logging. By default uses Console but with node can also use a stream.Writable for stdout and stderr output.
- Configure default LogLevel
- Configure and register a root Log
- Log.extend creates a new Log extending it's name and any configuration not overridden.
- Log.copy creates a new Log instance sharing the configs that were not overridden but does not inherit naming like Log.extend does.
- You can add Interceptors to your LogConfig to perform additional tasks on log messages. In the node module there is an interceptor for Stackdriver and Datadog that can be combined with a FilterLogMessage to send critical messages.
- In your LogConfig you can provide a Context function to supply additional properties to make available in a MessageTemplate.
- You can configure a MessageTemplate and MessageFormat in your LogConfig.
MessageTemplate { format(ctx: MessageContext):string }
is generates a string that can include items supplied by a Context call.MessageFormat {(msg, ...args):string}
can be used to format you log message if you wanted to use something like printj instead ofconsole.log(message, ...args)
.
| Log Type | Browser | Node | Comment | |--- |--- |--- |--- | | ConsoleLog | yes | yes | Just a wrapper around console | | StdOutLog | no | yes | Uses a Writable defaulting to stdout/stderr | | JsonLog | no | yes | TODO |
APM
There are some method decorators for instrumenting methods with custom APM. The decorators can accept a Filter that can determine whether to run the decorator or just run the original method. For example only run the decorator N times or at intervals.
| Type | System | Provider |
|--- |--- |--- |
| @Timed | Browser | Console |
| @Timed | Node | Console, StatsD, Prometheus |
| @TimedAsync | Browser | Console |
| @TimedAsync | Node | Console, StatsD, Prometheus |
| @CountInvocations | Browser | Console |
| @CountInvocations | Node | Console, StatsD, Prometheus |
| @CountSuccess | Browser | Console |
| @CountSuccess | Node | Console, StatsD, Prometheus |
| @CountErrors | Browser | Console |
| @CountErrors | Node | Console, StatsD, Prometheus |
| @GaugeInvocations | Node | Console, StatsD, Prometheus |
| @GaugeSuccess | Node | Console, StatsD, Prometheus |
| @GaugeErrors | Node | Console, StatsD, Prometheus |
Time
Note most of the things in this package use BigInt which isn't super well supported yet.
Classes
- TimeMeasurement -- Holds a bigint of arbitrary nanoseconds.
- Duration -- Holds a bigint representing time elapsed between 2 arbitrary nano seconds.
- Instant -- Holds a bigint of nanoseconds representing a point in time.
- Period -- Holds a bigint of elapsed nanoseconds between 2 Instants.
Utilities
- timePart(nanos, unit) -- Returns the count of unit in nanos and the remainder.
[count:bigint, remainder:bigint]
- timeParts(nanos, parts:{name:string, unit:bigint}[]) -- Returns a
Record<string, bigint>
with the count for each name based on the remainder of larger units. - millisAsString(nanos:bigint, postfix?: '' | 'n') -- Turn nanoseconds into a string... Lots of things don't serialize bigint yet.
- millisAsNanoString(milliseconds:number, postfix?: '' | 'n') -- Turn milliseconds into a string with all the extra 0s for a nanosecond.
Other Stuff
- Providers -- A service locator/lookup utility.
- Env -- Load and access environmental configs with support for nested EnvProps configs based on a prefix. There are loaders for:
- processEnvLoader -- For process.env on node.
- loadEnvWithObj -- For providing arbitrary variables.
- documentQueryExtractor -- For loading data from the DOM in the browser.
- loadEnvWithMetaTags -- For loading meta tags with a specific prefix in the browser.
- debugEnv -- Initializes internal components to full debug.
- PropEnabled -- Matches keys in an EnvProps against a set of accepted values.
- bootstrap() -- Helper can be called from your entry point to initialize the different components you want to use.