@timmons-group/snack-utils
v0.1.1
Published
SNACK is a collection of components that can be used to build a shared node architecture. The utils module contains a collection of utilities that can be used to build a serveless node application.
Downloads
306
Maintainers
Keywords
Readme
Shared Node Architecture Component Kit (SNACK) Utils
SNACK is a collection of components that can be used to build a shared node architecture. The utils module contains a collection of utilities that can be used to build a serveless node application.
Roadmap
- [x] Collect Shared Utilities
- [x] Refactor
- [x] Add Documentation
- [ ] Publish v1
Utilities
Logger
The logger utility is a simple utility that can be used to log messages to the console. It supports different log levels and can be configured to log to different destinations.t
By default, the logger supresses all log messages. To enable logging, set the LOG_LEVEL environment variable to one of the following values:
INFO- Logs info, warnings, and errorsWARN- Logs warnings and errorsERROR- Default
To log a message, first create the logger instance:
import { createLoggers } from '@timmons-group/snack-utils';
const { log, warn, error } = createLoggers('MyModule');Then use the logger to log messages:
log('This is an info message');
warn('This is a warning message');
error('This is an error message');You can programatically set the log level by calling the setLogLevel function:
import { setLogLevel } from '@timmons-group/snack-utils';
setLogLevel('INFO');Profiler
The profiler utility is a simple utility that can be used to profile the performance. Using a set of labels and timestamps, the profiler can be used to measure the time it takes to execute a block of code.
To use the profiler, first create an instance of the profiler:
import { createProfiler } from '@timmons-group/snack-utils';
const profiler = createProfiler();Then use the profiler to profile a block of code:
profiler.tab('Block 1');
// Code to profile
profiler.tab('Block 2');
// Code to profile
profiler.tab('Block 3');
// Code to profile
profiler.end();
profiler.report();The profiler will output the time it took to execute each block of code.
Responses
The responses utility is a simple utility that can be used to generate HTTP responses for use with lambda. It supports different response types and can be used to generate responses for different status codes.
Supported response types:
- jsonSuccess - Generates a successful (200) JSON response and sets the
Content-Typeheader toapplication/json - noContent - Generates a successful (204) response with no content
- notFound - Generates a not found (404) response
- badRequest - Generates a bad request (400) response
- unauthorized - Generates an unauthorized (401) response
- serverError - Generates a server error (500) response
To use the responses utility, first import the utility:
import { responses } from '@timmons-group/snack-utils';Then use the utility to generate responses:
const response = responses.jsonSuccess({ message: 'Hello, World!' });Alternatively, you can import the individual response types:
import { jsonSuccess } from '@timmons-group/snack-utils/responses';
const response = jsonSuccess({ message: 'Hello, World!' });