@foal/core
Advanced tools
Comparing version 0.3.0 to 0.4.0-alpha.0
@@ -0,1 +1,7 @@ | ||
/** | ||
* FoalTS | ||
* Copyright(c) 2017-2017 Loïc Poullain <loic.poullain@centraliens.net> | ||
* Released under the MIT License. | ||
*/ | ||
"use strict"; | ||
@@ -2,0 +8,0 @@ function __export(m) { |
import 'reflect-metadata'; | ||
import { Decorator, MethodBinding } from './controllers/interfaces'; | ||
import { Injector } from './di/injector'; | ||
import { Type } from './interfaces'; | ||
export interface FoalModule { | ||
services: Type<any>[]; | ||
controllerBindings?: ((injector: Injector) => MethodBinding[])[]; | ||
preHooks?: Decorator[]; | ||
imports?: { | ||
module: FoalModule; | ||
path?: string; | ||
}[]; | ||
} | ||
import { FoalModule, LowLevelRoute } from './interfaces'; | ||
import { ServiceManager } from './service-manager'; | ||
export declare class Foal { | ||
readonly injector: Injector; | ||
readonly methodsBindings: MethodBinding[]; | ||
readonly services: ServiceManager; | ||
readonly lowLevelRoutes: LowLevelRoute[]; | ||
constructor(foalModule: FoalModule, parentModule?: Foal); | ||
private getPreMiddlewares(preHooks); | ||
private getMiddlewares(hooks); | ||
} |
@@ -0,57 +1,57 @@ | ||
/** | ||
* FoalTS | ||
* Copyright(c) 2017-2017 Loïc Poullain <loic.poullain@centraliens.net> | ||
* Released under the MIT License. | ||
*/ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("reflect-metadata"); | ||
var injector_1 = require("./di/injector"); | ||
var Foal = /** @class */ (function () { | ||
function Foal(foalModule, parentModule) { | ||
var _this = this; | ||
this.methodsBindings = []; | ||
var controllerBindings = foalModule.controllerBindings || []; | ||
var imports = foalModule.imports || []; | ||
var modulePreHooks = foalModule.preHooks || []; | ||
const service_manager_1 = require("./service-manager"); | ||
class Foal { | ||
constructor(foalModule, parentModule) { | ||
this.lowLevelRoutes = []; | ||
const controllers = foalModule.controllers || []; | ||
const modules = foalModule.modules || []; | ||
const moduleHooks = foalModule.hooks || []; | ||
if (parentModule) { | ||
this.injector = new injector_1.Injector(parentModule.injector); | ||
this.services = new service_manager_1.ServiceManager(parentModule.services); | ||
} | ||
else { | ||
this.injector = new injector_1.Injector(); | ||
this.services = new service_manager_1.ServiceManager(); | ||
} | ||
foalModule.services.forEach(function (service) { return _this.injector.inject(service); }); | ||
var modulePreMiddlewares = this.getPreMiddlewares(modulePreHooks); | ||
for (var _i = 0, controllerBindings_1 = controllerBindings; _i < controllerBindings_1.length; _i++) { | ||
var controllerBinding = controllerBindings_1[_i]; | ||
for (var _a = 0, _b = controllerBinding(this.injector); _a < _b.length; _a++) { | ||
var methodBinding = _b[_a]; | ||
this.methodsBindings.push(__assign({}, methodBinding, { middlewares: modulePreMiddlewares.map(function (e) { return (function (ctx) { return e(ctx, _this.injector); }); }).concat(methodBinding.middlewares) })); | ||
foalModule.services.forEach(service => this.services.add(service)); | ||
const { modulePreMiddlewares, modulePostMiddlewares } = this.getMiddlewares(moduleHooks); | ||
for (const controller of controllers) { | ||
for (const lowLevelRoute of controller(this.services)) { | ||
this.lowLevelRoutes.push(Object.assign({}, lowLevelRoute, { middlewares: [ | ||
...modulePreMiddlewares.map(e => (ctx => e(ctx, this.services))), | ||
...lowLevelRoute.middlewares, | ||
...modulePostMiddlewares.map(e => (ctx => e(ctx, this.services))), | ||
] })); | ||
} | ||
} | ||
for (var _c = 0, imports_1 = imports; _c < imports_1.length; _c++) { | ||
var imp = imports_1[_c]; | ||
var importedModule = new Foal(imp.module, this); | ||
var path = imp.path || ''; | ||
for (var _d = 0, _e = importedModule.methodsBindings; _d < _e.length; _d++) { | ||
var methodBinding = _e[_d]; | ||
this.methodsBindings.push(__assign({}, methodBinding, { middlewares: modulePreMiddlewares.map(function (e) { return (function (ctx) { return e(ctx, _this.injector); }); }).concat(methodBinding.middlewares), paths: [path].concat(methodBinding.paths) })); | ||
for (const mod of modules) { | ||
const importedModule = new Foal(mod.module, this); | ||
const path = mod.path || ''; | ||
for (const lowLevelRoute of importedModule.lowLevelRoutes) { | ||
this.lowLevelRoutes.push(Object.assign({}, lowLevelRoute, { middlewares: [ | ||
...modulePreMiddlewares.map(e => (ctx => e(ctx, this.services))), | ||
...lowLevelRoute.middlewares, | ||
...modulePostMiddlewares.map(e => (ctx => e(ctx, this.services))), | ||
], paths: [path, ...lowLevelRoute.paths] })); | ||
} | ||
} | ||
} | ||
Foal.prototype.getPreMiddlewares = function (preHooks) { | ||
var FakeModule = /** @class */ (function () { | ||
function FakeModule() { | ||
} | ||
return FakeModule; | ||
}()); | ||
getMiddlewares(hooks) { | ||
class FakeModule { | ||
} | ||
// Reverse the array to apply decorators in the proper order. | ||
preHooks.reverse().forEach(function (decorator) { return decorator(FakeModule); }); | ||
return Reflect.getMetadata('pre-middlewares', FakeModule) || []; | ||
}; | ||
return Foal; | ||
}()); | ||
hooks.reverse().forEach(hook => hook(FakeModule)); | ||
return { | ||
modulePostMiddlewares: Reflect.getMetadata('post-middlewares', FakeModule) || [], | ||
modulePreMiddlewares: Reflect.getMetadata('pre-middlewares', FakeModule) || [], | ||
}; | ||
} | ||
} | ||
exports.Foal = Foal; |
@@ -1,3 +0,6 @@ | ||
export { Service } from './di/injector'; | ||
export * from './foal'; | ||
export * from './controllers'; | ||
export * from './factories'; | ||
export * from './interfaces'; | ||
export { combineHooks } from './utils'; | ||
export * from './errors'; | ||
export { Foal } from './foal'; | ||
export { ServiceManager, Service } from './service-manager'; |
@@ -0,1 +1,7 @@ | ||
/** | ||
* FoalTS | ||
* Copyright(c) 2017-2017 Loïc Poullain <loic.poullain@centraliens.net> | ||
* Released under the MIT License. | ||
*/ | ||
"use strict"; | ||
@@ -6,5 +12,10 @@ function __export(m) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var injector_1 = require("./di/injector"); | ||
exports.Service = injector_1.Service; | ||
__export(require("./foal")); | ||
__export(require("./controllers")); | ||
__export(require("./factories")); | ||
var utils_1 = require("./utils"); | ||
exports.combineHooks = utils_1.combineHooks; | ||
__export(require("./errors")); | ||
var foal_1 = require("./foal"); | ||
exports.Foal = foal_1.Foal; | ||
var service_manager_1 = require("./service-manager"); | ||
exports.ServiceManager = service_manager_1.ServiceManager; | ||
exports.Service = service_manager_1.Service; |
{ | ||
"name": "@foal/core", | ||
"version": "0.3.0", | ||
"description": "Backend framework written in TypeScript", | ||
"version": "0.4.0-alpha.0", | ||
"description": "A Node.js framework for building robust web apps.", | ||
"main": "./dist/index.js", | ||
@@ -9,6 +9,12 @@ "types": "./dist/index.d.ts", | ||
"test": "mocha --require ts-node/register \"./src/**/*.spec.ts\"", | ||
"test-watch": "mocha --require ts-node/register --watch --watch-extensions ts \"./src/**/*.spec.ts\"", | ||
"compile": "tsc", | ||
"prepublish": "tsc" | ||
"dev:test": "mocha --require ts-node/register --watch --watch-extensions ts \"./src/**/*.spec.ts\"", | ||
"build": "gulp clean && tsc && gulp add-header", | ||
"prepublish": "gulp clean && tsc && gulp add-header" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
@@ -18,3 +24,5 @@ "backend", | ||
"typescript", | ||
"server" | ||
"server", | ||
"foal", | ||
"rest" | ||
], | ||
@@ -43,2 +51,5 @@ "bugs": { | ||
"chai": "^4.1.2", | ||
"del": "^3.0.0", | ||
"gulp": "^3.9.1", | ||
"gulp-header": "^1.8.9", | ||
"mocha": "^3.5.3", | ||
@@ -45,0 +56,0 @@ "ts-node": "^3.3.0", |
# FoalTS | ||
**This work is in progress. Future releases may break current features, so use it at your own risk!** | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/FoalTS/foal/blob/master/LICENSE) | ||
![node version](https://img.shields.io/badge/node-%3E%3D8-brightgreen.svg) | ||
[![npm version](https://badge.fury.io/js/%40foal%2Fcore.svg)](https://badge.fury.io/js/%40foal%2Fcore) | ||
[![Build Status](https://travis-ci.org/FoalTS/foal.svg?branch=add-travis)](https://travis-ci.org/FoalTS/foal) | ||
## Installation | ||
A Node.js framework for building robust web apps. | ||
```ts | ||
npm install --save express body-parser @foal/core @foal/express | ||
``` | ||
Github: [https://github.com/FoalTS/foal](https://github.com/FoalTS/foal) | ||
## Get started | ||
Twitter: [https://twitter.com/FoalTs](https://twitter.com/FoalTs) | ||
```json | ||
// tsconfig.json | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
] | ||
... | ||
} | ||
``` | ||
Website: [https://foalts.org/](https://foalts.org/) | ||
```ts | ||
import * as bodyParser from 'body-parser'; | ||
import * as express from 'express'; | ||
import { getCallback } from '@foal/express'; | ||
import { Foal, Service, rest, RestController, RestParams } from '@foal/core'; | ||
@Service() | ||
class User implements RestController { | ||
constructor () {} | ||
async create(data: any, params: RestParams) { | ||
console.log(params.query); | ||
data.createdAt = Date.now(); | ||
return data; | ||
} | ||
} | ||
const foal = new Foal({ | ||
services: [ User ], | ||
controllerBindings: [ rest.bindController('/users', User) ] | ||
}); | ||
const app = express(); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.json()); | ||
app.use(getCallback(foal)); | ||
app.listen(3000, () => console.log('Listening...')); | ||
``` | ||
## Documentation | ||
Find docs [here](https://foalts.gitbooks.io/docs/content/). | ||
## Contributing | ||
There are several ways to contribute. | ||
- Submit a PR to fix typos/grammatical errors. | ||
- Open an issue to report a bug. | ||
- Open an issue to suggest a new feature. | ||
- Improve the docs. | ||
## Packages | ||
- @foal/core | ||
- @foal/sequelize | ||
## License | ||
MIT | ||
Documentation: [https://foalts.gitbooks.io/docs/](https://foalts.gitbooks.io/docs/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23277
9
39
559
17