0.3.1 • Published 1 year ago

@bucky24/toolbox v0.3.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
1 year ago

Toolbox

This is intended to be a collection of misc things that I've had to do over and over again. It is small but will hopefully continue to grow.

Frontend

Import the frontend code by using:

import { Polygon } from '@bucky24/toolbox/client';

Backend

Import the backend code by using:

const { Api } = require('@bucky24/toolbox/server');

Classes/Components

Note components end with .jsx. Make sure if you are using webpack that you handle this file extension accordingly.

Polygon (Frontend)

The Polygon class contains methods for manipulating and getting data about polygons.

pointInsidePolygon

This method returns a boolean indicating if the given Point is inside the given Polygon

paramtypedescription
pointPointThe point to check against the polygon
polygonPolygonThe Polygon to check

Api (Frontend)

The Api class contains methods for calling api data

callApi

This method returns a Promise resolving to the result. This method expects to receive JSON as a response.

paramtypedescription
getBaseUrlFnFunctionThis is run every call to determine what the base url (hostname) is
methodStringThis is expected to be an HTTP method, all uppercase
urlStringThe url to add to the base url
dataObjectAn optional object containing data to send with the API request
headersObjectAn optional object containing headers to send

Auth (Frontend)

The Auth class on the Frontend handles making authenticated requests with previously stored data

authedFetch

This method calls Api.callApi with an Authentication header mapping to a Bearer token. The parameters are the same as Api.callApi

Auth (Backend)

The Auth class on the Backend handles registering and validating auth tokens.

createToken

This method can take in some data, create a JWT token, and send it both as a cookie, and as a token field in the JSON response to the client.

paramtypedescription
resExpress ResponseThe response object for the api
dataObjectThe object to embed in JWT
timeoutSecIntegerThe timestamp of how long the token should last. Defaults to 1 hour

checkAuth

This method is an Express middleware. It verifies a valid token exists on either the session cookie, or as part of the Authentication header. It will place the extracted data on req.authData.

AuthContext (Frontend)

The AuthContext (and AuthProvider) handle token storage and retrieval

Variables

variabletypedescription
loadingBooleanTrue while system is being initalized
loggedInBooleanTrue if a valid session token is found

setToken

This method saves the token to localstorage

paramtypedescription
tokenStringToken to store

authedFetch

This method is basically the same as Auth.authenticatedFetch.

logout

This method provides functionality to handle clearing tokens.

Router

The Router component wraps routes for react-router-dom adding in the distinction between routes that need a logged in user and normal routes.

proptypedescription
routesRouteData[]List of routes that can be accessed without auth
authedRoutesRouteData[]List of routes that can be accessed with auth
loaderComponentComponent to display while page is loaded
loginRouteStringRoute to redirect to when the user tries to access an authenciated route without proper auth

Types

Point

A point is a simple object that contains a numeric x and a numeric y.

Polygon

The Polygon type is simply an array of Points.

RouteData

A RouteData object contains the following properties

proptypedescription
pathStringPath to the route
elementReact ElementThe element to render for the route
0.3.1

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

3 years ago