@foal/core
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -1,2 +0,2 @@ | ||
import { ModuleData } from '@foal/core'; | ||
export declare const AppModule: ModuleData; | ||
import { FoalModule } from '@foal/core'; | ||
export declare const AppModule: FoalModule; |
@@ -1,2 +0,2 @@ | ||
import { ModuleData } from '@foal/core'; | ||
export declare const Module1Module: ModuleData; | ||
import { FoalModule } from '@foal/core'; | ||
export declare const Module1Module: FoalModule; |
@@ -32,3 +32,3 @@ "use strict"; | ||
TotoController = __decorate([ | ||
core_1.Injectable(), | ||
core_1.Service(), | ||
__metadata("design:paramtypes", [my_controller_controller_1.MyController]) | ||
@@ -35,0 +35,0 @@ ], TotoController); |
@@ -33,3 +33,3 @@ "use strict"; | ||
MyController = __decorate([ | ||
core_1.Injectable(), | ||
core_1.Service(), | ||
__metadata("design:paramtypes", []) | ||
@@ -36,0 +36,0 @@ ], MyController); |
@@ -6,2 +6,3 @@ import { RestController, RestParams } from '@foal/core'; | ||
get(id: any, params: RestParams): Promise<any>; | ||
getAll(params: RestParams): Promise<any>; | ||
update(id: any, data: any, params: RestParams): Promise<any>; | ||
@@ -8,0 +9,0 @@ patch(id: any, data: any, params: RestParams): Promise<any>; |
@@ -68,2 +68,9 @@ "use strict"; | ||
}; | ||
User.prototype.getAll = function (params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, 'You got it all']; | ||
}); | ||
}); | ||
}; | ||
User.prototype.update = function (id, data, params) { | ||
@@ -126,2 +133,8 @@ return __awaiter(this, void 0, void 0, function () { | ||
__decorate([ | ||
core_1.methodNotAllowed(), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Promise) | ||
], User.prototype, "getAll", null); | ||
__decorate([ | ||
core_1.expressLogger('update'), | ||
@@ -147,3 +160,3 @@ __metadata("design:type", Function), | ||
User = __decorate([ | ||
core_1.Injectable(), | ||
core_1.Service(), | ||
core_1.expressLogger('User service'), | ||
@@ -150,0 +163,0 @@ core_1.newExpressDecorator(bodyParser.urlencoded({ extended: false })), |
@@ -7,3 +7,3 @@ "use strict"; | ||
var app = express(); | ||
app.use(new core_1.FoalModule(app_module_1.AppModule).expressRouter()); | ||
app.use(new core_1.Foal(app_module_1.AppModule).expressRouter()); | ||
app.listen(3000, function () { return console.log("Listening on port 3000"); }); |
import { ExpressMiddleware } from '../../interfaces'; | ||
import { RestController } from './rest-controller.interface'; | ||
export declare function getDeleteHandler(controller: RestController): ExpressMiddleware; | ||
export declare function getGetAllHandler(controller: RestController): ExpressMiddleware; | ||
export declare function getGetHandler(controller: RestController): ExpressMiddleware; | ||
@@ -5,0 +6,0 @@ export declare function getPatchHandler(controller: RestController): ExpressMiddleware; |
@@ -62,2 +62,23 @@ "use strict"; | ||
exports.getDeleteHandler = getDeleteHandler; | ||
function getGetAllHandler(controller) { | ||
return utils_1.catchErrors(function handler(req, res) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!controller.getAll) { | ||
throw new errors_1.NotImplementedError(); | ||
} | ||
return [4 /*yield*/, controller.getAll(req.foal.context.params)]; | ||
case 1: | ||
data = _a.sent(); | ||
helpers_1.sendSuccess(res, 200, data); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
exports.getGetAllHandler = getGetAllHandler; | ||
function getGetHandler(controller) { | ||
@@ -64,0 +85,0 @@ return utils_1.catchErrors(function handler(req, res) { |
@@ -30,3 +30,3 @@ "use strict"; | ||
router.route(path) | ||
.get(getExpressMiddlewares('get', defaultcontextDef), handlers_1.getGetHandler(controller)) | ||
.get(getExpressMiddlewares('getAll', defaultcontextDef), handlers_1.getGetAllHandler(controller)) | ||
.post(getExpressMiddlewares('create', defaultcontextDef), handlers_1.getPostHandler(controller)); | ||
@@ -33,0 +33,0 @@ router.route(path + "/:id") |
@@ -14,2 +14,3 @@ export interface RestParams { | ||
get?: (id: any, params: any) => Promise<any>; | ||
getAll?: (params: any) => Promise<any>; | ||
update?: (id: any, data: any, params: any) => Promise<any>; | ||
@@ -16,0 +17,0 @@ patch?: (id: any, data: any, params: any) => Promise<any>; |
export { addToContextFromExpress } from './add-to-context-from-express.decorator'; | ||
export { combineDecorators } from './combine-decorators.decorator'; | ||
export { expressLogger } from './express-logger.decorator'; | ||
export { methodNotAllowed } from './method-not-allowed.decorator'; |
@@ -9,1 +9,3 @@ "use strict"; | ||
exports.expressLogger = express_logger_decorator_1.expressLogger; | ||
var method_not_allowed_decorator_1 = require("./method-not-allowed.decorator"); | ||
exports.methodNotAllowed = method_not_allowed_decorator_1.methodNotAllowed; |
@@ -24,2 +24,6 @@ export declare abstract class HttpError extends Error { | ||
} | ||
export declare class MethodNotAllowedError extends ClientError { | ||
statusCode: number; | ||
constructor(); | ||
} | ||
export declare class ConflictError extends ClientError { | ||
@@ -26,0 +30,0 @@ statusCode: number; |
@@ -82,2 +82,13 @@ "use strict"; | ||
exports.NotFoundError = NotFoundError; | ||
var MethodNotAllowedError = /** @class */ (function (_super) { | ||
__extends(MethodNotAllowedError, _super); | ||
function MethodNotAllowedError() { | ||
var _this = _super.call(this) || this; | ||
_this.statusCode = 405; | ||
Object.setPrototypeOf(_this, MethodNotAllowedError.prototype); | ||
return _this; | ||
} | ||
return MethodNotAllowedError; | ||
}(ClientError)); | ||
exports.MethodNotAllowedError = MethodNotAllowedError; | ||
var ConflictError = /** @class */ (function (_super) { | ||
@@ -84,0 +95,0 @@ __extends(ConflictError, _super); |
@@ -9,3 +9,3 @@ "use strict"; | ||
if (expressHooks) { | ||
console.log("WARNING: contextual decorators should be specified after express hooks."); | ||
throw new Error("Contextual decorators should be specified after express decorators."); | ||
} | ||
@@ -12,0 +12,0 @@ } |
import 'reflect-metadata'; | ||
import { Type } from '../interfaces'; | ||
export declare function Injectable(): (target: any) => void; | ||
export declare function Service(): (target: any) => void; | ||
export declare class Injector { | ||
@@ -5,0 +5,0 @@ private parentInjector; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("reflect-metadata"); | ||
function Injectable() { | ||
function Service() { | ||
return function decorator(target) { }; | ||
} | ||
exports.Injectable = Injectable; | ||
exports.Service = Service; | ||
var Injector = /** @class */ (function () { | ||
@@ -20,3 +20,3 @@ function Injector(parentInjector) { | ||
if (!dependencies) { | ||
throw new Error(Service.name + " has no dependencies. Please check that:\n - The service has a constructor.\n - The service has the @Injectable() decorator.\n - The \"emitDecoratorMetadata\" is set to true in the tsconfig.json file."); | ||
throw new Error(Service.name + " has no dependencies. Please check that:\n - The service has a constructor.\n - The service has the @Service() decorator.\n - The \"emitDecoratorMetadata\" is set to true in the tsconfig.json file."); | ||
} | ||
@@ -23,0 +23,0 @@ if (dependencies.length > 0) { |
@@ -21,3 +21,3 @@ "use strict"; | ||
Foobar = __decorate([ | ||
injector_1.Injectable(), | ||
injector_1.Service(), | ||
__metadata("design:paramtypes", []) | ||
@@ -39,3 +39,3 @@ ], Foobar); | ||
Bar = __decorate([ | ||
injector_1.Injectable() | ||
injector_1.Service() | ||
], Bar); | ||
@@ -55,3 +55,3 @@ return Bar; | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) instanceof Foobar); | ||
chai_1.expect(injector.get(Foobar)).to.be.an.instanceof(Foobar); | ||
}); | ||
@@ -64,3 +64,3 @@ it('should instantiate the dependencies of the given Service.', function () { | ||
Foobar2 = __decorate([ | ||
injector_1.Injectable(), | ||
injector_1.Service(), | ||
__metadata("design:paramtypes", [Foobar]) | ||
@@ -72,4 +72,4 @@ ], Foobar2); | ||
var foobar = injector.get(Foobar); | ||
chai_1.assert(foobar instanceof Foobar); | ||
chai_1.assert(injector.get(Foobar2).foobar === foobar); | ||
chai_1.expect(foobar).to.be.an.instanceof(Foobar); | ||
chai_1.expect(injector.get(Foobar2).foobar).to.equal(foobar); | ||
}); | ||
@@ -80,3 +80,3 @@ it('should not instantiate twice the given Service.', function () { | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
chai_1.expect(injector.get(Foobar)).to.equal(foobar); | ||
}); | ||
@@ -92,3 +92,3 @@ }); | ||
Foobar = __decorate([ | ||
injector_1.Injectable(), | ||
injector_1.Service(), | ||
__metadata("design:paramtypes", []) | ||
@@ -106,3 +106,3 @@ ], Foobar); | ||
var foobar = parentInjector.get(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
chai_1.expect(injector.get(Foobar)).to.equal(foobar); | ||
}); | ||
@@ -115,3 +115,3 @@ }); | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
chai_1.expect(injector.get(Foobar)).to.equal(foobar); | ||
}); | ||
@@ -118,0 +118,0 @@ }); |
@@ -5,3 +5,3 @@ import 'reflect-metadata'; | ||
import { Type } from './interfaces'; | ||
export interface ModuleData { | ||
export interface FoalModule { | ||
services: Type<any>[]; | ||
@@ -13,11 +13,11 @@ controllerBindings?: ((injector: Injector, controllerHooks: ModuleHooks, controllerContextDef: ModuleContextDef) => { | ||
imports?: { | ||
module: ModuleData; | ||
module: FoalModule; | ||
path?: string; | ||
}[]; | ||
} | ||
export declare class FoalModule { | ||
export declare class Foal { | ||
readonly injector: Injector; | ||
private readonly router; | ||
constructor(data: ModuleData, parentModule?: FoalModule); | ||
constructor(foalModule: FoalModule, parentModule?: Foal); | ||
expressRouter(): any; | ||
} |
@@ -6,9 +6,9 @@ "use strict"; | ||
var injector_1 = require("./di/injector"); | ||
var FoalModule = /** @class */ (function () { | ||
function FoalModule(data, parentModule) { | ||
var Foal = /** @class */ (function () { | ||
function Foal(foalModule, parentModule) { | ||
var _this = this; | ||
this.router = express.Router(); | ||
data.controllerBindings = data.controllerBindings || []; | ||
data.imports = data.imports || []; | ||
data.sharedControllerDecorators = data.sharedControllerDecorators || []; | ||
foalModule.controllerBindings = foalModule.controllerBindings || []; | ||
foalModule.imports = foalModule.imports || []; | ||
foalModule.sharedControllerDecorators = foalModule.sharedControllerDecorators || []; | ||
if (parentModule) { | ||
@@ -20,3 +20,3 @@ this.injector = new injector_1.Injector(parentModule.injector); | ||
} | ||
data.services.forEach(function (service) { return _this.injector.inject(service); }); | ||
foalModule.services.forEach(function (service) { return _this.injector.inject(service); }); | ||
var FakeModule = /** @class */ (function () { | ||
@@ -28,17 +28,17 @@ function FakeModule() { | ||
// Reverse the array to apply decorators in the proper order. | ||
data.sharedControllerDecorators.reverse().forEach(function (decorator) { return decorator(FakeModule); }); | ||
foalModule.sharedControllerDecorators.reverse().forEach(function (decorator) { return decorator(FakeModule); }); | ||
var expressHooks = Reflect.getMetadata('hooks:express', FakeModule) || []; | ||
var contextualHooks = Reflect.getMetadata('hooks:contextual', FakeModule) || []; | ||
var expressContextDef = Reflect.getMetadata('contextDef:express', FakeModule) || []; | ||
data.controllerBindings.forEach(function (getRouters) { | ||
foalModule.controllerBindings.forEach(function (getRouters) { | ||
var expressRouter = getRouters(_this.injector, { express: expressHooks, contextual: contextualHooks }, { express: expressContextDef }).expressRouter; | ||
_this.router.use(expressRouter); | ||
}); | ||
data.imports.forEach(function (imp) { return _this.router.use(imp.path || '/', new FoalModule(imp.module, _this).router); }); | ||
foalModule.imports.forEach(function (imp) { return _this.router.use(imp.path || '/', new Foal(imp.module, _this).router); }); | ||
} | ||
FoalModule.prototype.expressRouter = function () { | ||
Foal.prototype.expressRouter = function () { | ||
return this.router; | ||
}; | ||
return FoalModule; | ||
return Foal; | ||
}()); | ||
exports.FoalModule = FoalModule; | ||
exports.Foal = Foal; |
@@ -1,3 +0,3 @@ | ||
export { Injectable } from './di/injector'; | ||
export { Service } from './di/injector'; | ||
export * from './foal'; | ||
export * from './controllers'; |
@@ -7,4 +7,4 @@ "use strict"; | ||
var injector_1 = require("./di/injector"); | ||
exports.Injectable = injector_1.Injectable; | ||
exports.Service = injector_1.Service; | ||
__export(require("./foal")); | ||
__export(require("./controllers")); |
{ | ||
"name": "@foal/core", | ||
"version": "0.1.1", | ||
"description": "", | ||
"version": "0.2.0", | ||
"description": "Backend framework written in TypeScript", | ||
"main": "./dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "./dist/index.d.ts", |
@@ -30,5 +30,5 @@ # FoalTS | ||
import * as express from 'express'; | ||
import { FoalModule, Injectable, newExpressDecorator, rest, RestController, RestParams } from '@foal/core'; | ||
import { Foal, Service, newExpressDecorator, rest, RestController, RestParams } from '@foal/core'; | ||
@Injectable() | ||
@Service() | ||
class User implements RestController { | ||
@@ -45,3 +45,3 @@ constructor () {} | ||
const app = express(); | ||
const foal = new FoalModule({ | ||
const foal = new Foal({ | ||
services: [ User ], | ||
@@ -48,0 +48,0 @@ controllerBindings: [ rest.bindController('/users', User) ], |
76532
78
1562