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.
LOG_LEVEL environment variable to one of the following values:
INFO - Logs info, warnings, and errors
WARN - Logs warnings and errors
ERROR - Default
javascript
import { createLoggers } from '@timmons-group/snack-utils';
const { log, warn, error } = createLoggers('MyModule');
`
Then use the logger to log messages:
`javascript
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:
`javascript
import { setLogLevel } from '@timmons-group/snack-utils';
setLogLevel('INFO');
`
$3
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:
`javascript
import { createProfiler } from '@timmons-group/snack-utils';
const profiler = createProfiler();
`
Then use the profiler to profile a block of code:
`javascript
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.
$3
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-Type header to application/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:
`javascript
import { responses } from '@timmons-group/snack-utils';
`
Then use the utility to generate responses:
`javascript
const response = responses.jsonSuccess({ message: 'Hello, World!' });
`
Alternatively, you can import the individual response types:
`javascript
import { jsonSuccess } from '@timmons-group/snack-utils/responses';
const response = jsonSuccess({ message: 'Hello, World!' });
``