update docs

master
garrettmills 4 years ago
parent 5d9d4f39e4
commit 199f8ae53c

@ -1,7 +1,19 @@
/**
* @module libflitter/NamedService
*/
const { Service } = require('flitter-di')
const ImplementationError = require('./errors/ImplementationError')
/**
* A self-named service.
* @extends module:flitter-di/src/Service~Service
*/
class NamedService extends Service {
/**
* Get the name of the service.
* @return {string}
*/
static get name() {
throw new ImplementationError()
}

@ -7,9 +7,14 @@ const Service = require('./NamedService')
/**
* Base class for all Flitter Units.
* A Unit provides one piece of functionality in Flitter.
* @class
* @extends {module:libflitter/NamedService~NamedService}
*/
class Unit extends Service {
/**
* Defines the services required by this unit.
* The 'app' service is provided by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'app']
}

@ -13,10 +13,18 @@ const Unit = require('../Unit')
* @extends module:libflitter/Unit~Unit
*/
class AppUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output', 'configs', 'express']
}
/**
* Defines the name of the service provided by this unit: 'http'
* @returns {string} - 'http'
*/
static get name() {
return "http"
}

@ -1,39 +1,96 @@
/**
* @module libflitter/app/FlitterApp
*/
const { Container, DependencyInjector } = require('flitter-di')
const Service = require('../NamedService')
const output = new (require('../utility/services/Output.service'))()
const Unit = require('../Unit')
/**
* The Flitter application.
* @extends module:flitter-di/src/Service~Service
*/
class FlitterApp extends Service {
/**
* Get the name of this app's service: 'app'
* @returns {string} - 'app'
*/
static get name() {
return 'app'
}
/**
* Instantiate the application.
* @param {object} units - mapping of names to static Unit CLASS definitions
*/
constructor(units) {
super()
/**
* The app's units.
* @type {Array<module:libflitter/Unit~Unit>}
*/
this.__units = Array.isArray(units) ? units : Object.values(units)
/**
* Instance of the output service.
* @type {module:libflitter/utility/services/Output~Output}
*/
this.output = output
/**
* Array of unit names.
* @type {Array<string>}
* @private
*/
this.__unit_array = []
/**
* Mapping of directory names to paths.
* @type {object}
*/
this.directories = {}
this.__init_dependency_injector()
/**
* The underlying Express.js app.
* @type {express}
*/
this.express = require('express')()
}
/**
* Inject dependencies into the static class.
* @param {module:flitter-di/src/Injectable~Injectable} Class - the static CLASS
* @returns {*} - the injected CLASS
*/
make(Class) {
return this.__di.make(Class)
}
/**
* Get the DI.
* @returns {module:flitter-di/src/DependencyInjector~DependencyInjector}
*/
di() {
return this.__di
}
/**
* Run the application by starting, then cleanly stopping all units.
* @returns {Promise<void>}
*/
async run() {
await this.up()
await this.down()
}
/**
* Initialize the application by starting all units.
* @returns {Promise<void>}
*/
async up() {
this.output.message('Starting Flitter...', 0)
@ -44,6 +101,10 @@ class FlitterApp extends Service {
}
}
/**
* Stop the application cleanly by stopping all units.
* @returns {Promise<void>}
*/
async down() {
this.output.message('Stopping flitter...', 0)
@ -56,6 +117,12 @@ class FlitterApp extends Service {
}
}
/**
* Start a single unit.
* @param {module:libflitter/Unit~Unit} unit
* @returns {Promise<*>}
* @private
*/
async __init_unit(unit) {
this.output.info(`Starting ${unit.name()}...`)
@ -77,6 +144,12 @@ class FlitterApp extends Service {
}
}
/**
* Stop a single unit.
* @param {module:libflitter/Unit~Unit} unit
* @returns {Promise<void>}
* @private
*/
async __stop_unit(unit) {
this.output.info(`Stopping ${unit.name()}...`)
@ -92,6 +165,11 @@ class FlitterApp extends Service {
}
}
/**
* Initialize the dependency injector for this app, adding the configured
* units to the container as services.
* @private
*/
__init_dependency_injector() {
const service_definitions = {
app: this.constructor

@ -1,15 +1,43 @@
/**
* @module libflitter/canon/CanonicalAccessUnit
*/
const Unit = require('../Unit')
/**
* Unit service providing access to all canonical accessors by name.
* @extends module:libflitter/Unit~Unit
*/
class CanonicalAccessUnit extends Unit {
/**
* Get the name of the service provided by this unit: 'canon'
* @returns {string} - 'canon'
*/
static get name() {
return 'canon'
}
/**
* Instantiate the unit.
*/
constructor() {
super()
/**
* Mapping of canonical item type to resolver function.
* @type {object}
*/
this.resources = {}
}
/**
* Get a canonical resource by its fully-qualified name.
* e.g. 'controller::Home.welcome'
* e.g. 'middleware::Logger'
* e.g. 'model::auth:User'
*
* @param {string} resource - the fully-qualified canonical resource name
* @returns {*} - the corresponding resource
*/
get(resource) {
const parts = resource.split('::')
let descending_item = this.resources
@ -23,6 +51,11 @@ class CanonicalAccessUnit extends Unit {
return descending_item
}
/**
* Register a canonical resolver under the specified scope.
* @param {string} scope - e.g. 'middleware'/'controller'/&c.
* @param {function} retriever - resolver that takes the unqualified canonical name and returns the resource
*/
register_resource(scope, retriever) {
this.app.output.debug('Registering canonical resource for scope: '+scope)
this.resources[scope] = retriever

@ -1,3 +1,7 @@
/**
* @module libflitter/canon/CanonicalUnit
*/
const path = require('path')
const rra = require('recursive-readdir-async')
const Unit = require('../Unit')
@ -17,24 +21,67 @@ const priv = {
}
}
/**
* Base class for canonical units that load resources from the filesystem
* and name resources based on the file's location within that system.
* @extends module:libflitter/Unit~Unit
*/
class CanonicalUnit extends Unit {
/**
* Defines the services required by this unit.
* The 'canon' service is provided by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'canon']
}
/**
* Get the name of the service provided by this unit.
* @returns {string}
*/
static get name() {
return this.prototype.canonical_item+'s'
}
/**
* Instantiate the unit.
* @param {string} base_directory - the base search directory
*/
constructor(base_directory) {
super()
/**
* The root directory for this canonical resource's files.
* @type {Promise<void> | Promise<string>}
*/
this.directory = path.resolve(base_directory)
/**
* The file extension of the canonical item files.
* @type {string} - '.js'
*/
this.suffix = '.js'
/**
* The canonical name of the item.
* @type {string} = 'item'
*/
this.canonical_item = 'item'
/**
* Mapping of canonical names to instances for this item.
* @type {object}
*/
this.canonical_items = {}
}
/**
* Initializes the unit. Recursively iterates over the base directory and finds
* all valid files. Loads the instances from those files and initializes them.
* @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
* @returns {Promise<void>}
*/
async go(app) {
const files = await rra.list(this.directory)
for ( let key in files ) {
@ -56,16 +103,33 @@ class CanonicalUnit extends Unit {
this.canon.register_resource(this.canonical_item, this.get.bind(this))
}
/**
* Prepare a single canonical item and return the value that should be given by the resolver.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - the exports from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
return instance
}
/**
* Get the directories provided by this unit.
* @returns {object}
*/
directories() {
return {
[this.canonical_item+'s']: this.directory,
}
}
/**
* Resolve an unqualified canonical name to a registered canonical item.
* @param {string} name
* @returns {object}
*/
get(name) {
const name_parts = name.split('.')
let descending_value = this.canonical_items

@ -1,19 +1,49 @@
/**
* @module libflitter/config/ConfigUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
const helpers = require('../utility/Helpers')
const path = require('path')
/**
* Unit to load and manage config files.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class ConfigUnit extends CanonicalUnit {
/**
* Name of the service provided by this unit: 'configs'
* @returns {string} - 'configs'
*/
static get name() {
return 'configs'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './config'] - the base directory
*/
constructor(base_directory = './config') {
super(base_directory)
/**
* The canonical name of the item.
* @type {string} = 'config'
*/
this.canonical_item = 'config'
/**
* The file extension of the canonical item files.
* @type {string} - '.config.js'
*/
this.suffix = '.config.js'
}
/**
* Initialize the unit. Load the dotenv configuration.
* @param app
* @returns {Promise<void>}
*/
async go(app) {
require('dotenv').config()
global.env = helpers.env.bind(helpers)

@ -1,6 +1,18 @@
/**
* @module libflitter/controller/Controller
*/
const { Injectable } = require('flitter-di')
/**
* Base controller for handling requests.
* @extends module:flitter-di/src/Injectable~Injectable
*/
class Controller extends Injectable {
/**
* Defines the services required by this controller.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'app', 'configs']
}

@ -1,19 +1,52 @@
/**
* @module libflitter/controller/ControllerUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
const Controller = require('./Controller')
const SoftError = require('../errors/SoftError')
/**
* Unit to load and manage controller class definitions.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class ControllerUnit extends CanonicalUnit {
/**
* Get the name of the service provided by this unit: 'controllers'
* @returns {string} - 'controllers'
*/
static get name() {
return 'controllers'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './app/controllers'] - the base search directory
*/
constructor(base_directory = './app/controllers') {
super(base_directory)
/**
* The canonical name of the item.
* @type {string} = 'item'
*/
this.canonical_item = 'controller'
/**
* The file extension of the canonical item files.
* @type {string} - '.js'
*/
this.suffix = '.controller.js'
}
/**
* Prepare a single canonical controller and return the value that should be given by the resolver.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - the static CLASS definition of the controller from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
if ( instance.prototype instanceof Controller ) {
return new instance(app)
@ -22,6 +55,11 @@ class ControllerUnit extends CanonicalUnit {
}
}
/**
* Resolve an unqualified canonical name to a registered canonical controller or method.
* @param {string} name
* @returns {module:libflitter/controller/Controller~Controller|function}
*/
get(name) {
const name_parts = name.split('.')
const controller_instance = this.canonical_items[name_parts[0]]

@ -1,28 +1,61 @@
/**
* @module libflitter/database/DatabaseModelsUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
// const Model = require('./Model')
const Model = require('flitter-orm/src/model/Model')
const { createModel } = require('mongo-schematic-class')
const mongoose = require('mongoose')
const Setting = require('./model/Setting.model')
const ScaffoldService = require('flitter-orm/src/services/Connection')
/**
* Unit to load and manage database models.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class DatabaseModelsUnit extends CanonicalUnit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'app', 'output']
}
/**
* Defines the name of the service provided by this unit: 'models'
* @returns {string} - 'models'
*/
static get name() {
return 'models'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './app/models'] - base directory to search
*/
constructor(base_directory = './app/models') {
super(base_directory)
/**
* The canonical name of the item.
* @type {string} = 'model'
*/
this.canonical_item = 'model'
/**
* The file extension of the canonical item files.
* @type {string} - '.model.js'
*/
this.suffix = '.model.js'
}
/**
* Prepare a single canonical model and return the value that should be given by the resolver.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - the static model CLASS from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
// return this.register_model(name, instance)
if ( instance.prototype instanceof Model ) {
@ -33,6 +66,11 @@ class DatabaseModelsUnit extends CanonicalUnit {
}
}
/**
* Initializes the unit. Ensures that the Flitter-ORM scaffold service exists.
* @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
* @returns {Promise<void>}
*/
async go(app) {
if ( !app.di().has(ScaffoldService.name) ) {
throw new Error('Models are not supported unless the flitter-orm scaffold system is provided.')

@ -16,17 +16,25 @@ const ScaffoldService = require('flitter-orm/src/services/Connection')
* @extends module:libflitter/Unit~Unit
*/
class DatabaseUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output', 'configs']
}
/**
* Gets the name of the service provided by this unit: 'database'
* @returns {string} - 'database'
*/
static get name() {
return 'database'
}
/**
* Instantiate the unit. Resolve the path to the directory with the model definition schemata.
* @param {string} models_directory - path to the directory with the model definition schemata
* @param {string} [models_directory = './app/models'] - path to the directory with the model definition schemata
*/
constructor(models_directory = './app/models'){
super()
@ -39,6 +47,11 @@ class DatabaseUnit extends Unit {
this.directory = path.resolve(models_directory)
}
/**
* Get the standard formatted MongoDB connect string using the configured information.
* e.g. 'mongodb://user:pass@host:port/db' &c.
* @returns {string}
*/
get connect_string() {
/*
* Get the database information from the config.

@ -3,7 +3,16 @@
*/
const Model = require('flitter-orm/src/model/Model')
/**
* A model for tracking one-off settings.
* @extends module:flitter-orm/src/model/Model~Model
*/
class Setting extends Model {
/**
* Get the schema for this model.
* @return {object}
*/
static get schema() {
return {
key: String,

@ -6,7 +6,6 @@ const Directive = require('flitter-cli/Directive')
/**
* Provides the Flitter CLI command to launch a normal Flitter HTTP server.
*
* @extends module:flitter-cli/Directive~Directive
*/
class ServerDirective extends Directive {

@ -15,10 +15,18 @@ const Unit = require('../Unit')
* @extends module:libflitter/Unit~Unit
*/
class ErrorUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output', 'views', 'configs']
}
/**
* Gets the name of the service provided by this unit: 'error_handling'
* @returns {string} - 'error_handling'
*/
static get name() {
return 'error_handling'
}

@ -4,15 +4,14 @@
/**
* An error class that provides some extra functionality specific to Flitter.
*
* @extends Error
*/
class FlitterError extends Error {
/**
* Instantiate the error. Set a default component name and message.
* @param msg
* @param args
* @param {string} msg
* @param {...*} args
*/
constructor(msg = "Flitter encountered an error.", ...args){
super(msg, ...args)
@ -26,8 +25,8 @@ class FlitterError extends Error {
/**
* Creates an error message for missing services by name.
* @param {string|string[]} service - the name of the service or array of services that are required
* @returns {FlitterError} - instance of self to allow chaining
* @param {string|Array<string>} service - the name of the service or array of services that are required
* @returns {module:libflitter/errors/FlitterError~FlitterError} - instance of self to allow chaining
*/
required(service){
let message = 'Flitter: The following service or services are required for '+this.unit()+' to start: '
@ -48,7 +47,7 @@ class FlitterError extends Error {
/**
* Get or set the component name.
* @param {string|null} [name = null] - name of the component
* @returns {string|FlitterError} - If no name is provided, returns the current value of {@link module:libflitter/errors/FlitterError~FlitterError#component}. If a name is provided, returns an instance of self to allow chaining.
* @returns {string|module:libflitter/errors/FlitterError~FlitterError} - If no name is provided, returns the current value of {@link module:libflitter/errors/FlitterError~FlitterError#component}. If a name is provided, returns an instance of self to allow chaining.
*/
unit(name = null){
if ( name === null ) return this.component

@ -6,6 +6,7 @@ const StopError = require('./StopError')
/**
* An error that indicates that a required method has not been implemented.
* @extends module:libflitter/errors/StopError@StopError
*/
class ImplementationError extends StopError {

@ -7,6 +7,7 @@ const FlitterError = require('./FlitterError')
/**
* An error that, if thrown within Flitter, indicates that a non-fatal error has occurred.
* Flitter will not shut down after this error, but the exit code will change.
* @extends module:libflitter/errors/FlitterError~FlitterError
*/
class StopError extends FlitterError {

@ -8,6 +8,7 @@ const FlitterError = require('./FlitterError')
* An error that, if thrown within Flitter, will cause Flitter to stop loading units. When this is thrown,
* Flitter will begin cleaning up units to stop gracefully. This error should be preferred over
* {@link libflitter/errors/FatalError~FatalError} when possible.
* @extends module:libflitter/errors/FlitterError~FlitterError
*/
class StopError extends FlitterError {

@ -16,14 +16,21 @@ const https = require('https')
* that Flitter makes available into the underlying Express framework
* so they can be used in lower contexts. Currently, that includes the
* body parser and session store.
*
* @extends module:libflitter/Unit~Unit
*/
class ExpressUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'configs', 'output', 'database']
}
/**
* Gets the name of the service provided by this unit: 'express'
* @returns {string} - 'express'
*/
static get name() {
return 'express'
}

@ -6,8 +6,14 @@ const { Injectable } = require('flitter-di')
/**
* The base class for all app-space middleware.
* @extends module:flitter-di/src/Injectable~Injectable
*/
class Middleware extends Injectable {
/**
* Defines the services required by this unit.
* The 'app' service is included by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'app']
}

@ -1,25 +1,65 @@
/**
* @module libflitter/middleware/MiddlewareUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
const Middleware = require('./Middleware')
const SoftError = require('../errors/SoftError')
const path = require('path')
/**
* Unit to load and manage middleware class definitions.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class MiddlewareUnit extends CanonicalUnit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output']
}
/**
* Get the name of the service provided by this unit: 'middlewares'
* @returns {string} - 'middlewares'
*/
static get name() {
return 'middlewares'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './app/routing/middleware']
* @param {string} [globals_file = './app/routing/Middleware.js']
*/
constructor(base_directory = './app/routing/middleware', globals_file = './app/routing/Middleware.js') {
super(base_directory)
/**
* Fully-qualified path to the file with the definitions for globally-applied middleware.
* @type {Promise<void> | Promise<string>}
*/
this.globals_file = path.resolve(globals_file)
/**
* The canonical name of the item.
* @type {string} = 'middleware'
*/
this.canonical_item = 'middleware'
/**
* The file extension of the canonical item files.
* @type {string} - '.middleware.js'
*/
this.suffix = '.middleware.js'
}
/**
* Initializes the unit. Registers global middleware.
* @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
* @returns {Promise<void>}
*/
async go(app) {
await super.go(app)
@ -38,6 +78,14 @@ class MiddlewareUnit extends CanonicalUnit {
}
}
/**
* Prepare a single canonical middleware definition and return the value that should be given by the resolver.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - the static middleware CLASS from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
if ( instance.prototype instanceof Middleware ) {
this.output.debug('Registering middleware: '+name)

@ -12,8 +12,13 @@ const statuses = require('http-status')
* discrimination. The point of this type of middleware is to modify objects like Express
* requests and responses to add Flitter-specific helper methods. ResponseSystemMiddleware
* specifically adds helpers to the Express response.
* @extends module:flitter-di/src/Injectable~Injectable
*/
class ResponseSystemMiddleware extends Injectable {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'app', 'views', 'configs']
}

@ -1,23 +1,61 @@
/**
* @module libflitter/routing/RoutingUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
const ResponseSystemMiddleware = require('./ResponseSystemMiddleware')
const express = require('express')
/**
* Unit to load, parse, and manage router definitions.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class RoutingUnit extends CanonicalUnit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output']
}
/**
* Gets the name of the service provided by this unit: 'routers'
* @returns {string} - 'routers'
*/
static get name() {
return 'routers'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './app/routing/routers']
*/
constructor(base_directory = './app/routing/routers') {
super(base_directory)
/**
* The canonical name of the item.
* @type {string} = 'router'
*/
this.canonical_item = 'router'
/**
* The file extension of the canonical item files.
* @type {string} - '.router.js'
*/
this.suffix = '.routes.js'
}
/**
* Prepare a single canonical router and return the value that should be given by the resolver.
* This creates a new Express.js router and applies the appropriate middlewares.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - router definition schema from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
const router = express.Router()

@ -1,22 +1,60 @@
/**
* @module libflitter/services/ServicesUnit
*/
const CanonicalUnit = require('../canon/CanonicalUnit')
/**
* Unit to load and manage externally-defined services.
* @extends module:libflitter/canon/CanonicalUnit~CanonicalUnit
*/
class ServicesUnit extends CanonicalUnit {
/**
* Get the name of the service provided by this unit: 'services'
* @returns {string} - 'services'
*/
static get name() {
return 'services'
}
/**
* Instantiate the unit.
* @param {string} [base_directory = './app/services']
*/
constructor(base_directory = './app/services') {
super(base_directory)
/**
* The canonical name of the item.
* @type {string} = 'service'
*/
this.canonical_item = 'service'
/**
* The file extension of the canonical item files.
* @type {string} - '.service.js'
*/
this.suffix = '.service.js'
}
/**
* Prepare a single canonical service and return the value that should be given by the resolver.
* The services are registered into the dependency injector by their canonical name.
* @param {object} info
* @param {module:libflitter/app/FlitterApp} info.app
* @param {string} info.name - the unqualified canonical name
* @param {*} info.instance - the static service CLASS from the file
* @returns {Promise<*>}
*/
async init_canonical_file({app, name, instance}) {
await this.app.di().container.register(name, instance)
return instance
}
/**
* Get the templates provided by this unit: 'service'
* @returns {object}
*/
templates() {
return {
service: {

@ -15,6 +15,10 @@ const fs = require('fs')
* @extends module:libflitter/Unit~Unit
*/
class StaticUnit extends Unit {
/**
* Get the name of the service provided by this unit: 'static'
* @returns {string} - 'static'
*/
static get name() {
return 'static'
}

@ -23,7 +23,7 @@ class ${name} extends Middleware {
* It should either call the next function in the stack,
* or it should handle the response accordingly.
*/
test(req, res, next, args = {}){
async test(req, res, next, args = {}){
// Do stuff here
/*

@ -8,7 +8,7 @@
* @returns {string}
*/
module.exports = exports = (name) => {
return `const Model = require('libflitter/database/Model')
return `const Model = require('flitter-orm/src/model/Model')
/*
* ${name} Model
@ -16,8 +16,8 @@ module.exports = exports = (name) => {
* Put some description here!
*/
class ${name} extends Model {
static get __context() {
// Return a mongoose schema here.
static get schema() {
// Return a flitter-orm schema here.
return {
some_field: String,
}

@ -9,6 +9,7 @@ module.exports = exports = {
* Grabs an environment variable by name and tries to infer its type.
* @param {string} name
* @param default_value
* @deprecated Prefer using {@link module:libflitter/utility/UtilityUnit~UtilityUnit}
* @return {string|null|boolean|number}
*/
env(name, default_value){
@ -20,6 +21,7 @@ module.exports = exports = {
/**
* Attempt to infer the variable type of a string's data.
* @param {string} val
* @deprecated Prefer using {@link module:libflitter/utility/UtilityUnit~UtilityUnit}
* @return {boolean|null|*|number|undefined}
*/
infer(val){
@ -36,6 +38,7 @@ module.exports = exports = {
/**
* Checks if a string is valid JSON.
* @param string
* @deprecated Prefer using {@link module:libflitter/utility/UtilityUnit~UtilityUnit}
* @return {boolean} - true if the string is valid JSON
*/
is_json(string){

@ -4,8 +4,6 @@
const Unit = require('../Unit')
const path = require('path')
// const ServerDirective = require('../directives/ServerDirective')
// const Helpers = require('./Helpers')
const Output = require('./services/Output.service')
@ -18,17 +16,39 @@ const Output = require('./services/Output.service')
* @extends module:libflitter/Unit~Unit
*/
class UtilityUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'configs']
}
/**
* Gets the name of the service provided by this unit: 'utility'
* @returns {string} - 'utility'
*/
static get name() {
return 'utility'
}
/**
* Instantiate the unit.
* @param {string} [app_root = './app'] - path to the 'app' folder
*/
constructor(app_root = './app'){
super()
/**
* Fully-qualified path to the 'app' folder.
* @type {Promise<void> | Promise<string>}
*/
this.directory = path.resolve(app_root)
/**
* Fully-qualified path to the 'app/services' folder.
* @type {Promise<void> | Promise<string>}
*/
this.services_dir = path.resolve(app_root, 'services')
}
@ -92,6 +112,10 @@ class UtilityUnit extends Unit {
}
}
/**
* Get the directories managed by this unit. Includes 'root' for the application root.
* @returns {{root: string}}
*/
directories() {
return {
root: this.root()

@ -1,44 +1,106 @@
/**
* @module libflitter/utility/services/Output
*/
const Service = require('../../NamedService')
const color = require('colors/safe')
/**
* Service for managing output to the console based on logging level.
* @extends module:flitter-di/src/Service~Service
*/
class Output extends Service {
/**
* Get the name of this service: 'output'
* @returns {string} - 'output'
*/
static get name() { return 'output' }
constructor() {
super()
/**
* The logging level.
* @type {number}
*/
this.level = 1
/**
* If true, includes a timestamp in the output. Default false.
* @type {boolean}
*/
this.timestamp = false
}
/**
* Get a color-formatted timestamp string.
* @returns {string}
* @private
*/
__timestamp() {
return this.timestamp ? color.cyan(`[${(new Date).toLocaleString()}]`) : ''
}
/**
* Send a [DEBUG] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 5] - the message level
*/
debug(msg, level = 5) {
if ( this.level < level ) return;
console.log(color.magenta(' [DEBUG]'), this.__timestamp(), msg)
}
/**
* Send an [INFO] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 4] - the message level
*/
info(msg, level = 4) {
if ( this.level < level ) return;
console.log(color.blue(' [INFO]'), this.__timestamp(), msg)
}
/**
* Send a [MESSAGE] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 3] - the message level
*/
message(msg, level = 3) {
if ( this.level < level ) return;
console.log(color.cyan(' [MESSAGE]'), this.__timestamp(), msg)
}
/**
* Send a [WARNING] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 2] - the message level
*/
warn(msg, level = 2) {
if ( this.level < level ) return;
console.log(color.yellow(' [WARNING]'), this.__timestamp(), msg)
}
/**
* Send an [ERROR] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 1] - the message level
*/
error(msg, level = 1) {
if ( this.level < level ) return;
console.log(color.red(' [ERROR]'), this.__timestamp(), msg)
}
/**
* Send a [SUCCESS] message.
* The logging level must be greater than or equal to the message level for it to display.
* @param {string} msg
* @param {number} [level = 1] - the message level
*/
success(msg, level = 1) {
if ( this.level < level ) return;
console.log(color.green(' [SUCCESS]'), this.__timestamp(), msg)

@ -14,10 +14,18 @@ const Unit = require('../Unit')
* @extends module:libflitter/Unit~Unit
*/
class ViewEngineUnit extends Unit {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'output', 'configs', 'models']
}
/**
* Get the name of the service provided by this unit: 'views'
* @returns {string} - 'views'
*/
static get name() {
return 'views'
}

Loading…
Cancel
Save