npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tramway-core

v2.2.2

Published

A simple framework extension to an Express application offering a variety of common abstractions

Downloads

74

Readme

Tramway is a simple framework core that can be used with Express to speed up and structure apps built in ES2015+. It includes:

  1. A dynamic routing system that separates routes from routing logic and is adaptable
  2. Restful routes to save time building APIs
  3. An adaptive Connection system
  4. Models and Entities which are separate but when used together with Connections can speed up workflow.
  5. Authentication policies that allow for multiple strategies to be used and interchanged without needing the logic throughout the code. and so much more.

This library provides the common core and it is encouraged to install the compatible pieces you need for your project.

Installation:

  1. npm install tramway-core

Example project

https://gitlab.com/tramwayjs/tramway-example

Documentation

Recommended Folder Structure

  • config
  • errors
  • services

App

The App class encompasses your express app into a clean shell that can be built with the tramway-core-dependency-injector library.

To use App in your server.js the declarative way:

import {App} from 'tramway-core';
...

let app = new App(router, express, port);
app.use(cors());

app.initialize().start();

To use App with dependency injection in the services declaration:

import {App} from 'tramway-core';

export default {
    "app": {
        "class": App,
        "constructor": [
            {"type": "service", "key": "router"},
            {"type": "parameter", "key": "app"},
            {"type": "parameter", "key": "PORT"},
        ],
        "functions": [
            {
                "function": "use",
                "args": [{"type": "parameter", "key": "cors"}],
            },
        ],
    }
}

In your server.js:

let app = DependencyResolver.getService('app');
app.initialize().start();

HTTPS Support

An additional final parameter can be added to the App constructor which passes extra data that can be used to start an https server.

The object takes this form:

const httpsConfig = {
    port: 8443,
    privateKey: {
        file: 'sslcert/server.key',
        encoding: 'utf8'
    },
    certificate: {
        file: 'sslcert/server.cert',
        encoding: 'utf8'
    },
}

The object can be passed as a param directly to App or saved in the parameters and passed via the dependency injection configuration.

Example:

In config/parameters/global/index.js

Add a reference to the above config saved in httpsConfig.js

...
import httpsConfig from './httpsConfig';

export default {
    ...
    httpsConfig,
}

In config/services/core.js:

import {App} from 'tramway-core';

export default {
    "app": {
        "class": App,
        "constructor": [
            {"type": "service", "key": "router"},
            {"type": "parameter", "key": "app"},
            {"type": "parameter", "key": "PORT"},
            {"type": "parameter", "key": "httpsConfig"},
        ],
        "functions": [
            {
                "function": "use",
                "args": [{"type": "parameter", "key": "cors"}],
            },
        ],
    }
}

app.set

The App provides an injectable wrapper over the Express app's set method which can be used to managed trusted proxies.

It takes an object with a key and value, where the value is expected to be an array that can be spread as many arguments in the express set method.

Declarative:

let app = new App(router, express, port);
app.set({key: 'trust proxy', value: true});

To use with dependency injection in the services declaration:

Create src/config/parameters/global/trustedProxy.js

export default {
    key: 'trust proxy',
    value: true,
}

In src/config/parameters/global/index.js

Add a reference to the above config saved in trustedProxy.js

...
import trustedProxy from './trustedProxy';

export default {
    ...
    trustedProxy,
}

In config/services/core.js:

import {App} from 'tramway-core';

export default {
    "app": {
        "class": App,
        "constructor": [
            {"type": "service", "key": "router"},
            {"type": "parameter", "key": "app"},
            {"type": "parameter", "key": "PORT"},
            {"type": "parameter", "key": "httpsConfig"},
        ],
        "functions": [
            {
                "function": "use",
                "args": [{"type": "parameter", "key": "cors"}],
            },
            {
                "function": "set",
                "args": [{"type": "parameter", "key": "trustedProxy"}],
            },
        ],
    }
}

Config

Here is where you can put all the parameters for express - like CORS

Services

The services folder is to place any logic that handles specific core tasks. The framework comes with a service itself.

Type Enforcement Service

The type enforcement service makes up for the short-comings of typing in JavaScript and lets your app cleanly enforce types if need be. The utility service comes with a few static methods to let you enforce basic types or custom classes and either throw an Error (default) or override the behavior with a less obstructive way to handle it.

To use the TypeEnforcementService just import it.

import {services} from 'tramway-core`;
let {TypeEnforcementService} = services;

| Function | Usage | Notes | | --- | --- | --- | | enforceTypes(value: any, types: Set<string>/string[]/string, errorHandler: function(value): value): value | TypeEnforcementService.enforceTypes(someValue, ["string", "number"]); | errorHandler is optional. Will check basic types using typeof and return value if valid or throw WrongTypeError | | enforceInstance(value: any, expectedClass: Object, errorHandler: function(value): value): value | TypeEnforcementService.enforceTypes(someValue, SomeClass); | errorHandler is optional. Will check basic types using instanceof and return value if valid or throw WrongTypeError |

Errors

All errors extend Javascript's Error class and naming repeated ones comes in handy when reading code and writing it quickly. The framework comes with errors which can be accessed and used.

import {errors} from 'tramway-core';
let {WrongTypeError, AbstractMethodError} = errors;

| Function | Usage | Notes | | --- | --- | --- | | WrongTypeError | new WrongTypeError(expectedType: string, gotType: string)| Used when the wrong type is passed. | | AbstractMethodError | new AbstractMethodError() | Used when an abstract method is made to ensure it gets overridden in a child class - there to fill a shortcoming of Javascript missing Interfaces and Abstract classes. |