@timmons-group/snack-pack
v0.1.1
Published
This package contains a collection of components that are used in the Shared Node Architecture Component Kit (SNACK) to serve as scaffolding for building applications.
Maintainers
Keywords
Readme
Shared Node Architecture Component Kit (SNACK) Pack
This package contains a collection of components that are used in the Shared Node Architecture Component Kit (SNACK) to serve as scaffolding for building applications.
Roadmap
- [ ] Collect Shared Scaffolding
- [ ] Refactor
- [ ] Add Documentation
- [ ] Publish v1
Scaffold
REST API
We provide helpers for building a rest api via the snack-pack/REST module. This module provides a set of utilities for building a REST API using the snack-pack.
The providers listed below are available:
- provideDatabase - This will provide a database connection to the supplied funcin using the configuration
- provideUser - this will call the provided function with the rest request and user object
- provideErrorHandling - This will wrap the function and return a 500 error if the function throws an error
- provideRESTDefaults - this provides the database, the user, and the error handling to the function with the supplied permissions
Most often the provideRESTDefaults will be used from this package
import { provideRESTDefaults } from '@timmons-group/snack-pack/REST';
const myHandler = async (apiEvent, databaseDriver, user) => {
// do something with the request and response
return {
statusCode: 200,
body: 'Hello World!',
};
};
export const handler = provideRESTDefaults( myHandler, ['read', 'write'] );This will provide the database connection and user to the function, and will also handle any errors that are thrown. The permissions array will be used to check if the user has the required permissions to access the endpoint. If the user does not have the read or write permissions, a 403 error will be returned.
import { provideDatabase } from '@timmons-group/snack-pack/REST';
const myHandler = async (apiEvent, databaseDriver) => {
// Here we can run a query against the database
const result = await databaseDriver.query('SELECT * FROM hello_world');
return {
statusCode: 200,
body: 'Hello ' + result.join(', ') + '!',
};
};
export const handler = provideDatabase( myHandler );This will provide the database connection to the function. The databaseDriver will be passed to the function as the second argument. The first argument will be the apiEvent object. This is useful for running queries against the database.
import { provideUser } from '@timmons-group/snack-pack/REST';
const myHandler = async (apiEvent, user) => {
// Here we can run a query against the database
return {
statusCode: 200,
body: 'Hello ' + user.name + '!',
};
};
export const handler = provideUser( myHandler, myDatabaseDriver );This will provide the user to the function. The user object will be passed to the function as the second argument. The first argument will be the apiEvent object. This is useful for getting information about the user that is making the request.
import { provideErrorHandling } from '@timmons-group/snack-pack/REST';
const myHandler = async (apiEvent) => {
// Here we can run a query against the database
throw new Error('Something went wrong');
};
export const handler = provideErrorHandling( myHandler );This will provide error handling to the function. If the function throws an error, a 500 error will be returned. The error message will be logged to the console. This is useful for handling errors that occur in the function.
REST Endpoint Builder
The RESTEndpointBuilder is a utility that allows you to build a REST endpoint using the snack-pack/REST module. This is useful for building a REST API using the snack-pack without having to manually provide the database, user, and error handling to the function.
Long form example of using the RESTEndpointBuilder to build a REST endpoint:
import { RESTEndpointBuilder } from '@timmons-group/snack-pack/REST/Endpoints';
const internalHandlerFunction = async (apiEvent, {databaseDriver, user}) => {
// do something with the request and response
return {
statusCode: 200,
body: 'Hello World!',
};
};
export const handler = new RESTEndpointBuilder(internalHandlerFunction)
.withDatabase()
.withUser()
.withErrorHandling()
.withPermissions(['read', 'write'])
.build();
Short form example of using the RESTEndpointBuilder to build a REST endpoint:
import { RESTEndpointBuilder } from '@timmons-group/snack-pack/REST/Endpoints';
const internalHandlerFunction = async (apiEvent, {databaseDriver, user}) => {
// do something with the request and response
return {
statusCode: 200,
body: 'Hello World!',
};
};
export const handler = new RESTEndpointBuilder(internalHandlerFunction)
.withRESTDefaults(['read', 'write'])
.build();
Auth
Using the provided oAuth endpoints as follows
import { handler as oAuthHandler } from '@timmons-group/snack-pack/Auth/Endpoints'
export const handler = oAuthHandler;